KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 import bsh.Interpreter;
22
23 /**
24  * Tool agent that executes bean shell 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 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 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          Interpreter interpreter=new Interpreter();
66          prepareContext (t, interpreter,parameters);
67          Reader JavaDoc sr = new StringReader JavaDoc (script);
68          //System.out.println("Executing script "+script);
69
interpreter.eval(sr);
70          prepareResult(parameters,interpreter);
71
72          status=APP_STATUS_FINISHED;
73       } catch (IOException JavaDoc 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 JavaDoc ex) {
77          status=APP_STATUS_INVALID;
78          //ex.printStackTrace();
79
cus.error("BshToolAgent - application "+appName+" terminated incorrectly: "+ex);
80          throw new ToolAgentGeneralException(ex);
81       }
82
83    }
84
85    public String JavaDoc getInfo (SharkTransaction t) throws ToolAgentGeneralException {
86       String JavaDoc 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 JavaDoc {
107       if (context!=null) {
108          // ignore 1. param - it is ext. attribs
109
for (int i=1; i<context.length; i++) {
110             String JavaDoc key = context[i].the_formal_name;
111             java.lang.Object JavaDoc value = context[i].the_value;
112             //System.out.println("Value for key "+key+" is "+value);
113
//System.out.println("Putting fp "+context[i].the_formal_name+" which class is "+value.getClass().getName());
114
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 JavaDoc {
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                //java.lang.Object val = interpreter.get(context[i].the_formal_name);
130
//System.out.println("Getting fp "+context[i].the_formal_name+" - the class is "+val.getClass().getName());
131
/*if (context[i].the_value instanceof Boolean) {
132                 context[i].the_value=(Boolean)val;
133                 } else if (context[i].the_value instanceof String) {
134                 context[i].the_value=(String)val;
135                 } else if (context[i].the_value instanceof Integer) {
136                 context[i].the_value=new Integer((int)Double.parseDouble(val.toString()));
137                 } else if (context[i].the_value instanceof Long) {
138                 context[i].the_value=new Long((long)Double.parseDouble(val.toString()));
139                 } else if (context[i].the_value instanceof Double) {
140                 context[i].the_value=(Double)val;
141                 } else if (context[i].the_value instanceof Date) {
142                 context[i].the_value=java.text.DateFormat.getDateInstance().parse(val.toString());
143                 } else {
144                 context[i].the_value=val;
145                 }*/

146                context[i].the_value=interpreter.get(context[i].the_formal_name);
147                if (context[i].the_value instanceof Integer JavaDoc) {
148                   context[i].the_value =new Long JavaDoc(((Integer JavaDoc)context[i].the_value).intValue());
149                }
150             //System.out.println(context[i].the_formal_name+" converted to a class "+context[i].the_value.getClass().getName());
151

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