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