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