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 | 34 | public class TurbineYaafiComponentService |
47 | |
extends TurbineBaseService |
48 | |
implements AvalonComponentService, Initializable, Disposable |
49 | |
{ |
50 | |
|
51 | 34 | 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 | 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 | |
|
92 | |
|
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 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
@Override |
113 | |
public void initialize() throws Exception |
114 | |
{ |
115 | |
|
116 | |
|
117 | 45 | Configuration conf = this.getConfiguration(); |
118 | |
|
119 | |
|
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 | |
|
129 | 45 | ServiceContainerConfiguration config = |
130 | |
this.createServiceContainerConfiguration(conf, home); |
131 | |
|
132 | |
|
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 | |
|
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 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
@Override |
167 | |
public Object lookup(String roleName) throws ServiceException |
168 | |
{ |
169 | 512 | return this.container.lookup(roleName); |
170 | |
} |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
@Override |
178 | |
public void release(Object component) |
179 | |
{ |
180 | 0 | this.container.release( component ); |
181 | 0 | } |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
@Override |
187 | |
public boolean hasService(String roleName) |
188 | |
{ |
189 | 1028 | 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 | 45 | ServiceContainerConfiguration result = new ServiceContainerConfiguration(); |
205 | |
|
206 | 45 | result.setApplicationRootDir(applicationRoot.getAbsolutePath()); |
207 | 45 | result.setLogger( this.createAvalonLogger() ); |
208 | |
|
209 | |
|
210 | |
|
211 | 45 | if( conf.containsKey(CONTAINER_CONFIGURATION_KEY) ) |
212 | |
{ |
213 | |
|
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 | |
|
224 | |
|
225 | 26 | String roleConfigurationFileName = conf.getString( |
226 | |
COMPONENT_ROLE_KEY, |
227 | |
COMPONENT_ROLE_VALUE |
228 | |
); |
229 | |
|
230 | |
|
231 | |
|
232 | 26 | String componentConfigurationFileName = conf.getString( |
233 | |
COMPONENT_CONFIG_KEY, |
234 | |
COMPONENT_CONFIG_VALUE |
235 | |
); |
236 | |
|
237 | |
|
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 | |
|
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 | |
|
265 | |
|
266 | |
|
267 | |
protected Logger createAvalonLogger() |
268 | |
{ |
269 | 45 | Logger result = new CommonsLogger(log, AVALON_LOG_CATEGORY); |
270 | 45 | return result; |
271 | |
} |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
@Override |
281 | |
public boolean exists(String roleName) |
282 | |
{ |
283 | 1028 | return this.hasService(roleName); |
284 | |
} |
285 | |
|
286 | |
|
287 | |
|
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 | |
} |