package com.bea.ps.maven.util;

import java.lang.reflect.Field;
import java.util.Map;

import org.apache.maven.plugin.ContextEnabled;
import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;

public class MojoInvoker {

        private Log log;
        private Map pluginContext;
        
        public MojoInvoker(Log log, Map pluginContext) {
                this.log = log;
                this.pluginContext = pluginContext;
        }

        public void invoke(String mojoClass, Map<String,Object> parameters) 
throws MojoExecutionException, MojoFailureException {
                try {
                        Class clazz = Class.forName(mojoClass);
                        invoke(clazz,parameters);
                } catch (ClassNotFoundException e) {
                        throw new MojoExecutionException("No class was found 
for "+mojoClass,e);
                }
        }

        
        public void invoke(Class mojoClass, Map<String,Object> parameters) 
throws MojoExecutionException, MojoFailureException {
                Object object = null;
                try {
                        object = mojoClass.newInstance();
                } catch (InstantiationException e) {
                        throw new MojoExecutionException(mojoClass.getName()+" 
could not be instantiated",e);
                } catch (IllegalAccessException e) {
                        throw new MojoExecutionException(mojoClass.getName()+" 
could not be instantiated",e);
                }
                if (!(object instanceof Mojo)) throw new 
MojoExecutionException(mojoClass.getName()+" does not implement the Mojo 
interface, execution aborted");
                Mojo mojo = (Mojo)object;
                mojo.setLog(log);
                if (mojo instanceof ContextEnabled) 
((ContextEnabled)mojo).setPluginContext(pluginContext);
                setParameters(mojo,parameters);
                mojo.execute();
        }
        
        public Log getLog() {
                return log;
        }
        
        public Map getPluginContext() {
                return pluginContext;
        }
        
        private void setParameters(Mojo mojo, Map<String,Object> parameters)  
throws MojoExecutionException, MojoFailureException  {
                log.debug("    Assigning values to mojo 
'"+mojo.getClass().getName()+"'");
                
                Class current = mojo.getClass();
                while (current!=null) {
                        log.debug("Applying parameters to "+current.getName());

                        Field[] fields = current.getDeclaredFields();
                        for (int i=0; i<fields.length; i++) {
                                log.debug("    Getting value for field 
'"+fields[i].getName()+"'");
                                Object value = 
parameters.get(fields[i].getName());
                                log.debug("    Value for field 
'"+fields[i].getName()+"' equals '"+value+"'");
        
                                if (value!=null) {
                                        fields[i].setAccessible(true);
                                        try {
                                                log.debug("    Assigning value 
'"+value+"' to field '"+fields[i].getName()+"'");
                                                fields[i].set(mojo,value);
                                        } catch (IllegalAccessException e) {
                                                throw new 
MojoExecutionException("Could not access field '"+fields[i].getName()+"' for 
class '"+mojo.getClass().getName()+"'",e);
                                        }
                                }
                        }
                        current = current.getSuperclass();
                }
        }
}

-----Original Message-----
From: Wayne Fay [mailto:[EMAIL PROTECTED]
Sent: Wednesday, June 06, 2007 10:43 AM
To: Maven Users List
Subject: Re: calling plugin in another plugin?


If you attached something to your email, it was removed before being
sent on to the rest of the list, unfortunately.

Please resend (in the message body) or provide a download link -- it
sounds useful.

Wayne

On 6/6/07, Nunn, Gerald <[EMAIL PROTECTED]> wrote:
> Here is a MojoInvoker class I wrote that invokes one Mojo from another using 
> reflection, I use it to invoke the install plugin from a plugin that handles 
> breaking up and installing weblogic shared libraries into the repository. The 
> philosphy is that the plugin doing the invoking is familiar with the invoked 
> plugin and is responsible for passing parameters accordingly through a map. 
> Reflection is used to assign the parameters to the mojo's attributes.
>
> While it's only been tested with the install plugin it might be useful for 
> what you want to do and could be broadened out into a general API.
>
> Gerald
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>

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


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

Reply via email to