View Javadoc

1   package org.apache.turbine.services.avaloncomponent;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import org.apache.avalon.framework.activity.Disposable;
26  import org.apache.avalon.framework.activity.Initializable;
27  import org.apache.avalon.framework.logger.CommonsLogger;
28  import org.apache.avalon.framework.logger.Logger;
29  import org.apache.avalon.framework.service.ServiceException;
30  import org.apache.commons.configuration.Configuration;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
34  import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration;
35  import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
36  import org.apache.turbine.Turbine;
37  import org.apache.turbine.services.InitializationException;
38  import org.apache.turbine.services.InstantiationException;
39  import org.apache.turbine.services.TurbineBaseService;
40  
41  /**
42   * An implementation of Turbine service initializing the YAAFI container
43   *
44   * @author <a href="mailto:siegfried.goescfl@it20one.at">Siegfried Goeschl</a>
45   */
46  public class TurbineYaafiComponentService
47          extends TurbineBaseService
48          implements AvalonComponentService, Initializable, Disposable
49  {
50      /** the logger to be used */
51      private static Log log = LogFactory.getLog(AVALON_LOG_CATEGORY);
52  
53      /** property to lookup the container configuration file */
54      public static final String CONTAINER_CONFIGURATION_KEY = "containerConfiguration";
55  
56      /** the default value for the container configuration file */
57      public static final String CONTAINER_CONFIGURATION_VALUE = "/WEB-INF/conf/containerConfiguration.xml";
58  
59      /** property to lookup the properties file */
60      public static final String COMPONENT_PARAMETERS_KEY = "parameters";
61  
62      /** the default value for the parameter file */
63      public static final String COMPONENT_PARAMETERS_VALUE = "/WEB-INF/conf/parameters.properties";
64  
65      /** YAFFI container */
66      private ServiceContainer container;
67  
68      // -------------------------------------------------------------
69      // Service initialization
70      // -------------------------------------------------------------
71  
72      public TurbineYaafiComponentService()
73      {
74          // nothing to do
75      }
76  
77      /**
78       * Load all configured components and initialize them. This is a zero parameter variant which
79       * queries the Turbine Servlet for its config.
80       *
81       * @throws InitializationException Something went wrong in the init stage
82       */
83      public void init() throws InitializationException
84      {
85          try
86          {
87              log.info( "Initializing TurbineYaafiComponentService ..." );
88              initialize();
89              setInit(true);
90          }
91          catch (Exception e)
92          {
93              log.error("Exception caught initialising service: ", e);
94              throw new InitializationException("Initializing TurbineYaafiComponentService failed", e);
95          }
96      }
97  
98      /**
99       * Shuts the Component Service down, calls dispose on the components that implement this
100      * interface
101      *
102      */
103     public void shutdown()
104     {
105         log.info( "Disposing TurbineYaafiComponentService ..." );
106         dispose();
107         setInit(false);
108     }
109 
110     // -------------------------------------------------------------
111     // Avalon lifecycle interfaces
112     // -------------------------------------------------------------
113 
114     /**
115      * Initializes the container
116      *
117      * @throws Exception generic exception
118      */
119     public void initialize() throws Exception
120     {
121         // get the configuration from the baseclass
122 
123         Configuration conf = this.getConfiguration();
124 
125         // determine the home directory
126         String homePath = Turbine.getRealPath("/");
127         if (homePath == null)
128         {
129             homePath = Turbine.getApplicationRoot();
130         }
131         File home = new File(homePath);
132         log.info("Using the following home : " + home.getAbsolutePath());
133 
134         // create the configuration for YAAFI
135         ServiceContainerConfiguration config =
136             this.createServiceContainerConfiguration(conf, home);
137 
138         // initialize the container
139         try
140         {
141             this.container = ServiceContainerFactory.create(
142                 config
143                 );
144         }
145         catch (Exception e)
146         {
147             String msg = "Initializing YAAFI failed";
148             log.error(msg,e);
149             throw e;
150         }
151     }
152 
153     /**
154      * Disposes of the container and releases resources
155      */
156     public void dispose()
157     {
158         if (this.container != null)
159         {
160             this.container.dispose();
161             this.container = null;
162         }
163     }
164 
165     /**
166      * Returns an instance of the named component
167      *
168      * @param roleName Name of the role the component fills.
169      * @return an instance of the named component
170      * @throws Exception generic exception
171      */
172     public Object lookup(String roleName) throws ServiceException
173     {
174         return this.container.lookup(roleName);
175     }
176 
177     /**
178      * Releases the component.
179      *
180      * @param component the component to release
181      */
182     public void release(Object component)
183     {
184         this.container.release( component );
185     }
186 
187     /**
188      * @see org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String)
189      */
190     public boolean hasService(String roleName)
191     {
192         return this.container.hasService(roleName);
193     }
194 
195     /**
196      * Create a ServiceContainerConfiguration based on the Turbine configuration
197      *
198      * @param conf the Turbine configuration
199      * @param applicationRoot the application root directory
200      *
201      * @return the YAAFI configuration
202      * @throws IOException creating the YAAFI configuration failed
203      */
204     protected ServiceContainerConfiguration createServiceContainerConfiguration( Configuration conf, File applicationRoot )
205         throws IOException
206     {
207         ServiceContainerConfiguration result = new ServiceContainerConfiguration();
208 
209         result.setApplicationRootDir(applicationRoot.getAbsolutePath());
210         result.setLogger( this.createAvalonLogger() );
211 
212         // are we using a "containerConfiguration.xml" ?!
213 
214         if( conf.containsKey(CONTAINER_CONFIGURATION_KEY) )
215         {
216             // determine the container configuration file
217 
218             String containerConfiguration = conf.getString(
219                 CONTAINER_CONFIGURATION_KEY
220                 );
221 
222             result.loadContainerConfiguration(containerConfiguration);
223         }
224         else if( conf.containsKey(COMPONENT_ROLE_KEY) )
225         {
226             // determine the location of the role configuraton file
227 
228             String roleConfigurationFileName = conf.getString(
229                 COMPONENT_ROLE_KEY,
230                 COMPONENT_ROLE_VALUE
231                 );
232 
233             // determine the location of component configuration file
234 
235             String componentConfigurationFileName = conf.getString(
236                 COMPONENT_CONFIG_KEY,
237                 COMPONENT_CONFIG_VALUE
238                 );
239 
240             // determine the location of parameters file
241 
242             String parametersFileName = conf.getString(
243                 COMPONENT_PARAMETERS_KEY,
244                 COMPONENT_PARAMETERS_VALUE
245                 );
246 
247             result.setComponentRolesLocation( roleConfigurationFileName );
248             result.setComponentConfigurationLocation( componentConfigurationFileName );
249             result.setParametersLocation( parametersFileName );
250         }
251         else
252         {
253             // determine the container configuration file
254 
255             String containerConfiguration = conf.getString(
256                 CONTAINER_CONFIGURATION_KEY,
257                 CONTAINER_CONFIGURATION_VALUE
258                 );
259 
260             result.loadContainerConfiguration(containerConfiguration);
261         }
262 
263         return result;
264     }
265 
266     /**
267      * Create the Avalon logger to be passed to YAAFI
268      * @return an Avalon Logger
269      */
270     protected Logger createAvalonLogger()
271     {
272         Logger result = new CommonsLogger(log, AVALON_LOG_CATEGORY);
273         return result;
274     }
275 
276     // -------------------------------------------------------------
277     // TurbineServiceProvider
278     // -------------------------------------------------------------
279 
280     /**
281      * @see org.apache.turbine.services.TurbineServiceProvider#exists(java.lang.String)
282      */
283     public boolean exists(String roleName)
284     {
285         return this.hasService(roleName);
286     }
287 
288     /**
289      * @see org.apache.turbine.services.TurbineServiceProvider#get(java.lang.String)
290      */
291     public Object get(String roleName) throws InstantiationException
292     {
293         try
294         {
295             return this.lookup(roleName);
296         }
297         catch (ServiceException e)
298         {
299             String msg = "Unable to get the following service : " + roleName;
300             log.error(msg);
301             throw new InstantiationException(msg);
302         }
303         catch (Throwable t)
304         {
305             String msg = "Unable to get the following service : " + roleName;
306             log.error(msg,t);
307             throw new InstantiationException(msg,t);
308         }
309     }
310 }