1 package org.enhydra.shark.toolagent; 2 3 import java.io.BufferedReader ; 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.io.InputStreamReader ; 7 import java.io.Reader ; 8 import java.io.StringReader ; 9 import java.net.URL ; 10 11 import org.enhydra.shark.api.SharkTransaction; 12 import org.enhydra.shark.api.internal.toolagent.AppParameter; 13 import org.enhydra.shark.api.internal.toolagent.ApplicationBusy; 14 import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined; 15 import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted; 16 import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException; 17 import org.enhydra.shark.xpdl.XPDLConstants; 18 import org.enhydra.shark.xpdl.elements.ExtendedAttribute; 19 import org.enhydra.shark.xpdl.elements.ExtendedAttributes; 20 import org.mozilla.javascript.Context; 21 import org.mozilla.javascript.Scriptable; 22 23 30 public class JavaScriptToolAgent extends AbstractToolAgent { 31 32 public static final long APP_MODE_FILE=0; 33 public static final long APP_MODE_TEXT=1; 34 35 public static final String SCRIPT_EXT_ATTR_NAME="Script"; 36 37 private String script; 38 39 public void invokeApplication (SharkTransaction t, 40 long handle, 41 String applicationName, 42 String procInstId, 43 String assId, 44 AppParameter[] parameters, 45 Integer appMode) 46 throws ApplicationNotStarted, ApplicationNotDefined, 47 ApplicationBusy, ToolAgentGeneralException { 48 49 super.invokeApplication(t,handle,applicationName,procInstId,assId,parameters,appMode); 50 51 try { 52 status=APP_STATUS_RUNNING; 53 54 if (appName==null || appName.trim().length()==0) { 55 readParamsFromExtAttributes((String )parameters[0].the_value); 56 } 57 if (script==null) { 58 if (appMode!=null && appMode.intValue()==APP_MODE_FILE) { 59 script=readScriptFromFile(appName); 60 } else { 61 script=appName; 62 } 63 } 64 65 org.mozilla.javascript.Context cx = org.mozilla.javascript.Context.enter(); 66 Scriptable scope = prepareContext (t, cx,parameters); 67 Reader sr = new StringReader (script); 68 cx.evaluateReader (scope, sr, "<script>", 1, null); 70 prepareResult(parameters,scope); 71 72 status=APP_STATUS_FINISHED; 73 } catch (IOException ioe) { 74 cus.error("JavaScriptToolAgent - application "+appName+" terminated incorrectly, can't find script file: "+ioe); 75 throw new ApplicationNotDefined("Can't find script file "+appName,ioe); 76 } catch (Throwable ex) { 77 cus.error("JavaScriptToolAgent - application "+appName+" terminated incorrectly: "+ex); 78 status=APP_STATUS_INVALID; 80 throw new ToolAgentGeneralException(ex); 81 } finally { 82 Context.exit(); 83 } 84 85 } 86 87 public String getInfo (SharkTransaction t) throws ToolAgentGeneralException { 88 String i="Executes scripts written in Java script syntax." + 89 "\nIf you set application mode to 0 (zero), tool agent will search for a script "+ 90 "\nfile given as applicationName parameter (this file has to be in the class path),"+ 91 "\nand if it founds it, it will try to execute it. Otherwise, it will consider "+ 92 "applicationName parameter to be the script itself, and will try to execute it."+ 93 "\n"+ 94 "\nThis tool agent is able to understand the extended attributes with the following names:"+ 95 "\n * AppName - value of this attribute should represent the name of script file to "+ 96 "\n execute (this file has to be in class path)"+ 97 "\n * Script - the value of this represents the script to be executed. I.e. this"+ 98 "\n extended attribute in XPDL can be defined as follows:"+ 99 "\n <ExtendedAttribute Name=\"Script\" Value=\"c=a-b;\"/>"+ 100 "\n (a, b and c in above text are Formal parameter Ids from XPDL Application definition)"+ 101 "\n"+ 102 "\n NOTE: Tool agent will read extended attributes only if they are called through"+ 103 "\n Default tool agent (not by shark directly) and this is the case when information "+ 104 "\n on which tool agent to start for XPDL application definition is not contained in mappings"; 105 return i; 106 } 107 108 private Scriptable prepareContext (SharkTransaction t, org.mozilla.javascript.Context cx,AppParameter[] context) { 109 Scriptable scope = cx.initStandardObjects(null); 110 if (context!=null) { 111 for (int i=1; i<context.length; i++) { 113 String key = context[i].the_formal_name; 114 java.lang.Object value = context[i].the_value; 115 if (key.equals("assId")||key.equals("procInstId")||key.equals("sharkTransaction")) { 118 cus.warn("Process \""+this.procInstId+"\", activity \""+this.assId+"\" variable conflict"); 119 } 120 scope.put(key,scope,value); 121 } 122 } 123 scope.put("assId", scope ,this.assId); 124 scope.put("procInstId", scope, this.procInstId); 125 scope.put("sharkTransaction", scope, t); 126 return scope; 127 } 128 129 private void prepareResult (AppParameter[] context,Scriptable scope) { 130 if (context!=null) { 131 for (int i = 0; i < context.length; i++) { 132 if(context[i].the_mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_OUT) || context[i].the_mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) { 133 java.lang.Object val = scope.get(context[i].the_formal_name,scope); 134 150 context[i].the_value=Context.toType(val,context[i].the_class); 151 153 } 154 } 155 } 156 } 157 158 private static String readScriptFromFile (String filename) throws IOException { 159 String script=null; 160 Reader rdr=null; 161 InputStream in = null; 162 URL url = null; 163 ClassLoader cl = JavaScriptToolAgent.class.getClassLoader(); 164 url = cl.getResource(filename); 165 if (url != null) { 166 try { 167 in = url.openStream(); 168 } catch (IOException e) { 169 } 170 } 171 if (in != null) { 172 rdr=new InputStreamReader (in); 173 } 174 175 if (rdr != null) { 176 try { 177 BufferedReader brdr = new BufferedReader (rdr); 178 StringBuffer sb = new StringBuffer (); 179 String line; 180 while ((line = brdr.readLine()) != null) { 181 sb.append(line); 182 sb.append('\n'); 183 } 184 script = sb.toString(); 185 } finally { 186 rdr.close(); 187 } 188 } 189 return script; 190 } 191 192 protected ExtendedAttributes readParamsFromExtAttributes (String extAttribs) throws Exception { 193 ExtendedAttributes eas=super.readParamsFromExtAttributes(extAttribs); 194 if (appName==null || appName.trim().length()==0) { 195 ExtendedAttribute ea=eas.getFirstExtendedAttributeForName(JavaScriptToolAgent.SCRIPT_EXT_ATTR_NAME); 196 if (ea!=null) { 197 script=ea.getVValue(); 198 } 199 } 200 201 return eas; 202 } 203 204 } 205 | Popular Tags |