001: package org.enhydra.shark.toolagent;
002:
003: import java.io.BufferedReader;
004: import java.io.IOException;
005: import java.io.InputStream;
006: import java.io.InputStreamReader;
007: import java.io.Reader;
008: import java.io.StringReader;
009: import java.net.URL;
010:
011: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
012: import org.enhydra.shark.api.client.wfservice.WMEntity;
013: import org.enhydra.shark.api.internal.toolagent.AppParameter;
014: import org.enhydra.shark.api.internal.toolagent.ApplicationBusy;
015: import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined;
016: import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted;
017: import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException;
018: import org.enhydra.shark.xpdl.XPDLConstants;
019: import org.enhydra.shark.xpdl.elements.ExtendedAttribute;
020: import org.enhydra.shark.xpdl.elements.ExtendedAttributes;
021: import org.mozilla.javascript.Context;
022: import org.mozilla.javascript.Scriptable;
023:
024: /**
025: * Tool agent that executes java scripts. Script can be executed as a file that
026: * is stored in tool agent repository, or may be contained within the given
027: * application name.
028: * @author Sasa Bojanic
029: * @version 1.0
030: */
031: public class JavaScriptToolAgent extends AbstractToolAgent {
032:
033: public static final long APP_MODE_FILE = 0;
034: public static final long APP_MODE_TEXT = 1;
035:
036: public static final String SCRIPT_EXT_ATTR_NAME = "Script";
037:
038: private String script;
039:
040: public void invokeApplication(WMSessionHandle shandle, long handle,
041: WMEntity appInfo, WMEntity toolInfo,
042: String applicationName, String procInstId, String assId,
043: AppParameter[] parameters, Integer appMode)
044: throws ApplicationNotStarted, ApplicationNotDefined,
045: ApplicationBusy, ToolAgentGeneralException {
046:
047: super
048: .invokeApplication(shandle, handle, appInfo, toolInfo,
049: applicationName, procInstId, assId, parameters,
050: appMode);
051:
052: try {
053: status = APP_STATUS_RUNNING;
054:
055: if (appName == null || appName.trim().length() == 0) {
056: readParamsFromExtAttributes((String) parameters[0].the_value);
057: }
058: if (script == null) {
059: if (appMode != null
060: && appMode.intValue() == APP_MODE_FILE) {
061: script = readScriptFromFile(appName);
062: } else {
063: script = appName;
064: }
065: }
066:
067: org.mozilla.javascript.Context cx = org.mozilla.javascript.Context
068: .enter();
069: Scriptable scope = prepareContext(cx, parameters);
070: Reader sr = new StringReader(script);
071: //System.out.println("Executing script "+script);
072: cx.evaluateReader(scope, sr, "<script>", 1, null);
073: prepareResult(parameters, scope);
074:
075: status = APP_STATUS_FINISHED;
076: } catch (IOException ioe) {
077: cus
078: .error(
079: shandle,
080: "JavaScriptToolAgent - application "
081: + appName
082: + " terminated incorrectly, can't find script file: "
083: + ioe);
084: throw new ApplicationNotDefined("Can't find script file "
085: + appName, ioe);
086: } catch (Throwable ex) {
087: cus.error(shandle, "JavaScriptToolAgent - application "
088: + appName + " terminated incorrectly: " + ex);
089: //ex.printStackTrace();
090: status = APP_STATUS_INVALID;
091: throw new ToolAgentGeneralException(ex);
092: } finally {
093: Context.exit();
094: }
095:
096: }
097:
098: public String getInfo(WMSessionHandle shandle)
099: throws ToolAgentGeneralException {
100: String i = "Executes scripts written in Java script syntax."
101: + "\nIf you set application mode to 0 (zero), tool agent will search for a script "
102: + "\nfile given as applicationName parameter (this file has to be in the class path),"
103: + "\nand if it founds it, it will try to execute it. Otherwise, it will consider "
104: + "applicationName parameter to be the script itself, and will try to execute it."
105: + "\n"
106: + "\nThis tool agent is able to understand the extended attributes with the following names:"
107: + "\n * AppName - value of this attribute should represent the name of script file to "
108: + "\n execute (this file has to be in class path)"
109: + "\n * Script - the value of this represents the script to be executed. I.e. this"
110: + "\n extended attribute in XPDL can be defined as follows:"
111: + "\n <ExtendedAttribute Name=\"Script\" Value=\"c=a-b;\"/>"
112: + "\n (a, b and c in above text are Formal parameter Ids from XPDL Application definition)"
113: + "\n"
114: + "\n NOTE: Tool agent will read extended attributes only if they are called through"
115: + "\n Default tool agent (not by shark directly) and this is the case when information "
116: + "\n on which tool agent to start for XPDL application definition is not contained in mappings";
117: return i;
118: }
119:
120: private Scriptable prepareContext(
121: org.mozilla.javascript.Context cx, AppParameter[] context) {
122: Scriptable scope = cx.initStandardObjects(null);
123: if (context != null) {
124: // ignore 1. param - it is ext. attribs
125: for (int i = 1; i < context.length; i++) {
126: String key = context[i].the_formal_name;
127: java.lang.Object value = context[i].the_value;
128: //System.out.println("Value for key "+key+" is "+value);
129: //System.out.println("Putting fp "+context[i].the_formal_name+" which class is "+value.getClass().getName());
130: if (key.equals("assId") || key.equals("procInstId")
131: || key.equals("shandle")) {
132: cus.warn(shandle, "Process \"" + this .procInstId
133: + "\", activity \"" + this .assId
134: + "\" variable conflict");
135: }
136: scope.put(key, scope, value);
137: }
138: }
139: scope.put("assId", scope, this .assId);
140: scope.put("procInstId", scope, this .procInstId);
141: scope.put("shandle", scope, this .shandle);
142: return scope;
143: }
144:
145: private void prepareResult(AppParameter[] context, Scriptable scope) {
146: if (context != null) {
147: for (int i = 0; i < context.length; i++) {
148: if (context[i].the_mode
149: .equals(XPDLConstants.FORMAL_PARAMETER_MODE_OUT)
150: || context[i].the_mode
151: .equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) {
152: java.lang.Object val = scope.get(
153: context[i].the_formal_name, scope);
154: //System.out.println("Getting fp "+context[i].the_formal_name+" - the class is "+val.getClass().getName());
155: /*if (context[i].the_value instanceof Boolean) {
156: context[i].the_value=(Boolean)val;
157: } else if (context[i].the_value instanceof String) {
158: context[i].the_value=(String)val;
159: } else if (context[i].the_value instanceof Integer) {
160: context[i].the_value=new Integer((int)Double.parseDouble(val.toString()));
161: } else if (context[i].the_value instanceof Long) {
162: context[i].the_value=new Long((long)Double.parseDouble(val.toString()));
163: } else if (context[i].the_value instanceof Double) {
164: context[i].the_value=(Double)val;
165: } else if (context[i].the_value instanceof Date) {
166: context[i].the_value=java.text.DateFormat.getDateInstance().parse(val.toString());
167: } else {
168: context[i].the_value=val;
169: }*/
170: context[i].the_value = Context.toType(val,
171: context[i].the_class);
172: //System.out.println(context[i].the_formal_name+" converted to a class "+context[i].the_value.getClass().getName());
173:
174: }
175: }
176: }
177: }
178:
179: private static String readScriptFromFile(String filename)
180: throws IOException {
181: String script = null;
182: Reader rdr = null;
183: InputStream in = null;
184: URL url = null;
185: ClassLoader cl = JavaScriptToolAgent.class.getClassLoader();
186: url = cl.getResource(filename);
187: if (url != null) {
188: try {
189: in = url.openStream();
190: } catch (IOException e) {
191: }
192: }
193: if (in != null) {
194: rdr = new InputStreamReader(in);
195: }
196:
197: if (rdr != null) {
198: try {
199: BufferedReader brdr = new BufferedReader(rdr);
200: StringBuffer sb = new StringBuffer();
201: String line;
202: while ((line = brdr.readLine()) != null) {
203: sb.append(line);
204: sb.append('\n');
205: }
206: script = sb.toString();
207: } finally {
208: rdr.close();
209: }
210: }
211: return script;
212: }
213:
214: protected ExtendedAttributes readParamsFromExtAttributes(
215: String extAttribs) throws Exception {
216: ExtendedAttributes eas = super
217: .readParamsFromExtAttributes(extAttribs);
218: if (appName == null || appName.trim().length() == 0) {
219: ExtendedAttribute ea = eas
220: .getFirstExtendedAttributeForName(JavaScriptToolAgent.SCRIPT_EXT_ATTR_NAME);
221: if (ea != null) {
222: script = ea.getVValue();
223: }
224: }
225:
226: return eas;
227: }
228:
229: }
|