001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 package org.apache.fulcrum.yaafi.framework.configuration; 020 021 import java.io.File; 022 import java.io.IOException; 023 import java.io.InputStream; 024 import java.util.Properties; 025 026 import org.apache.avalon.framework.configuration.Configurable; 027 import org.apache.avalon.framework.configuration.Configuration; 028 import org.apache.avalon.framework.configuration.ConfigurationException; 029 import org.apache.avalon.framework.context.Context; 030 import org.apache.avalon.framework.context.ContextException; 031 import org.apache.avalon.framework.context.Contextualizable; 032 import org.apache.avalon.framework.logger.LogEnabled; 033 import org.apache.avalon.framework.logger.Logger; 034 import org.apache.fulcrum.yaafi.framework.constant.AvalonYaafiConstants; 035 import org.apache.fulcrum.yaafi.framework.util.InputStreamLocator; 036 037 /** 038 * Base class to expand the value and all attributes. This class is intentend 039 * to be sub-classed if you hook up your own configuration mechanism. 040 * 041 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a> 042 */ 043 public abstract class ComponentConfigurationPropertiesResolverBaseImpl 044 implements ComponentConfigurationPropertiesResolver, LogEnabled, Contextualizable, Configurable 045 { 046 /** the logger of the container */ 047 private Logger logger; 048 049 /** the Avalon context */ 050 private Context context; 051 052 /** the container configuration */ 053 private Configuration configuration; 054 055 /* 056 * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger) 057 */ 058 public void enableLogging(Logger logger) 059 { 060 this.logger = logger; 061 } 062 063 /* 064 * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context) 065 */ 066 public void contextualize(Context context) throws ContextException 067 { 068 this.context = context; 069 } 070 071 /** 072 * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration) 073 */ 074 public void configure(Configuration configuration) throws ConfigurationException 075 { 076 this.configuration = configuration; 077 } 078 079 /** 080 * @return Returns the logger. 081 */ 082 protected Logger getLogger() 083 { 084 return logger; 085 } 086 087 /** 088 * @return Returns the context. 089 */ 090 protected Context getContext() 091 { 092 return context; 093 } 094 095 /** 096 * @return the home directory of the application 097 */ 098 protected File getApplicationRootDir() 099 { 100 try 101 { 102 return (File) this.getContext().get(AvalonYaafiConstants.URN_AVALON_HOME); 103 } 104 catch(Exception e) 105 { 106 throw new RuntimeException(e.getMessage()); 107 } 108 } 109 110 /** 111 * @return Returns the configuration. 112 */ 113 protected Configuration getConfiguration() 114 { 115 return configuration; 116 } 117 118 /** 119 * @return Returns the componentConfigurationPropertiesLocation. 120 */ 121 protected String getLocation() 122 { 123 return configuration.getChild("location").getValue(COMPONENT_CONFIG_PROPERTIES_VALUE ); 124 } 125 126 /** 127 * Creates an InputStream using a Locator. 128 * @return the InputStrem or null if the resource was not found 129 */ 130 protected InputStream createInputStream(String location) throws IOException 131 { 132 InputStreamLocator locator = new InputStreamLocator(this.getApplicationRootDir(), this.getLogger()); 133 return locator.locate(location); 134 } 135 136 /** 137 * Add the Avalon context variables. 138 */ 139 protected void addAvalonContext(Properties properties) throws ContextException 140 { 141 properties.put( 142 AvalonYaafiConstants.URN_AVALON_NAME, 143 this.getContext().get(AvalonYaafiConstants.URN_AVALON_NAME) 144 ); 145 146 properties.put( 147 AvalonYaafiConstants.URN_AVALON_PARTITION, 148 this.getContext().get(AvalonYaafiConstants.URN_AVALON_PARTITION) 149 ); 150 151 properties.put( 152 AvalonYaafiConstants.URN_AVALON_HOME, 153 this.getContext().get(AvalonYaafiConstants.URN_AVALON_HOME) 154 ); 155 156 properties.put( 157 AvalonYaafiConstants.URN_AVALON_TEMP, 158 this.getContext().get(AvalonYaafiConstants.URN_AVALON_TEMP) 159 ); 160 } 161 162 protected Properties loadProperties(String location) throws Exception 163 { 164 Properties result = new Properties(); 165 InputStream is = this.createInputStream(location); 166 167 try 168 { 169 if(is != null) 170 { 171 result.load(is); 172 is.close(); 173 is = null; 174 } 175 else 176 { 177 this.getLogger().debug("Unable to load the following optional file :" + location); 178 } 179 180 return result; 181 } 182 catch ( Exception e ) 183 { 184 String msg = "Unable to parse the following file : " + location; 185 this.getLogger().error( msg , e ); 186 throw e; 187 } 188 } 189 }