You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by mc...@apache.org on 2002/11/19 13:21:18 UTC

cvs commit: jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/assembly/type TypeManager.java DefaultTypeManager.java

mcconnell    2002/11/19 04:21:18

  Modified:    assembly/src/java/org/apache/excalibur/assembly/type
                        TypeManager.java DefaultTypeManager.java
  Log:
  Improving the low-level type management to provide static suport for
  Type instance creation, improving interfaces, and adding required registry
  functionality.
  
  Revision  Changes    Path
  1.3       +11 -12    jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/assembly/type/TypeManager.java
  
  Index: TypeManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/assembly/type/TypeManager.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TypeManager.java	19 Nov 2002 03:12:20 -0000	1.2
  +++ TypeManager.java	19 Nov 2002 12:21:18 -0000	1.3
  @@ -56,6 +56,7 @@
   package org.apache.excalibur.assembly.type;
   
   import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.excalibur.assembly.service.ServiceManager;
   import org.apache.excalibur.meta.info.Type;
   import org.apache.excalibur.meta.info.builder.TypeCreator;
   import org.apache.excalibur.meta.info.DependencyDescriptor;
  @@ -68,18 +69,16 @@
    * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
    * @version $Revision$ $Date$
    */
  -public interface TypeManager
  +public interface TypeManager extends ServiceManager
   {
  +
       /**
  -     * Register a type.  The implementation will create and verirfy 
  -     * a component type instance for the entry if not already known and
  -     * return the existing or new instance to the invoking client.
  -     *
  -     * @param path the component class name
  -     * @return the component type
  -     * @exception Exception if a registration failure occurs
  +     * Locate a {@link Type} instances associated with the 
  +     * supplied implementation classname.
  +     * @return the type matching the supplied implementation classname.
  +     * @exception UnknownTypeException if a matching type cannot be found
        */
  -    Type registerType( String path ) throws TypeException;
  +    public Type getType( Class clazz ) throws UnknownTypeException;
   
       /**
        * Locate a {@link Type} instances associated with the 
  @@ -87,12 +86,12 @@
        * @return the type matching the supplied implementation classname.
        * @exception UnknownTypeException if a matching type cannot be found
        */
  -    Type getType( String classname ) throws UnknownTypeException;
  +    public Type getType( String classname ) throws UnknownTypeException;
   
      /**
       * Locate the set of component types capable of services the supplied 
       * dependency.
  -    * @param a service depedency descriptor
  +    * @param a service dependency descriptor
       * @return a set of types capable of servicing the supplied dependency
       */
       Type[] getTypes( DependencyDescriptor dependency );
  
  
  
  1.4       +129 -58   jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/assembly/type/DefaultTypeManager.java
  
  Index: DefaultTypeManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/assembly/type/DefaultTypeManager.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DefaultTypeManager.java	19 Nov 2002 03:12:20 -0000	1.3
  +++ DefaultTypeManager.java	19 Nov 2002 12:21:18 -0000	1.4
  @@ -71,6 +71,7 @@
   import org.apache.excalibur.meta.info.ServiceDescriptor;
   import org.apache.excalibur.meta.info.StageDescriptor;
   import org.apache.excalibur.meta.verifier.ComponentVerifier;
  +import org.apache.excalibur.assembly.service.DefaultServiceManager;
   
   /**
    * A type manager implemetation provides support for the creation, 
  @@ -79,9 +80,72 @@
    * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
    * @version $Revision$ $Date$
    */
  -public class DefaultTypeManager extends AbstractLogEnabled implements TypeManager
  +public class DefaultTypeManager extends DefaultServiceManager implements TypeManager
   {
       //==============================================================
  +    // static
  +    //==============================================================
  +
  +   /**
  +    * The type builder.
  +    */
  +    private static final TypeBuilder m_builder = new TypeBuilder();
  +
  +    /**
  +     * Create a new type instance.
  +     *
  +     * @param clazz the component implementation class
  +     * @return the component type
  +     */
  +    public static Type createType( Class clazz ) throws TypeException
  +    {
  +        if( clazz == null )
  +        {
  +            throw new NullPointerException("clazz");
  +        }
  +
  +        try
  +        {
  +            return m_builder.build( clazz.getName(), clazz.getClassLoader() );
  +        }
  +        catch( Throwable e )
  +        {
  +            final String error = 
  +              "Could not register a type relative to the path: " 
  +              + clazz.getName()
  +              + " due to a type build error.";
  +            throw new TypeException( error, e );
  +        }
  +    }
  +
  +    /**
  +     * Register a type instance based on a supplied classname.
  +     *
  +     * @param classname the component implementation classname
  +     * @param loader the classloader to use
  +     * @return the component type
  +     */
  +    public static Type createType( String classname, ClassLoader loader ) throws TypeException
  +    {
  +        if( classname == null )
  +        {
  +            throw new NullPointerException("classname");
  +        }
  +
  +        try
  +        {
  +            Class clazz = loader.loadClass( classname );
  +            return createType( clazz );
  +        }
  +        catch( Throwable e )
  +        {
  +            final String error = 
  +              "Unexpected error while attempting to build a type from the classname: " + classname;
  +                throw new TypeException( error, e );
  +        }
  +    }
  +
  +    //==============================================================
       // state
       //==============================================================
   
  @@ -95,15 +159,10 @@
       */
       private final TypeManager m_parent;
   
  -   /**
  -    * The type builder.
  -    */
  -    private final TypeBuilder m_builder = new TypeBuilder();
  -
       /**
        * Table of component types keyed by implementation classname.
        */
  -    private Hashtable m_types = new Hashtable();
  +    private final Hashtable m_types = new Hashtable();
   
       //==============================================================
       // constructor
  @@ -117,6 +176,7 @@
       */
       public DefaultTypeManager( TypeManager parent, ClassLoader classloader )
       {
  +        super( parent, classloader );
           if( classloader == null )
           {
               throw new NullPointerException("classloader");
  @@ -126,65 +186,39 @@
       }
   
       //==============================================================
  -    // LogEnabled
  -    //==============================================================
  -
  -    /**
  -     * Setup logging for all subcomponents
  -     * @param logger the logging channel
  -     */
  -    public void enableLogging( final Logger logger )
  -    {
  -        super.enableLogging( logger );
  -        m_builder.enableLogging( logger.getChildLogger( "builder" ) );
  -    }
  -
  -    //==============================================================
       // TypeManager
       //==============================================================
   
  -    /**
  -     * Register a type.  The implementation will create and verirfy 
  -     * a component type instance for the entry if not already known and
  -     * return the existing or new instance to the invoking client.
  -     *
  -     * @param path the component class name
  -     * @return the component type
  -     */
  -    public Type registerType( String path ) throws TypeException
  +   /**
  +    * Add a type to the manager.
  +    * @param type the component type description.
  +    * @exception DuplicateTypeException if the supplied type is already registered
  +    * @exception TypeException if a type verification failure occurs
  +    */
  +    public void addType( Type type ) throws DuplicateTypeException, TypeException
       {
  -        final String classname = path.replace( '/', '.' );
  +        if( type == null )
  +        {
  +            throw new NullPointerException("type");
  +        }
   
  -        Type type;
  +        final String classname = type.getInfo().getClassname();
           try
           {
               type = getType( classname );
  +            throw new DuplicateTypeException( classname );
           }
           catch( UnknownTypeException ute )
           {
               try
               {
  -                type = m_builder.build( classname, m_classloader );
  -            }
  -            catch( Throwable e )
  -            {
  -                final String error = 
  -                  "Could not register a type relative to the path: " 
  -                  + path
  -                  + " due to a type build error.";
  -                throw new TypeException( error, e );
  -            }
  -
  -            try
  -            {
                   verify( type );
               }
               catch( Throwable e )
               {
                   final String error = 
  -                  "Could not register a type relative to the path: " 
  -                  + path
  -                  + " due to a type verification failure.";
  +                  "Could not register the type: " + classname
  +                  + " due to a verification failure.";
                   throw new TypeException( error, e );
               }
   
  @@ -195,8 +229,21 @@
   
               m_types.put( classname, type );
           }
  +    }
   
  -        return type;
  +    /**
  +     * Locate a {@link Type} instances associated with the 
  +     * supplied implementation classname.
  +     * @return the type matching the supplied implementation classname.
  +     * @exception UnknownTypeException if a matching type cannot be found
  +     */
  +    public Type getType( Class clazz ) throws UnknownTypeException
  +    {
  +        if( clazz == null )
  +        {
  +            throw new NullPointerException("clazz");
  +        }
  +        return getType( clazz.getName() );
       }
   
       /**
  @@ -207,10 +254,22 @@
        */
       public Type getType( String classname ) throws UnknownTypeException
       {
  +        if( classname == null )
  +        {
  +            throw new NullPointerException("classname");
  +        }
  +
           Type type = (Type) m_types.get( classname );
           if( type == null )
           {
  -            throw new UnknownTypeException( classname );
  +            if( m_parent != null )
  +            {
  +                return m_parent.getType( classname );
  +            }
  +            else
  +            {
  +                throw new UnknownTypeException( classname );
  +            }
           }
           return type;
       }
  @@ -223,6 +282,11 @@
       */
       public Type[] getTypes( DependencyDescriptor dependency )
       {
  +        if( dependency == null )
  +        {
  +            throw new NullPointerException("dependency");
  +        }
  +
           ArrayList list = new ArrayList();
           if( m_parent != null )
           {
  @@ -255,6 +319,11 @@
       */
       public Type[] getTypes( StageDescriptor stage )
       {
  +        if( stage == null )
  +        {
  +            throw new NullPointerException("stage");
  +        }
  +
           ArrayList list = new ArrayList();
           if( m_parent != null )
           {
  @@ -284,7 +353,6 @@
           Class clazz = getComponentClass( type );
           Class[] classes = getServiceClasses( type );
           ComponentVerifier verifier = new ComponentVerifier();
  -        verifier.enableLogging( getLogger().getChildLogger( "verifier" ) );
           verifier.verifyComponent( name, clazz, classes );
       }
   
  @@ -304,8 +372,9 @@
           for( int i = 0; i < services.length; i++ )
           {
               ServiceDescriptor service = services[ i ];
  -            if( ( service.getAttribute( "avalon:service.protocol", "native" ).equals( "native" ) )
  -                && ( service.getAttribute( "avalon:service.accessor", null ) == null ) )
  +            if( ( service.getAttribute( 
  +              "avalon:service.protocol", "native" ).equals( "native" ) )
  +              && ( service.getAttribute( "avalon:service.accessor", null ) == null ) )
               {
                   list.add( getServiceClass( services[ i ] ) );
               }
  @@ -334,8 +403,9 @@
           }
           catch( Throwable e )
           {
  -            final String error = "Could not load implementation class for component type: "
  -                + classname;
  +            final String error = 
  +              "Could not load implementation class for component type: "
  +              + classname;
               throw new TypeException( error, e );
           }
       }
  @@ -355,8 +425,9 @@
           }
           catch( Throwable e )
           {
  -            final String error = "Could not load implementation class for service type: "
  -                + classname;
  +            final String error = 
  +              "Could not load implementation class for service type: "
  +              + classname;
               throw new TypeRuntimeException( error, e );
           }
       }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>