1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.cocoon.components;
18
19 import org.apache.avalon.framework.activity.Disposable;
20 import org.apache.avalon.framework.activity.Initializable;
21 import org.apache.avalon.framework.activity.Startable;
22 import org.apache.avalon.framework.component.ComponentManager;
23 import org.apache.avalon.framework.component.Composable;
24 import org.apache.avalon.framework.configuration.Configurable;
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.context.Context;
27 import org.apache.avalon.framework.context.Contextualizable;
28 import org.apache.avalon.framework.logger.LogEnabled;
29 import org.apache.avalon.framework.logger.Logger;
30 import org.apache.avalon.framework.parameters.Parameterizable;
31 import org.apache.avalon.framework.parameters.Parameters;
32 import org.apache.avalon.framework.service.ServiceManager;
33 import org.apache.avalon.framework.service.Serviceable;
34
35 import org.apache.avalon.excalibur.component.RoleManageable;
36 import org.apache.avalon.excalibur.component.RoleManager;
37
38 /**
39 * Utility class for setting up Avalon components. Similar to Excalibur's
40 * <code>DefaultComponentFactory</code>, but on existing objects.
41 * <p>
42 *
43 * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
44 * @version CVS $Id: LifecycleHelper.java 433543 2006-08-22 06:22:54Z crossley $
45 */
46 public class LifecycleHelper {
47
48 /** The Logger for the component
49 */
50 final private Logger logger;
51
52 /** The Context for the component
53 */
54 final private Context context;
55
56 /** The component manager for this component.
57 */
58 final private ComponentManager componentManager;
59
60 /** The service manager for this component.
61 */
62 final private ServiceManager serviceManager;
63
64 /** The configuration for this component.
65 */
66 final private Configuration configuration;
67
68 /** The RoleManager for child ComponentSelectors
69 */
70 final private RoleManager roles;
71
72 /**
73 * Construct a new <code>LifecycleHelper</code> that can be used repeatedly to
74 * setup several components.
75 * <p>
76 * <b>Note</b> : if a parameter is <code>null</code>,
77 * the corresponding method isn't called (e.g. if <code>configuration</code> is
78 * <code>null</code>, <code>configure()</code> isn't called).
79 *
80 * @param logger the <code>Logger</code> to pass to <code>LogEnabled</code>s, unless there is
81 * a <code>LogKitManager</code> and the configuration specifies a logger name.
82 * @param context the <code>Context</code> to pass to <code>Contexutalizable</code>s.
83 * @param componentManager the component manager to pass to <code>Composable</code>s.
84 * @param roles the <code>RoleManager</code> to pass to <code>DefaultComponentSelector</code>s.
85 * @param configuration the <code>Configuration</code> object to pass to new instances.
86 * @deprecated ComponentManager and RoleManager are deprecated
87 */
88 public LifecycleHelper(final Logger logger,
89 final Context context,
90 final ComponentManager componentManager,
91 final RoleManager roles,
92 final Configuration configuration) {
93 this(logger, context, null, componentManager, roles, configuration);
94 }
95
96 /**
97 * Construct a new <code>LifecycleHelper</code> that can be used repeatedly to
98 * setup several components.
99 * <p>
100 * <b>Note</b> : if a parameter is <code>null</code>,
101 * the corresponding method isn't called (e.g. if <code>configuration</code> is
102 * <code>null</code>, <code>configure()</code> isn't called).
103 *
104 * @param logger the <code>Logger</code> to pass to <code>LogEnabled</code>s, unless there is
105 * a <code>LogKitManager</code> and the configuration specifies a logger name.
106 * @param context the <code>Context</code> to pass to <code>Contexutalizable</code>s.
107 * @param serviceManager the service manager to pass to <code>Serviceable</code>s.
108 * @param roles the <code>RoleManager</code> to pass to <code>DefaultComponentSelector</code>s.
109 * @param configuration the <code>Configuration</code> object to pass to new instances.
110 * @deprecated RoleManager is deprecated
111 */
112 public LifecycleHelper(final Logger logger,
113 final Context context,
114 final ServiceManager serviceManager,
115 final RoleManager roles,
116 final Configuration configuration) {
117 this(logger, context, serviceManager, null, roles, configuration);
118 }
119
120 /**
121 * Construct a new <code>LifecycleHelper</code> that can be used repeatedly to
122 * setup several components.
123 * <p>
124 * <b>Note</b> : if a parameter is <code>null</code>,
125 * the corresponding method isn't called (e.g. if <code>configuration</code> is
126 * <code>null</code>, <code>configure()</code> isn't called).
127 *
128 * @param logger the <code>Logger</code> to pass to <code>LogEnabled</code>s, unless there is
129 * a <code>LogKitManager</code> and the configuration specifies a logger name.
130 * @param context the <code>Context</code> to pass to <code>Contexutalizable</code>s.
131 * @param serviceManager the service manager to pass to <code>Serviceable</code>s.
132 * @param configuration the <code>Configuration</code> object to pass to new instances.
133 */
134 public LifecycleHelper(final Logger logger,
135 final Context context,
136 final ServiceManager serviceManager,
137 final Configuration configuration) {
138 this(logger, context, serviceManager, null, null, configuration);
139 }
140
141 /**
142 * Construct a new <code>LifecycleHelper</code> that can be used repeatedly to
143 * setup several components.
144 * <p>
145 * <b>Note</b> : if a parameter is <code>null</code>,
146 * the corresponding method isn't called (e.g. if <code>configuration</code> is
147 * <code>null</code>, <code>configure()</code> isn't called).
148 *
149 * @param logger the <code>Logger</code> to pass to <code>LogEnabled</code>s, unless there is
150 * a <code>LogKitManager</code> and the configuration specifies a logger name.
151 * @param context the <code>Context</code> to pass to <code>Contexutalizable</code>s.
152 * @param serviceManager the service manager to pass to <code>Serviceable</code>s.
153 * @param componentManager the component manager to pass to <code>Composable</code>s.
154 * @param roles the <code>RoleManager</code> to pass to <code>DefaultComponentSelector</code>s.
155 * @param configuration the <code>Configuration</code> object to pass to new instances.
156 * @deprecated ComponentManager and RoleManager are deprecated
157 */
158 public LifecycleHelper(final Logger logger,
159 final Context context,
160 final ServiceManager serviceManager,
161 final ComponentManager componentManager,
162 final RoleManager roles,
163 final Configuration configuration) {
164 this.logger = logger;
165 this.context = context;
166 this.serviceManager = serviceManager;
167 this.componentManager = componentManager;
168 this.roles = roles;
169 this.configuration = configuration;
170 }
171
172
173 /**
174 * Setup a component, including initialization and start.
175 *
176 * @param component the component to setup.
177 * @return the component passed in, to allow function chaining.
178 * @throws Exception if something went wrong.
179 */
180 public Object setupComponent(Object component) throws Exception {
181 return setupComponent(component, true);
182 }
183
184 /**
185 * Setup a component, and optionnaly initializes (if it's <code>Initializable</code>)
186 * and starts it (if it's <code>Startable</code>).
187 *
188 * @param component the component to setup.
189 * @param initializeAndStart if true, <code>intialize()</code> and <code>start()</code>
190 * will be called.
191 * @return the component passed in, to allow function chaining.
192 * @throws Exception if something went wrong.
193 */
194 public Object setupComponent(Object component, boolean initializeAndStart)
195 throws Exception {
196 return setupComponent(
197 component,
198 this.logger,
199 this.context,
200 this.serviceManager,
201 this.componentManager,
202 this.roles,
203 this.configuration,
204 initializeAndStart);
205 }
206
207 /**
208 * Static equivalent to {@link #setupComponent(Object)}, to be used when there's only one
209 * component to setup.
210 * @deprecated ComponentManager and RoleManager are deprecated
211 */
212 public static Object setupComponent(final Object component,
213 final Logger logger,
214 final Context context,
215 final ComponentManager componentManager,
216 final RoleManager roles,
217 final Configuration configuration)
218 throws Exception {
219 return setupComponent(
220 component,
221 logger,
222 context,
223 componentManager,
224 roles,
225 configuration,
226 true);
227 }
228
229 /**
230 * Alternative setupComponent method that takes a ServiceManager instead of a ComponentManger.
231 * @deprecated RoleManager is deprecated
232 */
233 public static Object setupComponent(final Object component,
234 final Logger logger,
235 final Context context,
236 final ServiceManager serviceManager,
237 final RoleManager roles,
238 final Configuration configuration)
239 throws Exception {
240 return setupComponent(
241 component,
242 logger,
243 context,
244 serviceManager,
245 roles,
246 configuration,
247 true);
248 }
249
250 /**
251 * Alternative setupComponent method that takes a ServiceManager instead of a ComponentManger.
252 */
253 public static Object setupComponent(final Object component,
254 final Logger logger,
255 final Context context,
256 final ServiceManager serviceManager,
257 final Configuration configuration)
258 throws Exception {
259 return setupComponent(
260 component,
261 logger,
262 context,
263 serviceManager,
264 null,
265 configuration,
266 true);
267 }
268
269 /**
270 * Static equivalent to {@link #setupComponent(Object, boolean)}, to be used when there's only one
271 * component to setup.
272 * @deprecated ComponentManager and RoleManager are deprecated
273 */
274 public static Object setupComponent(final Object component,
275 final Logger logger,
276 final Context context,
277 final ComponentManager componentManager,
278 final RoleManager roles,
279 final Configuration configuration,
280 final boolean initializeAndStart)
281 throws Exception {
282 return setupComponent(
283 component,
284 logger,
285 context,
286 null,
287 componentManager,
288 roles,
289 configuration,
290 initializeAndStart);
291 }
292
293 /**
294 * Alternative setupComponent method that takes a ServiceManager instead of a ComponentManger.
295 * @deprecated RoleManager is deprecated
296 */
297 public static Object setupComponent(final Object component,
298 final Logger logger,
299 final Context context,
300 final ServiceManager serviceManager,
301 final RoleManager roles,
302 final Configuration configuration,
303 final boolean initializeAndStart)
304 throws Exception {
305 return setupComponent(
306 component,
307 logger,
308 context,
309 serviceManager,
310 null,
311 roles,
312 configuration,
313 initializeAndStart);
314 }
315
316 /**
317 * Alternative setupComponent method that takes a ServiceManager instead of a ComponentManger.
318 */
319 public static Object setupComponent(final Object component,
320 final Logger logger,
321 final Context context,
322 final ServiceManager serviceManager,
323 final Configuration configuration,
324 final boolean initializeAndStart)
325 throws Exception {
326 return setupComponent(
327 component,
328 logger,
329 context,
330 serviceManager,
331 null,
332 null,
333 configuration,
334 initializeAndStart);
335 }
336
337 /**
338 * Static equivalent to {@link #setupComponent(Object, boolean)}, to be used when there's only one
339 * component to setup.
340 * @deprecated ComponentManager and RoleManager are deprecated
341 */
342 public static Object setupComponent(final Object component,
343 final Logger logger,
344 final Context context,
345 final ServiceManager serviceManager,
346 final ComponentManager componentManager,
347 final RoleManager roles,
348 final Configuration configuration,
349 final boolean initializeAndStart)
350 throws Exception {
351 if (component instanceof LogEnabled) {
352 ((LogEnabled) component).enableLogging(logger);
353 }
354
355 if (null != context && component instanceof Contextualizable) {
356 ((Contextualizable) component).contextualize(context);
357 }
358
359 if (null != componentManager && component instanceof Composable) {
360 ((Composable) component).compose(componentManager);
361 }
362
363 if (null != serviceManager && component instanceof Serviceable) {
364 ((Serviceable) component).service(serviceManager);
365 }
366
367 if (null != roles && component instanceof RoleManageable) {
368 ((RoleManageable) component).setRoleManager(roles);
369 }
370
371 if (null != configuration && component instanceof Configurable) {
372 ((Configurable) component).configure(configuration);
373 }
374
375 if (null != configuration && component instanceof Parameterizable) {
376 ((Parameterizable) component).parameterize(
377 Parameters.fromConfiguration(configuration));
378 }
379
380 if (initializeAndStart && component instanceof Initializable) {
381 ((Initializable) component).initialize();
382 }
383
384 if (initializeAndStart && component instanceof Startable) {
385 ((Startable) component).start();
386 }
387
388 return component;
389 }
390
391 /**
392 * Decomission a component, by stopping (if it's <code>Startable</code>) and
393 * disposing (if it's <code>Disposable</code>) a component.
394 */
395 public static final void decommission(final Object component)
396 throws Exception {
397 if (component instanceof Startable) {
398 ((Startable) component).stop();
399 }
400
401 dispose(component);
402 }
403
404 /**
405 * Dispose a component if it's <code>Disposable</code>. Otherwhise, do nothing.
406 */
407 public static final void dispose(final Object component) {
408 if (component instanceof Disposable) {
409 ((Disposable) component).dispose();
410 }
411 }
412 }