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