1 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 85 public final class ContainerUtil 86 { 87 90 private ContainerUtil() 91 { 92 } 93 94 101 public static void shutdown( final Object object ) 102 throws Exception 103 { 104 stop( object ); 105 dispose( object ); 106 } 107 108 117 public static void enableLogging( final Object object, 118 final Logger logger ) 119 { 120 if( object instanceof LogEnabled ) 121 { 122 if( null == logger ) 123 { 124 final String message = "logger is null"; 125 throw new IllegalArgumentException ( message ); 126 } 127 ( (LogEnabled)object ).enableLogging( logger ); 128 } 129 } 130 131 143 public static void contextualize( final Object object, 144 final Context context ) 145 throws ContextException 146 { 147 if( object instanceof Contextualizable ) 148 { 149 if( null == context ) 150 { 151 final String message = "context is null"; 152 throw new IllegalArgumentException ( message ); 153 } 154 ( (Contextualizable)object ).contextualize( context ); 155 } 156 } 157 158 170 public static void service( final Object object, 171 final ServiceManager serviceManager ) 172 throws ServiceException 173 { 174 if( object instanceof Serviceable ) 175 { 176 if( null == serviceManager ) 177 { 178 final String message = "ServiceManager is null"; 179 throw new IllegalArgumentException ( message ); 180 } 181 ( (Serviceable)object ).service( serviceManager ); 182 } 183 } 184 185 200 public static void compose( final Object object, 201 final ComponentManager componentManager ) 202 throws ComponentException 203 { 204 if( object instanceof Composable ) 205 { 206 if( null == componentManager ) 207 { 208 final String message = "componentManager is null"; 209 throw new IllegalArgumentException ( message ); 210 } 211 ( (Composable)object ).compose( componentManager ); 212 } 213 } 214 215 228 public static void configure( final Object object, 229 final Configuration configuration ) 230 throws ConfigurationException 231 { 232 if( object instanceof Configurable ) 233 { 234 if( null == configuration ) 235 { 236 final String message = "configuration is null"; 237 throw new IllegalArgumentException ( message ); 238 } 239 ( (Configurable)object ).configure( configuration ); 240 } 241 } 242 243 255 public static void parameterize( final Object object, 256 final Parameters parameters ) 257 throws ParameterException 258 { 259 if( object instanceof Parameterizable ) 260 { 261 if( null == parameters ) 262 { 263 final String message = "parameters is null"; 264 throw new IllegalArgumentException ( message ); 265 } 266 ( (Parameterizable)object ).parameterize( parameters ); 267 } 268 } 269 270 277 public static void initialize( final Object object ) 278 throws Exception 279 { 280 if( object instanceof Initializable ) 281 { 282 ( (Initializable)object ).initialize(); 283 } 284 } 285 286 293 public static void start( final Object object ) 294 throws Exception 295 { 296 if( object instanceof Startable ) 297 { 298 ( (Startable)object ).start(); 299 } 300 } 301 302 309 public static void stop( final Object object ) 310 throws Exception 311 { 312 if( object instanceof Startable ) 313 { 314 ( (Startable)object ).stop(); 315 } 316 } 317 318 324 public static void dispose( final Object object ) 325 { 326 if( object instanceof Disposable ) 327 { 328 ( (Disposable)object ).dispose(); 329 } 330 } 331 } 332 | Popular Tags |