N1 Grid Service Provisioning System 5.0 Plug-in Development Guide

execJava API

execJava functionality is provided through the XML schema for plans and components. Through the XML, you can execute a piece of Java code as needed. In addition, execJava also exists as an API.

Both preflight and actual behavior may be specified. The classes are typically deployed using a JAR resource of a component. For more information about the execJava classes, methods, and interfaces, see the JavaDoc software.

<execJava
className= classname of the executor factory class
class Path=...
>

The execJava API is contained in the com.sun.n1.sps.plugin.execJava package. The execJava API consists of five interfaces and two exception classes:

ActualExecJavaContext

This interface publishes the services available to the execJava implementations when they are invoked during the deployment or actual phase of the execution.

ExecJavaContext

This interface provides an execution context to an execJava implementation that is common to both the preflight and actual run levels.

Executor

This interface is implemented by classes that need to execute code on the agent through execJava

ExecutorFactory

This interface is part of the infrastructure to execute arbitrary code on the remote agent using execJava steps.

PreflightExecJavaContext

This interface publishes the services available to the execJava implementations when they are invoked during the preflight phase of the execution.

ExecutionException

Instances of ExecutionException are used to flag failure or warnings from execJava invocations.

ExecutionTimeoutException

Instances of this exception are thrown when execJava execution is timed out.

ExecutorFactory Interface

The ExecutorFactory interface is used to obtain the preflight and actual executor instances for a particular step:

Executor getActualExecutor(AgentContext callContext)
Executor getPreflightExecutor(AgentContext callContext)

The call context passed between preflight and actual execution steps need not be the same.

AgentContext Method

The AgentContext method provides an invocation context on a particular remote agent.

VariableSettingsHolder getVariables()
    // Returns the variables passed to the execJava step using <argList>

PrintStream getStandardOutput()
PrintStream getStandardError()
InputStream getStandardInput()
File getWorkingDir()

Executor Interface

The Executor interface provides an entry point that is used to execute the step body:

void execute() throw ExecutionException

Execution output and error output are written into the stdout and stderr streams of the associated agent context. Input is read from the input stream of the associated agent context. Errors are reported by calling an instance of the ExecutionException class.

execJava Examples


Example 3–3 execJava in Java Code

public class StopServerFactory extends WLFactoryBase {
    
    public static final String TARGET = "serverName";
    
    public Executor 
        getActualExecutor(AgentContext inAgentContext, ActualExecJavaContext inContext) 
        {

        VariableSettingsSource variableSettings = inContext.getVariableSettings();
        String target = variableSettings.getVarValue(TARGET);
        return new StopServerExecutor(getConnect(variableSettings), target);
    }
    
    public VariableSettingsSource getParams() {
        VariableSettingsHolder list = getWLParams();
        list.setVarValue(TARGET, null);
        return list;
    }

}
public class StopServerExecutor implements Executor {
    private WLConnect mConnect;
    private String mTarget;

    /**
     *
     **/
    public StopServerExecutor(WLConnect connect, String target) {
           mConnect = connect;
           mTarget = target;
    }

    /**
     *
     **/
    public void execute() throws ExecutionException {

  try {
    WLAdminServer server = new WLAdminServer(mConnect);
    server.stopServer(server.getServer(mTarget));
    } 
catch (Exception e) {
    throw new ExecutionException
    (new PluginMessage(WLPluginHierarchyException.MSG_WEBLOGIC_ERROR), e);
    }
  }
}


Example 3–4 Another execJava Code Sample

public class SampleExecutorFactory implements ExecutorFactory
{
    public Executor getActualExecutor(AgentContext inAgentContext, 
           ActualExecJavaContext inActualExecJavaContext)
    {
        return new EnvParamSettingActualExecutor(inActualExecJavaContext);
    }

    public Executor getPreflightExecutor(AgentContext inAgentContext,
           PreflightExecJavaContext inPreflightExecJavaContext)
    {
        return new EnvParamSettingPreflightExecutor(inPreflightExecJavaContext);
    }

    public VariableSettingsSource getParams()
    {
        VariableSettingsHolder params = new VariableSettingsHolder();
        params.setVarValue(PARAM_NAME, "");
        params.setVarValue(PARAM_VALUE, "");
        return params;
    }

    public static final String PARAM_NAME = "nameParam";
    public static final String PARAM_VALUE = "valueParam";
}
public class EnvParamSettingPreflightExecutor implements Executor
{
    VariableSettingsSource mVars;
    public EnvParamSettingPreflightExecutor
           (PreflightExecJavaContext inPreflightExecJavaContext)
    {
        mVars = inPreflightExecJavaContext.getVariableSettings();
    }

    public void execute() throws ExecutionException
    {
        String propName = mVars.getVarValue(SampleExecutorFactory.PARAM_NAME);
        if("".equals(propName)) {
            throw new ExecutionException(new PluginMessage("sample.noNameParam"));
        }

        String propValue=System.getProperty(propName);
        if(!(propValue == null || "".equals(propValue))) {
            // property already set, error out
            throw new ExecutionException(new PluginMessage("sample.propAlreadySet",
                                        new String[]{propName, propValue}));
        }
    }
}

public class EnvParamSettingActualExecutor implements Executor
{
    VariableSettingsSource mVars;
    public EnvParamSettingActualExecutor(ActualExecJavaContext inCtx)
    {
        mVars  = inCtx.getVariableSettings();
    }

    public void execute() throws ExecutionException
    {
        String propName = mVars.getVarValue(SampleExecutorFactory.PARAM_NAME);
        String propValue = mVars.getVarValue(SampleExecutorFactory.PARAM_VALUE);
        System.setProperty(propName, propValue);
        if(Logger.isDebugEnabled(this)) {
            Logger.debug("Setting prop "+propName + " to " + propValue, this);
        }
        System.out.println("Setting prop "+propName + " to " + propValue);
    }
}