001: package org.enhydra.shark.toolagent;
002:
003: import java.io.IOException;
004:
005: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
006: import org.enhydra.shark.api.client.wfservice.WMEntity;
007: import org.enhydra.shark.api.internal.toolagent.AppParameter;
008: import org.enhydra.shark.api.internal.toolagent.ApplicationBusy;
009: import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined;
010: import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted;
011: import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException;
012: import org.enhydra.shark.xpdl.XPDLConstants;
013:
014: /**
015: * Tool agent that can execute some system application.
016: * @author Sasa Bojanic
017: * @version 1.0
018: */
019: public class RuntimeApplicationToolAgent extends AbstractToolAgent {
020:
021: public static final long APP_MODE_SYNCHRONOUS = 0;
022: public static final long APP_MODE_ASYNCHRONOUS = 1;
023:
024: private Process p;
025:
026: public void invokeApplication(WMSessionHandle shandle, long handle,
027: WMEntity appInfo, WMEntity toolInfo,
028: String applicationName, String procInstId, String assId,
029: AppParameter[] parameters, Integer appMode)
030: throws ApplicationNotStarted, ApplicationNotDefined,
031: ApplicationBusy, ToolAgentGeneralException {
032:
033: super
034: .invokeApplication(shandle, handle, appInfo, toolInfo,
035: applicationName, procInstId, assId, parameters,
036: appMode);
037:
038: try {
039: status = APP_STATUS_RUNNING;
040:
041: StringBuffer buffer = new StringBuffer();
042:
043: if (appName == null || appName.trim().length() == 0) {
044: readParamsFromExtAttributes((String) parameters[0].the_value);
045: }
046:
047: buffer.append(appName);
048:
049: // ignore 1. param, this is ext. attribs.
050: buffer.append(" ");
051: if (parameters != null) {
052: for (int i = 1; i < parameters.length; i++) {
053: if (parameters[i].the_mode
054: .equals(XPDLConstants.FORMAL_PARAMETER_MODE_IN)
055: || parameters[i].the_mode
056: .equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) {
057: try {
058: buffer
059: .append((String) parameters[i].the_value);
060: buffer.append(" ");
061: } catch (Throwable ex) {
062: }
063: }
064: }
065: }
066:
067: Runtime rt = Runtime.getRuntime();
068: p = rt.exec(buffer.toString().substring(0,
069: buffer.length() - 1));
070: if (appMode != null
071: && appMode.intValue() == APP_MODE_SYNCHRONOUS) {
072: p.waitFor();
073: }
074:
075: status = APP_STATUS_FINISHED;
076:
077: } catch (IOException ioe) {
078: cus
079: .error(
080: shandle,
081: "RuntimeApplicationToolAgent - application "
082: + appName
083: + " terminated incorrectly, can't find executable: "
084: + ioe);
085: throw new ApplicationNotDefined("Can't find executable "
086: + appName, ioe);
087: } catch (Throwable ex) {
088: cus
089: .error(shandle,
090: "RuntimeApplicationToolAgent - application "
091: + appName
092: + " terminated incorrectly " + ex);
093: status = APP_STATUS_INVALID;
094: throw new ToolAgentGeneralException(ex);
095: }
096:
097: }
098:
099: /*public void terminate () {
100: try {
101: p.destroy();
102: if (appMode.equals(APP_MODE_SYNCHRONOUS)) {
103: status=APP_STATUS_TERMINATED;
104: }
105: } catch (Throwable ex) {}
106: }*/
107:
108: public String getInfo(WMSessionHandle shandle)
109: throws ToolAgentGeneralException {
110: String i = "Executes some system applications like notepad or any other executable application."
111: + "\nIt is important that this application should be in the system path of machine where shark is running."
112: + "\nf you use application mode 0 (zero), the tool agent will wait until the executable application "
113: + "\nis completed, and if you choose application status other then 0 the tool agent will finish its work as "
114: + "\nsoon as the executable application is started (this usually happens immediately), and shark "
115: + "\nwill proceed to the next activity, even if the executable application is still running "
116: + "\n(this is asynchronous starting of some external applications)"
117: + "\nThis tool agent accepts parameters (AppParameter class instances), but does not modify any."
118: + "\nThe parameters sent to this tool agents, for which the corresponding application definition "
119: + "\nformal parameters are of \"IN\" type, and whose data type is string, are added as suffixes to "
120: + "\nthe application name, and resulting application that is started could be something like "
121: + "\n \"notepad c:\\Shark\\readme\""
122: + "\n"
123: + "\nThis tool agent is able to understand the extended attributes with the following names:"
124: + "\n * AppName - value of this attribute should represent the executable application name to "
125: + "\n be executed by tool agent (must be in a system path)"
126: + "\n * AppMode - value of this attribute should represent the mode of execution, if set to "
127: + "\n 0 (zero), tool agent will wait until the executable application is finished."
128: + "\n"
129: + "\n NOTE: Tool agent will read extended attributes only if they are called through"
130: + "\n Default tool agent (not by shark directly) and this is the case when information "
131: + "\n on which tool agent to start for XPDL application definition is not contained in mappings";
132: return i;
133: }
134:
135: }
|