KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > container > ContainerUtil


1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowledgment may appear in the software
24  * itself, if and wherever such third-party acknowledgments
25  * normally appear.
26  *
27  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
28  * must not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.apache.avalon.framework.container;
56
57 import org.apache.avalon.framework.activity.Disposable;
58 import org.apache.avalon.framework.activity.Initializable;
59 import org.apache.avalon.framework.activity.Startable;
60 import org.apache.avalon.framework.component.ComponentException;
61 import org.apache.avalon.framework.component.ComponentManager;
62 import org.apache.avalon.framework.component.Composable;
63 import org.apache.avalon.framework.configuration.Configurable;
64 import org.apache.avalon.framework.configuration.Configuration;
65 import org.apache.avalon.framework.configuration.ConfigurationException;
66 import org.apache.avalon.framework.context.Context;
67 import org.apache.avalon.framework.context.ContextException;
68 import org.apache.avalon.framework.context.Contextualizable;
69 import org.apache.avalon.framework.logger.LogEnabled;
70 import org.apache.avalon.framework.logger.Logger;
71 import org.apache.avalon.framework.parameters.ParameterException;
72 import org.apache.avalon.framework.parameters.Parameterizable;
73 import org.apache.avalon.framework.parameters.Parameters;
74 import org.apache.avalon.framework.service.ServiceException;
75 import org.apache.avalon.framework.service.ServiceManager;
76 import org.apache.avalon.framework.service.Serviceable;
77
78 /**
79  * Utility class that makes it easier to transfer
80  * a component throught it's lifecycle stages.
81  *
82  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
83  * @version CVS $Revision: 1.13 $ $Date: 2003/02/11 15:58:40 $
84  */

85 public final class ContainerUtil
86 {
87     /**
88      * Private constructor to block instantiation.
89      */

90     private ContainerUtil()
91     {
92     }
93
94     /**
95      * Run specified object through shutdown lifecycle stages
96      * (Stop and Dispose).
97      *
98      * @param object the object to shutdown
99      * @throws Exception if there is a problem stoppping object
100      */

101     public static void shutdown( final Object JavaDoc object )
102         throws Exception JavaDoc
103     {
104         stop( object );
105         dispose( object );
106     }
107
108     /**
109      * Supply specified object with Logger if it implements the
110      * {@link LogEnabled} interface.
111      *
112      * @param object the object to Start
113      * @param logger the logger to enable component with. May be null
114      * in which case the specified object must not implement LogEnabled.
115      * @throws IllegalArgumentException if the object is LogEnabled but Logger is null
116      */

117     public static void enableLogging( final Object JavaDoc object,
118                                       final Logger logger )
119     {
120         if( object instanceof LogEnabled )
121         {
122             if( null == logger )
123             {
124                 final String JavaDoc message = "logger is null";
125                 throw new IllegalArgumentException JavaDoc( message );
126             }
127             ( (LogEnabled)object ).enableLogging( logger );
128         }
129     }
130
131     /**
132      * Supply specified object with a Context object if it implements the
133      * {@link Contextualizable} interface.
134      *
135      * @param object the object to contextualize
136      * @param context the context object to use for object.
137      * May be null in which case the specified object must not
138      * implement Contextualizable.
139      * @throws ContextException if there is a problem contextualizing object
140      * @throws IllegalArgumentException if the object is Contextualizable but
141      * context is null
142      */

143     public static void contextualize( final Object JavaDoc object,
144                                       final Context context )
145         throws ContextException
146     {
147         if( object instanceof Contextualizable )
148         {
149             if( null == context )
150             {
151                 final String JavaDoc message = "context is null";
152                 throw new IllegalArgumentException JavaDoc( message );
153             }
154             ( (Contextualizable)object ).contextualize( context );
155         }
156     }
157
158     /**
159      * Supply specified object with ServiceManager if it implements the
160      * {@link Serviceable} interface.
161      *
162      * @param object the object to service
163      * @param serviceManager the serviceManager object to use for object.
164      * May be null in which case the specified object must not
165      * implement Serviceable.
166      * @throws ServiceException if there is a problem servicing object
167      * @throws IllegalArgumentException if the object is Servicable but
168      * ServiceManager is null
169      */

170     public static void service( final Object JavaDoc object,
171                                 final ServiceManager serviceManager )
172         throws ServiceException
173     {
174         if( object instanceof Serviceable )
175         {
176             if( null == serviceManager )
177             {
178                 final String JavaDoc message = "ServiceManager is null";
179                 throw new IllegalArgumentException JavaDoc( message );
180             }
181             ( (Serviceable)object ).service( serviceManager );
182         }
183     }
184
185     /**
186      * Supply specified object with ComponentManager if it implements the
187      * {@link Composable} interface.
188      *
189      * @param object the object to compose
190      * @param componentManager the ComponentManager object to use for object.
191      * May be null in which case the specified object must not
192      * implement Composable.
193      * @throws ComponentException if there is a problem composing object
194      * @deprecated compose() is no longer the preferred method via
195      * which components will be supplied with Components. Please
196      * Use service() from Composable instead.
197      * @throws IllegalArgumentException if the object is Composable but
198      * ComponentManager is null
199      */

200     public static void compose( final Object JavaDoc object,
201                                 final ComponentManager componentManager )
202         throws ComponentException
203     {
204         if( object instanceof Composable )
205         {
206             if( null == componentManager )
207             {
208                 final String JavaDoc message = "componentManager is null";
209                 throw new IllegalArgumentException JavaDoc( message );
210             }
211             ( (Composable)object ).compose( componentManager );
212         }
213     }
214
215     /**
216      * Configure specified object if it implements the
217      * {@link Configurable} interface.
218      *
219      * @param object the object to Start
220      * @param configuration the configuration object to use during
221      * configuration. May be null in which case the specified object
222      * must not implement Configurable
223      * @throws ConfigurationException if there is a problem Configuring object,
224      * or the object is Configurable but Configuration is null
225      * @throws IllegalArgumentException if the object is Configurable but
226      * Configuration is null
227      */

228     public static void configure( final Object JavaDoc object,
229                                   final Configuration configuration )
230         throws ConfigurationException
231     {
232         if( object instanceof Configurable )
233         {
234             if( null == configuration )
235             {
236                 final String JavaDoc message = "configuration is null";
237                 throw new IllegalArgumentException JavaDoc( message );
238             }
239             ( (Configurable)object ).configure( configuration );
240         }
241     }
242
243     /**
244      * Parameterize specified object if it implements the
245      * {@link Parameterizable} interface.
246      *
247      * @param object the object to Parameterize.
248      * @param parameters the parameters object to use during Parameterization.
249      * May be null in which case the specified object must not
250      * implement Parameterizable.
251      * @throws ParameterException if there is a problem Parameterizing object
252      * @throws IllegalArgumentException if the object is Parameterizable but
253      * parameters is null
254      */

255     public static void parameterize( final Object JavaDoc object,
256                                      final Parameters parameters )
257         throws ParameterException
258     {
259         if( object instanceof Parameterizable )
260         {
261             if( null == parameters )
262             {
263                 final String JavaDoc message = "parameters is null";
264                 throw new IllegalArgumentException JavaDoc( message );
265             }
266             ( (Parameterizable)object ).parameterize( parameters );
267         }
268     }
269
270     /**
271      * Initialize specified object if it implements the
272      * {@link Initializable} interface.
273      *
274      * @param object the object to Initialize
275      * @throws Exception if there is a problem Initializing object
276      */

277     public static void initialize( final Object JavaDoc object )
278         throws Exception JavaDoc
279     {
280         if( object instanceof Initializable )
281         {
282             ( (Initializable)object ).initialize();
283         }
284     }
285
286     /**
287      * Start specified object if it implements the
288      * {@link Startable} interface.
289      *
290      * @param object the object to Start
291      * @throws Exception if there is a problem Starting object
292      */

293     public static void start( final Object JavaDoc object )
294         throws Exception JavaDoc
295     {
296         if( object instanceof Startable )
297         {
298             ( (Startable)object ).start();
299         }
300     }
301
302     /**
303      * Stop specified object if it implements the
304      * {@link Startable} interface.
305      *
306      * @param object the object to stop
307      * @throws Exception if there is a problem stoppping object
308      */

309     public static void stop( final Object JavaDoc object )
310         throws Exception JavaDoc
311     {
312         if( object instanceof Startable )
313         {
314             ( (Startable)object ).stop();
315         }
316     }
317
318     /**
319      * Dispose specified object if it implements the
320      * {@link Disposable} interface.
321      *
322      * @param object the object to dispose
323      */

324     public static void dispose( final Object JavaDoc object )
325     {
326         if( object instanceof Disposable )
327         {
328             ( (Disposable)object ).dispose();
329         }
330     }
331 }
332
Popular Tags