jvanzyl     2004/05/07 17:15:34

  Modified:    maven-core/src/main/java/org/apache/maven/plugin/plexus/executor
                        FieldPluginExecutor.java SetterPluginExecutor.java
  Removed:     maven-core/src/main/java/org/apache/maven/plugin/plexus
                        FieldPluginConfigurator.java
                        SetterPluginConfigurator.java
  Log:
  
  
  Revision  Changes    Path
  1.4       +63 -9     
maven-components/maven-core/src/main/java/org/apache/maven/plugin/plexus/executor/FieldPluginExecutor.java
  
  Index: FieldPluginExecutor.java
  ===================================================================
  RCS file: 
/home/cvs/maven-components/maven-core/src/main/java/org/apache/maven/plugin/plexus/executor/FieldPluginExecutor.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FieldPluginExecutor.java  7 May 2004 23:46:51 -0000       1.3
  +++ FieldPluginExecutor.java  8 May 2004 00:15:34 -0000       1.4
  @@ -18,11 +18,13 @@
   
   import org.apache.maven.plugin.PluginExecutionRequest;
   import org.apache.maven.plugin.PluginExecutionResponse;
  -import org.apache.maven.plugin.plexus.FieldPluginConfigurator;
   import org.apache.maven.plugin.plexus.PluginConfigurationException;
   
  +import java.lang.reflect.Field;
   import java.lang.reflect.InvocationTargetException;
   import java.lang.reflect.Method;
  +import java.util.Iterator;
  +import java.util.Map;
   
   /**
    *
  @@ -34,13 +36,6 @@
   public class FieldPluginExecutor
       implements PluginExecutor
   {
  -    private FieldPluginConfigurator fieldPluginConfigurator;
  -
  -    public FieldPluginExecutor()
  -    {
  -        fieldPluginConfigurator = new FieldPluginConfigurator();
  -    }
  -
       public void execute( PluginExecutionRequest request, PluginExecutionResponse 
response )
       {
           Object plugin = request.getPlugin();
  @@ -49,7 +44,7 @@
           {
               try
               {
  -                plugin = fieldPluginConfigurator.configure( plugin, 
request.getParameters() );
  +                plugin = configure( plugin, request.getParameters() );
               }
               catch ( PluginConfigurationException e )
               {
  @@ -80,5 +75,64 @@
               response.setException( e );
           }
       }
  +    // ----------------------------------------------------------------------
  +    //
  +    // ----------------------------------------------------------------------
  +
  +    protected Object configure( Object plugin, Map parameters )
  +        throws PluginConfigurationException
  +    {
  +        Class pluginClass = plugin.getClass();
  +
  +        for ( Iterator iterator = parameters.keySet().iterator(); 
iterator.hasNext(); )
  +        {
  +            String fieldName = (String) iterator.next();
  +
  +            Object value = parameters.get( fieldName );
  +
  +            try
  +            {
  +                Field field = getField( pluginClass, fieldName );
  +
  +                field.setAccessible( true );
  +
  +                field.set( plugin, value );
  +            }
  +            catch ( Exception e )
  +            {
  +                throw new PluginConfigurationException(
  +                    "Error setting value of field " + fieldName + " with " + value 
+ ".", e );
  +            }
  +        }
  +
  +        return plugin;
  +    }
  +
  +    private Field getField( Class clazz, String fieldName )
  +        throws Exception
  +    {
  +        Field field = null;
  +
  +        try
  +        {
  +            field = clazz.getDeclaredField( fieldName );
  +        }
  +        catch ( NoSuchFieldException e )
  +        {
  +            if ( clazz.getSuperclass() != Object.class )
  +            {
  +                field = getField( clazz.getSuperclass(), fieldName );
  +            }
  +            else
  +            {
  +                throw e;
  +            }
  +        }
  +        catch ( Exception e )
  +        {
  +            throw e;
  +        }
   
  +        return field;
  +    }
   }
  
  
  
  1.4       +45 -10    
maven-components/maven-core/src/main/java/org/apache/maven/plugin/plexus/executor/SetterPluginExecutor.java
  
  Index: SetterPluginExecutor.java
  ===================================================================
  RCS file: 
/home/cvs/maven-components/maven-core/src/main/java/org/apache/maven/plugin/plexus/executor/SetterPluginExecutor.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SetterPluginExecutor.java 7 May 2004 23:46:51 -0000       1.3
  +++ SetterPluginExecutor.java 8 May 2004 00:15:34 -0000       1.4
  @@ -19,10 +19,11 @@
   import org.apache.maven.plugin.PluginExecutionRequest;
   import org.apache.maven.plugin.PluginExecutionResponse;
   import org.apache.maven.plugin.plexus.PluginConfigurationException;
  -import org.apache.maven.plugin.plexus.SetterPluginConfigurator;
   
   import java.lang.reflect.InvocationTargetException;
   import java.lang.reflect.Method;
  +import java.util.Iterator;
  +import java.util.Map;
   
   /**
    *
  @@ -34,13 +35,6 @@
   public class SetterPluginExecutor
       implements PluginExecutor
   {
  -    private SetterPluginConfigurator setterPluginConfigurator;
  -
  -    public SetterPluginExecutor()
  -    {
  -        setterPluginConfigurator = new SetterPluginConfigurator();
  -    }
  -
       public void execute( PluginExecutionRequest request, PluginExecutionResponse 
response )
       {
           Object plugin = request.getPlugin();
  @@ -49,7 +43,7 @@
           {
               try
               {
  -                plugin = setterPluginConfigurator.configure( plugin, 
request.getParameters() );
  +                plugin = configure( plugin, request.getParameters() );
               }
               catch ( PluginConfigurationException e )
               {
  @@ -79,5 +73,46 @@
           {
               response.setException( e );
           }
  +    }
  +
  +    // ----------------------------------------------------------------------
  +    //
  +    // ----------------------------------------------------------------------
  +
  +    public Object configure( Object plugin, Map parameters )
  +        throws PluginConfigurationException
  +    {
  +        Class pluginClass = plugin.getClass();
  +
  +        for ( Iterator iterator = parameters.keySet().iterator(); 
iterator.hasNext(); )
  +        {
  +            String key = (String) iterator.next();
  +
  +            Object value = parameters.get( key);
  +
  +            String methodName = "set" + capitalise( key );
  +
  +            try
  +            {
  +                Method method = pluginClass.getMethod( methodName, new 
Class[]{value.getClass()} );
  +
  +                method.invoke( plugin, new Object[]{value} );
  +            }
  +            catch ( Exception e )
  +            {
  +                throw new PluginConfigurationException(
  +                    "Error setting value with method " + methodName + " with " + 
value + ".", e );
  +            }
  +        }
  +
  +        return plugin;
  +    }
  +
  +    private String capitalise( String str )
  +    {
  +        return new StringBuffer( str.length() )
  +            .append( Character.toTitleCase( str.charAt( 0 ) ) )
  +            .append( str.substring( 1 ) )
  +            .toString();
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to