View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.fulcrum.yaafi.framework.configuration;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.Properties;
25  
26  import org.apache.avalon.framework.configuration.Configurable;
27  import org.apache.avalon.framework.configuration.Configuration;
28  import org.apache.avalon.framework.configuration.ConfigurationException;
29  import org.apache.avalon.framework.context.Context;
30  import org.apache.avalon.framework.context.ContextException;
31  import org.apache.avalon.framework.context.Contextualizable;
32  import org.apache.avalon.framework.logger.LogEnabled;
33  import org.apache.avalon.framework.logger.Logger;
34  import org.apache.fulcrum.yaafi.framework.constant.AvalonYaafiConstants;
35  import org.apache.fulcrum.yaafi.framework.util.InputStreamLocator;
36  
37  /**
38   * Base class to expand the value and all attributes. This class is intentend
39   * to be sub-classed if you hook up your own configuration mechanism.
40   *
41   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
42   */
43  public abstract class ComponentConfigurationPropertiesResolverBaseImpl
44  	implements ComponentConfigurationPropertiesResolver, LogEnabled, Contextualizable, Configurable
45  {
46      /** the logger of the container */
47      private Logger logger;
48  
49      /** the Avalon context */
50      private Context context;
51  
52      /** the container configuration */
53      private Configuration configuration;
54  
55      /**
56       * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger)
57       * @param logger the logger instance 
58       */
59      public void enableLogging(Logger logger)
60      {
61          this.logger = logger;
62      }
63  
64      /**
65       * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
66       * @param context the Context to add
67       */
68      public void contextualize(Context context) throws ContextException
69      {
70          this.context = context;
71      }
72  
73      /**
74       * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
75       * @param configuration the configuration object to use
76       */
77      public void configure(Configuration configuration) throws ConfigurationException
78      {
79          this.configuration = configuration;
80      }
81  
82      /**
83       * @return Returns the logger.
84       */
85      protected Logger getLogger()
86      {
87          return logger;
88      }
89  
90      /**
91       * @return Returns the context.
92       */
93      protected Context getContext()
94      {
95          return context;
96      }
97  
98      /**
99       * @return the home directory of the application
100      */
101     protected File getApplicationRootDir()
102     {
103         try
104         {
105             return (File) this.getContext().get(AvalonYaafiConstants.URN_AVALON_HOME);
106         }
107         catch(Exception e)
108         {
109             throw new RuntimeException(e.getMessage());
110         }
111     }
112 
113     /**
114      * @return Returns the configuration.
115      */
116     protected Configuration getConfiguration()
117     {
118         return configuration;
119     }
120 
121     /**
122      * @return Returns the componentConfigurationPropertiesLocation.
123      */
124     protected String getLocation()
125     {
126         return configuration.getChild("location").getValue(COMPONENT_CONFIG_PROPERTIES_VALUE );
127     }
128 
129     /**
130      * Creates an InputStream using a Locator.
131      * @return the InputStrem or null if the resource was not found
132      * @param location the location of the file
133      * @throws IOException if file not found
134      */
135     protected InputStream createInputStream(String location) throws IOException
136     {
137         InputStreamLocator locator = new InputStreamLocator(this.getApplicationRootDir(), this.getLogger());
138         return locator.locate(location);
139     }
140 
141     /**
142      * Add the Avalon context variables.
143      * @param properties properties to be set
144      * @throws ContextException if context not found
145      */
146     protected void addAvalonContext(Properties properties) throws ContextException
147     {
148         properties.put(
149             AvalonYaafiConstants.URN_AVALON_NAME,
150             this.getContext().get(AvalonYaafiConstants.URN_AVALON_NAME)
151             );
152 
153         properties.put(
154             AvalonYaafiConstants.URN_AVALON_PARTITION,
155             this.getContext().get(AvalonYaafiConstants.URN_AVALON_PARTITION)
156             );
157 
158         properties.put(
159             AvalonYaafiConstants.URN_AVALON_HOME,
160             this.getContext().get(AvalonYaafiConstants.URN_AVALON_HOME)
161             );
162 
163         properties.put(
164             AvalonYaafiConstants.URN_AVALON_TEMP,
165             this.getContext().get(AvalonYaafiConstants.URN_AVALON_TEMP)
166             );
167     }
168 
169     
170     /**
171      * Set properties from a file location
172      * @param fileLocation file location of properties properties to be set
173      * @return the properties
174      * @throws Exception if unable to parse the properties file
175      */
176     protected Properties loadProperties(String fileLocation) throws Exception
177     {
178         Properties result = new Properties();
179         InputStream is = this.createInputStream(fileLocation);
180 
181         try
182         {
183             if(is != null)
184             {
185 		        result.load(is);
186 		        is.close();
187 		        is = null;
188             }
189             else
190             {
191                 this.getLogger().debug("Unable to load the following optional file :" + fileLocation);
192             }
193 
194             return result;
195         }
196         catch ( Exception e )
197         {
198             String msg = "Unable to parse the following file : " + fileLocation;
199             this.getLogger().error( msg , e );
200             throw e;
201         }
202     }
203 }