ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/type DefaultTypeManager.java TypeManager.java TypedComponentSelector.java
Date Sat, 02 Jun 2001 05:47:29 GMT
donaldp     01/06/01 22:47:29

  Modified:    proposal/myrmidon/src/java/org/apache/myrmidon/components/type
                        TypedComponentSelector.java
  Added:       proposal/myrmidon/src/java/org/apache/myrmidon/components/type
                        DefaultTypeManager.java TypeManager.java
  Log:
  Continued work on registry (which I am now terming TypeManager).
  
  Revision  Changes    Path
  1.2       +31 -7     jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/type/TypedComponentSelector.java
  
  Index: TypedComponentSelector.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/type/TypedComponentSelector.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TypedComponentSelector.java	2001/05/31 17:21:49	1.1
  +++ TypedComponentSelector.java	2001/06/02 05:47:28	1.2
  @@ -21,13 +21,25 @@
   public class TypedComponentSelector
       implements ComponentSelector
   {
  -    private final HashMap  m_factorys = new HashMap();
  +    ///Parent Selector
  +    private final TypedComponentSelector  m_parent;
   
  -    private final Class    m_type;
  +    ///Map of name->factory list
  +    private final HashMap                 m_factorys = new HashMap();
   
  +    ///Type expected to be created from factorys
  +    private final Class                   m_type;
  +
       public TypedComponentSelector( final Class type )
       {
           m_type = type;
  +        m_parent = null;
  +    }
  +
  +    public TypedComponentSelector( final TypedComponentSelector parent )
  +    {
  +        m_type = parent.getType();
  +        m_parent = parent;
       }
   
       /**
  @@ -46,14 +58,15 @@
               throw new ComponentException( "Invalid hint, expected a string not a " + 
                                             hint.getClass().getName() );
           }
  -        
  -        final Component component = createComponent( (String)hint );
  +
  +        final String name = (String)hint;
  +        final Component component = createComponent( name );
   
           if( null != component )
           {
               if( m_type.isInstance( component ) )
               {
  -                throw new ComponentException( "Implementation of " + hint + " is not of
" +
  +                throw new ComponentException( "Implementation of " + name + " is not of
" +
                                                 "correct type (" + m_type.getClass().getName()
+ ")" );
               }
   
  @@ -61,7 +74,7 @@
           }
           else
           {
  -            throw new ComponentException( "Unable to provide implementation for " + hint
);
  +            throw new ComponentException( "Unable to provide implementation for " + name
);
           }
       }
   
  @@ -77,9 +90,20 @@
       /**
        * Populate the ComponentSelector.
        */
  -    public void put( final String name, final ComponentFactory factory )
  +    public void register( final String name, final ComponentFactory factory )
       {
           m_factorys.put( name, factory );
  +    }
  +
  +    /**
  +     * Retrieve type managed by selector.
  +     * Used by other instances of TypedComponentSelector.
  +     *
  +     * @return the type class
  +     */
  +    protected final Class getType()
  +    {
  +        return m_type;
       }
   
       /**
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/type/DefaultTypeManager.java
  
  Index: DefaultTypeManager.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 file.
   */
  package org.apache.myrmidon.components.type;
  
  import java.util.HashMap;
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentSelector;
  
  /**
   * The interface that is used to manage types.
   *
   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
   */
  public class DefaultTypeManager
      implements TypeManager
  {
      ///Parent type manager to inherit values from.
      private final TypeManager  m_parent;
  
      ///Maps role to TypedComponentSelector.
      private final HashMap      m_roleMap      = new HashMap();
  
      public DefaultTypeManager( final TypeManager parent )
      {
          m_parent = parent;
      }
  
      public Component lookup( final String role )
          throws ComponentException
      {
          final ComponentSelector selector = (ComponentSelector)m_roleMap.get( role );
          if( null != selector )
          {
              return selector;
          }
          else
          {
              throw new ComponentException( "Unable to provide implementation for '" + 
                                            role + "'" );
          }
      }
  
      public void release( final Component component )
      {
      }
  
      public void registerType( final String role, 
                                final String shorthandName, 
                                final ComponentFactory factory ) 
          throws Exception
      {
          final TypedComponentSelector selector = createSelector( role );
          selector.register( shorthandName, factory );
      }
  
      /**
       * Get a selector of appropriate role.
       * Create a Selector if none exists with same name.
       *
       * @param role the role name(must be name of work interface)
       * @return the Selector for interface
       * @exception ComponentException if role exists and not a selector, role does not 
       *            specify accessible work interface, or 
       */
      private TypedComponentSelector createSelector( final String role )
          throws ComponentException
      {
          TypedComponentSelector selector = (TypedComponentSelector)m_roleMap.get( role );
          if( null != selector ) return selector;
  
          if( null != m_parent )
          {
              final TypedComponentSelector parentSelector = getTypedSelector( m_parent, role
);
  
              if( null != parentSelector )
              {
                  selector = new TypedComponentSelector( parentSelector );
              }
          }
  
          ///If we haven't goa selector try to create a new one
          if( null == selector )
          {
              try
              {
                  //TODO: Should we use ContextClassLoader here ??? Or perhaps try that on
failure??
                  final Class clazz = Class.forName( role );
                  selector = new TypedComponentSelector( clazz );
              }
              catch( final Exception e )
              {
                  throw new ComponentException( "Role '" + role + "' does not specify " +
                                                "accessible work interface" );
              }
          }        
  
          m_roleMap.put( role, selector );
  
          return selector;
      }
  
      private TypedComponentSelector getTypedSelector( final TypeManager typeManager, 
                                                       final String role )
      {
          try
          {
              return (TypedComponentSelector)typeManager.lookup( role );
          }
          catch( final ComponentException ce ) {}
          catch( final ClassCastException cce ) {}
          
          return null;
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/type/TypeManager.java
  
  Index: TypeManager.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 file.
   */
  package org.apache.myrmidon.components.type;
  
  import org.apache.avalon.framework.component.ComponentManager;
  
  /**
   * The interface that is used to manage types.
   *
   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
   */
  public interface TypeManager
      extends ComponentManager
  {
      String ROLE = "org.apache.myrmidon.components.type.TypeManager";
  
      void registerType( String role, String shorthandName, ComponentFactory factory )
          throws Exception;
  }
  
  
  

Mime
View raw message