From 1081cadc83321c93e551269a920df162dc4cc42e Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Sat, 23 Mar 2002 04:37:17 +0000 Subject: [PATCH] Merge DefaultConverterRegistry into DefaultMasterConverter git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271971 13f79535-47bb-0310-9956-ffa450edef68 --- .../converter/DefaultConverterRegistry.java | 47 -------------- .../converter/DefaultMasterConverter.java | 61 ++++++++++++++++--- .../components/embeddor/DefaultEmbeddor.java | 11 ++-- .../converter/ConverterRegistry.java | 10 --- .../components/AbstractComponentTest.java | 3 - 5 files changed, 59 insertions(+), 73 deletions(-) delete mode 100644 proposal/myrmidon/src/java/org/apache/myrmidon/components/converter/DefaultConverterRegistry.java diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/components/converter/DefaultConverterRegistry.java b/proposal/myrmidon/src/java/org/apache/myrmidon/components/converter/DefaultConverterRegistry.java deleted file mode 100644 index 8d3ba0d55..000000000 --- a/proposal/myrmidon/src/java/org/apache/myrmidon/components/converter/DefaultConverterRegistry.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.myrmidon.components.converter; - -import java.util.HashMap; -import org.apache.myrmidon.interfaces.converter.ConverterRegistry; - -/** - * Default implementation of Converter registry. - * - * @author Peter Donald - * @version $Revision$ $Date$ - */ -public class DefaultConverterRegistry - implements ConverterRegistry -{ - private final HashMap m_mapping = new HashMap(); - - public String getConverterName( final String source, final String destination ) - { - final HashMap map = (HashMap)m_mapping.get( source ); - if( null == map ) - { - return null; - } - return (String)map.get( destination ); - } - - public void registerConverter( final String className, - final String source, - final String destination ) - { - HashMap map = (HashMap)m_mapping.get( source ); - if( null == map ) - { - map = new HashMap(); - m_mapping.put( source, map ); - } - - map.put( destination, className ); - } -} diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/components/converter/DefaultMasterConverter.java b/proposal/myrmidon/src/java/org/apache/myrmidon/components/converter/DefaultMasterConverter.java index 38d37cc6c..fdb9a8a8f 100644 --- a/proposal/myrmidon/src/java/org/apache/myrmidon/components/converter/DefaultMasterConverter.java +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/components/converter/DefaultMasterConverter.java @@ -17,9 +17,9 @@ import org.apache.avalon.excalibur.i18n.Resources; import org.apache.avalon.framework.service.ServiceException; import org.apache.avalon.framework.service.ServiceManager; import org.apache.avalon.framework.service.Serviceable; -import org.apache.myrmidon.interfaces.converter.ConverterRegistry; import org.apache.myrmidon.interfaces.type.TypeFactory; import org.apache.myrmidon.interfaces.type.TypeManager; +import org.apache.myrmidon.interfaces.converter.ConverterRegistry; /** * Converter engine to handle converting between types. @@ -28,17 +28,22 @@ import org.apache.myrmidon.interfaces.type.TypeManager; * @version $Revision$ $Date$ */ public class DefaultMasterConverter - implements Converter, Serviceable + implements Converter, ConverterRegistry, Serviceable { private final static Resources REZ = ResourceManager.getPackageResources( DefaultMasterConverter.class ); - private ConverterRegistry m_registry; private TypeManager m_typeManager; /** Map from converter name to Converter. */ private Map m_converters = new HashMap(); + /** + * This holds the mapping between source/destination + * and converter name. + */ + private final HashMap m_mapping = new HashMap(); + /** * Retrieve relevent services needed to deploy. * @@ -48,10 +53,30 @@ public class DefaultMasterConverter public void service( final ServiceManager serviceManager ) throws ServiceException { - m_registry = (ConverterRegistry)serviceManager.lookup( ConverterRegistry.ROLE ); m_typeManager = (TypeManager)serviceManager.lookup( TypeManager.ROLE ); } + /** + * Register a converter + * + * @param className the className of converter + * @param source the source classname + * @param destination the destination classname + */ + public void registerConverter( final String className, + final String source, + final String destination ) + { + HashMap map = (HashMap)m_mapping.get( source ); + if( null == map ) + { + map = new HashMap(); + m_mapping.put( source, map ); + } + + map.put( destination, className ); + } + /** * Convert object to destination type. * @@ -76,7 +101,7 @@ public class DefaultMasterConverter try { // Search inheritance hierarchy for converter - final String name = getConverterName( originalClass, destination ); + final String name = findConverter( originalClass, destination ); // Create the converter Converter converter = (Converter)m_converters.get( name ); @@ -126,8 +151,8 @@ public class DefaultMasterConverter * Determine the name of the converter to use to convert between * original and destination classes. */ - private String getConverterName( final Class originalClass, - final Class destination ) + private String findConverter( final Class originalClass, + final Class destination ) throws ConverterException { //TODO: Maybe we should search the destination classes hierarchy as well @@ -157,8 +182,8 @@ public class DefaultMasterConverter } // Check if we can convert from current class to destination - final String name = m_registry.getConverterName( clazz.getName(), - destination.getName() ); + final String name = getConverterClassname( clazz.getName(), + destination.getName() ); if( name == null ) { continue; @@ -192,4 +217,22 @@ public class DefaultMasterConverter final String message = REZ.getString( "no-converter.error" ); throw new ConverterException( message ); } + + /** + * 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 + */ + private String getConverterClassname( final String source, final String destination ) + { + final HashMap map = (HashMap)m_mapping.get( source ); + if( null == map ) + { + return null; + } + return (String)map.get( destination ); + } } diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/components/embeddor/DefaultEmbeddor.java b/proposal/myrmidon/src/java/org/apache/myrmidon/components/embeddor/DefaultEmbeddor.java index c48d9a7ee..8edf64cee 100644 --- a/proposal/myrmidon/src/java/org/apache/myrmidon/components/embeddor/DefaultEmbeddor.java +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/components/embeddor/DefaultEmbeddor.java @@ -30,7 +30,6 @@ import org.apache.myrmidon.interfaces.aspect.AspectManager; import org.apache.myrmidon.interfaces.builder.ProjectBuilder; import org.apache.myrmidon.interfaces.classloader.ClassLoaderManager; import org.apache.myrmidon.interfaces.configurer.Configurer; -import org.apache.myrmidon.interfaces.converter.ConverterRegistry; import org.apache.myrmidon.interfaces.deployer.Deployer; import org.apache.myrmidon.interfaces.deployer.DeploymentException; import org.apache.myrmidon.interfaces.deployer.TypeDeployer; @@ -44,6 +43,7 @@ import org.apache.myrmidon.interfaces.service.MultiSourceServiceManager; import org.apache.myrmidon.interfaces.type.TypeFactory; import org.apache.myrmidon.interfaces.type.TypeManager; import org.apache.myrmidon.interfaces.workspace.Workspace; +import org.apache.myrmidon.interfaces.converter.ConverterRegistry; import org.apache.myrmidon.listeners.ProjectListener; /** @@ -253,9 +253,10 @@ public class DefaultEmbeddor throws Exception { // Create the components - createComponent( ConverterRegistry.class, PREFIX + "converter.DefaultConverterRegistry" ); createComponent( ExtensionManager.class, PREFIX + "extensions.DefaultExtensionManager" ); - createComponent( Converter.class, PREFIX + "converter.DefaultMasterConverter" ); + final Object converter = + createComponent( Converter.class, PREFIX + "converter.DefaultMasterConverter" ); + m_serviceManager.put( ConverterRegistry.ROLE, converter ); createComponent( Configurer.class, PREFIX + "configurer.DefaultConfigurer" ); createComponent( TypeManager.class, PREFIX + "type.DefaultTypeManager" ); createComponent( RoleManager.class, PREFIX + "role.DefaultRoleManager" ); @@ -276,12 +277,14 @@ public class DefaultEmbeddor /** * Creates a component. */ - private void createComponent( Class roleType, String defaultImpl ) + private Object createComponent( final Class roleType, + final String defaultImpl ) throws Exception { final Object component = createService( roleType, defaultImpl ); m_serviceManager.put( roleType.getName(), component ); m_components.add( component ); + return component; } /** diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/converter/ConverterRegistry.java b/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/converter/ConverterRegistry.java index 961771d4e..93bcb4f56 100644 --- a/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/converter/ConverterRegistry.java +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/converter/ConverterRegistry.java @@ -17,16 +17,6 @@ public interface ConverterRegistry { String ROLE = ConverterRegistry.class.getName(); - /** - * 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 - */ - String getConverterName( String source, String destination ); - /** * Register a converter * diff --git a/proposal/myrmidon/src/test/org/apache/myrmidon/components/AbstractComponentTest.java b/proposal/myrmidon/src/test/org/apache/myrmidon/components/AbstractComponentTest.java index b50c65df9..ca4ea6ef4 100644 --- a/proposal/myrmidon/src/test/org/apache/myrmidon/components/AbstractComponentTest.java +++ b/proposal/myrmidon/src/test/org/apache/myrmidon/components/AbstractComponentTest.java @@ -75,9 +75,6 @@ public abstract class AbstractComponentTest Object component = createComponent( Converter.ROLE, DefaultMasterConverter.class ); m_serviceManager.put( Converter.ROLE, component ); - components.add( component ); - - component = createComponent( ConverterRegistry.ROLE, DefaultConverterRegistry.class ); m_serviceManager.put( ConverterRegistry.ROLE, component ); components.add( component );