You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by ad...@apache.org on 2002/05/18 06:07:08 UTC

cvs commit: jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/type AdaptingTypeFactory.java DefaultTypeFactory.java Resources.properties TypeFactory.java

adammurdoch    02/05/17 21:07:08

  Modified:    container/src/java/org/apache/myrmidon/interfaces/type
                        DefaultTypeFactory.java Resources.properties
                        TypeFactory.java
  Added:       container/src/java/org/apache/myrmidon/interfaces/type
                        AdaptingTypeFactory.java
  Log:
  Added AdaptingTypeFactory, which can be used to adapt instances of a role into
  instances of another.
  
  Revision  Changes    Path
  1.13      +3 -2      jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/type/DefaultTypeFactory.java
  
  Index: DefaultTypeFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/type/DefaultTypeFactory.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- DefaultTypeFactory.java	1 Apr 2002 09:56:27 -0000	1.12
  +++ DefaultTypeFactory.java	18 May 2002 04:07:08 -0000	1.13
  @@ -12,10 +12,11 @@
   import org.apache.avalon.excalibur.i18n.Resources;
   
   /**
  - * Create a type instance based on name.
  + * A {@link TypeFactory} implementation that creates instances using classes
  + * from the same classloader.
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  - * @version CVS $Revision: 1.12 $ $Date: 2002/04/01 09:56:27 $
  + * @version CVS $Revision: 1.13 $ $Date: 2002/05/18 04:07:08 $
    */
   public class DefaultTypeFactory
       implements TypeFactory
  
  
  
  1.2       +2 -0      jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/type/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/type/Resources.properties,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Resources.properties	31 Jan 2002 23:56:09 -0000	1.1
  +++ Resources.properties	18 May 2002 04:07:08 -0000	1.2
  @@ -1,2 +1,4 @@
   no-instantiate.error=Could not create an object of type "{0}".
   no-mapping.error=Unknown type "{0}".
  +too-many-constructors.error=Multiple matching constructors for adaptor class "{0}".
  +no-constructors.error=No matching constructors for adaptor class "{0}".
  \ No newline at end of file
  
  
  
  1.6       +4 -3      jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/type/TypeFactory.java
  
  Index: TypeFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/type/TypeFactory.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TypeFactory.java	1 Apr 2002 09:56:28 -0000	1.5
  +++ TypeFactory.java	18 May 2002 04:07:08 -0000	1.6
  @@ -11,7 +11,7 @@
    * Create an instance on name.
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  - * @version CVS $Revision: 1.5 $ $Date: 2002/04/01 09:56:28 $
  + * @version CVS $Revision: 1.6 $ $Date: 2002/05/18 04:07:08 $
    */
   public interface TypeFactory
   {
  @@ -19,12 +19,13 @@
        * Determines if this factory can create instances of a particular type.
        *
        * @param name the type name.
  -     * @return <code>true</code> if this is a valid factory for the named type.
  +     * @return <code>true</code> if this factory can create instances of the
  +     *         the named type, false otherwise.
        */
       boolean canCreate( String name );
   
       /**
  -     * Create a type instance based on name.
  +     * Creates a new instance of a particular type.
        *
        * @param name the type name
        * @return the type instance
  
  
  
  1.1                  jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/type/AdaptingTypeFactory.java
  
  Index: AdaptingTypeFactory.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.interfaces.type;
  
  import java.lang.reflect.Constructor;
  import java.util.HashMap;
  import java.util.Map;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  
  /**
   * A {@link TypeFactory} implementation which wraps types created by other
   * type factories in an adaptor.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/05/18 04:07:08 $
   */
  public class AdaptingTypeFactory
      implements TypeFactory
  {
      private static final Resources REZ =
          ResourceManager.getPackageResources( AdaptingTypeFactory.class );
  
      private final Class m_adaptorClass;
      private final Map m_nameMap = new HashMap();
      private Constructor m_constructor;
  
      public AdaptingTypeFactory( final Class adaptorClass )
      {
          m_adaptorClass = adaptorClass;
      }
  
      /**
       * Adds a type mapping.
       */
      public void addMapping( final String typeName, final TypeFactory factory )
      {
          m_nameMap.put( typeName, factory );
      }
  
      /**
       * Determines if this factory can create instances of a particular type.
       */
      public boolean canCreate( final String name )
      {
          return m_nameMap.containsKey( name );
      }
  
      /**
       * Create a type instance based on name.
       */
      public Object create( final String name )
          throws TypeException
      {
          final TypeFactory factory = (TypeFactory)m_nameMap.get( name );
          if( factory == null )
          {
              final String message = REZ.getString( "no-mapping.error", name );
              throw new TypeException( message );
          }
  
          try
          {
              // Locate the appropriate constructor
              if( m_constructor == null )
              {
                  m_constructor = findConstructor( m_adaptorClass );
              }
  
              // Instantiate the object, and wrap it in an adaptor
              final Object instance = factory.create( name );
              return m_constructor.newInstance( new Object[]{instance} );
          }
          catch( final Exception e )
          {
              final String message = REZ.getString( "no-instantiate.error", name );
              throw new TypeException( message, e );
          }
      }
  
      /**
       * Locates the constructor to use to instantiate adaptors.
       */
      private Constructor findConstructor( final Class adaptorClass )
          throws Exception
      {
          final Constructor[] constructors = adaptorClass.getConstructors();
          Constructor match = null;
          for( int i = 0; i < constructors.length; i++ )
          {
              final Constructor constructor = constructors[ i ];
              final Class[] params = constructor.getParameterTypes();
              if( params.length != 1 || params[ 0 ].isPrimitive() || params[ 0 ].isArray() )
              {
                  continue;
              }
              if( match != null )
              {
                  // Ambiguous constructors
                  final String message = REZ.getString( "too-many-constructors.error", adaptorClass.getName() );
                  throw new Exception( message );
              }
              match = constructor;
          }
  
          if( match == null )
          {
              final String message = REZ.getString( "no-constructors.error", adaptorClass.getName() );
              throw new Exception( message );
          }
  
          return match;
      }
  }
  
  
  

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