001: package org.enhydra.shark.toolagent;
002:
003: import org.enhydra.shark.api.RootError;
004: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
005: import org.enhydra.shark.api.client.wfservice.WMEntity;
006: import org.enhydra.shark.api.internal.toolagent.AppParameter;
007: import org.enhydra.shark.api.internal.toolagent.ApplicationBusy;
008: import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined;
009: import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted;
010: import org.enhydra.shark.api.internal.toolagent.ToolAgent;
011: import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException;
012: import org.enhydra.shark.xpdl.elements.ExtendedAttribute;
013: import org.enhydra.shark.xpdl.elements.ExtendedAttributes;
014:
015: /**
016: * Tool agent that is called when there are no any mapped tool agents. It reads the ext.
017: * attributes to find the tool agent to call. When it calls another tool agent, it just
018: * passes it parameters and ...
019: *
020: * @author Sasa Bojanic
021: * @version 1.0
022: */
023: public class DefaultToolAgent extends AbstractToolAgent {
024:
025: private final static String TOOL_AGENT_CLASS_EXT_ATTR_NAME = "ToolAgentClass";
026:
027: private String taClass;
028:
029: private String callingAppName;
030:
031: private Integer callingAppMode;
032:
033: public void invokeApplication(WMSessionHandle shandle, long handle,
034: WMEntity appInfo, WMEntity toolInfo,
035: String applicationName, String procInstId, String assId,
036: AppParameter[] parameters, Integer appMode)
037: throws ApplicationNotStarted, ApplicationNotDefined,
038: ApplicationBusy, ToolAgentGeneralException {
039:
040: super
041: .invokeApplication(shandle, handle, appInfo, toolInfo,
042: applicationName, procInstId, assId, parameters,
043: appMode);
044:
045: try {
046: status = APP_STATUS_RUNNING;
047:
048: String extAttribs = (String) parameters[0].the_value;
049: readParamsFromExtAttributes(extAttribs);
050:
051: // System.out.println("Calling "+taClass+" tool agent");
052:
053: Class cls = null;
054:
055: try {
056: cls = Class.forName(taClass);
057: } catch (ClassNotFoundException cnfe1) {
058: cls = ToolAgentLoader.load(cus, taClass);
059: }
060:
061: ToolAgent ta = (ToolAgent) cls.newInstance();
062: ta.configure(cus);
063:
064: // try to connect to the tool agent
065: WMSessionHandle taShandle;
066: try {
067: taShandle = ta.connect(this .wmci);
068: } catch (org.enhydra.shark.api.internal.toolagent.ConnectFailed cf) {
069: cus.error(shandle,
070: "Default Tool Agent - connection to Tool agent "
071: + taClass + " failed !");
072: throw cf;
073: }
074: ta.invokeApplication(shandle, taShandle.getId(), appInfo,
075: toolInfo, callingAppName, procInstId, assId,
076: parameters, callingAppMode);
077: status = ta.requestAppStatus(shandle, taShandle.getId(),
078: toolInfo, procInstId, assId, parameters);
079: // System.out.println("DTA finished execution");
080: ta.disconnect(taShandle);
081:
082: } catch (ClassNotFoundException cnf) {
083: cus.error(shandle, "DefaultToolAgent - application "
084: + appName
085: + " terminated incorrectly, can't find class: "
086: + cnf);
087: throw new ApplicationNotDefined("Can't find class "
088: + taClass, cnf);
089: } catch (NoClassDefFoundError ncdfe) {
090: cus
091: .error(
092: shandle,
093: "DefaultToolAgent - application "
094: + appName
095: + " terminated incorrectly, can't find class definition: "
096: + ncdfe);
097: throw new ApplicationNotDefined("Class " + appName
098: + " can't be executed", ncdfe);
099: } catch (Throwable ex) {
100: // ex.printStackTrace();
101: cus.error(shandle,
102: "DefaultToolAgent - can't execute tool agent class "
103: + taClass + " - application " + appName
104: + " terminated incorrectly: " + ex);
105: status = APP_STATUS_INVALID;
106: if (ex instanceof ToolAgentGeneralException) {
107: throw (ToolAgentGeneralException) ex;
108: } else if (ex instanceof ApplicationNotStarted) {
109: throw (ApplicationNotStarted) ex;
110: } else if (ex instanceof ApplicationNotDefined) {
111: throw (ApplicationNotDefined) ex;
112: } else if (ex instanceof ApplicationBusy) {
113: throw (ApplicationBusy) ex;
114: } else {
115: throw new RootError("Unexpected error", ex);
116: }
117: }
118: }
119:
120: public String getInfo() throws ToolAgentGeneralException {
121: String i = "It is used to execute other tool agents based on XPDL extended attributes"
122: + "\nof appropriate Application definition. It is called by kernel if there are"
123: + "\nno mapping information for some XPDL Application definition";
124: return i;
125: }
126:
127: protected ExtendedAttributes readParamsFromExtAttributes(
128: String extAttribs) throws Exception {
129: String oldAppName = appName;
130: Integer oldAppMode = appMode;
131: ExtendedAttributes eas = super
132: .readParamsFromExtAttributes(extAttribs);
133: callingAppName = appName;
134: callingAppMode = appMode;
135: appName = oldAppName;
136: appMode = oldAppMode;
137: ExtendedAttribute ea = eas
138: .getFirstExtendedAttributeForName(TOOL_AGENT_CLASS_EXT_ATTR_NAME);
139: if (ea != null) {
140: taClass = ea.getVValue();
141: }
142: return eas;
143: }
144:
145: }
|