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.IOException;
23
24 import org.apache.avalon.framework.activity.Disposable;
25 import org.apache.avalon.framework.activity.Initializable;
26 import org.apache.avalon.framework.logger.CommonsLogger;
27 import org.apache.avalon.framework.logger.Logger;
28 import org.apache.avalon.framework.service.ServiceException;
29 import org.apache.commons.configuration.Configuration;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
33 import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration;
34 import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
35 import org.apache.turbine.Turbine;
36 import org.apache.turbine.services.InitializationException;
37 import org.apache.turbine.services.InstantiationException;
38 import org.apache.turbine.services.TurbineBaseService;
39
40
41
42
43
44
45 public class TurbineYaafiComponentService
46 extends TurbineBaseService
47 implements AvalonComponentService, Initializable, Disposable
48 {
49
50 private static Log log = LogFactory.getLog(AVALON_LOG_CATEGORY);
51
52
53 public static final String CONTAINER_CONFIGURATION_KEY = "containerConfiguration";
54
55
56 public static final String CONTAINER_CONFIGURATION_VALUE = "/WEB-INF/conf/containerConfiguration.xml";
57
58
59 public static final String COMPONENT_PARAMETERS_KEY = "parameters";
60
61
62 public static final String COMPONENT_PARAMETERS_VALUE = "/WEB-INF/conf/parameters.properties";
63
64
65 private ServiceContainer container;
66
67
68
69
70
71 public TurbineYaafiComponentService()
72 {
73
74 }
75
76
77
78
79
80
81
82 public void init() throws InitializationException
83 {
84 try
85 {
86 log.info( "Initializing TurbineYaafiComponentService ..." );
87 initialize();
88 setInit(true);
89 }
90 catch (Exception e)
91 {
92 log.error("Exception caught initialising service: ", e);
93 throw new InitializationException("Initializing TurbineYaafiComponentService failed", e);
94 }
95 }
96
97
98
99
100
101
102 public void shutdown()
103 {
104 log.info( "Disposing TurbineYaafiComponentService ..." );
105 dispose();
106 setInit(false);
107 }
108
109
110
111
112
113
114
115
116
117
118 public void initialize() throws Exception
119 {
120
121
122 Configuration conf = this.getConfiguration();
123
124
125
126 String homePath = Turbine.getRealPath("/");
127 log.info( "Using the following home : " + homePath );
128
129
130
131 ServiceContainerConfiguration config =
132 this.createServiceContainerConfiguration(conf);
133
134 config.setLogger( this.createAvalonLogger() );
135 config.setApplicationRootDir( homePath );
136
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 protected ServiceContainerConfiguration createServiceContainerConfiguration( Configuration conf )
203 throws IOException
204 {
205 ServiceContainerConfiguration result = new ServiceContainerConfiguration();
206
207
208
209 if( conf.containsKey(CONTAINER_CONFIGURATION_KEY) )
210 {
211
212
213 String containerConfiguration = conf.getString(
214 CONTAINER_CONFIGURATION_KEY
215 );
216
217 result.loadContainerConfiguration(containerConfiguration);
218 }
219 else if( conf.containsKey(COMPONENT_ROLE_KEY) )
220 {
221
222
223 String roleConfigurationFileName = conf.getString(
224 COMPONENT_ROLE_KEY,
225 COMPONENT_ROLE_VALUE
226 );
227
228
229
230 String componentConfigurationFileName = conf.getString(
231 COMPONENT_CONFIG_KEY,
232 COMPONENT_CONFIG_VALUE
233 );
234
235
236
237 String parametersFileName = conf.getString(
238 COMPONENT_PARAMETERS_KEY,
239 COMPONENT_PARAMETERS_VALUE
240 );
241
242 result.setComponentRolesLocation( roleConfigurationFileName );
243 result.setComponentConfigurationLocation( componentConfigurationFileName );
244 result.setParametersLocation( parametersFileName );
245 }
246 else
247 {
248
249
250 String containerConfiguration = conf.getString(
251 CONTAINER_CONFIGURATION_KEY,
252 CONTAINER_CONFIGURATION_VALUE
253 );
254
255 result.loadContainerConfiguration(containerConfiguration);
256 }
257
258 return result;
259 }
260
261
262
263
264
265 protected Logger createAvalonLogger()
266 {
267 Logger result = new CommonsLogger(log, AVALON_LOG_CATEGORY);
268 return result;
269 }
270
271
272
273
274
275
276
277
278 public boolean exists(String roleName)
279 {
280 return this.hasService(roleName);
281 }
282
283
284
285
286 public Object get(String roleName) throws InstantiationException
287 {
288 try
289 {
290 return this.lookup(roleName);
291 }
292 catch (ServiceException e)
293 {
294 String msg = "Unable to get the following service : " + roleName;
295 log.error(msg);
296 throw new InstantiationException(msg);
297 }
298 catch (Throwable t)
299 {
300 String msg = "Unable to get the following service : " + roleName;
301 log.error(msg,t);
302 throw new InstantiationException(msg,t);
303 }
304 }
305 }