KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > toolagent > JavaScriptToolAgent


1 package org.enhydra.shark.toolagent;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.InputStreamReader JavaDoc;
7 import java.io.Reader JavaDoc;
8 import java.io.StringReader JavaDoc;
9 import java.net.URL JavaDoc;
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 /**
24  * Tool agent that executes java scripts. Script can be executed as a file that
25  * is stored in tool agent repository, or may be contained within the given
26  * application name.
27  * @author Sasa Bojanic
28  * @version 1.0
29  */

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 JavaDoc SCRIPT_EXT_ATTR_NAME="Script";
36
37    private String JavaDoc script;
38
39    public void invokeApplication (SharkTransaction t,
40                                   long handle,
41                                   String JavaDoc applicationName,
42                                   String JavaDoc procInstId,
43                                   String JavaDoc assId,
44                                   AppParameter[] parameters,
45                                   Integer JavaDoc 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 JavaDoc)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 JavaDoc sr = new StringReader JavaDoc (script);
68          //System.out.println("Executing script "+script);
69
cx.evaluateReader (scope, sr, "<script>", 1, null);
70          prepareResult(parameters,scope);
71
72          status=APP_STATUS_FINISHED;
73       } catch (IOException JavaDoc 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 JavaDoc ex) {
77          cus.error("JavaScriptToolAgent - application "+appName+" terminated incorrectly: "+ex);
78          //ex.printStackTrace();
79
status=APP_STATUS_INVALID;
80          throw new ToolAgentGeneralException(ex);
81       } finally {
82          Context.exit();
83       }
84
85    }
86
87    public String JavaDoc getInfo (SharkTransaction t) throws ToolAgentGeneralException {
88       String JavaDoc 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          // ignore 1. param - it is ext. attribs
112
for (int i=1; i<context.length; i++) {
113             String JavaDoc key = context[i].the_formal_name;
114             java.lang.Object JavaDoc value = context[i].the_value;
115             //System.out.println("Value for key "+key+" is "+value);
116
//System.out.println("Putting fp "+context[i].the_formal_name+" which class is "+value.getClass().getName());
117
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 JavaDoc val = scope.get(context[i].the_formal_name,scope);
134                //System.out.println("Getting fp "+context[i].the_formal_name+" - the class is "+val.getClass().getName());
135
/*if (context[i].the_value instanceof Boolean) {
136                 context[i].the_value=(Boolean)val;
137                 } else if (context[i].the_value instanceof String) {
138                 context[i].the_value=(String)val;
139                 } else if (context[i].the_value instanceof Integer) {
140                 context[i].the_value=new Integer((int)Double.parseDouble(val.toString()));
141                 } else if (context[i].the_value instanceof Long) {
142                 context[i].the_value=new Long((long)Double.parseDouble(val.toString()));
143                 } else if (context[i].the_value instanceof Double) {
144                 context[i].the_value=(Double)val;
145                 } else if (context[i].the_value instanceof Date) {
146                 context[i].the_value=java.text.DateFormat.getDateInstance().parse(val.toString());
147                 } else {
148                 context[i].the_value=val;
149                 }*/

150                context[i].the_value=Context.toType(val,context[i].the_class);
151                //System.out.println(context[i].the_formal_name+" converted to a class "+context[i].the_value.getClass().getName());
152

153             }
154          }
155       }
156    }
157
158    private static String JavaDoc readScriptFromFile (String JavaDoc filename) throws IOException JavaDoc {
159       String JavaDoc script=null;
160       Reader JavaDoc rdr=null;
161       InputStream JavaDoc in = null;
162       URL JavaDoc url = null;
163       ClassLoader JavaDoc cl = JavaScriptToolAgent.class.getClassLoader();
164       url = cl.getResource(filename);
165       if (url != null) {
166          try {
167             in = url.openStream();
168          } catch (IOException JavaDoc e) {
169          }
170       }
171       if (in != null) {
172          rdr=new InputStreamReader JavaDoc(in);
173       }
174
175       if (rdr != null) {
176          try {
177             BufferedReader JavaDoc brdr = new BufferedReader JavaDoc(rdr);
178             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
179             String JavaDoc 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 JavaDoc extAttribs) throws Exception JavaDoc {
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