001: package org.enhydra.shark.toolagent;
002:
003: import java.util.ArrayList;
004:
005: import org.enhydra.shark.api.client.wfmc.wapi.WMConnectInfo;
006: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
007: import org.enhydra.shark.api.client.wfservice.WMEntity;
008: import org.enhydra.shark.api.internal.toolagent.AppParameter;
009: import org.enhydra.shark.api.internal.toolagent.ApplicationBusy;
010: import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined;
011: import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted;
012: import org.enhydra.shark.api.internal.toolagent.ApplicationNotStopped;
013: import org.enhydra.shark.api.internal.toolagent.ConnectFailed;
014: import org.enhydra.shark.api.internal.toolagent.InvalidProcessInstance;
015: import org.enhydra.shark.api.internal.toolagent.InvalidSessionHandle;
016: import org.enhydra.shark.api.internal.toolagent.InvalidToolAgentHandle;
017: import org.enhydra.shark.api.internal.toolagent.InvalidWorkitem;
018: import org.enhydra.shark.api.internal.toolagent.ToolAgent;
019: import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException;
020: import org.enhydra.shark.api.internal.working.CallbackUtilities;
021: import org.enhydra.shark.utilities.MiscUtilities;
022: import org.enhydra.shark.utilities.SizeLimitedToolAgentMap;
023: import org.enhydra.shark.xpdl.XMLUtil;
024: import org.enhydra.shark.xpdl.XPDLConstants;
025: import org.enhydra.shark.xpdl.elements.ExtendedAttribute;
026: import org.enhydra.shark.xpdl.elements.ExtendedAttributes;
027:
028: /**
029: * Base class for all tool agents we implement. If one wants to extend this class and
030: * create tool agent, he should override invokeApplication method.
031: *
032: * @author Sasa Bojanic
033: */
034: public abstract class AbstractToolAgent implements ToolAgent {
035:
036: public static final String COMMA_SEPARATOR_STR = ",";
037: public static final String SEMICOLON_SEPARATOR_STR = ";";
038:
039: public static final String APP_NAME_EXT_ATTR_NAME = "AppName";
040:
041: public static final String APP_MODE_EXT_ATTR_NAME = "AppMode";
042:
043: protected WMConnectInfo wmci;
044:
045: protected WMSessionHandle tashandle;
046:
047: protected WMSessionHandle shandle;
048:
049: protected WMEntity appInfo;
050:
051: protected WMEntity toolInfo;
052:
053: protected long handle;
054:
055: protected String appName;
056:
057: protected String procInstId;
058:
059: protected String assId;
060:
061: protected AppParameter[] parameters;
062:
063: protected Integer appMode;
064:
065: protected long status = ToolAgent.APP_STATUS_ACTIVE;
066:
067: protected CallbackUtilities cus;
068:
069: protected static SizeLimitedToolAgentMap extAttributes = null;
070:
071: public void configure(CallbackUtilities cus) throws Exception {
072: this .cus = cus;
073:
074: if (AbstractToolAgent.extAttributes == null) {
075: int eacs = -1;
076: try {
077: eacs = Integer.parseInt(cus.getProperty(
078: "AbstractToolAgent.extAttribsCacheSize", "-1"));
079: } catch (Exception ex) {
080: }
081: AbstractToolAgent.extAttributes = new SizeLimitedToolAgentMap(
082: eacs);
083: }
084: }
085:
086: public WMSessionHandle connect(WMConnectInfo ci)
087: throws ConnectFailed, ToolAgentGeneralException {
088:
089: this .wmci = ci;
090: tashandle = new WMSessionHandle();
091: tashandle.setId(0);
092: tashandle.setVendorData(ci.getUserIdentification());
093:
094: return tashandle;
095: }
096:
097: public void disconnect(WMSessionHandle shandle)
098: throws InvalidSessionHandle, ToolAgentGeneralException {
099:
100: if (!this .tashandle.equals(shandle))
101: throw new InvalidSessionHandle();
102: }
103:
104: public void invokeApplication(WMSessionHandle shandle, long handle,
105: WMEntity appInfo, WMEntity toolInfo,
106: String applicationName, String procInstId, String assId,
107: AppParameter[] parameters, Integer appMode)
108: throws ApplicationNotStarted, ApplicationNotDefined,
109: ApplicationBusy, ToolAgentGeneralException {
110:
111: this .shandle = shandle;
112: this .appInfo = appInfo;
113: this .toolInfo = toolInfo;
114: this .handle = handle;
115: this .appName = applicationName;
116: this .procInstId = procInstId;
117: this .assId = assId;
118: this .parameters = parameters;
119: this .appMode = appMode;
120: this .status = ToolAgent.APP_STATUS_ACTIVE;
121:
122: }
123:
124: public long requestAppStatus(WMSessionHandle shandle, long handle,
125: WMEntity toolInfo, String procInstId, String assId,
126: AppParameter[] parameters) throws ApplicationBusy,
127: InvalidToolAgentHandle, InvalidWorkitem,
128: InvalidProcessInstance, ToolAgentGeneralException {
129:
130: if (this .handle == handle) {
131: // System.out.println("Status for class "+getClass().getName()+" = "+status);
132: if (!(status == APP_STATUS_FINISHED
133: || status == APP_STATUS_TERMINATED || status == APP_STATUS_INVALID)) {
134: // System.out.println("Throwing AB exc");
135: throw new ApplicationBusy();
136: }
137: if (status != APP_STATUS_INVALID) {
138: copyParams(getReturnParameters(), parameters);
139: // ai.getReturnParameters(),parameters);
140: }
141: return status;
142: } else {
143: throw new InvalidToolAgentHandle("TA "
144: + getClass().getName()
145: + " - Can't find app for [handle=" + handle
146: + ",pId=" + procInstId + ",assId=" + assId + "]");
147: }
148: }
149:
150: public void terminateApp(WMSessionHandle shandle, long handle,
151: WMEntity toolInfo, String procInstId, String assId)
152: throws ApplicationNotStopped, InvalidWorkitem,
153: InvalidProcessInstance, ApplicationBusy,
154: ToolAgentGeneralException {
155: cus
156: .info(shandle,
157: "Terminating tool agent applications not implemented !!!");
158: throw new ApplicationNotStopped();
159: }
160:
161: public String getInfo(WMSessionHandle shandle)
162: throws ToolAgentGeneralException {
163: return "";
164: }
165:
166: protected ExtendedAttributes readParamsFromExtAttributes(
167: String extAttribs) throws Exception {
168: ExtendedAttributes eas = (ExtendedAttributes) AbstractToolAgent.extAttributes
169: .get(extAttribs);
170: if (eas == null) {
171: if (extAttribs != null && !extAttribs.trim().equals("")) {
172: try {
173: eas = XMLUtil
174: .destringyfyExtendedAttributes(extAttribs);
175: eas.setReadOnly(true);
176: eas.initCaches();
177: } catch (Throwable ex) {
178: // System.out.println("FAiled to destr");
179: cus.warn(shandle, "AbstractToolAgent -> "
180: + ex.getMessage());
181: }
182: }
183:
184: // if (AbstractToolAgent.extAttributes.getMaximumSize()>0) {
185: AbstractToolAgent.extAttributes.put(extAttribs, eas);
186: // }
187: }
188: ExtendedAttribute ea = eas
189: .getFirstExtendedAttributeForName(AbstractToolAgent.APP_NAME_EXT_ATTR_NAME);
190: if (ea != null) {
191: appName = ea.getVValue();
192: }
193: ea = eas
194: .getFirstExtendedAttributeForName(AbstractToolAgent.APP_MODE_EXT_ATTR_NAME);
195: if (ea != null) {
196: try {
197: appMode = new Integer(Integer.parseInt(ea.getVValue()));
198: } catch (Exception ex) {
199: }
200: }
201: return eas;
202: }
203:
204: protected AppParameter[] getReturnParameters() {
205: // preparing result
206: ArrayList returnValues = new ArrayList();
207: if (parameters != null) {
208: for (int i = 0; i < parameters.length; i++) {
209: if (parameters[i].the_mode
210: .equals(XPDLConstants.FORMAL_PARAMETER_MODE_OUT)
211: || parameters[i].the_mode
212: .equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) {
213: returnValues.add(parameters[i]);
214: }
215: }
216: }
217: AppParameter[] retParameters = (AppParameter[]) returnValues
218: .toArray(new AppParameter[returnValues.size()]);
219:
220: return retParameters;
221: }
222:
223: public static void copyParams(AppParameter[] taApps,
224: AppParameter[] apps) {
225: if (taApps != null) {
226: for (int i = 0; i < taApps.length; i++) {
227: AppParameter taApp = taApps[i];
228: for (int j = 0; j < apps.length; j++) {
229: if (apps[j].the_actual_name
230: .equals(taApp.the_actual_name)) {
231: apps[j].the_value = taApp.the_value;
232: }
233: }
234: }
235: }
236: }
237:
238: public static AppParameter getParameterByName(
239: AppParameter[] sharkParameters, String name)
240: throws Exception {
241: if (sharkParameters != null) {
242: for (int i = 0; i < sharkParameters.length; i++) {
243: if (name.equals(sharkParameters[i].the_formal_name)) {
244: return sharkParameters[i];
245: }
246: }
247: }
248: return null;
249: }
250:
251: public static String getParameterString(
252: AppParameter[] sharkParameters, String parameterName)
253: throws Exception {
254: AppParameter param = AbstractToolAgent.getParameterByName(
255: sharkParameters, parameterName);
256: if (param != null && param.the_value != null) {
257: return param.the_value.toString();
258: }
259: return "";
260: }
261:
262: public static String[] getParameterStringArray(
263: AppParameter[] sharkParameters, String parameterName,
264: String separatorStr) throws Exception {
265: AppParameter param = AbstractToolAgent.getParameterByName(
266: sharkParameters, parameterName);
267: String[] ret = null;
268: if (param != null && param.the_value != null) {
269: String pval = param.the_value.toString();
270: ret = MiscUtilities.tokenize(pval, separatorStr);
271: }
272: return ret;
273: }
274:
275: }
|