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