mcconnell 2003/02/27 19:45:07 Modified: assembly/src/java/org/apache/avalon/assembly/appliance DefaultApplianceFactory.java DefaultApplianceRepository.java Log: Upgraded support for loading of custom appliance classes as first-class components. Revision Changes Path 1.11 +115 -63 avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/appliance/DefaultApplianceFactory.java Index: DefaultApplianceFactory.java =================================================================== RCS file: /home/cvs/avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/appliance/DefaultApplianceFactory.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- DefaultApplianceFactory.java 27 Feb 2003 23:06:38 -0000 1.10 +++ DefaultApplianceFactory.java 28 Feb 2003 03:45:07 -0000 1.11 @@ -197,48 +197,6 @@ "Could not create appliance factory using : " + classname; throw new ApplianceException( error, e ); } - - - /* - Class clazz; - try - { - clazz = loader.loadClass( classname ); - } - catch( ClassNotFoundException cnfe ) - { - throw new ApplianceException( - "Could not find appliance factory class " + classname, cnfe ); - } - - ApplianceFactory factory; - try - { - factory = (ApplianceFactory) clazz.newInstance(); - if( factory instanceof LogEnabled ) - { - ((LogEnabled)factory).enableLogging( logger ); - } - if( factory instanceof Contextualizable ) - { - DefaultLocator context = new DefaultLocator( system ); - context.put( "urn:assembly:appliance.repository", repository ); - ((Contextualizable)factory).contextualize( context ); - } - if( factory instanceof Initializable ) - { - ((Initializable)factory).initialize(); - } - return factory; - } - catch( Throwable e ) - { - final String error = - "Appliance factory creation failure for the class: " + classname; - throw new ApplianceException( error, e ); - } - } - */ } /** @@ -279,6 +237,8 @@ private ApplianceRepository m_repository; + private PoolManager m_pool; + //--------------------------------------------------------------------------- // contextualization //--------------------------------------------------------------------------- @@ -287,6 +247,7 @@ { m_system = context; m_repository = (ApplianceRepository) context.get( "urn:assembly:appliance.repository" ); + m_pool = (PoolManager) context.get( "urn:assembly:threads.manager" ); } //--------------------------------------------------------------------------- @@ -328,21 +289,102 @@ throw new NullPointerException( "logger" ); } - Appliance appliance = getApplianceInstance( engine, context ); - - appliance.enableLogging( logger ); - + // + // we are building a custom appliance + // + + LifestyleService lifestyle = createLifestyleService( engine, m_pool ); + DefaultLocator locator = null; try { - PoolManager pool = (PoolManager) system.get( "urn:assembly:threads.manager" ); - - DefaultLocator locator = new DefaultLocator( context ); + locator = new DefaultLocator( context ); locator.put( "urn:assembly:engine", engine ); - locator.put( "urn:assembly:lifestyle.service", createLifestyleService( engine, pool ) ); + locator.put( "urn:assembly:lifestyle.service", lifestyle ); locator.put( "urn:assembly:appliance.context", context ); locator.put( "urn:assembly:appliance.system", system ); locator.put( "urn:assembly:appliance.repository", m_repository ); + locator.put( "urn:assembly:threads.manager", m_pool ); locator.makeReadOnly(); + } + catch( Throwable e ) + { + final String error = + "Unexpected error while building appliance context."; + throw new ApplianceException( error, e ); + } + + String classname = context.getApplianceClassname(); + if(( classname == null ) || ( classname.equals( DefaultAppliance.class.getName() ) ) ) + { + + // + // bootstrap the appliance using DefaultAppliance + // + + DefaultAppliance appliance = new DefaultAppliance(); + appliance.enableLogging( logger ); + try + { + appliance.contextualize( locator ); + appliance.initialize(); + return appliance; + } + catch( Throwable e ) + { + final String error = + "Unable to create appliance from context: " + context.getName(); + throw new ApplianceException( error, e ); + } + } + else + { + // + // its a custom appliance so in this case we use an apppliance to + // construct the appliance + // + + try + { + Type type = engine.getRepository().getTypeManager().getType( classname ); + Profile profile = engine.getRepository().getProfileManager().getProfile( type ); + DefaultApplianceContext cntx = new DefaultApplianceContext( profile ); + cntx.put( "urn:assembly:engine", engine ); + cntx.put( "urn:assembly:lifestyle.service", lifestyle ); + cntx.put( "urn:assembly:appliance.context", context ); + cntx.put( "urn:assembly:appliance.system", system ); + cntx.put( "urn:assembly:appliance.repository", m_repository ); + cntx.put( "urn:assembly:threads.manager", m_pool ); + cntx.makeReadOnly(); + + Appliance appliance = engine.createAppliance( cntx, false ); + appliance.assemble( new DependencyGraph() ); + Object object = appliance.resolve( this ); + if( object instanceof Appliance ) + { + return (Appliance) object; + } + else + { + final String error = + "Supplied classname '" + classname + + "' does not implement the Appliance interface."; + throw new ApplianceException( error ); + } + } + catch( Throwable e ) + { + final String error = + "Could not create appliance using : " + classname; + throw new ApplianceException( error, e ); + } + } + + /* + Appliance appliance = getApplianceInstance( engine, context ); + appliance.enableLogging( logger ); + + try + { appliance.contextualize( locator ); appliance.initialize(); } @@ -353,6 +395,7 @@ throw new ApplianceException( error, e ); } return appliance; + */ } /** @@ -389,19 +432,28 @@ * @param pool the pool manager * @return the lifestyle service */ - private LifestyleService createLifestyleService( EngineClassLoader engine, PoolManager pool ) throws Exception + private LifestyleService createLifestyleService( EngineClassLoader engine, PoolManager pool ) throws ApplianceException { - DeploymentService deployment = createDeploymentService(); - DefaultLifestyleService lifestyle = new DefaultLifestyleService(); - lifestyle.enableLogging( getLogger().getChildLogger( "lifestyle" ) ); - DefaultLocator context = new DefaultLocator(); - context.put( "urn:assembly:pool-manager", pool ); - context.put( "urn:assembly:lifecycle.deployment", deployment ); - context.put( "urn:assembly:engine.classloader", engine ); - context.makeReadOnly(); - lifestyle.contextualize( context ); - lifestyle.initialize(); - return lifestyle; + try + { + DeploymentService deployment = createDeploymentService(); + DefaultLifestyleService lifestyle = new DefaultLifestyleService(); + lifestyle.enableLogging( getLogger().getChildLogger( "lifestyle" ) ); + DefaultLocator context = new DefaultLocator(); + context.put( "urn:assembly:pool-manager", pool ); + context.put( "urn:assembly:lifecycle.deployment", deployment ); + context.put( "urn:assembly:engine.classloader", engine ); + context.makeReadOnly(); + lifestyle.contextualize( context ); + lifestyle.initialize(); + return lifestyle; + } + catch( Throwable e ) + { + final String error = + "Unexpected error during bootstrap lifecycle service creation."; + throw new ApplianceException( error, e ); + } } private DeploymentService createDeploymentService() 1.5 +1 -2 avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/appliance/DefaultApplianceRepository.java Index: DefaultApplianceRepository.java =================================================================== RCS file: /home/cvs/avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/appliance/DefaultApplianceRepository.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- DefaultApplianceRepository.java 23 Feb 2003 14:08:43 -0000 1.4 +++ DefaultApplianceRepository.java 28 Feb 2003 03:45:07 -0000 1.5 @@ -127,7 +127,6 @@ } catch( Throwable e ) { - // should not happen final String error = "Unexpected error while creating a root URL for repository: " + name; throw new ApplianceRuntimeException( error, e );
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]