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/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer DefaultConfigurer.java
Date Sun, 27 Jan 2002 23:41:18 GMT
donaldp     02/01/27 15:41:18

  Modified:    proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer
                        DefaultConfigurer.java
  Log:
  Removed "nasty hack-o-rama" by re-catching and re-throwing ConfigurationExceptions up call
stack.
  
  Added support for reflection picking up typed adders
  
  Added suppot for adders that tak a Configuration object
  
  Revision  Changes    Path
  1.20      +85 -33    jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/DefaultConfigurer.java
  
  Index: DefaultConfigurer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/DefaultConfigurer.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- DefaultConfigurer.java	22 Jan 2002 11:15:25 -0000	1.19
  +++ DefaultConfigurer.java	27 Jan 2002 23:41:18 -0000	1.20
  @@ -70,16 +70,7 @@
                              final Context context )
           throws ConfigurationException
       {
  -        try
  -        {
  -            configureObject( object, configuration, context );
  -        }
  -        catch( InvocationTargetException ite )
  -        {
  -            // A configuration exception thrown from a nested object.  Unpack
  -            // and re-throw
  -            throw (ConfigurationException)ite.getTargetException();
  -        }
  +        configureObject( object, configuration, context );
       }
   
       /**
  @@ -88,7 +79,7 @@
       private void configureObject( final Object object,
                                     final Configuration configuration,
                                     final Context context )
  -        throws ConfigurationException, InvocationTargetException
  +        throws ConfigurationException
       {
           if( object instanceof Configurable )
           {
  @@ -122,6 +113,11 @@
                           REZ.getString( "no-such-attribute.error", elemName, name );
                       throw new ConfigurationException( message, nspe );
                   }
  +                catch( final ConfigurationException ce )
  +                {
  +                    ce.fillInStackTrace();
  +                    throw ce;
  +                }
                   catch( final CascadingException ce )
                   {
                       final String message =
  @@ -146,6 +142,11 @@
                           REZ.getString( "no-content.error", elemName );
                       throw new ConfigurationException( message, nspe );
                   }
  +                catch( final ConfigurationException ce )
  +                {
  +                    ce.fillInStackTrace();
  +                    throw ce;
  +                }
                   catch( final CascadingException ce )
                   {
                       final String message =
  @@ -170,6 +171,11 @@
                           REZ.getString( "no-such-element.error", elemName, name );
                       throw new ConfigurationException( message, nspe );
                   }
  +                catch( final ConfigurationException ce )
  +                {
  +                    ce.fillInStackTrace();
  +                    throw ce;
  +                }
                   catch( final CascadingException ce )
                   {
                       final String message =
  @@ -230,7 +236,7 @@
       private void configureElement( final ConfigurationState state,
                                      final Configuration element,
                                      final Context context )
  -        throws CascadingException, InvocationTargetException
  +        throws CascadingException
       {
           final String elementName = element.getName();
           if( elementName.toLowerCase().endsWith( "-ref" ) )
  @@ -251,46 +257,92 @@
       private void configureInline( final ConfigurationState state,
                                     final Configuration element,
                                     final Context context )
  -        throws CascadingException, InvocationTargetException
  +        throws CascadingException
       {
  -        final String elementName = element.getName();
  +        final String name = element.getName();
   
           // Locate the configurer for the child element
  -        final PropertyConfigurer childConfigurer = state.getConfigurer().getProperty( elementName
);
  +        final PropertyConfigurer childConfigurer = state.getConfigurer().getProperty( name
);
  +
  +        // Create & configure the child element
  +        final Object child =
  +            setupChild( state, element, context, childConfigurer );
  +
  +        // Set the child element
  +        childConfigurer.addValue( state, child );
  +    }
   
  -        // Create the child element
  +    private Object setupChild( final ConfigurationState state,
  +                               final Configuration element,
  +                               final Context context,
  +                               final PropertyConfigurer childConfigurer )
  +        throws ConfigurationException
  +    {
  +        final String name = element.getName();
  +        final Class type = childConfigurer.getType();
           Object child = childConfigurer.createValue( state );
  -        if( child == null )
  +
  +        if( null == child && Configuration.class == type )
  +        {
  +            //special case where you have add(Configuration)
  +            return element;
  +        }
  +        else if( null == child )
           {
               // Create an instance using the default constructor
  -            try
  +            if( type.isInterface() )
               {
  -                child = childConfigurer.getType().newInstance();
  +                child = createdTypedObject( name, type );
  +                configureObject( child, element, context );
               }
  -            catch( final Exception e )
  +            else
               {
  -                final String message =
  -                    REZ.getString( "create-object.error",
  -                                   childConfigurer.getType().getName() );
  -                throw new ConfigurationException( message, e );
  +                child = createObject( type );
  +                configureObject( child, element, context );
               }
           }
  +        configureObject( child, element, context );
  +        return child;
  +    }
   
  -        // Configure the child element
  +    /**
  +     * Utility method to create an instance of the
  +     * specified type that satisfied supplied interface.
  +     */
  +    private Object createdTypedObject( final String name,
  +                                       final Class type )
  +        throws ConfigurationException
  +    {
           try
           {
  -            configureObject( child, element, context );
  +            return type.newInstance();
           }
  -        catch( final ConfigurationException ce )
  +        catch( final Exception e )
           {
  -            // Nasty hack-o-rama, used to get this exception up through
  -            // the stack of doConfigure() calls.  This is unpacked by the
  -            // top-most configure() call, and rethrown.
  -            throw new InvocationTargetException( ce );
  +            final String message =
  +                REZ.getString( "create-object.error",
  +                               type.getName() );
  +            throw new ConfigurationException( message, e );
           }
  +    }
   
  -        // Set the child element
  -        childConfigurer.addValue( state, child );
  +    /**
  +     * Utility method to instantiate an instance of the specified class.
  +     */
  +    private Object createObject( final Class type )
  +        throws ConfigurationException
  +    {
  +        try
  +        {
  +            return type.newInstance();
  +        }
  +        catch( final Exception e )
  +        {
  +            final String message =
  +                REZ.getString( "create-object.error",
  +                               type.getName() );
  +            throw new ConfigurationException( message, e );
  +        }
       }
   
       /**
  
  
  

--
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