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