ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/configurer DefaultObjectConfigurer.java PropertyConfigurer.java DefaultPropertyConfigurer.java
Date Sun, 23 Jun 2002 08:37:36 GMT
donaldp     2002/06/23 01:37:36

  Modified:    container/src/java/org/apache/myrmidon/components/configurer
                        DefaultObjectConfigurer.java
                        PropertyConfigurer.java
  Removed:     container/src/java/org/apache/myrmidon/components/configurer
                        DefaultPropertyConfigurer.java
  Log:
  PropertyConfigurer did not need to be an interface as it was never used as an interface
so lets merge interface and implementation.
  
  Revision  Changes    Path
  1.24      +7 -7      jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/configurer/DefaultObjectConfigurer.java
  
  Index: DefaultObjectConfigurer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/configurer/DefaultObjectConfigurer.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- DefaultObjectConfigurer.java	25 Apr 2002 02:55:41 -0000	1.23
  +++ DefaultObjectConfigurer.java	23 Jun 2002 08:37:36 -0000	1.24
  @@ -99,8 +99,8 @@
               final Class type = method.getParameterTypes()[ 0 ];
               final String propName = extractName( 3, method.getName() );
   
  -            final DefaultPropertyConfigurer setter =
  -                new DefaultPropertyConfigurer( getPropertyCount(),
  +            final PropertyConfigurer setter =
  +                new PropertyConfigurer( getPropertyCount(),
                                                  type,
                                                  method,
                                                  1 );
  @@ -132,8 +132,8 @@
               final Class type = method.getParameterTypes()[ 0 ];
               final String propName = extractName( 3, methodName );
   
  -            final DefaultPropertyConfigurer configurer =
  -                new DefaultPropertyConfigurer( getPropertyCount(),
  +            final PropertyConfigurer configurer =
  +                new PropertyConfigurer( getPropertyCount(),
                                                  type,
                                                  method,
                                                  Integer.MAX_VALUE );
  @@ -166,7 +166,7 @@
           }
   
           m_typedPropertyConfigurer
  -            = new DefaultPropertyConfigurer( getPropertyCount(),
  +            = new PropertyConfigurer( getPropertyCount(),
                                                type,
                                                method,
                                                Integer.MAX_VALUE );
  @@ -188,7 +188,7 @@
   
           final Method method = (Method)methods.iterator().next();
           final Class type = method.getParameterTypes()[ 0 ];
  -        m_contentConfigurer = new DefaultPropertyConfigurer( getPropertyCount(),
  +        m_contentConfigurer = new PropertyConfigurer( getPropertyCount(),
                                                                type,
                                                                method,
                                                                1 );
  
  
  
  1.7       +116 -14   jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/configurer/PropertyConfigurer.java
  
  Index: PropertyConfigurer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/configurer/PropertyConfigurer.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- PropertyConfigurer.java	21 Apr 2002 02:41:23 -0000	1.6
  +++ PropertyConfigurer.java	23 Jun 2002 08:37:36 -0000	1.7
  @@ -2,35 +2,137 @@
    * 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
  + * version 1.1, a copy of which has been included  with this distribution in
    * the LICENSE.txt file.
    */
   package org.apache.myrmidon.components.configurer;
   
  +import java.lang.reflect.InvocationTargetException;
  +import java.lang.reflect.Method;
  +import org.apache.avalon.excalibur.i18n.ResourceManager;
  +import org.apache.avalon.excalibur.i18n.Resources;
   import org.apache.myrmidon.api.metadata.ModelException;
   
   /**
  - * Configures a property of an object.
  - * TODO - axe createValue().
  + * The default property configurer implementation, which uses reflection to
  + * create and set property values.
    *
    * @author <a href="mailto:adammurdoch_ml@yahoo.com">Adam Murdoch</a>
    * @version $Revision$ $Date$
    */
  -interface PropertyConfigurer
  +class PropertyConfigurer
   {
  +    private static final Resources REZ =
  +        ResourceManager.getPackageResources( PropertyConfigurer.class );
  +
  +    private final int m_propertyIndex;
  +    private final Class m_type;
  +    private final Method m_method;
  +    private final int m_maxCount;
  +
  +    PropertyConfigurer( final int propIndex,
  +                               final Class type,
  +                               final Method method,
  +                               final int maxCount )
  +    {
  +        m_propertyIndex = propIndex;
  +        if( type.isPrimitive() )
  +        {
  +            m_type = getComplexTypeFor( type );
  +        }
  +        else
  +        {
  +            m_type = type;
  +        }
  +        m_method = method;
  +        m_maxCount = maxCount;
  +
  +        if( null == m_method )
  +        {
  +            throw new NullPointerException( "method" );
  +        }
  +    }
  +
       /**
  -     * Returns the type of this property.
  +     * Returns the type of the element.
        */
  -    Class getType();
  +    Class getType()
  +    {
  +        return m_type;
  +    }
   
       /**
        * Adds a value for this property, to an object.
  -     *
  -     * @param state The state object, representing the object being configured.
  -     * @param value The property value.  This must be assignable to the type
  -     *              returned by {@link #getType}.
  -     * @throws ModelException If the property cannot be set.
        */
  -    void addValue( ConfigurationState state, Object value )
  -        throws ModelException;
  +    void addValue( final ConfigurationState state, final Object value )
  +        throws ModelException
  +    {
  +        final ConfigurationState defState = (ConfigurationState)state;
  +        // Check the property count
  +        if( defState.getPropertyCount( m_propertyIndex ) >= m_maxCount )
  +        {
  +            final String message = REZ.getString( "too-many-values.error" );
  +            throw new ModelException( message );
  +        }
  +        defState.incPropertyCount( m_propertyIndex );
  +
  +        try
  +        {
  +            // Add the value
  +            m_method.invoke( defState.getObject(), new Object[]{value} );
  +        }
  +        catch( final InvocationTargetException ite )
  +        {
  +            final Throwable cause = ite.getTargetException();
  +            throw new ModelException( cause.getMessage(), cause );
  +        }
  +        catch( final Exception e )
  +        {
  +            throw new ModelException( e.getMessage(), e );
  +        }
  +    }
  +
  +    /**
  +     * Determines the complex type for a prmitive type.
  +     */
  +    private Class getComplexTypeFor( final Class clazz )
  +    {
  +        if( String.class == clazz )
  +        {
  +            return String.class;
  +        }
  +        else if( Integer.TYPE.equals( clazz ) )
  +        {
  +            return Integer.class;
  +        }
  +        else if( Long.TYPE.equals( clazz ) )
  +        {
  +            return Long.class;
  +        }
  +        else if( Short.TYPE.equals( clazz ) )
  +        {
  +            return Short.class;
  +        }
  +        else if( Byte.TYPE.equals( clazz ) )
  +        {
  +            return Byte.class;
  +        }
  +        else if( Boolean.TYPE.equals( clazz ) )
  +        {
  +            return Boolean.class;
  +        }
  +        else if( Float.TYPE.equals( clazz ) )
  +        {
  +            return Float.class;
  +        }
  +        else if( Double.TYPE.equals( clazz ) )
  +        {
  +            return Double.class;
  +        }
  +        else
  +        {
  +            final String message = REZ.getString( "no-complex-type.error", clazz.getName()
);
  +            throw new IllegalArgumentException( message );
  +        }
  +    }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message