KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > LifecycleHelper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components;
17
18 import org.apache.avalon.framework.activity.Disposable;
19 import org.apache.avalon.framework.activity.Initializable;
20 import org.apache.avalon.framework.activity.Startable;
21 import org.apache.avalon.framework.component.ComponentManager;
22 import org.apache.avalon.framework.component.Composable;
23 import org.apache.avalon.framework.configuration.Configurable;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.context.Context;
26 import org.apache.avalon.framework.context.Contextualizable;
27 import org.apache.avalon.framework.logger.LogEnabled;
28 import org.apache.avalon.framework.logger.Logger;
29 import org.apache.avalon.framework.parameters.Parameterizable;
30 import org.apache.avalon.framework.parameters.Parameters;
31 import org.apache.avalon.framework.service.ServiceManager;
32 import org.apache.avalon.framework.service.Serviceable;
33
34 import org.apache.avalon.excalibur.component.RoleManageable;
35 import org.apache.avalon.excalibur.component.RoleManager;
36
37 /**
38  * Utility class for setting up Avalon components. Similar to Excalibur's
39  * <code>DefaultComponentFactory</code>, but on existing objects.
40  * <p>
41  *
42  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
43  * @version CVS $Id: LifecycleHelper.java 55210 2004-10-21 08:02:35Z cziegeler $
44  */

45 public class LifecycleHelper {
46
47     /** The Logger for the component
48      */

49     final private Logger logger;
50
51     /** The Context for the component
52      */

53     final private Context context;
54
55     /** The component manager for this component.
56      */

57     final private ComponentManager componentManager;
58
59     /** The service manager for this component.
60      */

61     final private ServiceManager serviceManager;
62
63     /** The configuration for this component.
64      */

65     final private Configuration configuration;
66
67     /** The RoleManager for child ComponentSelectors
68      */

69     final private RoleManager roles;
70
71     /**
72      * Construct a new <code>LifecycleHelper</code> that can be used repeatedly to
73      * setup several components.
74      * <p>
75      * <b>Note</b> : if a parameter is <code>null</code>,
76      * the corresponding method isn't called (e.g. if <code>configuration</code> is
77      * <code>null</code>, <code>configure()</code> isn't called).
78      *
79      * @param logger the <code>Logger</code> to pass to <code>LogEnabled</code>s, unless there is
80      * a <code>LogKitManager</code> and the configuration specifies a logger name.
81      * @param context the <code>Context</code> to pass to <code>Contexutalizable</code>s.
82      * @param componentManager the component manager to pass to <code>Composable</code>s.
83      * @param roles the <code>RoleManager</code> to pass to <code>DefaultComponentSelector</code>s.
84      * @param configuration the <code>Configuration</code> object to pass to new instances.
85      * @deprecated ComponentManager and RoleManager are deprecated
86      */

87     public LifecycleHelper(final Logger logger,
88                            final Context context,
89                            final ComponentManager componentManager,
90                            final RoleManager roles,
91                            final Configuration configuration) {
92         this(logger, context, null, componentManager, roles, configuration);
93     }
94
95     /**
96      * Construct a new <code>LifecycleHelper</code> that can be used repeatedly to
97      * setup several components.
98      * <p>
99      * <b>Note</b> : if a parameter is <code>null</code>,
100      * the corresponding method isn't called (e.g. if <code>configuration</code> is
101      * <code>null</code>, <code>configure()</code> isn't called).
102      *
103      * @param logger the <code>Logger</code> to pass to <code>LogEnabled</code>s, unless there is
104      * a <code>LogKitManager</code> and the configuration specifies a logger name.
105      * @param context the <code>Context</code> to pass to <code>Contexutalizable</code>s.
106      * @param serviceManager the service manager to pass to <code>Serviceable</code>s.
107      * @param roles the <code>RoleManager</code> to pass to <code>DefaultComponentSelector</code>s.
108      * @param configuration the <code>Configuration</code> object to pass to new instances.
109      * @deprecated RoleManager is deprecated
110      */

111     public LifecycleHelper(final Logger logger,
112                            final Context context,
113                            final ServiceManager serviceManager,
114                            final RoleManager roles,
115                            final Configuration configuration) {
116         this(logger, context, serviceManager, null, roles, configuration);
117     }
118
119     /**
120      * Construct a new <code>LifecycleHelper</code> that can be used repeatedly to
121      * setup several components.
122      * <p>
123      * <b>Note</b> : if a parameter is <code>null</code>,
124      * the corresponding method isn't called (e.g. if <code>configuration</code> is
125      * <code>null</code>, <code>configure()</code> isn't called).
126      *
127      * @param logger the <code>Logger</code> to pass to <code>LogEnabled</code>s, unless there is
128      * a <code>LogKitManager</code> and the configuration specifies a logger name.
129      * @param context the <code>Context</code> to pass to <code>Contexutalizable</code>s.
130      * @param serviceManager the service manager to pass to <code>Serviceable</code>s.
131      * @param configuration the <code>Configuration</code> object to pass to new instances.
132      */

133     public LifecycleHelper(final Logger logger,
134                            final Context context,
135                            final ServiceManager serviceManager,
136                            final Configuration configuration) {
137         this(logger, context, serviceManager, null, null, configuration);
138     }
139
140     /**
141      * Construct a new <code>LifecycleHelper</code> that can be used repeatedly to
142      * setup several components.
143      * <p>
144      * <b>Note</b> : if a parameter is <code>null</code>,
145      * the corresponding method isn't called (e.g. if <code>configuration</code> is
146      * <code>null</code>, <code>configure()</code> isn't called).
147      *
148      * @param logger the <code>Logger</code> to pass to <code>LogEnabled</code>s, unless there is
149      * a <code>LogKitManager</code> and the configuration specifies a logger name.
150      * @param context the <code>Context</code> to pass to <code>Contexutalizable</code>s.
151      * @param serviceManager the service manager to pass to <code>Serviceable</code>s.
152      * @param componentManager the component manager to pass to <code>Composable</code>s.
153      * @param roles the <code>RoleManager</code> to pass to <code>DefaultComponentSelector</code>s.
154      * @param configuration the <code>Configuration</code> object to pass to new instances.
155      * @deprecated ComponentManager and RoleManager are deprecated
156      */

157     public LifecycleHelper(final Logger logger,
158                            final Context context,
159                            final ServiceManager serviceManager,
160                            final ComponentManager componentManager,
161                            final RoleManager roles,
162                            final Configuration configuration) {
163         this.logger = logger;
164         this.context = context;
165         this.serviceManager = serviceManager;
166         this.componentManager = componentManager;
167         this.roles = roles;
168         this.configuration = configuration;
169     }
170
171
172     /**
173      * Setup a component, including initialization and start.
174      *
175      * @param component the component to setup.
176      * @return the component passed in, to allow function chaining.
177      * @throws Exception if something went wrong.
178      */

179     public Object JavaDoc setupComponent(Object JavaDoc component) throws Exception JavaDoc {
180         return setupComponent(component, true);
181     }
182
183     /**
184      * Setup a component, and optionnaly initializes (if it's <code>Initializable</code>)
185      * and starts it (if it's <code>Startable</code>).
186      *
187      * @param component the component to setup.
188      * @param initializeAndStart if true, <code>intialize()</code> and <code>start()</code>
189      * will be called.
190      * @return the component passed in, to allow function chaining.
191      * @throws Exception if something went wrong.
192      */

193     public Object JavaDoc setupComponent(Object JavaDoc component, boolean initializeAndStart)
194     throws Exception JavaDoc {
195         return setupComponent(
196             component,
197             this.logger,
198             this.context,
199             this.serviceManager,
200             this.componentManager,
201             this.roles,
202             this.configuration,
203             initializeAndStart);
204     }
205
206     /**
207      * Static equivalent to {@link #setupComponent(Object)}, to be used when there's only one
208      * component to setup.
209      * @deprecated ComponentManager and RoleManager are deprecated
210      */

211     public static Object JavaDoc setupComponent(final Object JavaDoc component,
212                                         final Logger logger,
213                                         final Context context,
214                                         final ComponentManager componentManager,
215                                         final RoleManager roles,
216                                         final Configuration configuration)
217     throws Exception JavaDoc {
218         return setupComponent(
219             component,
220             logger,
221             context,
222             componentManager,
223             roles,
224             configuration,
225             true);
226     }
227
228     /**
229      * Alternative setupComponent method that takes a ServiceManager instead of a ComponentManger.
230      * @deprecated RoleManager is deprecated
231      */

232     public static Object JavaDoc setupComponent(final Object JavaDoc component,
233                                         final Logger logger,
234                                         final Context context,
235                                         final ServiceManager serviceManager,
236                                         final RoleManager roles,
237                                         final Configuration configuration)
238     throws Exception JavaDoc {
239         return setupComponent(
240             component,
241             logger,
242             context,
243             serviceManager,
244             roles,
245             configuration,
246             true);
247     }
248
249     /**
250      * Alternative setupComponent method that takes a ServiceManager instead of a ComponentManger.
251      */

252     public static Object JavaDoc setupComponent(final Object JavaDoc component,
253                                         final Logger logger,
254                                         final Context context,
255                                         final ServiceManager serviceManager,
256                                         final Configuration configuration)
257     throws Exception JavaDoc {
258         return setupComponent(
259             component,
260             logger,
261             context,
262             serviceManager,
263             null,
264             configuration,
265             true);
266     }
267
268     /**
269      * Static equivalent to {@link #setupComponent(Object, boolean)}, to be used when there's only one
270      * component to setup.
271      * @deprecated ComponentManager and RoleManager are deprecated
272      */

273     public static Object JavaDoc setupComponent(final Object JavaDoc component,
274                                         final Logger logger,
275                                         final Context context,
276                                         final ComponentManager componentManager,
277                                         final RoleManager roles,
278                                         final Configuration configuration,
279                                         final boolean initializeAndStart)
280     throws Exception JavaDoc {
281         return setupComponent(
282             component,
283             logger,
284             context,
285             null,
286             componentManager,
287             roles,
288             configuration,
289             initializeAndStart);
290     }
291
292     /**
293      * Alternative setupComponent method that takes a ServiceManager instead of a ComponentManger.
294      * @deprecated RoleManager is deprecated
295      */

296     public static Object JavaDoc setupComponent(final Object JavaDoc component,
297                                         final Logger logger,
298                                         final Context context,
299                                         final ServiceManager serviceManager,
300                                         final RoleManager roles,
301                                         final Configuration configuration,
302                                         final boolean initializeAndStart)
303     throws Exception JavaDoc {
304         return setupComponent(
305             component,
306             logger,
307             context,
308             serviceManager,
309             null,
310             roles,
311             configuration,
312             initializeAndStart);
313     }
314
315     /**
316      * Alternative setupComponent method that takes a ServiceManager instead of a ComponentManger.
317      */

318     public static Object JavaDoc setupComponent(final Object JavaDoc component,
319                                         final Logger logger,
320                                         final Context context,
321                                         final ServiceManager serviceManager,
322                                         final Configuration configuration,
323                                         final boolean initializeAndStart)
324     throws Exception JavaDoc {
325         return setupComponent(
326             component,
327             logger,
328             context,
329             serviceManager,
330             null,
331             null,
332             configuration,
333             initializeAndStart);
334     }
335
336     /**
337      * Static equivalent to {@link #setupComponent(Object, boolean)}, to be used when there's only one
338      * component to setup.
339      * @deprecated ComponentManager and RoleManager are deprecated
340      */

341     public static Object JavaDoc setupComponent(final Object JavaDoc component,
342                                  final Logger logger,
343                                  final Context context,
344                                  final ServiceManager serviceManager,
345                                  final ComponentManager componentManager,
346                                  final RoleManager roles,
347                                  final Configuration configuration,
348                                  final boolean initializeAndStart)
349     throws Exception JavaDoc {
350         if (component instanceof LogEnabled) {
351             ((LogEnabled) component).enableLogging(logger);
352         }
353
354         if (null != context && component instanceof Contextualizable) {
355             ((Contextualizable) component).contextualize(context);
356         }
357
358         if (null != componentManager && component instanceof Composable) {
359             ((Composable) component).compose(componentManager);
360         }
361
362         if (null != serviceManager && component instanceof Serviceable) {
363             ((Serviceable) component).service(serviceManager);
364         }
365         
366         if (null != roles && component instanceof RoleManageable) {
367             ((RoleManageable) component).setRoleManager(roles);
368         }
369
370         if (null != configuration && component instanceof Configurable) {
371             ((Configurable) component).configure(configuration);
372         }
373
374         if (null != configuration && component instanceof Parameterizable) {
375             ((Parameterizable) component).parameterize(
376                 Parameters.fromConfiguration(configuration));
377         }
378
379         if (initializeAndStart && component instanceof Initializable) {
380             ((Initializable) component).initialize();
381         }
382
383         if (initializeAndStart && component instanceof Startable) {
384             ((Startable) component).start();
385         }
386
387         return component;
388     }
389
390     /**
391      * Decomission a component, by stopping (if it's <code>Startable</code>) and
392      * disposing (if it's <code>Disposable</code>) a component.
393      */

394     public static final void decommission(final Object JavaDoc component)
395     throws Exception JavaDoc {
396         if (component instanceof Startable) {
397             ((Startable) component).stop();
398         }
399
400         dispose(component);
401     }
402
403     /**
404      * Dispose a component if it's <code>Disposable</code>. Otherwhise, do nothing.
405      */

406     public static final void dispose(final Object JavaDoc component) {
407         if (component instanceof Disposable) {
408             ((Disposable) component).dispose();
409         }
410     }
411 }
412
Popular Tags