KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark.test;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import org.enhydra.shark.Shark;
7 import org.enhydra.shark.api.RootException;
8 import org.enhydra.shark.api.client.wfbase.BaseException;
9 import org.enhydra.shark.api.client.wfmodel.WfProcess;
10 import org.enhydra.shark.api.client.wfmodel.WfResource;
11 import org.enhydra.shark.api.client.wfservice.ConnectFailed;
12 import org.enhydra.shark.api.client.wfservice.ExternalPackageInvalid;
13 import org.enhydra.shark.api.client.wfservice.NotConnected;
14 import org.enhydra.shark.api.client.wfservice.PackageAdministration;
15 import org.enhydra.shark.api.client.wfservice.PackageInvalid;
16 import org.enhydra.shark.api.client.wfservice.RepositoryMgr;
17 import org.enhydra.shark.api.client.wfservice.SharkConnection;
18 import org.enhydra.shark.api.client.wfservice.UserGroupAdministration;
19 import org.enhydra.shark.appmappersistence.DODSApplicationMappingTransaction;
20 import org.enhydra.shark.partmappersistence.DODSParticipantMappingTransaction;
21 import org.enhydra.shark.scriptmappersistence.DODSScriptMappingTransaction;
22 import org.enhydra.shark.transaction.SharkDODSTransaction;
23 import org.enhydra.shark.usertransaction.SharkDODSUserTransaction;
24
25 public class AutomaticTest extends Thread JavaDoc {
26    private SharkConnection sc;
27    private WfResource res;
28    private int hm;
29    private Map JavaDoc context;
30    private String JavaDoc pkgId, pDefId;
31
32    public AutomaticTest (String JavaDoc pkgId,String JavaDoc pDefId,int hm,Map JavaDoc context) throws BaseException, ConnectFailed, NotConnected {
33       sc=Shark.getInstance().getSharkConnection();
34       this.sc=sc;
35       this.hm=hm;
36       this.context=new HashMap JavaDoc(context);
37       this.pkgId=pkgId;
38       this.pDefId=pDefId;
39
40       sc.connect(username,pwd,"","");
41       res=sc.getResourceObject();
42
43       synchronized(pwd) {
44          ++counter;
45       }
46       start();
47    }
48
49    public void run () {
50       for (int i=0; i<hm;i++) {
51          try {
52             WfProcess proc=sc.createProcess(pkgId,pDefId);
53             if (context.size()>0) {
54                proc.set_process_context(context);
55             }
56             proc.start();
57             System.out.println("THE "+(i+1)+". PROCESS for Thread"+this+" has finished, active threads="+Thread.activeCount());
58          } catch (Exception JavaDoc ex) {
59             new RootException("EXITING THREAD "+Thread.currentThread()+" BECAUSE OF ERROR!!!",ex).printStackTrace();
60             break;
61          }
62       }
63
64       try {
65          Thread.sleep(1500);
66       } catch (Exception JavaDoc ex) {}
67       System.out.println("There are "+Thread.activeCount()+","+counter+" active threads");
68       synchronized(pwd) {
69          --counter;
70          if (0 == counter) {
71             // Shark.shutdown();
72
SharkDODSTransaction.info();
73             DODSParticipantMappingTransaction.info();
74             DODSApplicationMappingTransaction.info();
75             DODSScriptMappingTransaction.info();
76             SharkDODSUserTransaction.info();
77             System.exit(0);
78          }
79       }
80    }
81
82    static String JavaDoc username="admin";
83    static String JavaDoc pwd="enhydra";
84    static int counter = 0;
85
86    public static void main (String JavaDoc[] args) throws Exception JavaDoc {
87       if (args==null || args.length<3) {
88          printUsage();
89          return;
90       }
91       String JavaDoc confFilePath=args[0];
92       String JavaDoc xpdlName=args[1];
93       String JavaDoc pDefId=args[2];
94       int hmThreads=1;
95       if (args.length>3) {
96          hmThreads=Integer.parseInt(args[3]);
97       }
98       int hm=1;
99       if (args.length>4) {
100          hm=Integer.parseInt(args[4]);
101       }
102       Map JavaDoc cntxt=new HashMap JavaDoc();
103       if (args.length>5) {
104          for (int i=5; i<args.length; i++) {
105             String JavaDoc cnt=args[i];
106             int ind=cnt.indexOf("=");
107             String JavaDoc id=cnt.substring(0,ind);
108             String JavaDoc val=cnt.substring(ind+1,cnt.length());
109             if (val!=null && val.equals("true") || val.equals("false")) {
110                Boolean JavaDoc bool=new Boolean JavaDoc(val);
111                cntxt.put(id,bool);
112                continue;
113             }
114             try {
115                Double JavaDoc d=Double.valueOf(val);
116                if (val.indexOf(".")==-1) {
117                   Long JavaDoc l=new Long JavaDoc(d.longValue());
118                   cntxt.put(id,l);
119                } else {
120                   cntxt.put(id,d);
121                }
122             } catch (Exception JavaDoc ex) {
123                cntxt.put(id,val);
124             }
125
126          }
127       }
128
129       try {
130          Shark.configure(confFilePath);
131       } catch (Exception JavaDoc ex) {
132          ex.printStackTrace();
133          printUsage();
134          return;
135       }
136       Shark ss=Shark.getInstance();
137       PackageAdministration pa=ss.getAdminInterface().getPackageAdministration();
138       RepositoryMgr rm=ss.getRepositoryManager();
139       UserGroupAdministration uga=ss.getAdminInterface().getUserGroupAdministration();
140       String JavaDoc pkgId=null;
141       try {
142          if (!uga.doesGroupExist("test")) {
143             uga.createGroup("test","test group");
144          }
145          if (!uga.doesUserExist(username)) {
146             uga.createUser("test", username, pwd,"Jane", "Doe","");
147          }
148          System.out.println("All GRPS:");
149          String JavaDoc[] gns=uga.getAllGroupnames();
150          for (int i=0; i<gns.length; i++) {
151             System.out.println("Group - "+gns[i]);
152          }
153          System.out.println("All USERS:");
154          String JavaDoc[] uns=uga.getAllUsers();
155          for (int i=0; i<uns.length; i++) {
156             System.out.println("User - "+uns[i]);
157          }
158       } catch (Throwable JavaDoc t) {}
159
160       try {
161          pkgId=rm.getPackageId(xpdlName);
162          if (!pa.isPackageOpened(pkgId)) {
163             pa.openPackage(xpdlName);
164          }
165
166          for (int i=0;i<hmThreads; i++) {
167             new AutomaticTest(pkgId,pDefId,hm,cntxt);
168          }
169       } catch (Exception JavaDoc ex) {
170          if (ex instanceof PackageInvalid) {
171             System.out.println("PIERRS="+((PackageInvalid)ex).getXPDLValidationErrors());
172          }
173          if (ex instanceof ExternalPackageInvalid) {
174             System.out.println("PIERRS="+((ExternalPackageInvalid)ex).getXPDLValidationErrors());
175          }
176          ex.printStackTrace();
177          printUsage();
178          System.exit(1);
179       }
180    }
181
182    static void printUsage () {
183       System.err.println("This application is used to start and execute shark's processes, that contain only automatic activities, until all of them are finished.");
184       System.err.println();
185       System.err.println("usage: java ManualTest configFilePath xpdlName pDefId [noOfThreads [noOfProcToStart [var1=val1 [var2=val2 ... ]]]]");
186       System.err.println();
187       System.err.println("arguments:");
188       System.err.println(" configFilePath the path to Shark's configuration file.");
189       System.err.println(" xpdlName the path to XPDL file where process definition for the process one want to start can be found.");
190       System.err.println(" This path has to be relative to shark's external repository folder.");
191       System.err.println(" pDefId the id of xpdl process definition.");
192       System.err.println(" noOfThreads the number of threads that will instantiate and execute processes (default is 1).");
193       System.err.println(" noOfProcToStart the number of processes that will be started by each thread (default is 1).");
194       System.err.println(" vari=vali the process variable id and its value.");
195       System.err.println();
196       System.err.println("NOTE: variable value will be interpreted as:");
197       System.err.println(" Boolean - if you enter 'true' or 'false',");
198       System.err.println(" Long - if you enter only digits,");
199       System.err.println(" Double - if you enter only digits and full-stop,");
200       System.err.println(" String - otherwise.");
201    }
202
203 }
204
205
206
207
Popular Tags