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.component.Component;
28  import org.apache.avalon.framework.component.ComponentException;
29  import org.apache.avalon.framework.logger.Log4JLogger;
30  import org.apache.avalon.framework.service.ServiceException;
31  import org.apache.commons.configuration.Configuration;
32  import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
33  import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration;
34  import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
35  import org.apache.log4j.Logger;
36  import org.apache.turbine.Turbine;
37  import org.apache.turbine.services.InitializationException;
38  import org.apache.turbine.services.TurbineBaseService;
39  
40  /***
41   * An implementation of Turbine service initializing the YAAFI container
42   *
43   * @author <a href="mailto:siegfried.goescfl@it20one.at">Siegfried Goeschl</a>
44   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
45   * @version $Id$
46   */
47  public class ACSYaafiComponentService extends TurbineBaseService implements
48          AvalonComponentService, Initializable, Disposable
49  {
50      /*** The publically visible name of the service */
51      public static final String SERVICE_NAME = "YaafiComponentService";
52  
53      /*** property to lookup the container configuration file */
54      public static final String CONTAINER_CONFIGURATION_KEY
55              = "containerConfiguration";
56  
57      /*** the default value for the container configuration file */
58      public static final String CONTAINER_CONFIGURATION_VALUE
59              = "/WEB-INF/conf/containerConfiguration.xml";
60  
61      /*** property to lookup the properties file */
62      public static final String COMPONENT_PARAMETERS_KEY = "parameters";
63  
64      /*** the default value for the parameter file */
65      public static final String COMPONENT_PARAMETERS_VALUE
66              = "/WEB-INF/conf/parameters.properties";
67  
68      /*** YAFFI container */
69      private ServiceContainer container;
70  
71      /*** our Log4J logger */
72      private Logger logger;
73  
74      // -------------------------------------------------------------
75      // Service initialization
76      // -------------------------------------------------------------
77  
78      public ACSYaafiComponentService()
79      {
80          this.logger = Logger.getLogger(ACSYaafiComponentService.class);
81      }
82  
83      /***
84       * Load all configured components and initialize them. This is a zero
85       * parameter variant which queries the Turbine Servlet for its config.
86       *
87       * @throws InitializationException Something went wrong in the init stage
88       */
89      public void init() throws InitializationException
90      {
91          try
92          {
93              this.logger.info("Initializing ACSYaafiComponentService ...");
94              initialize();
95              setInit(true);
96          }
97          catch (Exception e)
98          {
99              this.logger.error("Exception caught initializing service: ", e);
100             throw new InitializationException(
101                     "Initializing ACSYaafiComponentService failed", e);
102         }
103     }
104 
105     /***
106      * Shuts the Component Service down, calls dispose on the components that
107      * implement this interface
108      */
109     public void shutdown()
110     {
111         this.logger.info("Disposing ACSYaafiComponentService ...");
112         dispose();
113         setInit(false);
114     }
115 
116     // -------------------------------------------------------------
117     // Avalon lifecycle interfaces
118     // -------------------------------------------------------------
119 
120     /***
121      * Initializes the container
122      *
123      * @throws Exception generic exception
124      */
125     public void initialize() throws Exception
126     {
127         // get the configuration from the base class
128         Configuration conf = this.getConfiguration();
129 
130         // determine the home directory
131         String homePath = Turbine.getRealPath("/");
132         if (homePath == null)
133         {
134             homePath = Turbine.getApplicationRoot();
135         }
136         File home = new File(homePath);
137 
138         this.logger.info("Using the following home : "
139                 + home.getAbsolutePath());
140 
141         // create the configuration for YAAFI
142         ServiceContainerConfiguration config = this
143                 .createServiceContainerConfiguration(conf,
144                         home.getAbsolutePath());
145 
146         try
147         {
148             this.container = ServiceContainerFactory.create(config);
149         }
150         catch (Throwable t)
151         {
152             throw new Exception("Initializing YAAFI failed", t);
153         }
154     }
155 
156     /***
157      * Disposes of the container and releases resources
158      */
159     public void dispose()
160     {
161         if (this.container != null)
162         {
163             this.container.dispose();
164             this.container = null;
165         }
166     }
167 
168     /***
169      * Returns an instance of the named component
170      *
171      * @param path Name of the role the component fills.
172      * @return an instance of the named component
173      * @throws Exception generic exception
174      */
175     public Component lookup(String path) throws ComponentException
176     {
177         try
178         {
179             return (Component) this.container.lookup(path);
180         }
181         catch (ServiceException e)
182         {
183             throw new ComponentException(e.getKey(), e.getMessage(), e);
184         }
185     }
186 
187     /***
188      * Releases the component
189      *
190      * @param component the component to release
191      */
192     public void release(Component component)
193     {
194         this.container.release(component);
195     }
196 
197     /***
198      * Create a ServiceContainerConfiguration based on the Turbine configuration
199      *
200      * @param conf the Turbine configuration
201      * @param appRoot the absolute path to the application root directory
202      * @return the YAAFI configuration
203      * @throws IOException creating the YAAFI configuration failed
204      */
205     protected ServiceContainerConfiguration createServiceContainerConfiguration(
206             Configuration conf, String appRoot) throws IOException
207     {
208         ServiceContainerConfiguration result
209                 = new ServiceContainerConfiguration();
210 
211         result.setLogger(this.createAvalonLogger(AVALON_LOG_CATEGORY));
212         result.setApplicationRootDir(appRoot);
213 
214         // are we using a "containerConfiguration.xml" ?!
215 
216         if (conf.containsKey(CONTAINER_CONFIGURATION_KEY))
217         {
218             // determine the container configuration file
219 
220             String containerConfiguration
221                     = conf.getString(CONTAINER_CONFIGURATION_KEY);
222 
223             result.loadContainerConfiguration(containerConfiguration);
224         }
225         else if (conf.containsKey(COMPONENT_ROLE_KEY))
226         {
227             // determine the location of the role configuration file
228 
229             String roleConfigurationFileName
230                     = conf.getString(COMPONENT_ROLE_KEY, COMPONENT_ROLE_VALUE);
231 
232             // determine the location of component configuration file
233 
234             String componentConfigurationFileName = conf.getString(
235                     COMPONENT_CONFIG_KEY, COMPONENT_CONFIG_VALUE);
236 
237             // determine the location of parameters file
238 
239             String parametersFileName = conf.getString(
240                     COMPONENT_PARAMETERS_KEY, COMPONENT_PARAMETERS_VALUE);
241 
242             result.setComponentRolesLocation(roleConfigurationFileName);
243             result.setComponentConfigurationLocation(
244                     componentConfigurationFileName);
245             result.setParametersLocation(parametersFileName);
246         }
247         else
248         {
249             // determine the container configuration file
250 
251             String containerConfiguration = conf.getString(
252                     CONTAINER_CONFIGURATION_KEY, CONTAINER_CONFIGURATION_VALUE);
253 
254             result.loadContainerConfiguration(containerConfiguration);
255         }
256 
257         return result;
258     }
259 
260     /***
261      * Create the Avalon logger to be passed to YAAFI
262      *
263      * @param name the name of the logger
264      * @return an Avalon Logger
265      */
266     protected org.apache.avalon.framework.logger.Logger createAvalonLogger(
267             String name)
268     {
269         return new Log4JLogger(Logger.getLogger(name));
270     }
271 }