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       */
58      public void enableLogging(Logger logger)
59      {
60          this.logger = logger;
61      }
62  
63      /*
64       * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
65       */
66      public void contextualize(Context context) throws ContextException
67      {
68          this.context = context;
69      }
70  
71      /**
72       * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
73       */
74      public void configure(Configuration configuration) throws ConfigurationException
75      {
76          this.configuration = configuration;
77      }
78  
79      /**
80       * @return Returns the logger.
81       */
82      protected Logger getLogger()
83      {
84          return logger;
85      }
86  
87      /**
88       * @return Returns the context.
89       */
90      protected Context getContext()
91      {
92          return context;
93      }
94  
95      /**
96       * @return the home directory of the application
97       */
98      protected File getApplicationRootDir()
99      {
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 }