##### Coverage Report - org.apache.turbine.services.avaloncomponent.TurbineYaafiComponentService

Classes in this File | Line Coverage | Branch Coverage | Complexity
TurbineYaafiComponentService |
69% | 46/66
62% | 5/8
| 2.1818181818181817;2,182

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