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/27 04:18:08 UTC
cvs commit: jakarta-ant-myrmidon/aut/src/java/org/apache/aut/converter/lib SimpleConverterFactory.java SimpleMasterConverter.java
adammurdoch 02/05/26 19:18:08
Modified: api/src/java/org/apache/myrmidon/api/event LogLevel.java
aut/src/java/org/apache/aut/converter
AbstractMasterConverter.java
aut/src/java/org/apache/aut/converter/lib
SimpleMasterConverter.java
Added: aut/src/java/org/apache/aut/converter ConverterFactory.java
aut/src/java/org/apache/aut/converter/lib
SimpleConverterFactory.java
Log:
Changed ConverterRegistry.registerConverter() to use a ConverterFactory, rather
than a converter name. This means that the TypeManager is no longer used by
the DefaultMasterConverter, which fixes the <converter-def> task.
Revision Changes Path
1.3 +2 -2 jakarta-ant-myrmidon/api/src/java/org/apache/myrmidon/api/event/LogLevel.java
Index: LogLevel.java
===================================================================
RCS file: /home/cvs/jakarta-ant-myrmidon/api/src/java/org/apache/myrmidon/api/event/LogLevel.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- LogLevel.java 24 May 2002 04:47:42 -0000 1.2
+++ LogLevel.java 27 May 2002 02:18:08 -0000 1.3
@@ -16,7 +16,7 @@
* for using enum to write to logger.
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
- * @version $Revision: 1.2 $ $Date: 2002/05/24 04:47:42 $
+ * @version $Revision: 1.3 $ $Date: 2002/05/27 02:18:08 $
*/
public final class LogLevel
{
@@ -96,7 +96,7 @@
}
/**
- * Test if LogLevel is less than other LogLevel.
+ * Test if this LogLevel is less than other LogLevel.
*
* @param other the other LogLevel
* @return true if less than
1.8 +56 -65 jakarta-ant-myrmidon/aut/src/java/org/apache/aut/converter/AbstractMasterConverter.java
Index: AbstractMasterConverter.java
===================================================================
RCS file: /home/cvs/jakarta-ant-myrmidon/aut/src/java/org/apache/aut/converter/AbstractMasterConverter.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- AbstractMasterConverter.java 23 May 2002 01:50:25 -0000 1.7
+++ AbstractMasterConverter.java 27 May 2002 02:18:08 -0000 1.8
@@ -14,23 +14,15 @@
import org.apache.avalon.excalibur.i18n.Resources;
/**
- * This is an abstract implementation of a <code>MasterConverter</code>.
- * A MasterConverter is capable of converting between many different
- * source and destination types. The <code>MasterConverter</code>
- * delegates to other converters that do the actual work.
+ * This is a Converter implementation that is capable of converting between
+ * many different source and destination types, by delegating delegates to
+ * other converters that do the actual work.
*
- * <p>To use this class you must subclass it, overide the
- * (@link #createConverter(String)) method and register some
- * converters using the (@link #registerConverter(String,String,String))
- * method.</p>
- *
- * <p>The reason this class deals with strings rather than Class objects
- * is because dealing with strings allows us to implement alternative
- * mechanisms for defining Converters in the future, only defining converter
- * when it is first used.</p>
+ * <p>To use this class you must subclass it, and register some converters
+ * using the (@link #registerConverter} method.</p>
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
- * @version $Revision: 1.7 $ $Date: 2002/05/23 01:50:25 $
+ * @version $Revision: 1.8 $ $Date: 2002/05/27 02:18:08 $
*/
public abstract class AbstractMasterConverter
implements Converter
@@ -39,13 +31,13 @@
ResourceManager.getPackageResources( AbstractMasterConverter.class );
/**
- * Map from converter classname to instance of converter.
+ * Cache of converter instances. This is a map from ConverterFactory to
+ * a Converter instance created by that factory.
*/
private final Map m_converters = new HashMap();
/**
- * This holds the mapping between source/destination
- * and converter name.
+ * This holds the mapping between source/destination and ConverterFactory.
*/
private final HashMap m_mapping = new HashMap();
@@ -72,16 +64,8 @@
try
{
- // Search inheritance hierarchy for converter
- final String name = findConverter( originalClass, destination );
-
- // Create the converter
- Converter converter = (Converter)m_converters.get( name );
- if( converter == null )
- {
- converter = createConverter( name );
- m_converters.put( name, converter );
- }
+ // Determine which converter to use
+ final Converter converter = findConverter( originalClass, destination );
// Convert
final Object object = converter.convert( destination, original, context );
@@ -106,13 +90,34 @@
}
/**
+ * Returns the Converter instance to use to convert between a particular
+ * pair of classes.
+ */
+ private Converter findConverter( final Class originalClass,
+ final Class destination )
+ throws Exception
+ {
+ // Locate the factory to use
+ final ConverterFactory factory = findConverterFactory( originalClass, destination );
+
+ // Create the converter
+ Converter converter = (Converter)m_converters.get( factory );
+ if( converter == null )
+ {
+ converter = factory.createConverter();
+ m_converters.put( factory, converter );
+ }
+ return converter;
+ }
+
+ /**
* Register a converter
*
- * @param classname the className of converter
+ * @param factory the factory to use to create converter instances.
* @param source the source classname
* @param destination the destination classname
*/
- protected void registerConverter( final String classname,
+ protected void registerConverter( final ConverterFactory factory,
final String source,
final String destination )
{
@@ -123,28 +128,18 @@
m_mapping.put( source, map );
}
- map.put( destination, classname );
+ map.put( destination, factory );
//Remove instance of converter if it has already been created
- m_converters.remove( classname );
+ m_converters.remove( factory );
}
/**
- * Create an instance of converter with specified name.
- *
- * @param name the name of converter
- * @return the created converter instance
- * @throws Exception if converter can not be created.
+ * Determine the type of converter (represented as a ConverterFactory) to
+ * use to convert between original and destination classes.
*/
- protected abstract Converter createConverter( final String name )
- throws Exception;
-
- /**
- * Determine the name of the converter to use to convert between
- * original and destination classes.
- */
- private String findConverter( final Class originalClass,
- final Class destination )
+ private ConverterFactory findConverterFactory( final Class originalClass,
+ final Class destination )
throws ConverterException
{
//TODO: Maybe we should search the destination classes hierarchy as well
@@ -153,14 +148,14 @@
// looking for a converter from source type -> destination type.
// If more than one is found, choose the most specialised.
- Class match = null;
- String converterName = null;
+ Class bestSrcMatch = null;
+ ConverterFactory matchFactory = null;
ArrayList queue = new ArrayList();
queue.add( originalClass );
while( !queue.isEmpty() )
{
- Class clazz = (Class)queue.remove( 0 );
+ final Class clazz = (Class)queue.remove( 0 );
// Add superclass and all interfaces
if( clazz.getSuperclass() != null )
@@ -174,20 +169,20 @@
}
// Check if we can convert from current class to destination
- final String name = getConverterClassname( clazz.getName(),
- destination.getName() );
- if( name == null )
+ final ConverterFactory factory =
+ getConverterFactory( clazz.getName(), destination.getName() );
+ if( factory == null )
{
continue;
}
// Choose the more specialised source class
- if( match == null || match.isAssignableFrom( clazz ) )
+ if( bestSrcMatch == null || bestSrcMatch.isAssignableFrom( clazz ) )
{
- match = clazz;
- converterName = name;
+ bestSrcMatch = clazz;
+ matchFactory = factory;
}
- else if( clazz.isAssignableFrom( clazz ) )
+ else if( clazz.isAssignableFrom( bestSrcMatch ) )
{
continue;
}
@@ -200,9 +195,9 @@
}
// TODO - should cache the (src, dest) -> converter mapping
- if( match != null )
+ if( bestSrcMatch != null )
{
- return converterName;
+ return matchFactory;
}
// Could not find a converter
@@ -211,20 +206,16 @@
}
/**
- * Retrieve name of ConverterInfo that describes converter that converts
- * from source to destination.
- *
- * @param source the source classname
- * @param destination the destination classname
- * @return the className of converter or null if none available
+ * Retrieve factory for the converter that converts from source to destination.
*/
- private String getConverterClassname( final String source, final String destination )
+ private ConverterFactory getConverterFactory( final String source,
+ final String destination )
{
final HashMap map = (HashMap)m_mapping.get( source );
if( null == map )
{
return null;
}
- return (String)map.get( destination );
+ return (ConverterFactory)map.get( destination );
}
}
1.1 jakarta-ant-myrmidon/aut/src/java/org/apache/aut/converter/ConverterFactory.java
Index: ConverterFactory.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.aut.converter;
import org.apache.aut.converter.Converter;
/**
* A factory used to create converter instances.
*
* @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/05/27 02:18:08 $
*/
public interface ConverterFactory
{
/**
* Creates an instance of a converter.
*/
Converter createConverter() throws Exception;
}
1.3 +12 -28 jakarta-ant-myrmidon/aut/src/java/org/apache/aut/converter/lib/SimpleMasterConverter.java
Index: SimpleMasterConverter.java
===================================================================
RCS file: /home/cvs/jakarta-ant-myrmidon/aut/src/java/org/apache/aut/converter/lib/SimpleMasterConverter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SimpleMasterConverter.java 14 Apr 2002 09:24:42 -0000 1.2
+++ SimpleMasterConverter.java 27 May 2002 02:18:08 -0000 1.3
@@ -8,14 +8,13 @@
package org.apache.aut.converter.lib;
import org.apache.aut.converter.AbstractMasterConverter;
-import org.apache.aut.converter.Converter;
/**
* A very simple master converter that is capable of using
* any of the converters in this package.
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
- * @version $Revision: 1.2 $ $Date: 2002/04/14 09:24:42 $
+ * @version $Revision: 1.3 $ $Date: 2002/05/27 02:18:08 $
*/
public class SimpleMasterConverter
extends AbstractMasterConverter
@@ -26,53 +25,38 @@
*/
public SimpleMasterConverter()
{
- registerConverter( ObjectToStringConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory( ObjectToStringConverter.class ),
"java.lang.Object",
"java.lang.String" );
- registerConverter( StringToBooleanConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory( StringToBooleanConverter.class ),
"java.lang.String",
"java.lang.Boolean" );
- registerConverter( StringToByteConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory( StringToByteConverter.class ),
"java.lang.String",
"java.lang.Byte" );
- registerConverter( StringToClassConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory( StringToClassConverter.class ),
"java.lang.String",
"java.lang.Class" );
- registerConverter( StringToDoubleConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory( StringToDoubleConverter.class ),
"java.lang.String",
"java.lang.Double" );
- registerConverter( StringToFloatConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory( StringToFloatConverter.class ),
"java.lang.String",
"java.lang.Float" );
- registerConverter( StringToIntegerConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory( StringToIntegerConverter.class ),
"java.lang.String",
"java.lang.Integer" );
- registerConverter( StringToLongConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory( StringToLongConverter.class ),
"java.lang.String",
"java.lang.Long" );
- registerConverter( StringToShortConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory( StringToShortConverter.class ),
"java.lang.String",
"java.lang.Short" );
- registerConverter( StringToURLConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory( StringToURLConverter.class ),
"java.lang.String",
"java.net.URL" );
- registerConverter( StringToDateConverter.class.getName(),
+ registerConverter( new SimpleConverterFactory( StringToDateConverter.class ),
"java.lang.String",
"java.util.Date" );
- }
-
- /**
- * Create an instance of converter with specified name.
- *
- * @param name the name of converter
- * @return the created converter instance
- * @throws Exception if converter can not be created.
- */
- protected Converter createConverter( final String name )
- throws Exception
- {
- final ClassLoader classLoader = getClass().getClassLoader();
- final Class clazz = classLoader.loadClass( name );
- return (Converter)clazz.newInstance();
}
}
1.1 jakarta-ant-myrmidon/aut/src/java/org/apache/aut/converter/lib/SimpleConverterFactory.java
Index: SimpleConverterFactory.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.aut.converter.lib;
import org.apache.aut.converter.ConverterFactory;
import org.apache.aut.converter.Converter;
/**
* A ConverterFactory that creates converter instances using reflection.
*
* @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/05/27 02:18:08 $
*/
public class SimpleConverterFactory
implements ConverterFactory
{
private Class m_converterClass;
public SimpleConverterFactory( final Class converterClass )
{
m_converterClass = converterClass;
}
/**
* Creates an instance of a converter.
*/
public Converter createConverter() throws Exception
{
return (Converter)m_converterClass.newInstance();
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>