KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark.toolagent;
2
3 import java.util.ArrayList JavaDoc;
4
5 import org.enhydra.shark.api.RootException;
6 import org.enhydra.shark.api.SharkTransaction;
7 import org.enhydra.shark.api.internal.toolagent.AppParameter;
8 import org.enhydra.shark.api.internal.toolagent.ApplicationBusy;
9 import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined;
10 import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted;
11 import org.enhydra.shark.api.internal.toolagent.ApplicationNotStopped;
12 import org.enhydra.shark.api.internal.toolagent.ConnectFailed;
13 import org.enhydra.shark.api.internal.toolagent.InvalidProcessInstance;
14 import org.enhydra.shark.api.internal.toolagent.InvalidSessionHandle;
15 import org.enhydra.shark.api.internal.toolagent.InvalidToolAgentHandle;
16 import org.enhydra.shark.api.internal.toolagent.InvalidWorkitem;
17 import org.enhydra.shark.api.internal.toolagent.SessionHandle;
18 import org.enhydra.shark.api.internal.toolagent.ToolAgent;
19 import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException;
20 import org.enhydra.shark.api.internal.working.CallbackUtilities;
21 import org.enhydra.shark.utilities.SizeLimitedToolAgentMap;
22 import org.enhydra.shark.xpdl.XMLUtil;
23 import org.enhydra.shark.xpdl.XPDLConstants;
24 import org.enhydra.shark.xpdl.elements.ExtendedAttribute;
25 import org.enhydra.shark.xpdl.elements.ExtendedAttributes;
26
27 /**
28  * Base class for all tool agents we implement.
29  * If one wants to extend this class and create tool agent, he should override
30  * invokeApplication method.
31  *
32  * @author Sasa Bojanic
33  */

34 public abstract class AbstractToolAgent implements ToolAgent {
35
36    public static final long APP_STATUS_INVALID=-1;
37    public static final long APP_STATUS_RUNNING=0;
38    public static final long APP_STATUS_ACTIVE=1;
39    public static final long APP_STATUS_WAITING=2;
40    public static final long APP_STATUS_TERMINATED=3;
41    public static final long APP_STATUS_FINISHED=4;
42
43    public static final String JavaDoc APP_NAME_EXT_ATTR_NAME="AppName";
44    public static final String JavaDoc APP_MODE_EXT_ATTR_NAME="AppMode";
45
46    protected String JavaDoc username;
47    protected String JavaDoc password;
48    protected String JavaDoc engineName;
49    protected String JavaDoc scope;
50    protected SessionHandle shandle;
51
52    protected long handle;
53    protected String JavaDoc appName;
54    protected String JavaDoc procInstId;
55    protected String JavaDoc assId;
56    protected AppParameter[] parameters;
57    protected Integer JavaDoc appMode;
58    protected long status=AbstractToolAgent.APP_STATUS_ACTIVE;
59
60    protected CallbackUtilities cus;
61
62    protected static SizeLimitedToolAgentMap extAttributes=null;
63
64    public void configure (CallbackUtilities cus) throws RootException {
65       this.cus=cus;
66
67       if (AbstractToolAgent.extAttributes==null) {
68          int eacs=-1;
69          try {
70             eacs=Integer.parseInt(cus.getProperty("AbstractToolAgent.extAttribsCacheSize","-1"));
71          } catch (Exception JavaDoc ex) {}
72          AbstractToolAgent.extAttributes=new SizeLimitedToolAgentMap(eacs);
73       }
74    }
75
76
77    public SessionHandle connect (SharkTransaction t,
78                                  String JavaDoc userId,
79                                  String JavaDoc password,
80                                  String JavaDoc engineName,
81                                  String JavaDoc scope)
82       throws ConnectFailed, ToolAgentGeneralException {
83
84       this.username=userId;
85       this.password=password;
86       this.engineName=engineName;
87       this.scope=scope;
88       shandle=new SessionHandleImpl(0,"");
89       return shandle;
90    }
91
92    public void disconnect (SharkTransaction t,SessionHandle shandle)
93       throws InvalidSessionHandle, ToolAgentGeneralException {
94
95       if (!this.shandle.equals(shandle)) throw new InvalidSessionHandle();
96    }
97
98    public void invokeApplication (SharkTransaction t,
99                                   long handle,
100                                   String JavaDoc applicationName,
101                                   String JavaDoc procInstId,
102                                   String JavaDoc assId,
103                                   AppParameter[] parameters,
104                                   Integer JavaDoc appMode)
105       throws ApplicationNotStarted, ApplicationNotDefined,
106       ApplicationBusy, ToolAgentGeneralException {
107
108       this.handle=handle;
109       this.appName=applicationName;
110       this.procInstId=procInstId;
111       this.assId=assId;
112       this.parameters=parameters;
113       this.appMode=appMode;
114       this.status=AbstractToolAgent.APP_STATUS_ACTIVE;
115             
116    }
117
118    public long requestAppStatus (SharkTransaction t,
119                                  long handle,
120                                  String JavaDoc procInstId,
121                                  String JavaDoc assId,
122                                  AppParameter[] parameters)
123       throws ApplicationBusy, InvalidToolAgentHandle,
124       InvalidWorkitem, InvalidProcessInstance, ToolAgentGeneralException {
125
126       if (this.handle==handle) {
127          //System.out.println("Status for class "+getClass().getName()+" = "+status);
128
if (!(status==APP_STATUS_FINISHED || status==APP_STATUS_TERMINATED ||
129                   status==APP_STATUS_INVALID)) {
130             //System.out.println("Throwing AB exc");
131
throw new ApplicationBusy();
132          }
133          if (status!=APP_STATUS_INVALID) {
134             copyParams(getReturnParameters(),parameters);
135             //ai.getReturnParameters(),parameters);
136
}
137          return status;
138       } else {
139          throw new InvalidToolAgentHandle("TA "+getClass().getName()+" - Can't find app for [handle="+handle+",pId="+procInstId+",assId="+assId+"]");
140       }
141    }
142
143    public void terminateApp (SharkTransaction t,
144                              long handle,
145                              String JavaDoc procInstId,
146                              String JavaDoc assId)
147       throws ApplicationNotStopped, InvalidWorkitem,
148       InvalidProcessInstance, ApplicationBusy, ToolAgentGeneralException {
149       cus.info("Terminating tool agent applications not implemented !!!");
150       throw new ApplicationNotStopped();
151    }
152
153    public String JavaDoc getInfo (SharkTransaction t) throws ToolAgentGeneralException {
154       return "";
155    }
156
157    protected ExtendedAttributes readParamsFromExtAttributes (String JavaDoc extAttribs) throws Exception JavaDoc {
158       ExtendedAttributes eas=(ExtendedAttributes)AbstractToolAgent.extAttributes.get(extAttribs);
159       if (eas==null) {
160          if (extAttribs!=null && !extAttribs.trim().equals("")) {
161             try {
162                eas=XMLUtil.destringyfyExtendedAttributes(extAttribs);
163                eas.setReadOnly(true);
164                eas.initCaches();
165             } catch (Throwable JavaDoc ex) {
166                System.out.println("FAiled to destr");
167                cus.warn("AbstractToolAgent -> "+ex.getMessage());
168             }
169          }
170
171          //if (AbstractToolAgent.extAttributes.getMaximumSize()>0) {
172
AbstractToolAgent.extAttributes.put(extAttribs,eas);
173          //}
174
}
175       ExtendedAttribute ea=eas.getFirstExtendedAttributeForName(AbstractToolAgent.APP_NAME_EXT_ATTR_NAME);
176       if (ea!=null) {
177          appName=ea.getVValue();
178       }
179       ea=eas.getFirstExtendedAttributeForName(AbstractToolAgent.APP_MODE_EXT_ATTR_NAME);
180       if (ea!=null) {
181          try {
182             appMode=new Integer JavaDoc(Integer.parseInt(ea.getVValue()));
183          } catch (Exception JavaDoc ex){}
184       }
185       return eas;
186    }
187
188    protected AppParameter[] getReturnParameters () {
189       // preparing result
190
ArrayList JavaDoc returnValues = new ArrayList JavaDoc();
191       if (parameters!=null) {
192          for(int i = 0; i < parameters.length; i++){
193             if(parameters[i].the_mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_OUT) ||
194                parameters[i].the_mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) {
195                returnValues.add(parameters[i]);
196             }
197          }
198       }
199       AppParameter[] retParameters=(AppParameter[])returnValues.toArray(new AppParameter[returnValues.size()]);
200
201       return retParameters;
202    }
203
204    public static void copyParams (AppParameter[] taApps,AppParameter[] apps) {
205       if (taApps!=null) {
206          for (int i=0; i<taApps.length; i++) {
207             AppParameter taApp=taApps[i];
208             for (int j=0; j<apps.length; j++) {
209                if (apps[j].the_actual_name.equals(taApp.the_actual_name)) {
210                   apps[j].the_value=taApp.the_value;
211                }
212             }
213          }
214       }
215    }
216
217 }
218
Popular Tags