mcconnell    2003/02/27 15:06:39

  Modified:    assembly/src/java/org/apache/avalon/assembly/appliance
                        ContextBuilder.java DefaultAppliance.java
                        DefaultApplianceContext.java
                        DefaultApplianceFactory.java
  Log:
  Enhancements to the appliance factory creation approach.  Factory classes are now 
handled as first class components.
  
  Revision  Changes    Path
  1.6       +1 -72     
avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/appliance/ContextBuilder.java
  
  Index: ContextBuilder.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/appliance/ContextBuilder.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ContextBuilder.java       7 Feb 2003 15:38:57 -0000       1.5
  +++ ContextBuilder.java       27 Feb 2003 23:06:38 -0000      1.6
  @@ -333,75 +333,4 @@
           return typeClass.isAssignableFrom( clazz );
       }
   
  -    /**
  -     * Internal utility to construct a custom context instance.  The object returned
  -     * is based on the declarations in the supplied directive and defaults to the 
  -     * [EMAIL PROTECTED] DefaultContext} class.
  -     *
  -     * @param classloader the classloader to use
  -     * @param directive the context creation directive
  -     * @param map a map to apply to the created instance under its constructor
  -     * @return a context object created in accordance with the supplied directive
  -     * @exception ContextException if a required context value cannot be resolved 
or an error
  -     *   occurs during context value creation
  -     */
  -    static Context createContextInstance( 
  -      ClassLoader classloader, ContextDescriptor descriptor, ContextDirective 
directive, Map map ) 
  -      throws ContextException
  -    {
  -        //
  -        // check for meta-data about how to create a specialized context type
  -        // and if its present then create it using the standard constructor 
  -        // pattern otherwise use the DefaultContext implementation
  -        //
  -
  -        if( directive != null )
  -        {
  -            String classname = directive.getClassname();
  -            Class clazz;
  -            try
  -            {
  -                clazz = classloader.loadClass( classname );
  -            }
  -            catch( ClassNotFoundException cnfe )
  -            {
  -                throw new ContextException(
  -                    "Could not find context class " + classname, cnfe );
  -            }
  -
  -            if( !Context.class.isAssignableFrom( clazz ) )
  -            {
  -                final String error = 
  -                  "Context directive references a type that is not derived from "
  -                  + Context.class.getName();
  -                throw new ContextException( error );
  -            }
  -
  -            try
  -            {
  -                Constructor constructor = clazz.getConstructor(
  -                    new Class[]{Map.class, Context.class} );
  -                return (Context) constructor.newInstance( new Object[]{map, null} );
  -            }
  -            catch( NoSuchMethodException e )
  -            {
  -                final String error = 
  -                  "Custom context class: [" + classname 
  -                  + "] does not implement the required constructor pattern <init>( 
Map, Locator ).";
  - 
  -                throw new ContextException( error, e );
  -            }
  -            catch( Throwable e )
  -            {
  -                throw new ContextException(
  -                    "Unexpected exception while creating context from "
  -                    + classname, e );
  -            }
  -        }
  -        else
  -        {
  -            return new DefaultLocator( map );
  -        }
  -        
  -    }
   }
  
  
  
  1.34      +36 -29    
avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/appliance/DefaultAppliance.java
  
  Index: DefaultAppliance.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/appliance/DefaultAppliance.java,v
  retrieving revision 1.33
  retrieving revision 1.34
  diff -u -r1.33 -r1.34
  --- DefaultAppliance.java     17 Feb 2003 12:06:03 -0000      1.33
  +++ DefaultAppliance.java     27 Feb 2003 23:06:38 -0000      1.34
  @@ -75,6 +75,8 @@
   import org.apache.avalon.assembly.lifestyle.LifestyleHandler;
   import org.apache.avalon.assembly.lifecycle.AssemblyService;
   import org.apache.avalon.assembly.lifecycle.AssemblyException;
  +import org.apache.avalon.assembly.lifecycle.context.Contextualizer;
  +import org.apache.avalon.assembly.lifecycle.context.AvalonContextualizer;
   import org.apache.avalon.assembly.locator.Locator;
   import org.apache.avalon.assembly.locator.Contextualizable;
   import org.apache.avalon.assembly.locator.LocatorException;
  @@ -154,6 +156,8 @@
   
       private static final ContextBuilder BUILDER = new ContextBuilder();
   
  +    private static final Contextualizer DEFAULT_CONTEXTUALIZER = new 
AvalonContextualizer();
  +
       //=====================================================================
       // state
       //=====================================================================
  @@ -180,7 +184,12 @@
       /**
        * The derived component context.
        */
  -    private Context m_context;
  +    private Map m_context;
  +
  +    /**
  +     * The feature map.
  +     */
  +    private Map m_features = new Hashtable();
   
       /**
        * The lifestyle handler.
  @@ -436,12 +445,22 @@
           return m_path;
       }
   
  +    /**
  +     * Return the context directive for the appliance.
  +     *
  +     * @return the context directive
  +     */
  +    public ContextDirective getContextDirective()
  +    {
  +        return m_applianceContext.getProfile().getContext();
  +    }
  +
      /**
       * Get the component context.
       *
       * @return the component context
       */
  -    public Context getContext()
  +    public Map getContextMap()
       {
           return m_context;
       }
  @@ -514,6 +533,16 @@
       }
   
       /**
  +     * Returns a map of appliance custom features.
  +     *
  +     * @return the feature map
  +     */
  +    public Map getFeatures()
  +    {
  +        return m_features;
  +    }
  +
  +    /**
        * Return the logging categories for the profile.
        *
        * @return the logger
  @@ -542,8 +571,7 @@
   
           if( isContextEnabled() )
           {
  -            Map map = buildContextMap();
  -            m_context = buildContextObject( map );
  +            m_context = buildContextMap();
           }
   
           setEnabled( true );
  @@ -627,13 +655,11 @@
           Map map = m_applianceContext.getDeploymentContext();
           try
           {
  -
               //
               // setup the name and the partition
               //
   
               map.put( "urn:avalon:name", m_name );
  -            //map.put( "urn:avalon:partition.name", 
m_applianceContext.getPartitionName() );
               map.put( "urn:avalon:partition.name", getURL().getPath() );
   
               //
  @@ -698,26 +724,6 @@
           }
       }
   
  -   /**
  -    * Assemble the appliance.
  -    * @exception AssemblyException if an error occurs during appliance assembly
  -    */
  -    private Context buildContextObject( Map map ) throws AssemblyException
  -    {
  -        ContextDescriptor descriptor = m_applianceContext.getType().getContext();
  -        ContextDirective directive = m_applianceContext.getContextDirective();
  -        try
  -        {
  -            return BUILDER.createContextInstance( m_engine, descriptor, directive, 
map );
  -        }
  -        catch( Throwable e )
  -        {
  -            final String error = 
  -              "Unable to create context carrier in appliance: " + this;
  -            throw new AssemblyException( error, e );
  -        }
  -    }
  -
       /**
        * Return the context provider.
        *
  @@ -827,8 +833,9 @@
           getLogger().debug( "assembly: " + this );
           m_visited.add( this );
           ContextDescriptor context = getType().getContext();
  -        String ext = context.getAttribute( 
"urn:assembly:lifecycle.context.strategy" );
  -        if(( ext != null ) && ( getContextProvider() == null ))
  +        String ext = context.getAttribute( 
  +          "urn:assembly:lifecycle.context.strategy" );
  +        if(( ext != null ) && ( getContextProvider() == null ) )
           {
               final String message = "resolving context handler for: " + this ;
               getLogger().debug( message );
  
  
  
  1.11      +13 -12    
avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/appliance/DefaultApplianceContext.java
  
  Index: DefaultApplianceContext.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/appliance/DefaultApplianceContext.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- DefaultApplianceContext.java      17 Feb 2003 12:06:34 -0000      1.10
  +++ DefaultApplianceContext.java      27 Feb 2003 23:06:38 -0000      1.11
  @@ -241,7 +241,7 @@
           m_factory = classname;
       }
   
  -  /**
  +   /**
       * Get the appliance base classname.
       * @return the classname of the appliance factory (null if undefined)
       */
  @@ -423,11 +423,11 @@
       * @exception NullPointerException if the supplied map is null
       * @see #getDeploymentContext()
       */
  -    public void setDeploymentContext( final Map map ) throws IllegalStateException
  -    {
  -        checkWriteable();
  -        m_context = map;
  -    }
  +    //public void setDeploymentContext( final Map map ) throws IllegalStateException
  +    //{
  +    //    checkWriteable();
  +    //    m_context = map;
  +    //}
   
      /**
       * Get the deployment context.
  @@ -435,11 +435,12 @@
       */
       public Map getDeploymentContext()
       {
  -        if( m_context == null )
  -        {
  -            return new Hashtable();
  -        }
  -        return m_context;
  +        return m_map;
  +        //if( m_context == null )
  +        //{
  +        //    return new Hashtable();
  +        //}
  +        //return m_context;
       }
   
       /**
  
  
  
  1.10      +120 -33   
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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- DefaultApplianceFactory.java      8 Feb 2003 08:52:11 -0000       1.9
  +++ DefaultApplianceFactory.java      27 Feb 2003 23:06:38 -0000      1.10
  @@ -63,13 +63,10 @@
   import org.apache.avalon.framework.context.ContextException;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.service.DefaultServiceManager;
  +import org.apache.avalon.framework.service.DefaultServiceManager;
   import org.apache.avalon.assembly.lifestyle.LifestyleException;
   import org.apache.avalon.assembly.lifecycle.AssemblyException;
   import org.apache.avalon.assembly.engine.EngineClassLoader;
  -import org.apache.avalon.meta.info.DependencyDescriptor;
  -import org.apache.avalon.meta.info.StageDescriptor;
  -import org.apache.avalon.meta.model.LoggingDirective;
  -import org.apache.avalon.meta.info.Type;
   import org.apache.avalon.assembly.lifestyle.LifestyleService;
   import org.apache.avalon.assembly.lifestyle.DefaultLifestyleService;
   import org.apache.avalon.assembly.lifestyle.LifestyleHandler;
  @@ -78,14 +75,18 @@
   import org.apache.avalon.assembly.lifecycle.DeploymentService;
   import org.apache.avalon.assembly.lifecycle.DefaultDeploymentService;
   import org.apache.avalon.assembly.locator.DefaultLocator;
  +import org.apache.avalon.assembly.locator.Locator;
  +import org.apache.avalon.assembly.locator.Contextualizable;
  +import org.apache.avalon.assembly.logging.LoggingManager;
  +import org.apache.avalon.meta.info.DependencyDescriptor;
  +import org.apache.avalon.meta.info.StageDescriptor;
  +import org.apache.avalon.meta.model.LoggingDirective;
  +import org.apache.avalon.meta.model.Profile;
  +import org.apache.avalon.meta.info.Type;
   import org.apache.excalibur.mpool.DefaultPoolManager;
   import org.apache.excalibur.mpool.PoolManager;
   import org.apache.excalibur.event.command.CommandManager;
   import org.apache.excalibur.event.command.TPCThreadManager;
  -import org.apache.avalon.framework.service.DefaultServiceManager;
  -import org.apache.avalon.assembly.locator.Locator;
  -import org.apache.avalon.assembly.locator.Contextualizable;
  -import org.apache.avalon.assembly.logging.LoggingManager;
   
   /**
    * An appliance factory is a service that provides support for the creation 
  @@ -102,15 +103,35 @@
     implements ApplianceFactory, Contextualizable, Initializable
   {
   
  +   /**
  +    * Utility method to create a new default appliance factory.  
  +    * @param loader the engine classloader
  +    * @param logger the logging channel to apply to the created factory
  +    * @param repository the appliance repository
  +    * @param system the context to apply to the appliance factory
  +    * @return the new appliance factory
  +    * @exception ApplianceException if a creation error occurs
  +    */
       public static ApplianceFactory createApplianceFactory( 
  -       ClassLoader loader, Logger logger, ApplianceRepository repository, Locator 
system ) 
  +       EngineClassLoader loader, Logger logger, ApplianceRepository repository, 
Locator system ) 
          throws ApplianceException
       {
  -        return createApplianceFactory( loader, 
DefaultApplianceFactory.class.getName(), logger, repository, system );
  +        return createApplianceFactory( 
  +          loader, DefaultApplianceFactory.class.getName(), logger, repository, 
system );
       }
   
  +   /**
  +    * Utility method to create a new appliance factory.  
  +    * @param loader the engine classloader
  +    * @param classname the classname of the appliance factory
  +    * @param logger the logging channel to apply to the created factory
  +    * @param repository the appliance repository
  +    * @param system the context to apply to the appliance factory
  +    * @return the new appliance factory
  +    * @exception ApplianceException if a creation error occurs
  +    */
       public static ApplianceFactory createApplianceFactory( 
  -      ClassLoader loader, String classname, Logger logger, ApplianceRepository 
repository, Locator system ) 
  +      EngineClassLoader loader, String classname, Logger logger, 
ApplianceRepository repository, Locator system ) 
         throws ApplianceException
       {
           if( classname == null )
  @@ -138,50 +159,116 @@
               throw new NullPointerException( "repository" );
           }
   
  -        Class clazz;
  +        if( classname.equals( DefaultApplianceFactory.class.getName() ) )
  +        {
  +            return createDefaultApplianceFactory( logger, system, repository );
  +        }
  +
  +        //
  +        // we are building a custom appliance factory
  +        // 
  +          
           try
           {
  -            if( classname.equals( DefaultApplianceFactory.class.getName() ) )
  +            Type type = loader.getRepository().getTypeManager().getType( classname 
);
  +            Profile profile = 
loader.getRepository().getProfileManager().getProfile( type );
  +            DefaultApplianceContext context = new DefaultApplianceContext( profile 
);
  +            context.put( "urn:assembly:appliance.repository", repository );
  +            context.put( "urn:assembly:logging", system.get( "urn:assembly:logging" 
) );
  +            context.makeReadOnly();
  +            Appliance appliance = loader.createAppliance( context, true );
  +            appliance.assemble( new DependencyGraph() );
  +            Object factory = appliance.resolve( loader );
  +            if( factory instanceof ApplianceFactory )
               {
  -                clazz = DefaultApplianceFactory.class;
  +                return (ApplianceFactory) factory;
               }
               else
               {
  -                clazz = loader.loadClass( classname );
  +                final String error = 
  +                  "Supplied classname '" + classname 
  +                  + "' does not implement the Appliancefactory interface.";
  +                throw new ApplianceException( error );
               }
           }
  -        catch( ClassNotFoundException cnfe )
  +        catch( Throwable e )
           {
  -            throw new ApplianceException(
  -                "Could not find appliance factory class " + classname, cnfe );
  +            final String error = 
  +             "Could not create appliance factory using : " + classname;
  +            throw new ApplianceException( error, e );
           }
   
  -        ApplianceFactory factory;
  -        try
  -        {
  -            factory = (ApplianceFactory) clazz.newInstance();
  -            if( factory instanceof LogEnabled )
  +
  +        /*
  +            Class clazz;
  +            try
  +            {
  +                clazz = loader.loadClass( classname );
  +            }
  +            catch( ClassNotFoundException cnfe )
               {
  -                ((LogEnabled)factory).enableLogging( logger );
  +                throw new ApplianceException(
  +                  "Could not find appliance factory class " + classname, cnfe );
               }
  -            if( factory instanceof Contextualizable )
  +
  +            ApplianceFactory factory;
  +            try
               {
  -                DefaultLocator context = new DefaultLocator( system  );
  -                context.put( "urn:assembly:appliance.repository", repository );
  -                ((Contextualizable)factory).contextualize( context );
  +                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;
               }
  -            if( factory instanceof Initializable )
  +            catch( Throwable e )
               {
  -                ((Initializable)factory).initialize();
  +                final String error = 
  +                  "Appliance factory creation failure for the class: " + classname;
  +                throw new ApplianceException( error, e );
               }
  -            return factory;
  +        }
  +        */
  +    }
  +
  +   /**
  +    * Create of a new instance of the default appliance factory.
  +    * @param logger the logging channel to apply to the factory
  +    * @param context the factory context
  +    * @param repository the appliance repository
  +    * @return the new factory
  +    * @exception ApplianceException if a creation error occurs
  +    */
  +    private static ApplianceFactory createDefaultApplianceFactory( 
  +       final Logger logger, final Locator context, final ApplianceRepository 
repository )
  +       throws ApplianceException
  +    {
  +        DefaultApplianceFactory factory = new DefaultApplianceFactory();
  +        factory.enableLogging( logger );
  +        try
  +        {
  +            DefaultLocator locator = new DefaultLocator( context );
  +            locator.put( "urn:assembly:appliance.repository", repository );
  +            factory.contextualize( locator );
  +            factory.initialize();
           }
           catch( Throwable e )
           {
               final String error = 
  -              "Appliance factory creation failure for the class: " + classname;
  +              "Unexpected error while attempting to create default appliance 
factory.";
               throw new ApplianceException( error, e );
           }
  +        return factory;
       }
   
       //---------------------------------------------------------------------------
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to