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