1 package org.enhydra.shark.toolagent; 2 3 import java.lang.reflect.*; 4 5 import org.enhydra.shark.api.*; 6 import org.enhydra.shark.api.internal.toolagent.*; 7 8 13 public class JavaClassToolAgent extends AbstractToolAgent { 14 15 private static final String EXECUTION_METHOD_NAME = "execute"; 16 17 public void invokeApplication (SharkTransaction t, 18 long handle, 19 String applicationName, 20 String procInstId, 21 String assId, 22 AppParameter[] parameters, 23 Integer appMode) 24 throws ApplicationNotStarted, ApplicationNotDefined, 25 ApplicationBusy, ToolAgentGeneralException { 26 27 super.invokeApplication(t,handle,applicationName,procInstId,assId,parameters,appMode); 28 29 try { 30 status=APP_STATUS_RUNNING; 31 32 if (appName==null || appName.trim().length()==0) { 33 readParamsFromExtAttributes((String )parameters[0].the_value); 34 } 35 36 ClassLoader cl=getClass().getClassLoader(); 37 Class c = cl.loadClass(appName); 38 39 Class [] parameterTypes=null; 41 if (parameters!=null) { 42 parameterTypes=new Class [parameters.length-1]; 43 for (int i=1; i<parameters.length; i++) { 44 parameterTypes[i-1]=AppParameter.class; 45 } 46 } 47 48 Method m = c.getMethod(EXECUTION_METHOD_NAME,parameterTypes); 49 50 AppParameter[] aps=new AppParameter[parameters.length-1]; 52 System.arraycopy(parameters,1,aps,0,aps.length); 53 m.invoke(null,aps); 54 status=APP_STATUS_FINISHED; 55 56 } catch (ClassNotFoundException cnf) { 57 cus.error("JavaClassToolAgent - application "+appName+" terminated incorrectly, can't find class: "+cnf); 58 throw new ApplicationNotDefined("Can't find class "+appName,cnf); 59 } catch (NoSuchMethodException nsm) { 60 cus.error("JavaClassToolAgent - application "+appName+" terminated incorrectly, can't find method "+EXECUTION_METHOD_NAME+" : "+nsm); 61 throw new ApplicationNotDefined("Class "+appName+" doesn't have method "+EXECUTION_METHOD_NAME,nsm); 62 } catch (NoClassDefFoundError ncdfe) { 63 cus.error("JavaClassToolAgent - application "+appName+" terminated incorrectly, can't find class definition: "+ncdfe); 64 throw new ApplicationNotDefined("Class "+appName+" can't be executed",ncdfe); 65 } catch (Throwable ex) { 66 cus.error("JavaClassToolAgent - application "+appName+" terminated incorrectly: "+ex); 67 status=APP_STATUS_INVALID; 68 throw new ToolAgentGeneralException(ex); 69 } 70 } 71 72 public String getInfo (SharkTransaction t) throws ToolAgentGeneralException { 73 String i="This tool agent executes Java classes, by calling its static method called \"execute\"."+ 74 "\nWhen calling this tool agent's invokeApplication() method, the application "+ 75 "\nname parameter should be the full name of the class that should be executed "+ 76 "\nby this tool agent (the classes had to be in the class path) "+ 77 "\n"+ 78 "\nThis tool agent is able to understand the extended attribute with the following name:"+ 79 "\n * AppName - value of this attribute should represent the class name to be executed"+ 80 "\n"+ 81 "\n NOTE: Tool agent will read extended attributes only if they are called through"+ 82 "\n Default tool agent (not by shark directly) and this is the case when information "+ 83 "\n on which tool agent to start for XPDL application definition is not contained in mappings"; 84 return i; 85 } 86 } 87 | Popular Tags |