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