ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From darr...@apache.org
Subject cvs commit: jakarta-ant-myrmidon/myrmidon/src/samples namespace-test.ant
Date Fri, 10 May 2002 01:44:48 GMT
darrell     02/05/09 18:44:48

  Modified:    container/src/java/org/apache/myrmidon/components/type
                        DefaultTypeManager.java MultiSourceTypeFactory.java
                        NamespaceAwareTypeFactory.java
               container/src/java/org/apache/myrmidon/interfaces/deployer
                        TypeDeployer.java
               container/src/java/org/apache/myrmidon/interfaces/type
                        TypeManager.java
               container/src/test/org/apache/myrmidon/components/type/test
                        DefaultTypeManagerTestCase.java
               myrmidon/src/samples namespace-test.ant
  Added:       container/src/java/org/apache/myrmidon/components/type
                        AbstractTypeFactory.java
                        AggregatingTypeFactory.java
  Log:
  * Added method for registering a complete TypeFactory of types with the TypeManager.
    This mechanism should be utilised by the deployer for registering complete
    type libraries, rather than registering each type individually (Not yet done).
  
  * Types registered using this method have a lower precedence than types
    registered individually.
  
  * Abstracted common code out of MultiSourceTypeFactory, NamespaceAwareTypeFactory,
    and AggregatingTypeFactory (new), into common superclass.
  
  Revision  Changes    Path
  1.22      +25 -3     jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/DefaultTypeManager.java
  
  Index: DefaultTypeManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/DefaultTypeManager.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- DefaultTypeManager.java	8 May 2002 04:10:26 -0000	1.21
  +++ DefaultTypeManager.java	10 May 2002 01:44:47 -0000	1.22
  @@ -24,7 +24,7 @@
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
    * @author <a href="mailto:darrell@apache.org">Darrell DeBoer</a>
  - * @version $Revision: 1.21 $ $Date: 2002/05/08 04:10:26 $
  + * @version $Revision: 1.22 $ $Date: 2002/05/10 01:44:47 $
    */
   public class DefaultTypeManager
       implements TypeManager, Serviceable
  @@ -67,6 +67,28 @@
           m_roleManager = (RoleManager)serviceManager.lookup( RoleManager.ROLE );
       }
   
  +
  +
  +    /**
  +     * @see TypeManager#registerTypes( String, TypeFactory )
  +     */
  +    public void registerTypes( String roleName, TypeFactory factory )
  +        throws TypeException
  +    {
  +        NamespaceAwareTypeFactory nsFactory = createFactory( roleName );
  +        nsFactory.registerTypes( factory );
  +    }
  +
  +    /**
  +     * @see TypeManager#registerTypes( String, String, TypeFactory )
  +     */
  +    public void registerTypes( String roleName, String namespace, TypeFactory factory )
  +        throws TypeException
  +    {
  +        NamespaceAwareTypeFactory nsFactory = createFactory( roleName );
  +        nsFactory.registerTypes( namespace, factory );
  +    }
  +
       /**
        * @see TypeManager#registerType( String, String, TypeFactory )
        */
  @@ -76,7 +98,7 @@
           throws TypeException
       {
           final NamespaceAwareTypeFactory nsFactory = createFactory( roleName );
  -        nsFactory.register( shorthandName, factory );
  +        nsFactory.registerType( shorthandName, factory );
       }
   
       /**
  @@ -89,7 +111,7 @@
           throws TypeException
       {
           final NamespaceAwareTypeFactory nsFactory = createFactory( roleName );
  -        nsFactory.register( namespace, shorthandName, factory );
  +        nsFactory.registerType( namespace, shorthandName, factory );
       }
   
       /**
  
  
  
  1.18      +8 -59     jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/MultiSourceTypeFactory.java
  
  Index: MultiSourceTypeFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/MultiSourceTypeFactory.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- MultiSourceTypeFactory.java	8 May 2002 04:10:26 -0000	1.17
  +++ MultiSourceTypeFactory.java	10 May 2002 01:44:47 -0000	1.18
  @@ -17,17 +17,14 @@
    * This factory acts as a proxy to set of object factories.
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  - * @version $Revision: 1.17 $ $Date: 2002/05/08 04:10:26 $
  + * @version $Revision: 1.18 $ $Date: 2002/05/10 01:44:47 $
    */
   class MultiSourceTypeFactory
  -    implements TypeFactory
  +    extends AbstractTypeFactory
   {
       private static final Resources REZ =
           ResourceManager.getPackageResources( MultiSourceTypeFactory.class );
   
  -    ///Parent Selector
  -    private final MultiSourceTypeFactory m_parent;
  -
       ///Map of name->factory list
       private final HashMap m_factories = new HashMap();
   
  @@ -37,13 +34,6 @@
       public MultiSourceTypeFactory( final Class type )
       {
           m_type = type;
  -        m_parent = null;
  -    }
  -
  -    public MultiSourceTypeFactory( final MultiSourceTypeFactory parent )
  -    {
  -        m_type = parent.getType();
  -        m_parent = parent;
       }
   
       /**
  @@ -55,33 +45,14 @@
       }
   
       /**
  -     * Determines if this factory can create instances of a particular type.
  -     */
  -    public boolean canCreate( final String name )
  -    {
  -        return ( findFactory( name ) != null );
  -    }
  -
  -    /**
  -     * Create a type instance based on name.
  -     *
  -     * @param name the name
  -     * @return the type instance
  -     * @exception TypeException if an error occurs
  +     * @see AbstractTypeFactory#doCreate( TypeFactory, String )
        */
  -    public Object create( final String name )
  +    public Object doCreate( final TypeFactory factory,
  +                            final String name )
           throws TypeException
       {
  -        // Locate the factory to use
  -        TypeFactory factory = findFactory( name );
  -        if( null == factory )
  -        {
  -            final String message = REZ.getString( "no-factory.error", name );
  -            throw new TypeException( message );
  -        }
  -
           // Create the object
  -        final Object object = factory.create( name );
  +        final Object object = super.doCreate( factory, name );
           if( m_type != null && !m_type.isInstance( object ) )
           {
               final String message = REZ.getString( "mismatched-type.error",
  @@ -93,31 +64,9 @@
       }
   
       /**
  -     * Locates the type factory to use for a particular type.
  -     */
  -    private TypeFactory findFactory( final String name )
  -    {
  -        TypeFactory factory = getTypeFactory( name );
  -        if( null == factory && null != m_parent )
  -        {
  -            factory = m_parent.getTypeFactory( name );
  -        }
  -
  -        return factory;
  -    }
  -
  -    /**
  -     * Retrieve type managed by selector.
  -     * Used by other instances of TypedComponentSelector.
  -     *
  -     * @return the type class
  +     * @see AbstractTypeFactory#findFactory( String )
        */
  -    private final Class getType()
  -    {
  -        return m_type;
  -    }
  -
  -    private final TypeFactory getTypeFactory( final String name )
  +    protected TypeFactory findFactory( final String name )
       {
           return (TypeFactory)m_factories.get( name );
       }
  
  
  
  1.3       +102 -51   jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/NamespaceAwareTypeFactory.java
  
  Index: NamespaceAwareTypeFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/NamespaceAwareTypeFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- NamespaceAwareTypeFactory.java	9 May 2002 05:09:04 -0000	1.2
  +++ NamespaceAwareTypeFactory.java	10 May 2002 01:44:47 -0000	1.3
  @@ -24,10 +24,10 @@
    * searched for a type matching the type name.
    *
    * @author <a href="mailto:darrell@apache.org">Darrell DeBoer</a>
  - * @version $Revision: 1.2 $ $Date: 2002/05/09 05:09:04 $
  + * @version $Revision: 1.3 $ $Date: 2002/05/10 01:44:47 $
    */
   class NamespaceAwareTypeFactory
  -    implements TypeFactory
  +    extends AbstractTypeFactory
   {
   
       private static final Resources REZ =
  @@ -44,7 +44,8 @@
   
       private NamespaceAwareTypeFactory m_parent;
   
  -    private Map m_namespaceFactories = new HashMap();
  +    private Map m_mulitSourceTypeFactories = new HashMap();
  +    private Map m_aggregatingTypeFactories = new HashMap();
   
       /**
        * A cache which stores a map of TypeName -> TypeFactory instance.
  @@ -73,33 +74,74 @@
           m_role = parent.m_role;
       }
   
  -    //    public void register( String namespace, TypeRegistry registry )
  -    //    {
  -    //        m_namespaces.add( namespace );
  -    //        m_namespaceFactories.put( namespace, registry.getFactory( m_role ) );
  -    //    }
  +    /**
  +     * Registers all types in a factory under the default namespace.
  +     * @param newTypes The factory containing types to register.
  +     */
  +    public void registerTypes( final TypeFactory newTypes )
  +    {
  +        registerTypes( null, newTypes );
  +    }
  +
  +    /**
  +     * Registers all types in a factory under the specified namespace.
  +     * @param namespace The namespace to register the types under, or
  +     *                  <code>null</code> to use the default namespace.
  +     * @param newTypes The factory containing types to register.
  +     */
  +    public void registerTypes( String namespace,
  +                               final TypeFactory newTypes )
  +    {
  +        // Use the default namespace if none provided.
  +        if ( namespace == null )
  +        {
  +            namespace = DEFAULT_NAMESPACE;
  +        }
  +
  +        // Get a multi-source factory for this namespace/role,
  +        // creating and adding to the namespace -> factory map if necessary.
  +        AggregatingTypeFactory namespaceFactory =
  +            (AggregatingTypeFactory)m_aggregatingTypeFactories.get( namespace );
  +
  +        if( namespaceFactory == null )
  +        {
  +            namespaceFactory = new AggregatingTypeFactory();
  +            m_aggregatingTypeFactories.put( namespace, namespaceFactory );
  +        }
  +
  +        // Register the type with the namespace-specific multisource factory.
  +        namespaceFactory.addFactory( newTypes );
  +
  +        // Clear the cache of type factories.
  +        clearCache();
  +
  +    }
   
       /**
        * Register a named type under the default namespace ("").
  -     * Equivelant to calling <code>register( null, name, factory )</code>
  +     * Equivelant to calling <code>registerType( null, name, factory )</code>
        * @param name The name of the type.
        * @param factory The factory for instances of this type.
        * @throws TypeException If the type name is invalid.
        */
  -    public void register( String name, TypeFactory factory )
  +    public void registerType( final String name,
  +                              final TypeFactory factory )
           throws TypeException
       {
  -        register( null, name, factory );
  +        registerType( null, name, factory );
       }
   
       /**
        * Register a named type under the specified namespace.
  -     * @param namespace The namespace for the type's registration.
  +     * @param namespace The namespace to register the types under, or
  +     *                  <code>null</code> to use the default namespace.
        * @param name The name of the type.
        * @param factory The factory for instances of this type.
        * @throws TypeException If the type name is invalid.
        */
  -    public void register( String namespace, String name, TypeFactory factory )
  +    public void registerType( String namespace,
  +                              final String name,
  +                              final TypeFactory factory )
           throws TypeException
       {
           // Type short-names may not contain the namespace separator.
  @@ -119,63 +161,48 @@
           // Get a multi-source factory for this namespace/role,
           // creating and adding to the namespace -> factory map if necessary.
           MultiSourceTypeFactory namespaceFactory =
  -            (MultiSourceTypeFactory)m_namespaceFactories.get( namespace );
  +            (MultiSourceTypeFactory)m_mulitSourceTypeFactories.get( namespace );
   
           if( namespaceFactory == null )
           {
               namespaceFactory = new MultiSourceTypeFactory( m_role.getImplementationClass()
);
  -            m_namespaceFactories.put( namespace, namespaceFactory );
  +            m_mulitSourceTypeFactories.put( namespace, namespaceFactory );
           }
   
           // Register the type with the namespace-specific multisource factory.
           namespaceFactory.register( name, factory );
   
           // Clear the cache of type factories.
  -        m_typeFactoryCache.clear();
  +        clearCache();
       }
   
       /**
  -     * @param name Can be either a fully qualified name with namespace,
  -     *             or a simple type name.
  -     * @see TypeFactory#canCreate( String )
  +     * Clears the cache of typename -> type factory.
        */
  -    public boolean canCreate( String name )
  +    private void clearCache()
       {
  -        TypeName qname = parseName( name );
  -        try {
  -            TypeFactory factory = findFactory( qname );
  -
  -            if( factory != null )
  -            {
  -                return true;
  -            }
  -        }
  -        catch( TypeException exc ) {}
  -
  -        return false;
  +        m_typeFactoryCache.clear();
       }
   
       /**
  -     * @param name Can be either a fully qualified name with namespace,
  -     *             or a simple type name.
  -     * @see TypeFactory#create( String )
  +     * @see AbstractTypeFactory#doCreate( TypeFactory, String )
        */
  -    public Object create( String name )
  +    protected Object doCreate( TypeFactory factory,
  +                               String name )
           throws TypeException
       {
  -        TypeName qname = parseName( name );
  -        TypeFactory factory = findFactory( qname );
  -        if( factory != null )
  -        {
  -            return factory.create( qname.getShortName() );
  -        }
  -        else
  -        {
  -            final String message = REZ.getString( "no-factory.error", name );
  -            throw new TypeException( message );
  -        }
  +        String shortName = parseName( name ).getShortName();
  +        return super.doCreate( factory, shortName );
       }
   
  +    /**
  +     * @see AbstractTypeFactory#findFactory( String )
  +     */
  +    protected TypeFactory findFactory( String name )
  +        throws TypeException
  +    {
  +        return findFactory( parseName( name ) );
  +    }
   
       /**
        * Find the factory for the types, first looking in locally registered
  @@ -186,7 +213,7 @@
        *         or <code>null</code> if none can be found.
        * @throws TypeException If an error occurs finding the factory.
        */
  -    private TypeFactory findFactory( TypeName qname )
  +    protected TypeFactory findFactory( TypeName qname )
           throws TypeException
       {
           TypeFactory factory = findLocalFactoryUseCache( qname );
  @@ -225,6 +252,30 @@
       /**
        * Finds the specific TypeFactory which can create an instance of the type
        * specified by a TypeName.
  +     * First looks in individually registered types, and if not found, then
  +     * searches in types registered via a factory registration.
  +     *
  +     * @param qname The TypeName to search for.
  +     * @return The TypeFactory which is able to create the named type,
  +     *         or <code>null</code> if none can be found.
  +     * @throws TypeException If the name cannot be resolved unambiguously.
  +     */
  +    private TypeFactory findLocalFactory( TypeName qname )
  +        throws TypeException
  +    {
  +        // First search in the individually registered types
  +        TypeFactory factory = findLocalFactory( qname, m_mulitSourceTypeFactories );
  +        if ( factory == null )
  +        {
  +            // Then in the factory-registered types.
  +            factory = findLocalFactory( qname, m_aggregatingTypeFactories );
  +        }
  +        return factory;
  +    }
  +
  +    /**
  +     * Searches a set of namespace factories for the factory to create instances
  +     * of the specified type.
        * If the TypeName contains a namespace, only the factory for that namespace
        * is searched, otherwise all namespaces are searched.
        *
  @@ -233,7 +284,7 @@
        *         or <code>null</code> if none can be found.
        * @throws TypeException If the name cannot be resolved unambiguously.
        */
  -    private TypeFactory findLocalFactory( TypeName qname )
  +    private TypeFactory findLocalFactory( TypeName qname, Map factoriesToSearch )
           throws TypeException
       {
           TypeFactory factory = null;
  @@ -241,7 +292,7 @@
           if( qname.hasNamespace() )
           {
               String namespace = qname.getNamespace();
  -            TypeFactory tryFactory = (TypeFactory)m_namespaceFactories.get( namespace );
  +            TypeFactory tryFactory = (TypeFactory)factoriesToSearch.get( namespace );
               if( tryFactory != null && tryFactory.canCreate( qname.getShortName()
) )
               {
                   factory = tryFactory;
  @@ -251,7 +302,7 @@
           {
               // Search all namespaces
               String shortName = qname.getShortName();
  -            Iterator factories = m_namespaceFactories.values().iterator();
  +            Iterator factories = factoriesToSearch.values().iterator();
               while( factories.hasNext() )
               {
                   TypeFactory tryFactory = (TypeFactory)factories.next();
  
  
  
  1.1                  jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/AbstractTypeFactory.java
  
  Index: AbstractTypeFactory.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.myrmidon.components.type;
  
  import org.apache.myrmidon.interfaces.type.TypeFactory;
  import org.apache.myrmidon.interfaces.type.TypeException;
  import org.apache.avalon.excalibur.i18n.Resources;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  
  /**
   * 
   *
   * @author <a href="mailto:darrell@apache.org">Darrell DeBoer</a>
   * @version $Revision: 1.1 $ $Date: 2002/05/10 01:44:47 $
   */
  abstract class AbstractTypeFactory implements TypeFactory
  {
      private static final Resources REZ =
          ResourceManager.getPackageResources( AbstractTypeFactory.class );
  
      /**
       * Determines if this factory can create instances of a particular type.
       * @see TypeFactory#canCreate( String )
       */
      public boolean canCreate( final String name )
      {
          try
          {
              TypeFactory factory = findFactory( name );
  
              if( factory != null )
              {
                  return true;
              }
          }
          catch( TypeException exc )
          {
              // Ignore exceptions caused by ambiguous type names,
              // and return false.
          }
  
          return false;
      }
  
      /**
       * Create a type instance based on name.
       * @see TypeFactory#create( String )
       */
      public Object create( final String name )
          throws TypeException
      {
          // Locate the factory to use
          TypeFactory factory = findFactory( name );
  
          if( null == factory )
          {
              final String message = REZ.getString( "no-factory.error", name );
              throw new TypeException( message );
          }
  
          // Create the object
          return doCreate( factory, name );
      }
  
      /**
       * Call the actual create method on the factory. Subclasses may override/extend
       * this to provide specific checks/functionality.
       * @param factory The factory for the type.
       * @param name The requested type name.
       * @return An instance of the named type.
       * @throws TypeException On problem creating the type.
       */
      protected Object doCreate( final TypeFactory factory,
                                 final String name )
          throws TypeException
      {
          return factory.create( name );
      }
  
      /**
       * Locate the specific TypeFactory for a type name.
       * @param name The name of the type to look for.
       * @return A TypeFactory capable of creating the required type.
       * @throws TypeException If an error occurs, such as the name being ambiguous.
       */
      protected abstract TypeFactory findFactory( final String name )
          throws TypeException;
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/AggregatingTypeFactory.java
  
  Index: AggregatingTypeFactory.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.myrmidon.components.type;
  
  import org.apache.myrmidon.interfaces.type.TypeFactory;
  import org.apache.myrmidon.interfaces.type.TypeException;
  import org.apache.avalon.excalibur.i18n.Resources;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import java.util.Set;
  import java.util.HashSet;
  import java.util.Iterator;
  
  /**
   * A TypeFactory implementation which simply aggregates another set of type factories.
   *
   * @author <a href="mailto:darrell@apache.org">Darrell DeBoer</a>
   * @version $Revision: 1.1 $ $Date: 2002/05/10 01:44:47 $
   */
  class AggregatingTypeFactory
      extends AbstractTypeFactory
  {
      private static final Resources REZ =
          ResourceManager.getPackageResources( AggregatingTypeFactory.class );
  
      private Set m_factories = new HashSet();
  
      /**
       * Adds a factory to the set of aggregated factories.
       * @param factory The factory to add.
       */
      public void addFactory( TypeFactory factory )
      {
          m_factories.add( factory );
      }
  
      /**
       * @see AbstractTypeFactory#findFactory( String )
       */
      protected TypeFactory findFactory( String name )
          throws TypeException
      {
          TypeFactory factory = null;
  
          Iterator iter = m_factories.iterator();
          while( iter.hasNext() )
          {
              TypeFactory tryFactory = (TypeFactory)iter.next();
              if ( tryFactory.canCreate( name ) )
              {
                  // If we've already found a factory, the name is ambiguous.
                  if ( factory != null )
                  {
                      String message = REZ.getString( "ambiguous-type-name.error",
                                                      name );
                      throw new TypeException( message );
                  }
                  factory = tryFactory;
              }
          }
  
          return factory;
      }
  }
  
  
  
  1.8       +2 -1      jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/deployer/TypeDeployer.java
  
  Index: TypeDeployer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/deployer/TypeDeployer.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TypeDeployer.java	8 May 2002 04:10:27 -0000	1.7
  +++ TypeDeployer.java	10 May 2002 01:44:48 -0000	1.8
  @@ -14,13 +14,14 @@
    * Deploys the types of a particular role.
    *
    * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
  - * @version $Revision: 1.7 $ $Date: 2002/05/08 04:10:27 $
  + * @version $Revision: 1.8 $ $Date: 2002/05/10 01:44:48 $
    */
   public interface TypeDeployer
   {
       /**
        * Deploys a type.
        *
  +     * @param namespace The namespace to deploy the types under.
        * @param typeDefinition The type to deploy.
        * @param typeFactory The factory to use for instantiating the type.
        * @throws java.lang.Exception On error.
  
  
  
  1.12      +24 -3     jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/type/TypeManager.java
  
  Index: TypeManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/type/TypeManager.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- TypeManager.java	8 May 2002 04:10:27 -0000	1.11
  +++ TypeManager.java	10 May 2002 01:44:48 -0000	1.12
  @@ -12,7 +12,7 @@
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
    * @author <a href="mailto:darrell@apache.org">Darrell DeBoer</a>
  - * @version $Revision: 1.11 $ $Date: 2002/05/08 04:10:27 $
  + * @version $Revision: 1.12 $ $Date: 2002/05/10 01:44:48 $
    */
   public interface TypeManager
   {
  @@ -25,7 +25,28 @@
       char NAMESPACE_SEPARATOR = '.';
   
       /**
  -     * Registers a new type, with no namespace specified.
  +     * Registers all types in a factor, with no namespace specified.
  +     * All types provided by the factory must be of the role specified.
  +     * @param roleName The role for the types.
  +     * @param factory The factory contianing types to register.
  +     * @throws TypeException If an error occurs.
  +     */
  +    void registerTypes( String roleName, TypeFactory factory )
  +        throws TypeException;
  +
  +    /**
  +     * Registers all types in a factor, under the namespace specified.
  +     * All types provided by the factory must be of the role specified.
  +     * @param roleName The role for the types.
  +     * @param namespace The namespace to register the type under
  +     * @param factory The factory contianing types to register.
  +     * @throws TypeException If an error occurs.
  +     */
  +    void registerTypes( String roleName, String namespace, TypeFactory factory )
  +        throws TypeException;
  +
  +    /**
  +     * Registers a single type, with no namespace specified.
        *
        * @param roleName The role for the type.
        * @param shorthandName The shorthand name for the type.
  @@ -36,7 +57,7 @@
           throws TypeException;
   
       /**
  -     * Registers a new type under the specified namespace.
  +     * Registers a single type under the specified namespace.
        * @param roleName The role for the type.
        * @param namespace The namespace to register the type under
        * @param shorthandName The shorthand name for the type.
  
  
  
  1.3       +55 -40    jakarta-ant-myrmidon/container/src/test/org/apache/myrmidon/components/type/test/DefaultTypeManagerTestCase.java
  
  Index: DefaultTypeManagerTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/test/org/apache/myrmidon/components/type/test/DefaultTypeManagerTestCase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DefaultTypeManagerTestCase.java	9 May 2002 05:09:04 -0000	1.2
  +++ DefaultTypeManagerTestCase.java	10 May 2002 01:44:48 -0000	1.3
  @@ -20,7 +20,7 @@
    * Test cases for the DefaultTypeManager
    *
    * @author <a href="mailto:darrell@apache.org">Darrell DeBoer</a>
  - * @version $Revision: 1.2 $ $Date: 2002/05/09 05:09:04 $
  + * @version $Revision: 1.3 $ $Date: 2002/05/10 01:44:48 $
    */
   public class DefaultTypeManagerTestCase
       extends AbstractComponentTest
  @@ -121,34 +121,21 @@
   
       public void testNamespaceSeparation() throws Exception
       {
  +        final String ns1 = "ns1";
  +        final String ns2 = "ns2";
  +
           // Register Type1 and Type2 with same name under different namespaces.
  -        String shortName = "my-type";
  -        String ns1 = "ns1";
  -        String ns2 = "ns2";
  -        registerType( m_typeManager, TEST_ROLE, ns1, shortName, TYPE_CLASS1 );
  -        registerType( m_typeManager, TEST_ROLE, ns2, shortName, TYPE_CLASS2 );
  -
  -        String qname1 = makeQName( ns1, shortName);
  -        String qname2 = makeQName( ns2, shortName);
  -        assertCreate( m_typeManager, TEST_ROLE, qname1, TYPE_CLASS1 );
  -        assertCreate( m_typeManager, TEST_ROLE, qname2, TYPE_CLASS2 );
  -
  -        // TODO; implement this functionality.
  -/*
  -        // Type names are ambiguous, should fail on lookup.
  -        TypeFactory factory = m_typeManager.getFactory( KNOWN_ROLE );
  -        assertTrue( ! factory.canCreate( shortName ) );
  -        try
  -        {
  -            factory.create( shortName );
  -            fail();
  -        }
  -        catch( TypeException e )
  -        {
  -            // Check exception.
  -        }
  -*/
  +        registerType( m_typeManager, TEST_ROLE, ns1, TYPE_NAME1, TYPE_CLASS1 );
  +        registerType( m_typeManager, TEST_ROLE, ns2, TYPE_NAME1, TYPE_CLASS2 );
  +
  +        // Check that we can create using the qnames.
  +        assertCreate( m_typeManager, TEST_ROLE, makeQName( ns1, TYPE_NAME1 ), TYPE_CLASS1
);
  +        assertCreate( m_typeManager, TEST_ROLE, makeQName( ns2, TYPE_NAME1 ), TYPE_CLASS2
);
   
  +        // Try to create with short name (ambiguous)
  +        String message = REZ.getString( "ambiguous-type-name.error",
  +                                        TYPE_NAME1 );
  +        assertCantCreate( m_typeManager, TEST_ROLE, TYPE_NAME1, message );
       }
   
       public void testChildIndependence() throws Exception
  @@ -281,23 +268,51 @@
           assertCreate( child, TEST_ROLE, TYPE_NAME1, TYPE_CLASS2 );
       }
   
  -    public void testAmbiguousLookup() throws Exception
  +    public void testRegisterFactory() throws Exception
       {
  -        final String ns1 = "ns1";
  -        final String ns2 = "ns2";
  +        final String groupNs = "group";
  +        final Class groupClass = TYPE_CLASS1;
   
  -        // Register 2 types with the same name, but different namespaces.
  -        registerType( m_typeManager, TEST_ROLE, ns1, TYPE_NAME1, TYPE_CLASS1 );
  -        registerType( m_typeManager, TEST_ROLE, ns2, TYPE_NAME1, TYPE_CLASS2 );
  +        final String individualNs = "single";
  +        final Class individualClass = TYPE_CLASS2;
   
  -        // Check that we can create using the qnames.
  -        assertCreate( m_typeManager, TEST_ROLE, makeQName( ns1, TYPE_NAME1 ), TYPE_CLASS1
);
  -        assertCreate( m_typeManager, TEST_ROLE, makeQName( ns2, TYPE_NAME1 ), TYPE_CLASS2
);
  +        // Create a set of types to register (all names map to Type1).
  +        DefaultTypeFactory factory = new DefaultTypeFactory( groupClass.getClassLoader()
);
  +        factory.addNameClassMapping( TYPE_NAME1, groupClass.getName() );
  +        factory.addNameClassMapping( TYPE_NAME2, groupClass.getName() );
  +
  +        // Register the factory
  +        m_typeManager.registerTypes( TEST_ROLE, groupNs, factory );
  +
  +        // Check that the types are available.
  +        assertCreate( m_typeManager, TEST_ROLE, TYPE_NAME1, groupClass );
  +        assertCreate( m_typeManager, TEST_ROLE,
  +                      makeQName( groupNs, TYPE_NAME1), groupClass );
  +
  +        // Now register a single type over these registrations
  +        registerType( m_typeManager, TEST_ROLE, individualNs,
  +                      TYPE_NAME1, individualClass );
  +
  +        // With shortname only, should get individually registered type (more specific)
  +        assertCreate( m_typeManager, TEST_ROLE, TYPE_NAME1, individualClass );
  +        // With FQNames, should be able to access either.
  +        assertCreate( m_typeManager, TEST_ROLE,
  +                      makeQName( groupNs, TYPE_NAME1 ), groupClass );
  +        assertCreate( m_typeManager, TEST_ROLE,
  +                      makeQName( individualNs, TYPE_NAME1 ), individualClass );
  +        // Typename2 isn't overridden, so should still access the group type
  +        assertCreate( m_typeManager, TEST_ROLE, TYPE_NAME2, groupClass );
   
  -        // Try to create with short name (ambiguous)
  -        String message = REZ.getString( "ambiguous-type-name.error",
  -                                        TYPE_NAME1 );
  -        assertCantCreate( m_typeManager, TEST_ROLE, TYPE_NAME1, message );
  +
  +
  +        // Test that ambiguity is OK between individual and library type registrations
  +        // (Individual wins)
  +        // Register a type individually under the group Namespace.
  +        registerType( m_typeManager, TEST_ROLE, groupNs, TYPE_NAME2, individualClass );
  +        // This type should be available both FQ and shortname.
  +        assertCreate( m_typeManager, TEST_ROLE, TYPE_NAME2, individualClass );
  +        assertCreate( m_typeManager, TEST_ROLE,
  +                      makeQName( groupNs, TYPE_NAME2 ), individualClass );
       }
   
       /**
  
  
  
  1.2       +4 -4      jakarta-ant-myrmidon/myrmidon/src/samples/namespace-test.ant
  
  Index: namespace-test.ant
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/myrmidon/src/samples/namespace-test.ant,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- namespace-test.ant	8 May 2002 04:10:27 -0000	1.1
  +++ namespace-test.ant	10 May 2002 01:44:48 -0000	1.2
  @@ -19,10 +19,10 @@
       <target name="main" depends="log-no-import, log-import-selftest.atl, log-taskdef-newlog"/>
   
       <target name="log-no-import">
  -        <!-- With no overriding imports, both <log> and <ant.log> refer
to
  +        <!-- With no overriding imports, both <log> and <core.log> refer
to
                the core Log task. -->
           <log message="message"/>
  -        <ant.log message="message"/>
  +        <core.log message="message"/>
       </target>
   
       <target name="log-import-selftest.atl">
  @@ -32,7 +32,7 @@
           <!-- The version of <log> in selftest.atl overrides the core Log task
-->
           <log message="message"/> <!-- selftest.LogTask2 -->
           <test.log message="message"/> <!-- selftest.LogTask2 -->
  -        <ant.log message="message"/> <!-- core.Log -->
  +        <core.log message="message"/> <!-- core.Log -->
       </target>
   
       <target name="log-taskdef-newlog">
  @@ -44,7 +44,7 @@
               classpath="../../dist/ext/selftest.atl" />
   
           <log message="message"/> <!-- selftest.LogTask3 -->
  -        <ant.log message="message"/> <!-- core.Log -->
  +        <core.log message="message"/> <!-- core.Log -->
           <test.log message="message"/> <!-- selftest.LogTask2 -->
           <task-deffed.log message="message"/> <!-- selftest.LogTask3 -->
       </target>
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message