git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269089 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -18,7 +18,7 @@ import org.apache.myrmidon.api.TaskException; | |||||
| import org.apache.myrmidon.components.converter.ConverterRegistry; | import org.apache.myrmidon.components.converter.ConverterRegistry; | ||||
| import org.apache.myrmidon.components.deployer.DeploymentException; | import org.apache.myrmidon.components.deployer.DeploymentException; | ||||
| import org.apache.myrmidon.components.deployer.TskDeployer; | import org.apache.myrmidon.components.deployer.TskDeployer; | ||||
| import org.apache.myrmidon.components.type.DefaultComponentFactory; | |||||
| import org.apache.myrmidon.components.type.DefaultTypeFactory; | |||||
| import org.apache.myrmidon.components.type.TypeManager; | import org.apache.myrmidon.components.type.TypeManager; | ||||
| import org.apache.myrmidon.converter.Converter; | import org.apache.myrmidon.converter.Converter; | ||||
| @@ -111,7 +111,7 @@ public class RegisterConverter | |||||
| { | { | ||||
| m_converterRegistry.registerConverter( m_classname, m_sourceType, m_destinationType ); | m_converterRegistry.registerConverter( m_classname, m_sourceType, m_destinationType ); | ||||
| final DefaultComponentFactory factory = new DefaultComponentFactory( new URL[] { url } ); | |||||
| final DefaultTypeFactory factory = new DefaultTypeFactory( new URL[] { url } ); | |||||
| factory.addNameClassMapping( m_classname, m_classname ); | factory.addNameClassMapping( m_classname, m_classname ); | ||||
| try { m_typeManager.registerType( Converter.ROLE, m_classname, factory ); } | try { m_typeManager.registerType( Converter.ROLE, m_classname, factory ); } | ||||
| @@ -11,7 +11,7 @@ import java.net.URL; | |||||
| import org.apache.myrmidon.api.DataType; | import org.apache.myrmidon.api.DataType; | ||||
| import org.apache.myrmidon.api.TaskException; | import org.apache.myrmidon.api.TaskException; | ||||
| import org.apache.myrmidon.components.deployer.DeploymentException; | import org.apache.myrmidon.components.deployer.DeploymentException; | ||||
| import org.apache.myrmidon.components.type.DefaultComponentFactory; | |||||
| import org.apache.myrmidon.components.type.DefaultTypeFactory; | |||||
| /** | /** | ||||
| * Method to register a single datatype. | * Method to register a single datatype. | ||||
| @@ -36,8 +36,7 @@ public class RegisterDataType | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| final DefaultComponentFactory factory = | |||||
| new DefaultComponentFactory( new URL[] { url } ); | |||||
| final DefaultTypeFactory factory = new DefaultTypeFactory( new URL[] { url } ); | |||||
| factory.addNameClassMapping( name, className ); | factory.addNameClassMapping( name, className ); | ||||
| try { getTypeManager().registerType( DataType.ROLE, name, factory ); } | try { getTypeManager().registerType( DataType.ROLE, name, factory ); } | ||||
| catch( final Exception e ) | catch( final Exception e ) | ||||
| @@ -11,7 +11,7 @@ import java.net.URL; | |||||
| import org.apache.myrmidon.api.Task; | import org.apache.myrmidon.api.Task; | ||||
| import org.apache.myrmidon.api.TaskException; | import org.apache.myrmidon.api.TaskException; | ||||
| import org.apache.myrmidon.components.deployer.DeploymentException; | import org.apache.myrmidon.components.deployer.DeploymentException; | ||||
| import org.apache.myrmidon.components.type.DefaultComponentFactory; | |||||
| import org.apache.myrmidon.components.type.DefaultTypeFactory; | |||||
| /** | /** | ||||
| * Method to register a single tasklet. | * Method to register a single tasklet. | ||||
| @@ -36,8 +36,7 @@ public class RegisterTasklet | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| final DefaultComponentFactory factory = | |||||
| new DefaultComponentFactory( new URL[] { url } ); | |||||
| final DefaultTypeFactory factory = new DefaultTypeFactory( new URL[] { url } ); | |||||
| factory.addNameClassMapping( name, className ); | factory.addNameClassMapping( name, className ); | ||||
| try { getTypeManager().registerType( Task.ROLE, name, factory ); } | try { getTypeManager().registerType( Task.ROLE, name, factory ); } | ||||
| catch( final Exception e ) | catch( final Exception e ) | ||||
| @@ -28,8 +28,7 @@ import org.apache.myrmidon.api.Task; | |||||
| import org.apache.myrmidon.api.DataType; | import org.apache.myrmidon.api.DataType; | ||||
| import org.apache.myrmidon.components.converter.ConverterRegistry; | import org.apache.myrmidon.components.converter.ConverterRegistry; | ||||
| import org.apache.myrmidon.components.executor.Executor; | import org.apache.myrmidon.components.executor.Executor; | ||||
| import org.apache.myrmidon.components.type.ComponentFactory; | |||||
| import org.apache.myrmidon.components.type.DefaultComponentFactory; | |||||
| import org.apache.myrmidon.components.type.DefaultTypeFactory; | |||||
| import org.apache.myrmidon.components.type.TypeManager; | import org.apache.myrmidon.components.type.TypeManager; | ||||
| import org.apache.myrmidon.converter.Converter; | import org.apache.myrmidon.converter.Converter; | ||||
| import org.xml.sax.SAXException; | import org.xml.sax.SAXException; | ||||
| @@ -116,9 +115,7 @@ public class DefaultTskDeployer | |||||
| throws DeploymentException | throws DeploymentException | ||||
| { | { | ||||
| final Configuration taskdefs = loadConfiguration( zipFile, TSKDEF_FILE ); | final Configuration taskdefs = loadConfiguration( zipFile, TSKDEF_FILE ); | ||||
| final DefaultComponentFactory factory = | |||||
| new DefaultComponentFactory( new URL[] { url } ); | |||||
| final DefaultTypeFactory factory = new DefaultTypeFactory( new URL[] { url } ); | |||||
| try | try | ||||
| { | { | ||||
| @@ -164,8 +161,7 @@ public class DefaultTskDeployer | |||||
| { | { | ||||
| if( converters[ i ].getAttribute( "classname" ).equals( name ) ) | if( converters[ i ].getAttribute( "classname" ).equals( name ) ) | ||||
| { | { | ||||
| final DefaultComponentFactory factory = | |||||
| new DefaultComponentFactory( new URL[] { url } ); | |||||
| final DefaultTypeFactory factory = new DefaultTypeFactory( new URL[] { url } ); | |||||
| handleConverter( converters[ i ], url, factory ); | handleConverter( converters[ i ], url, factory ); | ||||
| break; | break; | ||||
| } | } | ||||
| @@ -196,8 +192,7 @@ public class DefaultTskDeployer | |||||
| { | { | ||||
| if( datatypes[ i ].getAttribute( "name" ).equals( name ) ) | if( datatypes[ i ].getAttribute( "name" ).equals( name ) ) | ||||
| { | { | ||||
| final DefaultComponentFactory factory = | |||||
| new DefaultComponentFactory( new URL[] { url } ); | |||||
| final DefaultTypeFactory factory = new DefaultTypeFactory( new URL[] { url } ); | |||||
| handleDataType( datatypes[ i ], url, factory ); | handleDataType( datatypes[ i ], url, factory ); | ||||
| break; | break; | ||||
| } | } | ||||
| @@ -227,8 +222,7 @@ public class DefaultTskDeployer | |||||
| { | { | ||||
| if( tasks[ i ].getAttribute( "name" ).equals( name ) ) | if( tasks[ i ].getAttribute( "name" ).equals( name ) ) | ||||
| { | { | ||||
| final DefaultComponentFactory factory = | |||||
| new DefaultComponentFactory( new URL[] { url } ); | |||||
| final DefaultTypeFactory factory = new DefaultTypeFactory( new URL[] { url } ); | |||||
| handleTask( tasks[ i ], url, factory ); | handleTask( tasks[ i ], url, factory ); | ||||
| break; | break; | ||||
| } | } | ||||
| @@ -336,7 +330,7 @@ public class DefaultTskDeployer | |||||
| private void handleConverter( final Configuration converter, | private void handleConverter( final Configuration converter, | ||||
| final URL url, | final URL url, | ||||
| final DefaultComponentFactory factory ) | |||||
| final DefaultTypeFactory factory ) | |||||
| throws Exception | throws Exception | ||||
| { | { | ||||
| final String name = converter.getAttribute( "classname" ); | final String name = converter.getAttribute( "classname" ); | ||||
| @@ -354,7 +348,7 @@ public class DefaultTskDeployer | |||||
| private void handleTask( final Configuration task, | private void handleTask( final Configuration task, | ||||
| final URL url, | final URL url, | ||||
| final DefaultComponentFactory factory ) | |||||
| final DefaultTypeFactory factory ) | |||||
| throws Exception | throws Exception | ||||
| { | { | ||||
| final String name = task.getAttribute( "name" ); | final String name = task.getAttribute( "name" ); | ||||
| @@ -369,7 +363,7 @@ public class DefaultTskDeployer | |||||
| private void handleDataType( final Configuration datatype, | private void handleDataType( final Configuration datatype, | ||||
| final URL url, | final URL url, | ||||
| final DefaultComponentFactory factory ) | |||||
| final DefaultTypeFactory factory ) | |||||
| throws Exception | throws Exception | ||||
| { | { | ||||
| final String name = datatype.getAttribute( "name" ); | final String name = datatype.getAttribute( "name" ); | ||||
| @@ -10,17 +10,15 @@ package org.apache.myrmidon.components.type; | |||||
| import java.net.URL; | import java.net.URL; | ||||
| import java.util.HashMap; | import java.util.HashMap; | ||||
| import java.net.URLClassLoader; | import java.net.URLClassLoader; | ||||
| import org.apache.avalon.framework.component.Component; | |||||
| import org.apache.avalon.framework.component.ComponentException; | |||||
| /** | /** | ||||
| * Create a component based on name. | |||||
| * Create a type instance based on name. | |||||
| * | * | ||||
| * @author <a href="mailto:donaldp@apache.org">Peter Donald</a> | * @author <a href="mailto:donaldp@apache.org">Peter Donald</a> | ||||
| * @version CVS $Revision$ $Date$ | * @version CVS $Revision$ $Date$ | ||||
| */ | */ | ||||
| public class DefaultComponentFactory | |||||
| implements ComponentFactory | |||||
| public class DefaultTypeFactory | |||||
| implements TypeFactory | |||||
| { | { | ||||
| ///A Map of shortnames to classnames | ///A Map of shortnames to classnames | ||||
| private final HashMap m_classNames = new HashMap(); | private final HashMap m_classNames = new HashMap(); | ||||
| @@ -34,18 +32,18 @@ public class DefaultComponentFactory | |||||
| ///The parent classLoader (if any) | ///The parent classLoader (if any) | ||||
| private ClassLoader m_classLoader; | private ClassLoader m_classLoader; | ||||
| public DefaultComponentFactory( final URL[] urls ) | |||||
| public DefaultTypeFactory( final URL[] urls ) | |||||
| { | { | ||||
| this( urls, Thread.currentThread().getContextClassLoader() ); | this( urls, Thread.currentThread().getContextClassLoader() ); | ||||
| } | } | ||||
| public DefaultComponentFactory( final URL[] urls, final ClassLoader parent ) | |||||
| public DefaultTypeFactory( final URL[] urls, final ClassLoader parent ) | |||||
| { | { | ||||
| m_urls = urls; | m_urls = urls; | ||||
| m_parent = parent; | m_parent = parent; | ||||
| } | } | ||||
| public DefaultComponentFactory( final ClassLoader classLoader ) | |||||
| public DefaultTypeFactory( final ClassLoader classLoader ) | |||||
| { | { | ||||
| this( null, null ); | this( null, null ); | ||||
| m_classLoader = classLoader; | m_classLoader = classLoader; | ||||
| @@ -57,36 +55,36 @@ public class DefaultComponentFactory | |||||
| } | } | ||||
| /** | /** | ||||
| * Create a Component with appropriate name. | |||||
| * Create a type instance with appropriate name. | |||||
| * | * | ||||
| * @param name the name | * @param name the name | ||||
| * @return the created component | |||||
| * @exception ComponentException if an error occurs | |||||
| * @return the created instance | |||||
| * @exception TypeException if an error occurs | |||||
| */ | */ | ||||
| public Component create( final String name ) | |||||
| throws ComponentException | |||||
| public Object create( final String name ) | |||||
| throws TypeException | |||||
| { | { | ||||
| final String className = getClassName( name ); | final String className = getClassName( name ); | ||||
| try | try | ||||
| { | { | ||||
| return (Component)getClassLoader().loadClass( className ).newInstance(); | |||||
| return getClassLoader().loadClass( className ).newInstance(); | |||||
| } | } | ||||
| catch( final Exception e ) | catch( final Exception e ) | ||||
| { | { | ||||
| throw new ComponentException( "Unable to instantiate '" + name + "'", e ); | |||||
| throw new TypeException( "Unable to instantiate '" + name + "'", e ); | |||||
| } | } | ||||
| } | } | ||||
| private String getClassName( final String name ) | private String getClassName( final String name ) | ||||
| throws ComponentException | |||||
| throws TypeException | |||||
| { | { | ||||
| final String className = (String)m_classNames.get( name ); | final String className = (String)m_classNames.get( name ); | ||||
| if( null == className ) | if( null == className ) | ||||
| { | { | ||||
| throw new ComponentException( "Malconfigured factory, no clasname for '" + | |||||
| name + "'" ); | |||||
| throw new TypeException( "Malconfigured factory, no clasname for '" + | |||||
| name + "'" ); | |||||
| } | } | ||||
| return className; | return className; | ||||
| @@ -56,7 +56,7 @@ public class DefaultTypeManager | |||||
| public void registerType( final String role, | public void registerType( final String role, | ||||
| final String shorthandName, | final String shorthandName, | ||||
| final ComponentFactory factory ) | |||||
| final TypeFactory factory ) | |||||
| throws Exception | throws Exception | ||||
| { | { | ||||
| final TypedComponentSelector selector = createSelector( role + "Selector" ); | final TypedComponentSelector selector = createSelector( role + "Selector" ); | ||||
| @@ -0,0 +1,40 @@ | |||||
| /* | |||||
| * 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.CascadingException; | |||||
| /** | |||||
| * Exception to indicate problem with type instantiating. | |||||
| * | |||||
| * @author <a href="mailto:donaldp@apache.org">Peter Donald</a> | |||||
| */ | |||||
| public final class TypeException | |||||
| extends CascadingException | |||||
| { | |||||
| /** | |||||
| * Construct a new <code>TypeException</code> instance. | |||||
| * | |||||
| * @param message The detail message for this exception. | |||||
| */ | |||||
| public TypeException( final String message ) | |||||
| { | |||||
| this( message, null ); | |||||
| } | |||||
| /** | |||||
| * Construct a new <code>TypeException</code> instance. | |||||
| * | |||||
| * @param message The detail message for this exception. | |||||
| * @param throwable the root cause of the exception | |||||
| */ | |||||
| public TypeException( final String message, final Throwable throwable ) | |||||
| { | |||||
| super( message, throwable ); | |||||
| } | |||||
| } | |||||
| @@ -7,16 +7,13 @@ | |||||
| */ | */ | ||||
| package org.apache.myrmidon.components.type; | package org.apache.myrmidon.components.type; | ||||
| import org.apache.avalon.framework.component.Component; | |||||
| import org.apache.avalon.framework.component.ComponentException; | |||||
| /** | /** | ||||
| * Create a component based on role and hint. | * Create a component based on role and hint. | ||||
| * | * | ||||
| * @author <a href="mailto:donaldp@apache.org">Peter Donald</a> | * @author <a href="mailto:donaldp@apache.org">Peter Donald</a> | ||||
| * @version CVS $Revision$ $Date$ | * @version CVS $Revision$ $Date$ | ||||
| */ | */ | ||||
| public interface ComponentFactory | |||||
| public interface TypeFactory | |||||
| { | { | ||||
| /** | /** | ||||
| * Create a Component with appropriate name. | * Create a Component with appropriate name. | ||||
| @@ -25,6 +22,6 @@ public interface ComponentFactory | |||||
| * @return the created component | * @return the created component | ||||
| * @exception ComponentException if an error occurs | * @exception ComponentException if an error occurs | ||||
| */ | */ | ||||
| Component create( String name ) | |||||
| throws ComponentException; | |||||
| Object create( String name ) | |||||
| throws TypeException; | |||||
| } | } | ||||
| @@ -20,6 +20,6 @@ public interface TypeManager | |||||
| { | { | ||||
| String ROLE = "org.apache.myrmidon.components.type.TypeManager"; | String ROLE = "org.apache.myrmidon.components.type.TypeManager"; | ||||
| void registerType( String role, String shorthandName, ComponentFactory factory ) | |||||
| void registerType( String role, String shorthandName, TypeFactory factory ) | |||||
| throws Exception; | throws Exception; | ||||
| } | } | ||||
| @@ -90,7 +90,7 @@ public class TypedComponentSelector | |||||
| /** | /** | ||||
| * Populate the ComponentSelector. | * Populate the ComponentSelector. | ||||
| */ | */ | ||||
| public void register( final String name, final ComponentFactory factory ) | |||||
| public void register( final String name, final TypeFactory factory ) | |||||
| { | { | ||||
| m_factorys.put( name, factory ); | m_factorys.put( name, factory ); | ||||
| } | } | ||||
| @@ -114,12 +114,16 @@ public class TypedComponentSelector | |||||
| private Component createComponent( final String name ) | private Component createComponent( final String name ) | ||||
| throws ComponentException | throws ComponentException | ||||
| { | { | ||||
| final ComponentFactory factory = (ComponentFactory)m_factorys.get( name ); | |||||
| final TypeFactory factory = (TypeFactory)m_factorys.get( name ); | |||||
| if( null == factory ) return null; | if( null == factory ) return null; | ||||
| else | else | ||||
| { | { | ||||
| return factory.create( name ); | |||||
| try { return (Component)factory.create( name ); } | |||||
| catch( final TypeException te ) | |||||
| { | |||||
| throw new ComponentException( "Failed to create type " + name, te ); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||