1 87 package org.codehaus.loom.components.util.lifecycle; 88 89 import org.apache.avalon.framework.activity.Disposable; 90 import org.apache.avalon.framework.activity.Initializable; 91 import org.apache.avalon.framework.activity.Startable; 92 import org.apache.avalon.framework.component.ComponentManager; 93 import org.apache.avalon.framework.component.Composable; 94 import org.apache.avalon.framework.configuration.Configurable; 95 import org.apache.avalon.framework.configuration.Configuration; 96 import org.apache.avalon.framework.container.ContainerUtil; 97 import org.apache.avalon.framework.context.Context; 98 import org.apache.avalon.framework.context.Contextualizable; 99 import org.apache.avalon.framework.logger.LogEnabled; 100 import org.apache.avalon.framework.logger.Logger; 101 import org.apache.avalon.framework.parameters.Parameterizable; 102 import org.apache.avalon.framework.parameters.Parameters; 103 import org.apache.avalon.framework.service.ServiceManager; 104 import org.apache.avalon.framework.service.Serviceable; 105 import org.apache.excalibur.instrument.InstrumentManageable; 106 import org.apache.excalibur.instrument.InstrumentManager; 107 import org.apache.excalibur.instrument.Instrumentable; 108 import org.codehaus.loom.interfaces.LoomException; 109 import org.codehaus.spice.salt.i18n.ResourceManager; 110 import org.codehaus.spice.salt.i18n.Resources; 111 import org.codehaus.dna.AbstractLogEnabled; 112 113 121 public class LifecycleHelper 122 extends AbstractLogEnabled 123 { 124 private static final Resources REZ = 125 ResourceManager.getPackageResources( LifecycleHelper.class ); 126 127 private static final int STAGE_CREATE = 0; 129 private static final int STAGE_LOGGER = 1; 130 private static final int STAGE_INSTRUMENTMGR = 2; 131 private static final int STAGE_CONTEXT = 3; 132 private static final int STAGE_COMPOSE = 4; 133 private static final int STAGE_CONFIG = 5; 134 private static final int STAGE_PARAMETER = 6; 135 private static final int STAGE_INIT = 7; 136 private static final int STAGE_INSTRUMENTABLE = 8; 137 private static final int STAGE_START = 9; 138 private static final int STAGE_STOP = 10; 139 private static final int STAGE_DISPOSE = 11; 140 private static final int STAGE_DESTROY = 12; 141 142 154 public Object startup( final String name, 155 final Object entry, 156 final ResourceProvider provider ) 157 throws LoomException 158 { 159 int stage = 0; 160 try 161 { 162 stage = STAGE_CREATE; 164 notice( name, stage ); 165 final Object object = provider.createObject( entry ); 166 final InstrumentManager instrumentManager = provider.createInstrumentManager( 167 entry ); 168 169 stage = STAGE_LOGGER; 171 if( object instanceof LogEnabled ) 172 { 173 notice( name, stage ); 174 final Logger logger = provider.createLogger( entry ); 175 ContainerUtil.enableLogging( object, logger ); 176 } 177 178 stage = STAGE_INSTRUMENTMGR; 180 if( object instanceof InstrumentManageable ) 181 { 182 notice( name, stage ); 183 184 ( (InstrumentManageable)object ).setInstrumentManager( 185 instrumentManager ); 186 } 187 188 stage = STAGE_CONTEXT; 190 if( object instanceof Contextualizable ) 191 { 192 notice( name, stage ); 193 final Context context = provider.createContext( entry ); 194 ContainerUtil.contextualize( object, context ); 195 } 196 197 stage = STAGE_COMPOSE; 199 if( object instanceof Serviceable ) 200 { 201 notice( name, stage ); 202 final ServiceManager manager = 203 provider.createServiceManager( entry ); 204 ContainerUtil.service( object, manager ); 205 } 206 else if( object instanceof Composable ) 207 { 208 notice( name, stage ); 209 final ComponentManager componentManager = 210 provider.createComponentManager( entry ); 211 ContainerUtil.compose( object, componentManager ); 212 } 213 214 stage = STAGE_CONFIG; 216 if( object instanceof Configurable ) 217 { 218 notice( name, stage ); 219 final Configuration configuration = 220 provider.createConfiguration( entry ); 221 ContainerUtil.configure( object, configuration ); 222 } 223 224 stage = STAGE_PARAMETER; 226 if( object instanceof Parameterizable ) 227 { 228 notice( name, stage ); 229 final Parameters parameters = 230 provider.createParameters( entry ); 231 ContainerUtil.parameterize( object, parameters ); 232 } 233 234 stage = STAGE_INIT; 236 if( object instanceof Initializable ) 237 { 238 notice( name, stage ); 239 ContainerUtil.initialize( object ); 240 } 241 242 stage = STAGE_INSTRUMENTABLE; 244 if( object instanceof Instrumentable ) 245 { 246 notice( name, stage ); 247 final String instrumentableName = provider.createInstrumentableName( 248 entry ); 249 final Instrumentable instrumentable = (Instrumentable)object; 250 instrumentable.setInstrumentableName( instrumentableName ); 251 instrumentManager.registerInstrumentable( instrumentable, 252 instrumentableName ); 253 } 254 255 stage = STAGE_START; 257 if( object instanceof Startable ) 258 { 259 notice( name, stage ); 260 ContainerUtil.start( object ); 261 } 262 263 return object; 264 } 265 catch( final Throwable t ) 266 { 267 fail( name, stage, t ); 268 269 return null; 272 } 273 } 274 275 283 public void shutdown( final String name, 284 final Object object ) 285 throws LoomException 286 { 287 int stage = 0; 289 290 Throwable failure = null; 292 293 if( object instanceof Startable ) 295 { 296 notice( name, STAGE_STOP ); 297 try 298 { 299 ContainerUtil.stop( object ); 300 } 301 catch( final Throwable t ) 302 { 303 safeFail( name, STAGE_STOP, t ); 304 failure = t; 305 stage = STAGE_STOP; 306 } 307 } 308 309 if( object instanceof Disposable ) 311 { 312 notice( name, STAGE_DISPOSE ); 313 try 314 { 315 ContainerUtil.dispose( object ); 316 } 317 catch( final Throwable t ) 318 { 319 safeFail( name, STAGE_DISPOSE, t ); 320 failure = t; 321 stage = STAGE_DISPOSE; 322 } 323 } 324 325 notice( name, STAGE_DESTROY ); 326 327 if( null != failure ) 328 { 329 fail( name, stage, failure ); 330 } 331 } 332 333 340 private void notice( final String name, final int stage ) 341 { 342 if( getLogger().isDebugEnabled() ) 343 { 344 final String message = 345 REZ.format( "lifecycle.stage.notice", 346 name, 347 new Integer ( stage ) ); 348 getLogger().debug( message ); 349 } 350 } 351 352 360 private void safeFail( final String name, 361 final int stage, 362 final Throwable t ) 363 { 364 final String reason = t.toString(); 366 final String message = 367 REZ.format( "lifecycle.fail.error", 368 name, 369 new Integer ( stage ), 370 reason ); 371 getLogger().error( message ); 372 } 373 374 384 private void fail( final String name, 385 final int stage, 386 final Throwable t ) 387 throws LoomException 388 { 389 final String reason = t.toString(); 391 final String message = 392 REZ.format( "lifecycle.fail.error", 393 name, 394 new Integer ( stage ), reason ); 395 getLogger().error( message ); 396 throw new LoomException( message, t ); 397 } 398 } 399 | Popular Tags |