KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark.toolagent;
2
3
4 import org.enhydra.shark.api.RootError;
5 import org.enhydra.shark.api.SharkTransaction;
6 import org.enhydra.shark.api.internal.toolagent.AppParameter;
7 import org.enhydra.shark.api.internal.toolagent.ApplicationBusy;
8 import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined;
9 import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted;
10 import org.enhydra.shark.api.internal.toolagent.SessionHandle;
11 import org.enhydra.shark.api.internal.toolagent.ToolAgent;
12 import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException;
13 import org.enhydra.shark.xpdl.elements.ExtendedAttribute;
14 import org.enhydra.shark.xpdl.elements.ExtendedAttributes;
15
16 /**
17  * Tool agent that is called when there are no any mapped tool agents.
18  * It reads the ext. attributes to find the tool agent to call.
19  * When it calls another tool agent, it just passes it parameters and ...
20  * @author Sasa Bojanic
21  * @version 1.0
22  */

23 public class DefaultToolAgent extends AbstractToolAgent {
24
25    private final static String JavaDoc TOOL_AGENT_CLASS_EXT_ATTR_NAME="ToolAgentClass";
26
27    private String JavaDoc taClass;
28    private String JavaDoc callingAppName;
29    private Integer JavaDoc callingAppMode;
30
31    public void invokeApplication (SharkTransaction t,
32                                   long handle,
33                                   String JavaDoc applicationName,
34                                   String JavaDoc procInstId,
35                                   String JavaDoc assId,
36                                   AppParameter[] parameters,
37                                   Integer JavaDoc appMode)
38       throws ApplicationNotStarted, ApplicationNotDefined,
39       ApplicationBusy, ToolAgentGeneralException {
40
41       super.invokeApplication(t,handle,applicationName,procInstId,assId,parameters,appMode);
42
43       try {
44          status=APP_STATUS_RUNNING;
45
46          String JavaDoc extAttribs=(String JavaDoc)parameters[0].the_value;
47          readParamsFromExtAttributes(extAttribs);
48
49          //System.out.println("Calling "+taClass+" tool agent");
50

51          Class JavaDoc cls=null;
52
53          try {
54             cls=Class.forName(taClass);
55          } catch (ClassNotFoundException JavaDoc cnfe1) {
56             cls = ToolAgentLoader.load(cus,taClass);
57          }
58
59
60
61          ToolAgent ta=(ToolAgent)cls.newInstance();
62          ta.configure(cus);
63
64          // try to connect to the tool agent
65
SessionHandle taShandle;
66          try {
67             taShandle=ta.connect(t,username,password,engineName,scope);
68          } catch (org.enhydra.shark.api.internal.toolagent.ConnectFailed cf) {
69             cus.error("Default Tool Agent - connection to Tool agent "+taClass+" failed !");
70             throw cf;
71          }
72          ta.invokeApplication(t,taShandle.getHandle(),callingAppName,procInstId,assId,parameters,callingAppMode);
73          status=ta.requestAppStatus(t,taShandle.getHandle(),procInstId,assId,parameters);
74          //System.out.println("DTA finished execution");
75
ta.disconnect(t,taShandle);
76
77       } catch (ClassNotFoundException JavaDoc cnf) {
78          cus.error("DefaultToolAgent - application "+appName+" terminated incorrectly, can't find class: "+cnf);
79          throw new ApplicationNotDefined("Can't find class "+taClass,cnf);
80       } catch (NoClassDefFoundError JavaDoc ncdfe) {
81          cus.error("DefaultToolAgent - application "+appName+" terminated incorrectly, can't find class definition: "+ncdfe);
82          throw new ApplicationNotDefined("Class "+appName+" can't be executed",ncdfe);
83       } catch (Throwable JavaDoc ex) {
84          //ex.printStackTrace();
85
cus.error("DefaultToolAgent - application "+appName+" terminated incorrectly: "+ex);
86          status=APP_STATUS_INVALID;
87          if (ex instanceof ToolAgentGeneralException) {
88             throw (ToolAgentGeneralException)ex;
89          } else if (ex instanceof ApplicationNotStarted) {
90             throw (ApplicationNotStarted)ex;
91          } else if (ex instanceof ApplicationNotDefined) {
92             throw (ApplicationNotDefined)ex;
93          } else if (ex instanceof ApplicationBusy) {
94             throw (ApplicationBusy)ex;
95          } else {
96             throw new RootError("Unexpected error",ex);
97          }
98       }
99    }
100
101    public String JavaDoc getInfo (SharkTransaction t) throws ToolAgentGeneralException {
102       String JavaDoc i="It is used to execute other tool agents based on XPDL extended attributes"+
103          "\nof appropriate Application definition. It is called by kernel if there are"+
104          "\nno mapping information for some XPDL Application definition";
105       return i;
106    }
107
108    protected ExtendedAttributes readParamsFromExtAttributes (String JavaDoc extAttribs) throws Exception JavaDoc {
109       String JavaDoc oldAppName=appName;
110       Integer JavaDoc oldAppMode=appMode;
111       ExtendedAttributes eas=super.readParamsFromExtAttributes(extAttribs);
112       callingAppName=appName;
113       callingAppMode=appMode;
114       appName=oldAppName;
115       appMode=oldAppMode;
116       ExtendedAttribute ea=eas.getFirstExtendedAttributeForName(TOOL_AGENT_CLASS_EXT_ATTR_NAME);
117       if (ea!=null) {
118          taClass=ea.getVValue();
119       }
120       return eas;
121    }
122
123 }
124
Popular Tags