001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components;
018:
019: import org.apache.avalon.framework.activity.Disposable;
020: import org.apache.avalon.framework.activity.Initializable;
021: import org.apache.avalon.framework.activity.Startable;
022: import org.apache.avalon.framework.component.ComponentManager;
023: import org.apache.avalon.framework.component.Composable;
024: import org.apache.avalon.framework.configuration.Configurable;
025: import org.apache.avalon.framework.configuration.Configuration;
026: import org.apache.avalon.framework.context.Context;
027: import org.apache.avalon.framework.context.Contextualizable;
028: import org.apache.avalon.framework.logger.LogEnabled;
029: import org.apache.avalon.framework.logger.Logger;
030: import org.apache.avalon.framework.parameters.Parameterizable;
031: import org.apache.avalon.framework.parameters.Parameters;
032: import org.apache.avalon.framework.service.ServiceManager;
033: import org.apache.avalon.framework.service.Serviceable;
034:
035: import org.apache.avalon.excalibur.component.RoleManageable;
036: import org.apache.avalon.excalibur.component.RoleManager;
037:
038: /**
039: * Utility class for setting up Avalon components. Similar to Excalibur's
040: * <code>DefaultComponentFactory</code>, but on existing objects.
041: * <p>
042: *
043: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
044: * @version CVS $Id: LifecycleHelper.java 433543 2006-08-22 06:22:54Z crossley $
045: */
046: public class LifecycleHelper {
047:
048: /** The Logger for the component
049: */
050: final private Logger logger;
051:
052: /** The Context for the component
053: */
054: final private Context context;
055:
056: /** The component manager for this component.
057: */
058: final private ComponentManager componentManager;
059:
060: /** The service manager for this component.
061: */
062: final private ServiceManager serviceManager;
063:
064: /** The configuration for this component.
065: */
066: final private Configuration configuration;
067:
068: /** The RoleManager for child ComponentSelectors
069: */
070: final private RoleManager roles;
071:
072: /**
073: * Construct a new <code>LifecycleHelper</code> that can be used repeatedly to
074: * setup several components.
075: * <p>
076: * <b>Note</b> : if a parameter is <code>null</code>,
077: * the corresponding method isn't called (e.g. if <code>configuration</code> is
078: * <code>null</code>, <code>configure()</code> isn't called).
079: *
080: * @param logger the <code>Logger</code> to pass to <code>LogEnabled</code>s, unless there is
081: * a <code>LogKitManager</code> and the configuration specifies a logger name.
082: * @param context the <code>Context</code> to pass to <code>Contexutalizable</code>s.
083: * @param componentManager the component manager to pass to <code>Composable</code>s.
084: * @param roles the <code>RoleManager</code> to pass to <code>DefaultComponentSelector</code>s.
085: * @param configuration the <code>Configuration</code> object to pass to new instances.
086: * @deprecated ComponentManager and RoleManager are deprecated
087: */
088: public LifecycleHelper(final Logger logger, final Context context,
089: final ComponentManager componentManager,
090: final RoleManager roles, final Configuration configuration) {
091: this (logger, context, null, componentManager, roles,
092: configuration);
093: }
094:
095: /**
096: * Construct a new <code>LifecycleHelper</code> that can be used repeatedly to
097: * setup several components.
098: * <p>
099: * <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, final Context context,
112: final ServiceManager serviceManager,
113: final RoleManager roles, final Configuration configuration) {
114: this (logger, context, serviceManager, null, roles,
115: configuration);
116: }
117:
118: /**
119: * Construct a new <code>LifecycleHelper</code> that can be used repeatedly to
120: * setup several components.
121: * <p>
122: * <b>Note</b> : if a parameter is <code>null</code>,
123: * the corresponding method isn't called (e.g. if <code>configuration</code> is
124: * <code>null</code>, <code>configure()</code> isn't called).
125: *
126: * @param logger the <code>Logger</code> to pass to <code>LogEnabled</code>s, unless there is
127: * a <code>LogKitManager</code> and the configuration specifies a logger name.
128: * @param context the <code>Context</code> to pass to <code>Contexutalizable</code>s.
129: * @param serviceManager the service manager to pass to <code>Serviceable</code>s.
130: * @param configuration the <code>Configuration</code> object to pass to new instances.
131: */
132: public LifecycleHelper(final Logger logger, final Context context,
133: final ServiceManager serviceManager,
134: final Configuration configuration) {
135: this (logger, context, serviceManager, null, null, configuration);
136: }
137:
138: /**
139: * Construct a new <code>LifecycleHelper</code> that can be used repeatedly to
140: * setup several components.
141: * <p>
142: * <b>Note</b> : if a parameter is <code>null</code>,
143: * the corresponding method isn't called (e.g. if <code>configuration</code> is
144: * <code>null</code>, <code>configure()</code> isn't called).
145: *
146: * @param logger the <code>Logger</code> to pass to <code>LogEnabled</code>s, unless there is
147: * a <code>LogKitManager</code> and the configuration specifies a logger name.
148: * @param context the <code>Context</code> to pass to <code>Contexutalizable</code>s.
149: * @param serviceManager the service manager to pass to <code>Serviceable</code>s.
150: * @param componentManager the component manager to pass to <code>Composable</code>s.
151: * @param roles the <code>RoleManager</code> to pass to <code>DefaultComponentSelector</code>s.
152: * @param configuration the <code>Configuration</code> object to pass to new instances.
153: * @deprecated ComponentManager and RoleManager are deprecated
154: */
155: public LifecycleHelper(final Logger logger, final Context context,
156: final ServiceManager serviceManager,
157: final ComponentManager componentManager,
158: final RoleManager roles, final Configuration configuration) {
159: this .logger = logger;
160: this .context = context;
161: this .serviceManager = serviceManager;
162: this .componentManager = componentManager;
163: this .roles = roles;
164: this .configuration = configuration;
165: }
166:
167: /**
168: * Setup a component, including initialization and start.
169: *
170: * @param component the component to setup.
171: * @return the component passed in, to allow function chaining.
172: * @throws Exception if something went wrong.
173: */
174: public Object setupComponent(Object component) throws Exception {
175: return setupComponent(component, true);
176: }
177:
178: /**
179: * Setup a component, and optionnaly initializes (if it's <code>Initializable</code>)
180: * and starts it (if it's <code>Startable</code>).
181: *
182: * @param component the component to setup.
183: * @param initializeAndStart if true, <code>intialize()</code> and <code>start()</code>
184: * will be called.
185: * @return the component passed in, to allow function chaining.
186: * @throws Exception if something went wrong.
187: */
188: public Object setupComponent(Object component,
189: boolean initializeAndStart) throws Exception {
190: return setupComponent(component, this .logger, this .context,
191: this .serviceManager, this .componentManager, this .roles,
192: this .configuration, initializeAndStart);
193: }
194:
195: /**
196: * Static equivalent to {@link #setupComponent(Object)}, to be used when there's only one
197: * component to setup.
198: * @deprecated ComponentManager and RoleManager are deprecated
199: */
200: public static Object setupComponent(final Object component,
201: final Logger logger, final Context context,
202: final ComponentManager componentManager,
203: final RoleManager roles, final Configuration configuration)
204: throws Exception {
205: return setupComponent(component, logger, context,
206: componentManager, roles, configuration, true);
207: }
208:
209: /**
210: * Alternative setupComponent method that takes a ServiceManager instead of a ComponentManger.
211: * @deprecated RoleManager is deprecated
212: */
213: public static Object setupComponent(final Object component,
214: final Logger logger, final Context context,
215: final ServiceManager serviceManager,
216: final RoleManager roles, final Configuration configuration)
217: throws Exception {
218: return setupComponent(component, logger, context,
219: serviceManager, roles, configuration, true);
220: }
221:
222: /**
223: * Alternative setupComponent method that takes a ServiceManager instead of a ComponentManger.
224: */
225: public static Object setupComponent(final Object component,
226: final Logger logger, final Context context,
227: final ServiceManager serviceManager,
228: final Configuration configuration) throws Exception {
229: return setupComponent(component, logger, context,
230: serviceManager, null, configuration, true);
231: }
232:
233: /**
234: * Static equivalent to {@link #setupComponent(Object, boolean)}, to be used when there's only one
235: * component to setup.
236: * @deprecated ComponentManager and RoleManager are deprecated
237: */
238: public static Object setupComponent(final Object component,
239: final Logger logger, final Context context,
240: final ComponentManager componentManager,
241: final RoleManager roles, final Configuration configuration,
242: final boolean initializeAndStart) throws Exception {
243: return setupComponent(component, logger, context, null,
244: componentManager, roles, configuration,
245: initializeAndStart);
246: }
247:
248: /**
249: * Alternative setupComponent method that takes a ServiceManager instead of a ComponentManger.
250: * @deprecated RoleManager is deprecated
251: */
252: public static Object setupComponent(final Object component,
253: final Logger logger, final Context context,
254: final ServiceManager serviceManager,
255: final RoleManager roles, final Configuration configuration,
256: final boolean initializeAndStart) throws Exception {
257: return setupComponent(component, logger, context,
258: serviceManager, null, roles, configuration,
259: initializeAndStart);
260: }
261:
262: /**
263: * Alternative setupComponent method that takes a ServiceManager instead of a ComponentManger.
264: */
265: public static Object setupComponent(final Object component,
266: final Logger logger, final Context context,
267: final ServiceManager serviceManager,
268: final Configuration configuration,
269: final boolean initializeAndStart) throws Exception {
270: return setupComponent(component, logger, context,
271: serviceManager, null, null, configuration,
272: initializeAndStart);
273: }
274:
275: /**
276: * Static equivalent to {@link #setupComponent(Object, boolean)}, to be used when there's only one
277: * component to setup.
278: * @deprecated ComponentManager and RoleManager are deprecated
279: */
280: public static Object setupComponent(final Object component,
281: final Logger logger, final Context context,
282: final ServiceManager serviceManager,
283: final ComponentManager componentManager,
284: final RoleManager roles, final Configuration configuration,
285: final boolean initializeAndStart) throws Exception {
286: if (component instanceof LogEnabled) {
287: ((LogEnabled) component).enableLogging(logger);
288: }
289:
290: if (null != context && component instanceof Contextualizable) {
291: ((Contextualizable) component).contextualize(context);
292: }
293:
294: if (null != componentManager && component instanceof Composable) {
295: ((Composable) component).compose(componentManager);
296: }
297:
298: if (null != serviceManager && component instanceof Serviceable) {
299: ((Serviceable) component).service(serviceManager);
300: }
301:
302: if (null != roles && component instanceof RoleManageable) {
303: ((RoleManageable) component).setRoleManager(roles);
304: }
305:
306: if (null != configuration && component instanceof Configurable) {
307: ((Configurable) component).configure(configuration);
308: }
309:
310: if (null != configuration
311: && component instanceof Parameterizable) {
312: ((Parameterizable) component).parameterize(Parameters
313: .fromConfiguration(configuration));
314: }
315:
316: if (initializeAndStart && component instanceof Initializable) {
317: ((Initializable) component).initialize();
318: }
319:
320: if (initializeAndStart && component instanceof Startable) {
321: ((Startable) component).start();
322: }
323:
324: return component;
325: }
326:
327: /**
328: * Decomission a component, by stopping (if it's <code>Startable</code>) and
329: * disposing (if it's <code>Disposable</code>) a component.
330: */
331: public static final void decommission(final Object component)
332: throws Exception {
333: if (component instanceof Startable) {
334: ((Startable) component).stop();
335: }
336:
337: dispose(component);
338: }
339:
340: /**
341: * Dispose a component if it's <code>Disposable</code>. Otherwhise, do nothing.
342: */
343: public static final void dispose(final Object component) {
344: if (component instanceof Disposable) {
345: ((Disposable) component).dispose();
346: }
347: }
348: }
|