KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > test > CORBAProcStartClient


1 package org.enhydra.shark.test;
2
3 import org.omg.WfBase.NameValue;
4 import org.omg.WfBase.NameValueInfo;
5 import org.omg.WorkflowModel.WfProcess;
6 import org.omg.WorkflowModel.WfProcessMgr;
7 import org.enhydra.shark.corba.WorkflowService.ConnectFailed;
8 import org.enhydra.shark.corba.WorkflowService.ExecutionAdministration;
9 import org.enhydra.shark.corba.WorkflowService.SharkConnection;
10 import org.enhydra.shark.corba.WorkflowService.SharkInterface;
11 import org.enhydra.shark.corba.WorkflowService.SharkInterfaceHelper;
12 import java.io.ByteArrayOutputStream JavaDoc;
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.io.PrintStream JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Properties JavaDoc;
21 import org.omg.CORBA.Any JavaDoc;
22 import org.omg.CORBA.ORB JavaDoc;
23 import org.omg.CosNaming.NameComponent JavaDoc;
24 import org.omg.CosNaming.NamingContext JavaDoc;
25 import org.omg.CosNaming.NamingContextHelper JavaDoc;
26
27 public class CORBAProcStartClient {
28
29    static final String JavaDoc HOST_PROPERTY_NAME="host";
30    static final String JavaDoc PORT_PROPERTY_NAME="port";
31    static final String JavaDoc ENGINENAME_PROPERTY_NAME="enginename";
32    static final String JavaDoc USERNAME_PROPERTY_NAME="username";
33    static final String JavaDoc PASSWORD_PROPERTY_NAME="password";
34
35    static String JavaDoc lastStatusString="";
36
37    private String JavaDoc confFilePath;
38    private ORB JavaDoc orb;
39    private SharkInterface shark;
40    private SharkConnection sc;
41    private ExecutionAdministration ea;
42    private String JavaDoc statusString;
43
44    private Properties JavaDoc props=new Properties JavaDoc();
45
46    private String JavaDoc host;
47    private String JavaDoc port;
48    private String JavaDoc enginename;
49    private String JavaDoc username;
50    private String JavaDoc password;
51
52    protected CORBAProcStartClient (String JavaDoc confFilePath) {
53       this.confFilePath=confFilePath;
54    }
55
56    protected boolean init () {
57       statusString="";
58       try {
59          CORBAProcStartClient.configure(props,confFilePath);
60
61          host=props.getProperty(CORBAProcStartClient.HOST_PROPERTY_NAME);
62          port=props.getProperty(CORBAProcStartClient.PORT_PROPERTY_NAME);
63          enginename=props.getProperty(CORBAProcStartClient.ENGINENAME_PROPERTY_NAME);
64          username=props.getProperty(CORBAProcStartClient.USERNAME_PROPERTY_NAME);
65          password=props.getProperty(CORBAProcStartClient.PASSWORD_PROPERTY_NAME);
66
67          String JavaDoc host=props.getProperty(CORBAProcStartClient.HOST_PROPERTY_NAME);
68          String JavaDoc port=props.getProperty(CORBAProcStartClient.PORT_PROPERTY_NAME);
69          String JavaDoc enginename=props.getProperty(CORBAProcStartClient.ENGINENAME_PROPERTY_NAME);
70          String JavaDoc username=props.getProperty(CORBAProcStartClient.USERNAME_PROPERTY_NAME);
71          String JavaDoc password=props.getProperty(CORBAProcStartClient.PASSWORD_PROPERTY_NAME);
72
73          if (host==null) {
74             throw new Exception JavaDoc("host - property is not specified in configuration file");
75          }
76          if (port==null) {
77             throw new Exception JavaDoc("port - property is not specified in configuration file");
78          }
79          if (enginename==null) {
80             throw new Exception JavaDoc("enginename - property is not specified in configuration file");
81          }
82          if (username==null) {
83             throw new Exception JavaDoc("username - property is not specified in configuration file");
84          }
85          if (password==null) {
86             throw new Exception JavaDoc("password - property is not specified in configuration file");
87          }
88
89          String JavaDoc[] args={"-ORBInitialHost",host,"-ORBInitialPort",port};
90          // Create and initialize the ORB
91
orb = ORB.init(args,null);
92          // Get the root naming context
93
org.omg.CORBA.Object JavaDoc objRef = orb.resolve_initial_references("NameService");
94          NamingContext JavaDoc ncRef = NamingContextHelper.narrow(objRef);
95
96          // Resolve the object reference in naming
97
NameComponent JavaDoc nc = new NameComponent JavaDoc(enginename,"");
98          NameComponent JavaDoc path[] = {nc};
99          shark=SharkInterfaceHelper.narrow(ncRef.resolve(path));
100
101          sc=shark.getSharkConnection();
102          ea=shark.getExecutionAdministration();
103          sc.connect(username,password,"","");
104          ea.connect(username,password,"","");
105          return true;
106       } catch (Throwable JavaDoc thr) {
107          if (host==null || port==null || enginename==null || username==null || password==null) {
108             statusString=thr.getMessage();
109          } else if (shark==null) {
110             statusString="Connection to shark server is not established - maybe the server is down, or the connection parameters are wrong!";
111          } else if (thr instanceof ConnectFailed) {
112             statusString="Connection to shark server is not established - wrong username or password!";
113          } else {
114             statusString=thr.getMessage();
115          }
116          return false;
117       }
118    }
119
120    protected boolean startProcess (String JavaDoc pkgId,String JavaDoc pDefId,Map JavaDoc vars) {
121       statusString="";
122       WfProcessMgr mgr=null;
123       boolean mgrOK=false;
124
125       try {
126          mgr=ea.getProcessMgrByXPDLDefinition(pkgId,pDefId);
127
128          WfProcess proc=sc.createProcess(pkgId,pDefId);
129
130          NameValueInfo[] cs=mgr.context_signature();
131          mgrOK=true;
132          // converting process context to CORBA values
133
if (vars!=null && vars.size()>0) {
134             NameValue[] cntxt=new NameValue[vars.size()];
135             Iterator JavaDoc it=vars.entrySet().iterator();
136             int i=0;
137             while(it.hasNext()) {
138                Map.Entry JavaDoc me=(Map.Entry JavaDoc)it.next();
139                String JavaDoc id=(String JavaDoc)me.getKey();
140                java.lang.Object JavaDoc val=me.getValue();
141                Any JavaDoc any=orb.create_any();
142                String JavaDoc clsName=getClassName(cs,id);
143                if (clsName==null) {
144                   throw new Exception JavaDoc ("The process definition knows nothing about variable "+id+" - you can't put it into process context");
145                }
146                if (!clsName.equals(val.getClass().getName())) {
147                   throw new Exception JavaDoc ("You are trying to set wrong type "+val.getClass().getName()+" for variable "+id+" which type is "+clsName);
148                }
149
150                if (val instanceof Long JavaDoc) {
151                   any.insert_longlong(((Long JavaDoc)val).longValue());
152                } else if (val instanceof Double JavaDoc) {
153                   any.insert_double(((Double JavaDoc)val).doubleValue());
154                } else if (val instanceof Boolean JavaDoc) {
155                   any.insert_boolean(((Boolean JavaDoc)val).booleanValue());
156                } else if (val instanceof String JavaDoc) {
157                   any.insert_wstring((String JavaDoc)val);
158                } else {
159                   throw new Exception JavaDoc ("Unsupported type "+val.getClass().getName()+" for variable "+id);
160                }
161                cntxt[i]=new NameValue();
162                cntxt[i].the_name=id;
163                cntxt[i].the_value=any;
164                i++;
165             }
166             proc.set_process_context(cntxt);
167          }
168          proc.start();
169          return true;
170       } catch (Throwable JavaDoc thr) {
171          if (!mgrOK) {
172             lastStatusString="Improper package or process definition Id!";
173          } else {
174             statusString=thr.getMessage();
175             if (statusString==null || statusString.trim().equals("")) {
176                //statusString=thr.printStackTrace(
177
OutputStream JavaDoc os=new ByteArrayOutputStream JavaDoc();
178                PrintStream JavaDoc ps=new PrintStream JavaDoc(os);
179                thr.printStackTrace(ps);
180                ps.close();
181                statusString=os.toString();
182             }
183          }
184       }
185       return false;
186    }
187
188    public String JavaDoc getStatusString () {
189       return statusString;
190    }
191
192    public static boolean run (String JavaDoc[] args) {
193       if (args==null || args.length<3) {
194          lastStatusString="Invalid number of parameters - must be at least 3 parameters!";
195          return false;
196       }
197
198       String JavaDoc pkgId=args[1];
199       String JavaDoc pDefId=args[2];
200
201       WfProcessMgr mgr=null;
202       boolean mgrOK=false;
203       try {
204          CORBAProcStartClient cpsc=new CORBAProcStartClient(args[0]);
205          boolean isOK=cpsc.init();
206          if (!isOK) {
207             lastStatusString=cpsc.getStatusString();
208             return false;
209          }
210          mgr=cpsc.ea.getProcessMgrByXPDLDefinition(pkgId,pDefId);
211
212          NameValueInfo[] cs=mgr.context_signature();
213          mgrOK=true;
214          // getting initial process context
215
Map JavaDoc cntx=new HashMap JavaDoc();
216          if (args.length>3) {
217             for (int i=3; i<args.length; i++) {
218                String JavaDoc cnt=args[i];
219                int ind=cnt.indexOf("=");
220                String JavaDoc id=cnt.substring(0,ind);
221                String JavaDoc val=cnt.substring(ind+1,cnt.length());
222                if (getClassName(cs,id).equals("java.lang.Boolean")) {
223                   if (!val.equalsIgnoreCase("true") && !val.equalsIgnoreCase("false")) {
224                      throw new Exception JavaDoc("The value for Boolean variable "+id+" is not set properly - it must be true or false");
225                   } else {
226                      cntx.put(id,new Boolean JavaDoc(val));
227                   }
228                } else if (getClassName(cs,id).equals("java.lang.Long")) {
229                   cntx.put(id,new Long JavaDoc(val));
230                } else if (getClassName(cs,id).equals("java.lang.Double")) {
231                   cntx.put(id,new Double JavaDoc(val));
232                } else if (getClassName(cs,id).equals("java.lang.String")) {
233                   cntx.put(id,val);
234                } else {
235                   throw new Exception JavaDoc ("Can't init variable which type is "+getClassName(cs,id).equals("java.lang.String"));
236                }
237             }
238          }
239          isOK=cpsc.startProcess(pkgId,pDefId,cntx);
240          if (!isOK) {
241             lastStatusString=cpsc.getStatusString();
242             return false;
243          }
244          return true;
245       } catch (Throwable JavaDoc ex) {
246          if (!mgrOK) {
247             lastStatusString="Improper package or process definition Id!";
248          } else {
249             lastStatusString=ex.getMessage();
250          }
251          return false;
252       }
253
254    }
255
256    public static String JavaDoc getLastStatusString () {
257       return lastStatusString;
258    }
259
260    public static void main (String JavaDoc[] args) {
261       if (args==null || args.length<3) {
262          printUsage();
263          return;
264       }
265
266       if (!CORBAProcStartClient.run(args)) {
267          System.out.println("Error message: "+CORBAProcStartClient.getLastStatusString()+"\n");
268          printUsage();
269       }
270
271    }
272
273    static void configure (Properties JavaDoc props,String JavaDoc filePath) throws Exception JavaDoc {
274       if (filePath==null) {
275          throw new Exception JavaDoc("Client need to be configured properly - given path to configuration file is null!!!");
276       }
277       File JavaDoc configFile=new File JavaDoc(filePath);
278       if (!configFile.isAbsolute()) {
279          configFile=configFile.getAbsoluteFile();
280       }
281       if (configFile.exists()) {
282          FileInputStream JavaDoc fis=null;
283          fis=new FileInputStream JavaDoc(configFile);
284          props.load(fis);
285          fis.close();
286       } else {
287          throw new Exception JavaDoc("Client need to be configured properly - configuration file "+configFile+" does not exist!!!");
288       }
289    }
290
291    static String JavaDoc getClassName (NameValueInfo[] nvia,String JavaDoc varId) {
292       if (nvia!=null && nvia.length>0) {
293          for (int i=0; i<nvia.length; i++) {
294             NameValueInfo nvi=nvia[i];
295             if (nvi.attribute_name.equals(varId)) {
296                return nvi.type_name;
297             }
298          }
299       }
300       return null;
301    }
302
303    static void printUsage () {
304       System.err.println("This application is used to start shark's process through the CORBA.");
305       System.err.println("It is supposed that CORBA server is up, and that corresponding xpdl is loaded into shark.");
306       System.err.println();
307       System.err.println("usage: java org.enhydra.shark.test.CORBAProcStartClient configFilePath pkgId pDefId [var1=val1 [var2=val2 ... ]]");
308       System.err.println();
309       System.err.println("arguments:");
310       System.err.println(" configFilePath the path to the configuration file.");
311       System.err.println(" pkgId the id of xpdl package.");
312       System.err.println(" pDefId the id of xpdl process definition.");
313       System.err.println(" vari=vali the process variable id and its value.");
314       System.err.println();
315       System.err.println("NOTE: the configuration file should contain name-value pairs, and following should be specified:");
316       System.err.println(" - host the name of CORBA name server host.");
317       System.err.println(" - port the port number for the CORBA name server.");
318       System.err.println(" - enginename the name of shark server instance (the one registerd with nameserver).");
319       System.err.println(" - username username credential to connect to shark server.");
320       System.err.println(" - password password credential to connect to shark server.");
321       System.err.println();
322       System.err.println("NOTE: variable value will be interpreted as:");
323       System.err.println(" Boolean - if you enter 'true' or 'false',");
324       System.err.println(" Long - if you enter only digits,");
325       System.err.println(" Double - if you enter only digits and full-stop,");
326       System.err.println(" String - otherwise.");
327    }
328 }
329
330
Popular Tags