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 21 import bsh.Interpreter; 22 23 30 public class BshToolAgent 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 Interpreter interpreter=new Interpreter(); 66 prepareContext (t, interpreter,parameters); 67 Reader sr = new StringReader (script); 68 interpreter.eval(sr); 70 prepareResult(parameters,interpreter); 71 72 status=APP_STATUS_FINISHED; 73 } catch (IOException ioe) { 74 cus.error("BshToolAgent - 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 status=APP_STATUS_INVALID; 78 cus.error("BshToolAgent - application "+appName+" terminated incorrectly: "+ex); 80 throw new ToolAgentGeneralException(ex); 81 } 82 83 } 84 85 public String getInfo (SharkTransaction t) throws ToolAgentGeneralException { 86 String i="Executes scripts written in Java language syntax." + 87 "\nIf you set application mode to 0 (zero), tool agent will search for a script "+ 88 "\nfile given as applicationName parameter (this file has to be in the class path),"+ 89 "\nand if it founds it, it will try to execute it. Otherwise, it will consider "+ 90 "\napplicationName parameter to be the script itself, and will try to execute it."+ 91 "\n"+ 92 "\nThis tool agent is able to understand the extended attributes with the following names:"+ 93 "\n * AppName - value of this attribute should represent the name of script file to "+ 94 "\n execute (this file has to be in class path)"+ 95 "\n * Script - the value of this represents the script to be executed. I.e. this"+ 96 "\n extended attribute in XPDL can be defined as follows:"+ 97 "\n <ExtendedAttribute Name=\"Script\" Value=\"c=new java.lang.Long(a.longValue()-b.longValue());\"/>"+ 98 "\n (a, b and c in above text are Formal parameter Ids from XPDL Application definition)"+ 99 "\n"+ 100 "\n NOTE: Tool agent will read extended attributes only if they are called through"+ 101 "\n Default tool agent (not by shark directly) and this is the case when information "+ 102 "\n on which tool agent to start for XPDL application definition is not contained in mappings"; 103 return i; 104 } 105 106 private void prepareContext (SharkTransaction t, Interpreter interpreter,AppParameter[] context) throws Throwable { 107 if (context!=null) { 108 for (int i=1; i<context.length; i++) { 110 String key = context[i].the_formal_name; 111 java.lang.Object value = context[i].the_value; 112 if (key.equals("assId")||key.equals("procInstId")||key.equals("sharkTransaction")) { 115 cus.warn("Process \""+this.procInstId+"\", activity \""+this.assId+"\" variable conflict"); 116 } 117 interpreter.set(key,value); 118 } 119 interpreter.set("assId",this.assId); 120 interpreter.set("procInstId",this.procInstId); 121 interpreter.set("sharkTransaction", t); 122 } 123 } 124 125 private void prepareResult (AppParameter[] context,Interpreter interpreter) throws Throwable { 126 if (context!=null) { 127 for (int i = 0; i < context.length; i++) { 128 if(context[i].the_mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_OUT) || context[i].the_mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) { 129 146 context[i].the_value=interpreter.get(context[i].the_formal_name); 147 if (context[i].the_value instanceof Integer ) { 148 context[i].the_value =new Long (((Integer )context[i].the_value).intValue()); 149 } 150 152 } 153 } 154 } 155 } 156 157 private static String readScriptFromFile (String filename) throws IOException { 158 String script=null; 159 Reader rdr=null; 160 InputStream in = null; 161 URL url = null; 162 ClassLoader cl = BshToolAgent.class.getClassLoader(); 163 url = cl.getResource(filename); 164 if (url != null) { 165 try { 166 in = url.openStream(); 167 } catch (IOException e) { 168 } 169 } 170 if (in != null) { 171 rdr=new InputStreamReader (in); 172 } 173 174 if (rdr != null) { 175 try { 176 BufferedReader brdr = new BufferedReader (rdr); 177 StringBuffer sb = new StringBuffer (); 178 String line; 179 while ((line = brdr.readLine()) != null) { 180 sb.append(line); 181 sb.append('\n'); 182 } 183 script = sb.toString(); 184 } finally { 185 rdr.close(); 186 } 187 } 188 return script; 189 } 190 191 protected ExtendedAttributes readParamsFromExtAttributes (String extAttribs) throws Exception { 192 ExtendedAttributes eas=super.readParamsFromExtAttributes(extAttribs); 193 if (appName==null || appName.trim().length()==0) { 194 ExtendedAttribute ea=eas.getFirstExtendedAttributeForName(BshToolAgent.SCRIPT_EXT_ATTR_NAME); 195 if (ea!=null) { 196 script=ea.getVValue(); 197 } 198 } 199 return eas; 200 } 201 202 } 203 | Popular Tags |