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 | |
|
23 | |
|
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.List; |
27 | |
|
28 | |
import org.apache.avalon.excalibur.component.DefaultRoleManager; |
29 | |
import org.apache.avalon.excalibur.component.ExcaliburComponentManager; |
30 | |
import org.apache.avalon.excalibur.logger.Log4JLoggerManager; |
31 | |
import org.apache.avalon.excalibur.logger.LoggerManager; |
32 | |
import org.apache.avalon.framework.activity.Disposable; |
33 | |
import org.apache.avalon.framework.activity.Initializable; |
34 | |
import org.apache.avalon.framework.component.Component; |
35 | |
import org.apache.avalon.framework.component.ComponentException; |
36 | |
import org.apache.avalon.framework.configuration.Configuration; |
37 | |
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; |
38 | |
import org.apache.avalon.framework.context.DefaultContext; |
39 | |
import org.apache.avalon.framework.logger.Logger; |
40 | |
import org.apache.avalon.framework.service.ServiceException; |
41 | |
import org.apache.commons.logging.Log; |
42 | |
import org.apache.commons.logging.LogFactory; |
43 | |
import org.apache.turbine.Turbine; |
44 | |
import org.apache.turbine.services.InitializationException; |
45 | |
import org.apache.turbine.services.InstantiationException; |
46 | |
import org.apache.turbine.services.TurbineBaseService; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
@Deprecated |
76 | 0 | public class TurbineAvalonComponentService |
77 | |
extends TurbineBaseService |
78 | |
implements AvalonComponentService, Initializable, Disposable |
79 | |
{ |
80 | |
|
81 | 0 | private static Log log = LogFactory.getLog( |
82 | |
TurbineAvalonComponentService.class); |
83 | |
|
84 | |
|
85 | 0 | private ExcaliburComponentManager manager = null; |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
@Override |
100 | |
public void init() |
101 | |
throws InitializationException |
102 | |
{ |
103 | |
try |
104 | |
{ |
105 | 0 | initialize(); |
106 | |
|
107 | 0 | setInit(true); |
108 | |
} |
109 | 0 | catch (Exception e) |
110 | |
{ |
111 | 0 | throw new InitializationException("init failed", e); |
112 | 0 | } |
113 | 0 | } |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
@Override |
121 | |
public void shutdown() |
122 | |
{ |
123 | 0 | dispose(); |
124 | 0 | setInit(false); |
125 | 0 | } |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
@Override |
137 | |
public void initialize() throws Exception |
138 | |
{ |
139 | 0 | org.apache.commons.configuration.Configuration conf |
140 | |
= getConfiguration(); |
141 | |
|
142 | |
|
143 | 0 | String sysConfigFilename = Turbine.getRealPath( |
144 | |
conf.getString(COMPONENT_CONFIG_KEY, COMPONENT_CONFIG_VALUE)); |
145 | 0 | String roleConfigFilename = Turbine.getRealPath( |
146 | |
conf.getString(COMPONENT_ROLE_KEY, COMPONENT_ROLE_VALUE)); |
147 | |
|
148 | 0 | log.debug("Config File: " + sysConfigFilename); |
149 | 0 | log.debug("Role File: " + roleConfigFilename); |
150 | |
|
151 | |
|
152 | |
|
153 | 0 | DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); |
154 | 0 | Configuration sysConfig = builder.buildFromFile(sysConfigFilename); |
155 | 0 | Configuration roleConfig = builder.buildFromFile(roleConfigFilename); |
156 | |
|
157 | |
|
158 | 0 | LoggerManager lm = new Log4JLoggerManager(); |
159 | |
|
160 | |
|
161 | 0 | DefaultRoleManager roles = new DefaultRoleManager(); |
162 | |
|
163 | 0 | Logger logger = lm.getLoggerForCategory(AVALON_LOG_CATEGORY); |
164 | |
|
165 | 0 | roles.enableLogging(logger); |
166 | 0 | roles.configure(roleConfig); |
167 | |
|
168 | |
|
169 | 0 | manager = new ExcaliburComponentManager(); |
170 | |
|
171 | 0 | manager.setLoggerManager(lm); |
172 | 0 | manager.enableLogging(logger); |
173 | |
|
174 | 0 | DefaultContext context = new DefaultContext(); |
175 | 0 | String realPath = Turbine.getRealPath("/"); |
176 | |
|
177 | 0 | context.put(AvalonComponentService.COMPONENT_APP_ROOT, realPath); |
178 | |
|
179 | |
|
180 | 0 | context.put("urn:avalon:home", realPath); |
181 | 0 | System.setProperty("applicationRoot", realPath); |
182 | 0 | System.setProperty("urn:avalon:home", realPath); |
183 | |
|
184 | 0 | log.debug("Application Root is " + realPath); |
185 | |
|
186 | 0 | manager.contextualize(context); |
187 | 0 | manager.setRoleManager(roles); |
188 | 0 | manager.configure(sysConfig); |
189 | |
|
190 | |
|
191 | 0 | manager.initialize(); |
192 | |
|
193 | 0 | List<Object> lookupComponents = conf.getList(COMPONENT_LOOKUP_KEY, |
194 | |
new ArrayList<Object>()); |
195 | |
|
196 | 0 | for (Iterator<Object> it = lookupComponents.iterator(); it.hasNext();) |
197 | |
{ |
198 | 0 | String component = (String) it.next(); |
199 | |
try |
200 | |
{ |
201 | 0 | Component c = manager.lookup(component); |
202 | 0 | log.info("Lookup for Component " + component + " successful"); |
203 | 0 | manager.release(c); |
204 | |
} |
205 | 0 | catch (Exception e) |
206 | |
{ |
207 | 0 | log.error("Lookup for Component " + component + " failed!"); |
208 | 0 | } |
209 | 0 | } |
210 | 0 | } |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
@Override |
216 | |
public void dispose() |
217 | |
{ |
218 | 0 | manager.dispose(); |
219 | 0 | } |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
@Override |
228 | |
public Object lookup(String roleName) |
229 | |
throws ServiceException |
230 | |
{ |
231 | |
try |
232 | |
{ |
233 | 0 | return manager.lookup(roleName); |
234 | |
} |
235 | 0 | catch (ComponentException e) |
236 | |
{ |
237 | 0 | throw new ServiceException(name, e.getMessage()); |
238 | |
} |
239 | |
} |
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
@Override |
247 | |
public void release(Object component) |
248 | |
{ |
249 | 0 | if( component instanceof Component ) |
250 | |
{ |
251 | 0 | manager.release((Component)component); |
252 | |
} |
253 | 0 | } |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
@Override |
259 | |
public boolean hasService(String roleName) |
260 | |
{ |
261 | 0 | return manager.hasComponent(roleName); |
262 | |
} |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
@Override |
272 | |
public boolean exists(String roleName) |
273 | |
{ |
274 | 0 | return this.hasService(roleName); |
275 | |
} |
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
@Override |
281 | |
public Object get(String roleName) throws InstantiationException |
282 | |
{ |
283 | |
try |
284 | |
{ |
285 | 0 | return this.lookup(roleName); |
286 | |
} |
287 | 0 | catch (ServiceException e) |
288 | |
{ |
289 | 0 | String msg = "Unable to get the following service : " + roleName; |
290 | 0 | log.error(msg); |
291 | 0 | throw new InstantiationException(msg); |
292 | |
} |
293 | 0 | catch (Throwable t) |
294 | |
{ |
295 | 0 | String msg = "Unable to get the following service : " + roleName; |
296 | 0 | log.error(msg,t); |
297 | 0 | throw new InstantiationException(msg,t); |
298 | |
} |
299 | |
} |
300 | |
} |