You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by do...@apache.org on 2001/06/08 11:15:32 UTC

cvs commit: jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/deployer DefaultDeployer.java

donaldp     01/06/08 02:15:32

  Modified:    proposal/myrmidon/src/java/org/apache/myrmidon/components/deployer
                        DefaultDeployer.java
  Log:
  Broke up large method into multiple methods. Update descriptor format into two sections to make converters and types separate.
  
  Also scan base classloader for any default types defined by runtime. use ClassLoader.getResources() to scan for all descriptors to process.
  
  Revision  Changes    Path
  1.2       +112 -31   jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/deployer/DefaultDeployer.java
  
  Index: DefaultDeployer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/deployer/DefaultDeployer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DefaultDeployer.java	2001/06/03 13:11:40	1.1
  +++ DefaultDeployer.java	2001/06/08 09:15:30	1.2
  @@ -8,19 +8,28 @@
   package org.apache.myrmidon.components.deployer;
   
   import java.io.File;
  +import java.net.URL;
  +import java.net.URLClassLoader;
  +import java.util.Enumeration;
  +import java.util.HashMap;
  +import javax.xml.parsers.SAXParser;
  +import javax.xml.parsers.SAXParserFactory;
  +import org.apache.avalon.framework.activity.Initializable;
   import org.apache.avalon.framework.component.ComponentException;
   import org.apache.avalon.framework.component.ComponentManager;
   import org.apache.avalon.framework.component.Composable;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
   import org.apache.avalon.framework.logger.AbstractLoggable;
  +import org.apache.myrmidon.api.DataType;
  +import org.apache.myrmidon.api.Task;
   import org.apache.myrmidon.components.converter.ConverterRegistry;
  -import org.apache.myrmidon.components.executor.Executor;
   import org.apache.myrmidon.components.type.DefaultTypeFactory;
   import org.apache.myrmidon.components.type.TypeManager;
   import org.apache.myrmidon.converter.Converter;
  -import org.apache.myrmidon.api.DataType;
  -import org.apache.myrmidon.api.Task;
  +import org.xml.sax.SAXException;
  +import org.xml.sax.XMLReader;
   
   /**
    * This class deploys a .tsk file into a registry.
  @@ -29,8 +38,10 @@
    */
   public class DefaultDeployer
       extends AbstractLoggable
  -    implements Composable, Deployer
  +    implements Deployer, Initializable, Composable
   {
  +    private final static String TYPE_DESCRIPTOR = "META-INF/ant-types.xml";
  +
       private ConverterRegistry            m_converterRegistry;
       private TypeManager                  m_typeManager;
       private RoleManager                  m_roleManager;
  @@ -49,34 +60,47 @@
           m_roleManager = (RoleManager)componentManager.lookup( RoleManager.ROLE );
       }
   
  +    public void initialize()
  +        throws Exception
  +    {
  +        final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
  +        final SAXParser saxParser = saxParserFactory.newSAXParser();
  +        final XMLReader parser = saxParser.getXMLReader();
  +        //parser.setFeature( "http://xml.org/sax/features/namespace-prefixes", false );
  +
  +        final SAXConfigurationHandler handler = new SAXConfigurationHandler();
  +        parser.setContentHandler( handler );
  +        parser.setErrorHandler( handler );
  +
  +        final ClassLoader classLoader = getClass().getClassLoader();
  +        final DefaultTypeFactory factory = new DefaultTypeFactory( classLoader );
  +
  +        final Enumeration enum = classLoader.getResources( Deployment.DESCRIPTOR_NAME );
  +        while( enum.hasMoreElements() )
  +        {
  +            final URL url = (URL)enum.nextElement();
  +            parser.parse( url.toString() );
  +            getLogger().debug( "deploying " + url );
  +            deployFromDescriptor( handler.getConfiguration(), classLoader, url );
  +        }
  +    }
  +
       public void deploy( final File file )
           throws DeploymentException
       {
           getLogger().info( "Deploying AntLib file (" + file + ")" );
   
  -        try
  -        {
  -            checkFile( file );
  +        checkFile( file );
   
  -            final Deployment deployment = new Deployment( file );
  -            final Configuration descriptor = deployment.getDescriptor();
  -            final DefaultTypeFactory factory = new DefaultTypeFactory( deployment.getURL() );
  +        final Deployment deployment = new Deployment( file );
  +        final Configuration descriptor = deployment.getDescriptor();
  +        final URL[] urls = new URL[] { deployment.getURL() };
  +        final URLClassLoader classLoader = 
  +            new URLClassLoader( urls, Thread.currentThread().getContextClassLoader() );
   
  -            final Configuration[] children = descriptor.getChildren();
  -            for( int i = 0; i < children.length; i++ )
  -            {
  -                final String name = children[ i ].getName();
  -                
  -                if( name.equals( "converter" ) )
  -                {
  -                    handleConverter( children[ i ], factory );
  -                }
  -                else
  -                {
  -                    final String role = getRoleForName( name );
  -                    handleType( role, children[ i ], factory );
  -                }
  -            }
  +        try
  +        {
  +            deployFromDescriptor( descriptor, classLoader, deployment.getURL() );
           }
           catch( final DeploymentException de )
           {
  @@ -85,7 +109,7 @@
           catch( final Exception e )
           {
               throw new DeploymentException( "Error deploying library", e );
  -        }       
  +        }
       }
   
       public void deployConverter( final String name, final File file )
  @@ -99,7 +123,9 @@
   
           try
           {
  -            final Configuration[] converters = descriptor.getChildren( "converter" );
  +            final Configuration[] converters =
  +                descriptor.getChild( "converters" ).getChildren( "converter" );
  +
               for( int i = 0; i < converters.length; i++ )
               {
                   if( converters[ i ].getAttribute( "classname" ).equals( name ) )
  @@ -128,10 +154,12 @@
           final Deployment deployment = new Deployment( file );
           final Configuration descriptor = deployment.getDescriptor();
           final DefaultTypeFactory factory = new DefaultTypeFactory( deployment.getURL() );
  -        
  +
           try
           {
  -            final Configuration[] datatypes = descriptor.getChildren( shorthand );
  +            final Configuration[] datatypes =
  +                descriptor.getChild( "types" ).getChildren( shorthand );
  +
               for( int i = 0; i < datatypes.length; i++ )
               {
                   if( datatypes[ i ].getAttribute( "name" ).equals( name ) )
  @@ -151,6 +179,59 @@
           }
       }
   
  +    private void deployFromDescriptor( final Configuration descriptor,
  +                                       final ClassLoader classLoader,
  +                                       final URL url )
  +        throws DeploymentException, Exception
  +    {
  +        try
  +        {
  +            //Have to keep a new factory per role
  +            //To avoid name clashes (ie a datatype and task with same name)
  +            final HashMap factorys = new HashMap();
  +
  +            final Configuration[] types = descriptor.getChild( "types" ).getChildren();
  +            for( int i = 0; i < types.length; i++ )
  +            {
  +                final String name = types[ i ].getName();
  +                final String role = getRoleForName( name );
  +                final DefaultTypeFactory factory = getFactory( role, classLoader, factorys );
  +                handleType( role, types[ i ], factory );
  +            }
  +
  +            final DefaultTypeFactory factory = new DefaultTypeFactory( classLoader );
  +            final Configuration[] converters = descriptor.getChild( "converters" ).getChildren();
  +            for( int i = 0; i < converters.length; i++ )
  +            {
  +                final String name = converters[ i ].getName();
  +                handleConverter( converters[ i ], factory );
  +            }
  +        }
  +        catch( final DeploymentException de )
  +        {
  +            throw de;
  +        }
  +        catch( final Exception e )
  +        {
  +            throw new DeploymentException( "Error deploying library from " + url, e );
  +        }
  +    }
  +
  +    private DefaultTypeFactory getFactory( final String role, 
  +                                           final ClassLoader classLoader, 
  +                                           final HashMap factorys )
  +    {
  +        DefaultTypeFactory factory = (DefaultTypeFactory)factorys.get( role );
  +
  +        if( null == factory )
  +        {
  +            factory = new DefaultTypeFactory( classLoader );
  +            factorys.put( role, factory );
  +        }
  +
  +        return factory;
  +    }
  +
       private String getNameForRole( final String role )
           throws DeploymentException
       {
  @@ -160,7 +241,7 @@
           {
               throw new DeploymentException( "RoleManager does not know name for role " + role );
           }
  -        
  +
           return name;
       }
   
  @@ -173,7 +254,7 @@
           {
               throw new DeploymentException( "RoleManager does not know role for name " + name );
           }
  -        
  +
           return role;
       }