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