1 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 45 public class LifecycleHelper { 46 47 49 final private Logger logger; 50 51 53 final private Context context; 54 55 57 final private ComponentManager componentManager; 58 59 61 final private ServiceManager serviceManager; 62 63 65 final private Configuration configuration; 66 67 69 final private RoleManager roles; 70 71 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 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 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 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 179 public Object setupComponent(Object component) throws Exception { 180 return setupComponent(component, true); 181 } 182 183 193 public Object setupComponent(Object component, boolean initializeAndStart) 194 throws Exception { 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 211 public static Object setupComponent(final Object component, 212 final Logger logger, 213 final Context context, 214 final ComponentManager componentManager, 215 final RoleManager roles, 216 final Configuration configuration) 217 throws Exception { 218 return setupComponent( 219 component, 220 logger, 221 context, 222 componentManager, 223 roles, 224 configuration, 225 true); 226 } 227 228 232 public static Object setupComponent(final Object component, 233 final Logger logger, 234 final Context context, 235 final ServiceManager serviceManager, 236 final RoleManager roles, 237 final Configuration configuration) 238 throws Exception { 239 return setupComponent( 240 component, 241 logger, 242 context, 243 serviceManager, 244 roles, 245 configuration, 246 true); 247 } 248 249 252 public static Object setupComponent(final Object component, 253 final Logger logger, 254 final Context context, 255 final ServiceManager serviceManager, 256 final Configuration configuration) 257 throws Exception { 258 return setupComponent( 259 component, 260 logger, 261 context, 262 serviceManager, 263 null, 264 configuration, 265 true); 266 } 267 268 273 public static Object setupComponent(final Object 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 { 281 return setupComponent( 282 component, 283 logger, 284 context, 285 null, 286 componentManager, 287 roles, 288 configuration, 289 initializeAndStart); 290 } 291 292 296 public static Object setupComponent(final Object 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 { 304 return setupComponent( 305 component, 306 logger, 307 context, 308 serviceManager, 309 null, 310 roles, 311 configuration, 312 initializeAndStart); 313 } 314 315 318 public static Object setupComponent(final Object component, 319 final Logger logger, 320 final Context context, 321 final ServiceManager serviceManager, 322 final Configuration configuration, 323 final boolean initializeAndStart) 324 throws Exception { 325 return setupComponent( 326 component, 327 logger, 328 context, 329 serviceManager, 330 null, 331 null, 332 configuration, 333 initializeAndStart); 334 } 335 336 341 public static Object setupComponent(final Object 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 { 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 394 public static final void decommission(final Object component) 395 throws Exception { 396 if (component instanceof Startable) { 397 ((Startable) component).stop(); 398 } 399 400 dispose(component); 401 } 402 403 406 public static final void dispose(final Object component) { 407 if (component instanceof Disposable) { 408 ((Disposable) component).dispose(); 409 } 410 } 411 } 412 | Popular Tags |