001: package org.enhydra.shark.toolagent;
002:
003: import java.lang.reflect.Method;
004:
005: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
006: import org.enhydra.shark.api.client.wfservice.WMEntity;
007: import org.enhydra.shark.api.internal.toolagent.AppParameter;
008: import org.enhydra.shark.api.internal.toolagent.ApplicationBusy;
009: import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined;
010: import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted;
011: import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException;
012:
013: /**
014: * Tool agent that executes special kind of java class.
015: *
016: * @author Sasa Bojanic
017: * @version 1.0
018: */
019: public class JavaClassToolAgent extends AbstractToolAgent {
020:
021: private static final String EXECUTION_METHOD_NAME = "execute";
022:
023: public void invokeApplication(WMSessionHandle shandle, long handle,
024: WMEntity appInfo, WMEntity toolInfo,
025: String applicationName, String procInstId, String assId,
026: AppParameter[] parameters, Integer appMode)
027: throws ApplicationNotStarted, ApplicationNotDefined,
028: ApplicationBusy, ToolAgentGeneralException {
029:
030: super
031: .invokeApplication(shandle, handle, appInfo, toolInfo,
032: applicationName, procInstId, assId, parameters,
033: appMode);
034:
035: try {
036: status = APP_STATUS_RUNNING;
037:
038: if (appName == null || appName.trim().length() == 0) {
039: readParamsFromExtAttributes((String) parameters[0].the_value);
040: }
041:
042: ClassLoader cl = getClass().getClassLoader();
043: Class c = cl.loadClass(appName);
044:
045: // Get parameter types - ignore 1. param, these are ext.attribs
046: Class[] parameterTypes = null;
047: if (parameters != null) {
048: parameterTypes = new Class[parameters.length - 1];
049: for (int i = 1; i < parameters.length; i++) {
050: parameterTypes[i - 1] = AppParameter.class;
051: }
052: }
053:
054: Method m = c.getMethod(EXECUTION_METHOD_NAME,
055: parameterTypes);
056:
057: // invoke the method
058: AppParameter[] aps = new AppParameter[parameters.length - 1];
059: System.arraycopy(parameters, 1, aps, 0, aps.length);
060: m.invoke(null, aps);
061: status = APP_STATUS_FINISHED;
062:
063: } catch (ClassNotFoundException cnf) {
064: cus.error(shandle, "JavaClassToolAgent - application "
065: + appName
066: + " terminated incorrectly, can't find class: "
067: + cnf);
068: throw new ApplicationNotDefined("Can't find class "
069: + appName, cnf);
070: } catch (NoSuchMethodException nsm) {
071: cus.error(shandle, "JavaClassToolAgent - application "
072: + appName
073: + " terminated incorrectly, can't find method "
074: + EXECUTION_METHOD_NAME + " : " + nsm);
075: throw new ApplicationNotDefined("Class " + appName
076: + " doesn't have method " + EXECUTION_METHOD_NAME,
077: nsm);
078: } catch (NoClassDefFoundError ncdfe) {
079: cus
080: .error(
081: shandle,
082: "JavaClassToolAgent - application "
083: + appName
084: + " terminated incorrectly, can't find class definition: "
085: + ncdfe);
086: throw new ApplicationNotDefined("Class " + appName
087: + " can't be executed", ncdfe);
088: } catch (Throwable ex) {
089: cus.error(shandle, "JavaClassToolAgent - application "
090: + appName + " terminated incorrectly: " + ex);
091: status = APP_STATUS_INVALID;
092: throw new ToolAgentGeneralException(ex);
093: }
094: }
095:
096: public String getInfo(WMSessionHandle shandle)
097: throws ToolAgentGeneralException {
098: String i = "This tool agent executes Java classes, by calling its static method called \"execute\"."
099: + "\nWhen calling this tool agent's invokeApplication() method, the application "
100: + "\nname parameter should be the full name of the class that should be executed "
101: + "\nby this tool agent (the classes had to be in the class path) "
102: + "\n"
103: + "\nThis tool agent is able to understand the extended attribute with the following name:"
104: + "\n * AppName - value of this attribute should represent the class name to be executed"
105: + "\n"
106: + "\n NOTE: Tool agent will read extended attributes only if they are called through"
107: + "\n Default tool agent (not by shark directly) and this is the case when information "
108: + "\n on which tool agent to start for XPDL application definition is not contained in mappings";
109: return i;
110: }
111: }
|