git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271971 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -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 <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @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 ); | |||
| } | |||
| } | |||
| @@ -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 ); | |||
| } | |||
| } | |||
| @@ -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; | |||
| } | |||
| /** | |||
| @@ -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 | |||
| * | |||
| @@ -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 ); | |||