KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > toolagent > JavaClassToolAgent


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 /**
9  * Tool agent that executes special kind of java class.
10  * @author Sasa Bojanic
11  * @version 1.0
12  */

13 public class JavaClassToolAgent extends AbstractToolAgent {
14
15    private static final String JavaDoc EXECUTION_METHOD_NAME = "execute";
16
17    public void invokeApplication (SharkTransaction t,
18                                   long handle,
19                                   String JavaDoc applicationName,
20                                   String JavaDoc procInstId,
21                                   String JavaDoc assId,
22                                   AppParameter[] parameters,
23                                   Integer JavaDoc 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 JavaDoc)parameters[0].the_value);
34          }
35
36          ClassLoader JavaDoc cl=getClass().getClassLoader();
37          Class JavaDoc c = cl.loadClass(appName);
38
39          // Get parameter types - ignore 1. param, these are ext.attribs
40
Class JavaDoc[] parameterTypes=null;
41          if (parameters!=null) {
42             parameterTypes=new Class JavaDoc[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          // invoke the method
51
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getInfo (SharkTransaction t) throws ToolAgentGeneralException {
73       String JavaDoc 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