diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/components/type/MultiSourceTypeFactory.java b/proposal/myrmidon/src/java/org/apache/myrmidon/components/type/MultiSourceTypeFactory.java index ebe572efe..c1f6ca4bb 100644 --- a/proposal/myrmidon/src/java/org/apache/myrmidon/components/type/MultiSourceTypeFactory.java +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/components/type/MultiSourceTypeFactory.java @@ -54,6 +54,14 @@ public class MultiSourceTypeFactory m_factorys.put( name, factory ); } + /** + * 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. * @@ -64,30 +72,37 @@ public class MultiSourceTypeFactory public Object create( final String name ) throws TypeException { - TypeFactory factory = getTypeFactory( name ); - - if( null == factory && null != m_parent ) - { - factory = m_parent.getTypeFactory( name ); - } - + // 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 ); } - else + + // Create the object + final Object object = factory.create( name ); + if( !m_type.isInstance( object ) ) { - final Object object = factory.create( name ); + final String message = REZ.getString( "mismatched-type.error", name, object.getClass().getName() ); + throw new TypeException( message ); + } - if( !m_type.isInstance( object ) ) - { - final String message = REZ.getString( "mismatched-type.error", name, object.getClass().getName() ); - throw new TypeException( message ); - } + return object; + } - return object; + /** + * 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; } /** diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/DefaultTypeFactory.java b/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/DefaultTypeFactory.java index 67e4fe7bf..7b27a7234 100644 --- a/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/DefaultTypeFactory.java +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/DefaultTypeFactory.java @@ -58,6 +58,14 @@ public class DefaultTypeFactory m_classNames.put( name, className ); } + /** + * Determines if this factory can create instances of a particular type. + */ + public boolean canCreate( String name ) + { + return ( getClassName( name ) != null ); + } + /** * Create a type instance with appropriate name. * @@ -68,7 +76,15 @@ public class DefaultTypeFactory public Object create( final String name ) throws TypeException { + // Determine the name of the class to instantiate final String className = getClassName( name ); + if( null == className ) + { + final String message = REZ.getString( "no-mapping.error", name ); + throw new TypeException( message ); + } + + // Instantiate the object try { final ClassLoader classLoader = getClassLoader(); @@ -83,16 +99,8 @@ public class DefaultTypeFactory } private String getClassName( final String name ) - throws TypeException { - final String className = (String)m_classNames.get( name ); - if( null == className ) - { - final String message = REZ.getString( "no-mapping.error", name ); - throw new TypeException( message ); - } - - return className; + return (String)m_classNames.get( name ); } protected ClassLoader getClassLoader() diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/TypeFactory.java b/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/TypeFactory.java index c42886a3a..c2e5b5d6f 100644 --- a/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/TypeFactory.java +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/TypeFactory.java @@ -15,12 +15,19 @@ package org.apache.myrmidon.interfaces.type; */ public interface TypeFactory { + /** + * Determines if this factory can create instances of a particular type. + * + * @param name the type name. + */ + boolean canCreate( String name ); + /** * Create a type instance based on name. * - * @param name the name + * @param name the type name * @return the type instance - * @exception TypeException if an error occurs + * @exception TypeException if the type is unknown, or an error occurs. */ Object create( String name ) throws TypeException;