Source Code Cross Referenced for JavaScriptToolAgent.java in  » Workflow-Engines » shark » org » enhydra » shark » toolagent » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Workflow Engines » shark » org.enhydra.shark.toolagent 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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