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