View Javadoc

1   /*
2    * Copyright (C) The DNA Group. All rights reserved.
3    *
4    * This software is published under the terms of the DNA
5    * Software License version 1.1, a copy of which has been included
6    * with this distribution in the LICENSE.txt file.
7    */
8   package org.codehaus.dna.impl;
9   
10  import org.codehaus.dna.Active;
11  import org.codehaus.dna.Composable;
12  import org.codehaus.dna.Configurable;
13  import org.codehaus.dna.Configuration;
14  import org.codehaus.dna.ConfigurationException;
15  import org.codehaus.dna.LogEnabled;
16  import org.codehaus.dna.Logger;
17  import org.codehaus.dna.MissingResourceException;
18  import org.codehaus.dna.ResourceLocator;
19  
20  /***
21   * Utility class to make it easier to process a object
22   * through its lifecycle stages.
23   *
24   * @version $Revision: 1.2 $ $Date: 2004/05/01 09:51:48 $
25   */
26  public class ContainerUtil
27  {
28      /***
29       * Supply specified object with Logger if it implements the
30       * LogEnabled interface.
31       *
32       * @param object the object to process
33       * @param logger the logger. If null then the specified object must
34       *        not implement LogEnabled.
35       * @throws IllegalArgumentException if the object is LogEnabled and
36       *         Logger is null
37       */
38      public static void enableLogging( final Object object,
39                                        final Logger logger )
40      {
41          if( object instanceof LogEnabled )
42          {
43              if( null == logger )
44              {
45                  final String message = "Null logger.";
46                  throw new IllegalArgumentException( message );
47              }
48              ( (LogEnabled)object ).enableLogging( logger );
49          }
50      }
51  
52      /***
53       * Supply specified object with ResourceLocator if it implements the
54       * Composable interface.
55       *
56       * @param object the object to process
57       * @param locator the ResourceLocator. If null then the specified
58       *        object must not implement Composable.
59       * @throws IllegalArgumentException if the object is Composable
60       *         and locator is null
61       * @throws MissingResourceException if processing lifecycle stage on
62       *         object throws exception
63       */
64      public static void compose( final Object object,
65                                  final ResourceLocator locator )
66          throws MissingResourceException
67      {
68          if( object instanceof Composable )
69          {
70              if( null == locator )
71              {
72                  final String message = "Null locator.";
73                  throw new IllegalArgumentException( message );
74              }
75              ( (Composable)object ).compose( locator );
76          }
77      }
78  
79      /***
80       * Supply specified object with Configuration if it implements the
81       * Configurable interface.
82       *
83       * @param object the object to process
84       * @param configuration the Configuration. If null then the specified
85       *        object must not implement Configurable.
86       * @throws IllegalArgumentException if the object is Configurable
87       *         and configuration is null
88       * @throws ConfigurationException if processing lifecycle stage on
89       *         object throws exception
90       */
91      public static void configure( final Object object,
92                                    final Configuration configuration )
93          throws ConfigurationException
94      {
95          if( object instanceof Configurable )
96          {
97              if( null == configuration )
98              {
99                  final String message = "Null configuration.";
100                 throw new IllegalArgumentException( message );
101             }
102             ( (Configurable)object ).configure( configuration );
103         }
104     }
105 
106     /***
107      * Initialize specified object if it implements the
108      * Active interface.
109      *
110      * @param object the object to process
111      * @throws Exception if processing lifecycle stage on
112      *         object throws exception
113      */
114     public static void initialize( final Object object )
115         throws Exception
116     {
117         if( object instanceof Active )
118         {
119             ( (Active)object ).initialize();
120         }
121     }
122 
123     /***
124      * Dispose specified object if it implements the
125      * Active interface.
126      *
127      * @param object the object to process
128      * @throws Exception if processing lifecycle stage on
129      *         object throws exception
130      */
131     public static void dispose( final Object object )
132         throws Exception
133     {
134         if( object instanceof Active )
135         {
136             ( (Active)object ).dispose();
137         }
138     }
139 }