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
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
43
44
45
46 public class TurbineYaafiComponentService
47 extends TurbineBaseService
48 implements AvalonComponentService, Initializable, Disposable
49 {
50
51 private static Log log = LogFactory.getLog(AVALON_LOG_CATEGORY);
52
53
54 public static final String CONTAINER_CONFIGURATION_KEY = "containerConfiguration";
55
56
57 public static final String CONTAINER_CONFIGURATION_VALUE = "/WEB-INF/conf/containerConfiguration.xml";
58
59
60 public static final String COMPONENT_PARAMETERS_KEY = "parameters";
61
62
63 public static final String COMPONENT_PARAMETERS_VALUE = "/WEB-INF/conf/parameters.properties";
64
65
66 private ServiceContainer container;
67
68
69
70
71
72
73
74 @Override
75 public void init() throws InitializationException
76 {
77 try
78 {
79 log.info( "Initializing TurbineYaafiComponentService ..." );
80 initialize();
81 setInit(true);
82 }
83 catch (Exception e)
84 {
85 log.error("Exception caught initialising service: ", e);
86 throw new InitializationException("Initializing TurbineYaafiComponentService failed", e);
87 }
88 }
89
90
91
92
93
94
95 @Override
96 public void shutdown()
97 {
98 log.info( "Disposing TurbineYaafiComponentService ..." );
99 dispose();
100 setInit(false);
101 }
102
103
104
105
106
107
108
109
110
111
112 @Override
113 public void initialize() throws Exception
114 {
115
116
117 Configuration conf = this.getConfiguration();
118
119
120 String homePath = Turbine.getRealPath("/");
121 if (homePath == null)
122 {
123 homePath = Turbine.getApplicationRoot();
124 }
125 File home = new File(homePath);
126 log.info("Using the following home : " + home.getAbsolutePath());
127
128
129 ServiceContainerConfiguration config =
130 this.createServiceContainerConfiguration(conf, home);
131
132
133 try
134 {
135 this.container = ServiceContainerFactory.create(
136 config
137 );
138 }
139 catch (Exception e)
140 {
141 String msg = "Initializing YAAFI failed";
142 log.error(msg,e);
143 throw e;
144 }
145 }
146
147
148
149
150 @Override
151 public void dispose()
152 {
153 if (this.container != null)
154 {
155 this.container.dispose();
156 this.container = null;
157 }
158 }
159
160
161
162
163
164
165
166 @Override
167 public Object lookup(String roleName) throws ServiceException
168 {
169 return this.container.lookup(roleName);
170 }
171
172
173
174
175
176
177 @Override
178 public void release(Object component)
179 {
180 this.container.release( component );
181 }
182
183
184
185
186 @Override
187 public boolean hasService(String roleName)
188 {
189 return this.container.hasService(roleName);
190 }
191
192
193
194
195
196
197
198
199
200
201 protected ServiceContainerConfiguration createServiceContainerConfiguration( Configuration conf, File applicationRoot )
202 throws IOException
203 {
204 ServiceContainerConfiguration result = new ServiceContainerConfiguration();
205
206 result.setApplicationRootDir(applicationRoot.getAbsolutePath());
207 result.setLogger( this.createAvalonLogger() );
208
209
210
211 if( conf.containsKey(CONTAINER_CONFIGURATION_KEY) )
212 {
213
214
215 String containerConfiguration = conf.getString(
216 CONTAINER_CONFIGURATION_KEY
217 );
218
219 result.loadContainerConfiguration(containerConfiguration);
220 }
221 else if( conf.containsKey(COMPONENT_ROLE_KEY) )
222 {
223
224
225 String roleConfigurationFileName = conf.getString(
226 COMPONENT_ROLE_KEY,
227 COMPONENT_ROLE_VALUE
228 );
229
230
231
232 String componentConfigurationFileName = conf.getString(
233 COMPONENT_CONFIG_KEY,
234 COMPONENT_CONFIG_VALUE
235 );
236
237
238
239 String parametersFileName = conf.getString(
240 COMPONENT_PARAMETERS_KEY,
241 COMPONENT_PARAMETERS_VALUE
242 );
243
244 result.setComponentRolesLocation( roleConfigurationFileName );
245 result.setComponentConfigurationLocation( componentConfigurationFileName );
246 result.setParametersLocation( parametersFileName );
247 }
248 else
249 {
250
251
252 String containerConfiguration = conf.getString(
253 CONTAINER_CONFIGURATION_KEY,
254 CONTAINER_CONFIGURATION_VALUE
255 );
256
257 result.loadContainerConfiguration(containerConfiguration);
258 }
259
260 return result;
261 }
262
263
264
265
266
267 protected Logger createAvalonLogger()
268 {
269 Logger result = new CommonsLogger(log, AVALON_LOG_CATEGORY);
270 return result;
271 }
272
273
274
275
276
277
278
279
280 @Override
281 public boolean exists(String roleName)
282 {
283 return this.hasService(roleName);
284 }
285
286
287
288
289 @Override
290 public Object get(String roleName) throws InstantiationException
291 {
292 try
293 {
294 return this.lookup(roleName);
295 }
296 catch (ServiceException e)
297 {
298 String msg = "Unable to get the following service : " + roleName;
299 log.error(msg);
300 throw new InstantiationException(msg);
301 }
302 catch (Throwable t)
303 {
304 String msg = "Unable to get the following service : " + roleName;
305 log.error(msg,t);
306 throw new InstantiationException(msg,t);
307 }
308 }
309 }