KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark.toolagent;
2
3
4 import java.io.IOException JavaDoc;
5
6 import org.enhydra.shark.api.SharkTransaction;
7 import org.enhydra.shark.api.internal.toolagent.AppParameter;
8 import org.enhydra.shark.api.internal.toolagent.ApplicationBusy;
9 import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined;
10 import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted;
11 import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException;
12 import org.enhydra.shark.xpdl.XPDLConstants;
13
14 /**
15  * Tool agent that can execute some system application.
16  * @author Sasa Bojanic
17  * @version 1.0
18  */

19 public class RuntimeApplicationToolAgent extends AbstractToolAgent {
20
21    public static final long APP_MODE_SYNCHRONOUS=0;
22    public static final long APP_MODE_ASYNCHRONOUS=1;
23
24
25    private Process JavaDoc p;
26
27    public void invokeApplication (SharkTransaction t,
28                                   long handle,
29                                   String JavaDoc applicationName,
30                                   String JavaDoc procInstId,
31                                   String JavaDoc assId,
32                                   AppParameter[] parameters,
33                                   Integer JavaDoc appMode)
34       throws ApplicationNotStarted, ApplicationNotDefined,
35       ApplicationBusy, ToolAgentGeneralException {
36
37       super.invokeApplication(t,handle,applicationName,procInstId,assId,parameters,appMode);
38
39       try {
40          status=APP_STATUS_RUNNING;
41
42          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
43
44          if (appName==null || appName.trim().length()==0) {
45             readParamsFromExtAttributes((String JavaDoc)parameters[0].the_value);
46          }
47
48          buffer.append(appName);
49
50          // ignore 1. param, this is ext. attribs.
51
buffer.append(" ");
52          if (parameters!=null) {
53             for (int i=1; i<parameters.length; i++) {
54                if(parameters[i].the_mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_IN) ||
55                   parameters[i].the_mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) {
56                   try {
57                      buffer.append((String JavaDoc)parameters[i].the_value);
58                      buffer.append(" ");
59                   } catch (Throwable JavaDoc ex) {}
60                }
61             }
62          }
63
64          Runtime JavaDoc rt = Runtime.getRuntime();
65          p = rt.exec(buffer.toString().substring(0,buffer.length()-1));
66          if (appMode!=null && appMode.intValue()==APP_MODE_SYNCHRONOUS) {
67             p.waitFor();
68          }
69
70          status=APP_STATUS_FINISHED;
71
72       } catch (IOException JavaDoc ioe) {
73          cus.error("RuntimeApplicationToolAgent - application "+appName+" terminated incorrectly, can't find executable: "+ioe);
74          throw new ApplicationNotDefined("Can't find executable "+appName,ioe);
75       } catch (Throwable JavaDoc ex) {
76          cus.error("RuntimeApplicationToolAgent - application "+appName+" terminated incorrectly "+ex);
77          status=APP_STATUS_INVALID;
78          throw new ToolAgentGeneralException(ex);
79       }
80
81    }
82
83    /*public void terminate () {
84     try {
85     p.destroy();
86     if (appMode.equals(APP_MODE_SYNCHRONOUS)) {
87     status=APP_STATUS_TERMINATED;
88     }
89     } catch (Throwable ex) {}
90     }*/

91
92    public String JavaDoc getInfo (SharkTransaction t) throws ToolAgentGeneralException {
93       String JavaDoc i="Executes some system applications like notepad or any other executable application."+
94          "\nIt is important that this application should be in the system path of machine where shark is running." +
95          "\nf you use application mode 0 (zero), the tool agent will wait until the executable application "+
96          "\nis completed, and if you choose application status other then 0 the tool agent will finish its work as "+
97          "\nsoon as the executable application is started (this usually happens immediately), and shark "+
98          "\nwill proceed to the next activity, even if the executable application is still running "+
99          "\n(this is asynchronous starting of some external applications)"+
100          "\nThis tool agent accepts parameters (AppParameter class instances), but does not modify any."+
101          "\nThe parameters sent to this tool agents, for which the corresponding application definition "+
102          "\nformal parameters are of \"IN\" type, and whose data type is string, are added as suffixes to "+
103          "\nthe application name, and resulting application that is started could be something like "+
104          "\n \"notepad c:\\Shark\\readme\""+
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 executable application name to "+
108          "\n be executed by tool agent (must be in a system path)"+
109          "\n * AppMode - value of this attribute should represent the mode of execution, if set to "+
110          "\n 0 (zero), tool agent will wait until the executable application is finished."+
111          "\n"+
112          "\n NOTE: Tool agent will read extended attributes only if they are called through"+
113          "\n Default tool agent (not by shark directly) and this is the case when information "+
114          "\n on which tool agent to start for XPDL application definition is not contained in mappings";
115       return i;
116    }
117
118
119 }
120
Popular Tags