KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark.test;
2
3 import org.omg.WfBase.*;
4 import org.enhydra.shark.corba.WorkflowService.*;
5 import org.omg.WorkflowModel.*;
6
7 import org.omg.CORBA.*; // All CORBA applications need these classes.
8
import org.omg.CosNaming.*; // Client will use the naming service.
9

10 import java.util.*;
11 import java.io.*;
12
13 public class CORBAManualTest extends Thread JavaDoc {
14    static final String JavaDoc HOST_PROPERTY_NAME="host";
15    static final String JavaDoc PORT_PROPERTY_NAME="port";
16    static final String JavaDoc ENGINENAME_PROPERTY_NAME="enginename";
17    static final String JavaDoc USERNAME_PROPERTY_NAME="username";
18    static final String JavaDoc PASSWORD_PROPERTY_NAME="password";
19
20    static ORB orb;
21    static String JavaDoc host;
22    static String JavaDoc port;
23    static String JavaDoc enginename;
24    static String JavaDoc username;
25    static String JavaDoc password;
26
27    static int counter = 0;
28
29    static String JavaDoc pkgId;
30    static String JavaDoc pDefId;
31
32    private static long startT;
33    private static long endT;
34
35    private static SharkInterface shark;
36    static Properties props=new Properties();
37
38    private SharkConnection sc;
39    private WfResource res;
40    private int actCompleted;
41
42    public CORBAManualTest() throws BaseException, ConnectFailed, NotConnected {
43       sc=shark.getSharkConnection();
44       sc.connect(username,password,"","");
45       res=sc.getResourceObject();
46
47       this.actCompleted = 0;
48       synchronized(password) {
49          ++counter;
50       }
51       start();
52    }
53
54    public void run () {
55       for (WfAssignment[] ass=null;true;) {
56          try {
57             ass=res.get_sequence_work_item(0);
58          } catch(BaseException e) {
59             e.printStackTrace();
60             continue;
61          }
62          if (ass!=null && ass.length>0) {
63             for (int i=0; i<ass.length; i++) {
64                try {
65                   if (!ass[i].get_accepted_status()) {
66                      System.err.println("Thread "+this+" - Accepting ass ["+ass[i].activity().key()+","+ass[i].assignee().resource_key()+"], active threads="+Thread.activeCount());
67                      ass[i].set_accepted_status(true);
68                   }
69                   System.err.println("Thread "+this+" - Completing ass ["+ass[i].activity().key()+","+ass[i].assignee().resource_key()+"], active threads="+Thread.activeCount());
70                   WfActivity act=ass[i].activity();
71                   act.complete();
72                   ++actCompleted;
73                   sc.doneWith(act);
74                   sc.doneWith(ass[i]);
75                   //System.err.println("Thread "+this+" - Ass ["+ass[i].activity().key()+","+ass[i].assignee().resource_key()+"] completed, active threads="+Thread.activeCount());
76
} catch (Exception JavaDoc ex) {
77                   ex.printStackTrace();
78                   System.err.println("Thread "+this+" - Problems while executing ass "+ass[i]+", active threads="+Thread.activeCount()+" errMsg="+ex.getMessage());
79                }
80             }
81          } else {
82             break;
83          }
84       }
85       sc.doneWith(res);
86       try {
87          sc.disconnect();
88          Thread.sleep(1500);
89       } catch (Exception JavaDoc ex) {
90       }
91       System.err.print("There are "
92                           + Thread.activeCount()
93                           +" active threads; this one completed "
94                           + actCompleted
95                           +" activities ");
96       synchronized(password) {
97          --counter;
98          if (0 == counter) {
99             endT=System.currentTimeMillis();
100             try {
101                ExecutionAdministration ea=shark.getExecutionAdministration();
102                ea.connect(username,password,"","");
103                WfProcessMgr mgr=ea.getProcessMgrByXPDLDefinition(pkgId,pDefId);
104                WfProcess[] procs=mgr.get_sequence_process(0);
105                for (int i=0; i<procs.length; i++) {
106                   System.out.println("Process "+procs[i].key()+" state is "+procs[i].state());
107                   ea.doneWith(procs[i]);
108                }
109                ea.doneWith(mgr);
110                ea.disconnect();
111             } catch (Exception JavaDoc ex){}
112             System.out.println("The exec. lasted "+(endT-startT)+" ms");
113             System.exit(0);
114          }
115       }
116    }
117
118    public static void main (String JavaDoc[] args) throws Exception JavaDoc {
119       if (args==null || args.length<3) {
120          printUsage();
121          return;
122       }
123
124       try {
125          configure(args[0]);
126       } catch (Exception JavaDoc ex) {
127          ex.printStackTrace();
128          printUsage();
129       }
130       host=props.getProperty(CORBAProcStartClient.HOST_PROPERTY_NAME);
131       port=props.getProperty(CORBAProcStartClient.PORT_PROPERTY_NAME);
132       enginename=props.getProperty(CORBAProcStartClient.ENGINENAME_PROPERTY_NAME);
133       username=props.getProperty(CORBAProcStartClient.USERNAME_PROPERTY_NAME);
134       password=props.getProperty(CORBAProcStartClient.PASSWORD_PROPERTY_NAME);
135
136       if (host==null || port==null || enginename==null || username==null || password==null) {
137          printUsage();
138       }
139       if (host==null) {
140          throw new Exception JavaDoc("host - property is not specified in configuration file");
141       }
142       if (port==null) {
143          throw new Exception JavaDoc("port - property is not specified in configuration file");
144       }
145       if (enginename==null) {
146          throw new Exception JavaDoc("enginename - property is not specified in configuration file");
147       }
148       if (username==null) {
149          throw new Exception JavaDoc("username - property is not specified in configuration file");
150       }
151       if (password==null) {
152          throw new Exception JavaDoc("password - property is not specified in configuration file");
153       }
154
155
156       String JavaDoc xpdlName=args[1];
157       String JavaDoc pDefId=args[2];
158       int hmThreads=1;
159       if (args.length>3) {
160          hmThreads=Integer.parseInt(args[3]);
161       }
162       int hm=1;
163       if (args.length>4) {
164          hm=Integer.parseInt(args[4]);
165       }
166       Map cntx=new HashMap();
167       if (args.length>5) {
168          for (int i=5; i<args.length; i++) {
169             String JavaDoc cnt=args[i];
170             int ind=cnt.indexOf("=");
171             String JavaDoc id=cnt.substring(0,ind);
172             String JavaDoc val=cnt.substring(ind+1,cnt.length());
173             try {
174                Long JavaDoc l=Long.valueOf(val);
175                cntx.put(id,l);
176             } catch (Exception JavaDoc ex) {
177                cntx.put(id,val);
178             }
179          }
180       }
181
182       try {
183          shark=findWorkflowServer(host,port,enginename);
184       } catch (Exception JavaDoc ex) {
185          ex.printStackTrace();
186          printUsage();
187       }
188
189       NameValue[] cntxt=new NameValue[cntx.size()];
190       Iterator it=cntx.entrySet().iterator();
191       int j=0;
192       while(it.hasNext()) {
193          Map.Entry me=(Map.Entry)it.next();
194          String JavaDoc id=(String JavaDoc)me.getKey();
195          java.lang.Object JavaDoc val=me.getValue();
196          Any any=orb.create_any();
197          if (val instanceof Long JavaDoc) {
198             any.insert_longlong(((Long JavaDoc)val).longValue());
199          } else {
200             any.insert_wstring((String JavaDoc)val);
201          }
202          cntxt[j]=new NameValue();
203          cntxt[j].the_name=id;
204          cntxt[j].the_value=any;
205          j++;
206       }
207
208       PackageAdministration pa=shark.getPackageAdministration();
209       RepositoryMgr rm=shark.getRepositoryManager();
210       String JavaDoc pkgId=null;
211
212       try {
213          pa.connect(username,password,"","");
214          rm.connect(username,password,"","");
215          pkgId=rm.getPackageId(xpdlName);
216          if (!pa.isPackageOpened(pkgId)) {
217             pa.openPkg(xpdlName);
218          }
219          rm.disconnect();
220          pa.disconnect();
221          SharkConnection sc=shark.getSharkConnection();
222          sc.connect(username,password,"","");
223
224          startT=System.currentTimeMillis();
225
226          for (int i = 0; i < hm; ++i) {
227             WfProcess proc=sc.createProcess(pkgId,pDefId);
228             proc.set_process_context(cntxt);
229             proc.start();
230             sc.doneWith(proc);
231          }
232          sc.disconnect();
233          CORBAManualTest.pkgId=pkgId;
234          CORBAManualTest.pDefId=pDefId;
235
236          for (int i=0;i<hmThreads; i++) {
237             new CORBAManualTest();
238          }
239       } catch (Throwable JavaDoc ex) {
240          if (ex instanceof PackageInvalid) {
241             System.out.println("PIERRS="+((PackageInvalid)ex).XPDLValidationErrors);
242          }
243          if (ex instanceof ExternalPackageInvalid) {
244             System.out.println("PIERRS="+((ExternalPackageInvalid)ex).XPDLValidationErrors);
245          }
246          ex.printStackTrace();
247          printUsage();
248       }
249    }
250
251    static void configure (String JavaDoc filePath) throws Exception JavaDoc {
252       if (filePath==null) {
253          throw new Exception JavaDoc("Client need to be configured properly - given path to configuration file is null!!!");
254       }
255       File configFile=new File(filePath);
256       if (!configFile.isAbsolute()) {
257          configFile=configFile.getAbsoluteFile();
258       }
259       if (configFile.exists()) {
260          FileInputStream fis=null;
261          fis=new FileInputStream(configFile);
262          props.load(fis);
263          fis.close();
264       } else {
265          throw new Exception JavaDoc("Client need to be configured properly - configuration file "+configFile+" does not exist!!!");
266       }
267    }
268
269    public static SharkInterface findWorkflowServer (String JavaDoc host,
270                                                     String JavaDoc port,
271                                                     String JavaDoc workflowServerName) throws Exception JavaDoc {
272       String JavaDoc[] args={"-ORBInitialHost",host,"-ORBInitialPort",port};
273       // Create and initialize the ORB
274
orb = ORB.init(args,null);
275       // Get the root naming context
276
org.omg.CORBA.Object JavaDoc objRef = orb.resolve_initial_references("NameService");
277       NamingContext ncRef = NamingContextHelper.narrow(objRef);
278
279       // Resolve the object reference in naming
280
NameComponent nc = new NameComponent(workflowServerName,"");
281       NameComponent path[] = {nc};
282       return SharkInterfaceHelper.narrow(ncRef.resolve(path));
283    }
284
285    static void printUsage () {
286       System.err.println("This application is used to start and execute shark's processes that contain at least one manual activity through the CORBA, until all of them are finished.");
287       System.err.println("It is supposed that CORBA server is up.");
288       System.err.println();
289       System.err.println("usage: java CORBAManualTest configFilePath xpdlName pDefId [noOfThreads [noOfProcToStart [var1=val1 [var2=val2 ... ]]]]");
290       System.err.println();
291       System.err.println("arguments:");
292       System.err.println(" configFilePath the path to the configuration file.");
293       System.err.println(" xpdlName the path to XPDL file where process definition for the process one want to start can be found.");
294       System.err.println(" This path has to be relative to shark's external repository folder.");
295       System.err.println(" pDefId the id of xpdl process definition.");
296       System.err.println(" noOfThreads the number of threads that will fight to execute activities (default is 1).");
297       System.err.println(" noOfProcToStart the number of processes that will be started (default is 1).");
298       System.err.println(" vari=vali the process variable id and its value.");
299       System.err.println();
300       System.err.println("NOTE: the configuration file should contain name-value pairs, and following should be specified:");
301       System.err.println(" - host the name of CORBA name server host.");
302       System.err.println(" - port the port number for the CORBA name server.");
303       System.err.println(" - enginename the name of shark server instance (the one registerd with nameserver).");
304       System.err.println(" - username username credential to connect to shark server.");
305       System.err.println(" - password password credential to connect to shark server.");
306       System.err.println();
307       System.err.println("NOTE: variable value will be interpreted as:");
308       System.err.println(" Boolean - if you enter 'true' or 'false',");
309       System.err.println(" Long - if you enter only digits,");
310       System.err.println(" Double - if you enter only digits and full-stop,");
311       System.err.println(" String - otherwise.");
312    }
313
314 }
315
316
Popular Tags