KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > JSPClientUtilities


1 package test;
2
3 import java.util.*;
4 import org.enhydra.shark.api.client.wfmodel.*;
5 import org.enhydra.shark.api.client.wfservice.*;
6
7 import java.io.FileInputStream JavaDoc;
8 import org.enhydra.shark.Shark;
9 import org.enhydra.shark.api.common.SharkConstants;
10 import org.enhydra.shark.api.client.wfbase.BaseException;
11 import org.enhydra.shark.api.TransactionException;
12 import org.enhydra.shark.api.SharkTransaction;
13 import org.enhydra.shark.api.RootException;
14
15 public class JSPClientUtilities {
16    private static boolean _debug_ = false;
17    private static boolean sharkConfigured = false;
18    private static String JavaDoc userName = "qq";
19    private static String JavaDoc pwd = "lele";
20    private static String JavaDoc engineName = "SharkExampleJSP";
21
22    private static String JavaDoc EXT_ATT_NAME = "pageContent";
23
24    public static void init(String JavaDoc realPath){
25       if (_debug_)
26          System.err.println("#_init_#");
27       if (!sharkConfigured){
28          Properties p = new Properties();
29          try {
30             if (!realPath.endsWith("\\")) {
31                realPath=realPath+"\\";
32             }
33             realPath = replaceAll(realPath, "\\", "/");
34             p.load(new FileInputStream JavaDoc(realPath +"conf/Shark.conf"));
35             for (Iterator it = p.keySet().iterator(); it.hasNext();) {
36                String JavaDoc key = (String JavaDoc)it.next();
37                String JavaDoc value = p.getProperty(key);
38                if (0 <= value.indexOf("@@")) {
39                   if (_debug_)
40                      System.err.print("key is "+key+", old value is"+value);
41                   value = replaceAll(value, "@@/", realPath);
42                   p.setProperty(key, value);
43                   if (_debug_)
44                      System.err.println(", new value is"+value);
45                }
46             }
47          } catch (Exception JavaDoc e) {
48             e.printStackTrace();
49          }
50          p.setProperty("enginename", engineName);
51          Shark.configure(p);
52          sharkConfigured = true;
53       }
54       user();
55    }
56
57    private static void user() {
58       String JavaDoc groupName = "idle";
59       UserGroupAdministration uga= Shark
60       .getInstance()
61       .getAdminInterface()
62       .getUserGroupAdministration();
63       try {
64          if (!uga.doesGroupExist(groupName)) {
65             uga.createGroup(groupName,"test group");
66          }
67          if (!uga.doesUserExist(userName)) {
68             uga.createUser(groupName, userName, pwd,"Jane", "Doe","test@together.at");
69          }
70       } catch (Throwable JavaDoc t) {}
71    }
72
73    public static String JavaDoc packageLoad(String JavaDoc xpdlName) throws BaseException {
74       if (_debug_)
75          System.err.println("#_packageLoad_#");
76       PackageAdministration pa =Shark
77       .getInstance()
78       .getAdminInterface()
79       .getPackageAdministration();
80       RepositoryMgr rm = Shark
81       .getInstance()
82       .getRepositoryManager();
83       String JavaDoc pkgId=rm.getPackageId(xpdlName);
84       if (!pa.isPackageOpened(pkgId)) {
85          try {
86             pa.openPackage(xpdlName);
87          } catch (Throwable JavaDoc e) {
88             e.printStackTrace();
89             throw new BaseException(e);
90          }
91       }
92       return rm.getPackageId(xpdlName);
93    }
94
95    public static void processStart(String JavaDoc mgrName) throws BaseException {
96       if (_debug_)
97          System.err.println("#_processStartName_#");
98       SharkConnection sConn = null;
99       sConn = Shark
100       .getInstance()
101       .getSharkConnection();
102       try {
103          String JavaDoc pkgId = Shark
104          .getInstance()
105          .getAdminInterface()
106          .getAdminMisc()
107          .getProcessMgrPkgId(mgrName);
108          String JavaDoc pDefId = Shark
109          .getInstance()
110          .getAdminInterface()
111          .getAdminMisc()
112          .getProcessMgrProcDefId(mgrName);
113
114          if (!isProcessRunning(pkgId, pDefId)) {
115             sConn.connect(userName, pwd, engineName, null);
116             sConn
117             .createProcess(pkgId, pDefId)
118             .start();
119             sConn.disconnect();
120          }
121       } catch (Exception JavaDoc e) {
122          e.printStackTrace();
123          throw new BaseException(e);
124       }
125    }
126
127    public static void processStart(String JavaDoc mgrName, SharkConnection sConn) throws BaseException {
128       if (_debug_)
129          System.err.println("#_processStartName_#");
130       try {
131          String JavaDoc pkgId = Shark
132          .getInstance()
133          .getAdminInterface()
134          .getAdminMisc()
135          .getProcessMgrPkgId(mgrName);
136          String JavaDoc pDefId = Shark
137          .getInstance()
138          .getAdminInterface()
139          .getAdminMisc()
140          .getProcessMgrProcDefId(mgrName);
141
142          if (!isProcessRunning(pkgId, pDefId)) {
143             sConn
144             .createProcess(pkgId, pDefId)
145             .start();
146          }
147       } catch (Exception JavaDoc e) {
148          e.printStackTrace();
149          throw new BaseException(e);
150       }
151    }
152
153    public static void processStart(String JavaDoc pkgId, String JavaDoc pDefId) throws BaseException {
154       if (_debug_)
155          System.err.println("#_processStartIds_#");
156       SharkConnection sConn = null;
157       sConn = Shark
158       .getInstance()
159       .getSharkConnection();
160       try {
161          if (!isProcessRunning(pkgId, pDefId)) {
162             sConn.connect(userName, pwd, engineName, null);
163             sConn
164             .createProcess(pkgId, pDefId)
165             .start();
166             sConn.disconnect();
167          }
168       } catch (Exception JavaDoc e) {
169          e.printStackTrace();
170          throw new BaseException(e);
171       }
172    }
173
174    public static String JavaDoc extAttribute(String JavaDoc procId, String JavaDoc actId) throws BaseException {
175       return Shark
176       .getInstance()
177       .getAdminInterface()
178       .getAdminMisc()
179       .getActivitiesExtendedAttributeValue(procId, actId, EXT_ATT_NAME);
180    }
181
182    public static boolean isProcessRunning(String JavaDoc pkgId, String JavaDoc pDefId) throws BaseException {
183       // if (_debug_)
184
System.err.println("#_isProcessRunning_# ("+pkgId+", "+pDefId+")");
185       ExecutionAdministration ea = Shark
186       .getInstance()
187       .getAdminInterface()
188       .getExecutionAdministration();
189       try {
190          ea.connect(userName, pwd, engineName, null);
191          WfProcessMgr pMgr = ea.getProcessMgr(pkgId, pDefId);
192          WfProcessIterator pit = pMgr.get_iterator_process();
193          //pit.set_query_expression("state.equals(\"open.running\");");
194
pit.set_query_expression("state.equals(\""
195                                      + SharkConstants.STATE_OPEN_RUNNING
196                                      +"\")");
197          if (_debug_) {
198             System.err.println("#_"+pit.how_many()+"_#");
199             System.err.println("#_"+pit.get_next_n_sequence(0).length+"_#");
200          }
201          return 0 < pit.get_next_n_sequence(0).length;
202       } catch (Exception JavaDoc e) {
203          e.printStackTrace();
204          throw new BaseException(e);
205       } finally {
206          try {
207             ea.disconnect();
208          } catch (Exception JavaDoc e) {}
209       }
210    }
211
212    public static void activityComplete(SharkConnection sConn, String JavaDoc activityId) throws BaseException {
213       try {
214          if(null != activityId) {
215             try {
216                WfAssignment a = getAssignment(sConn, activityId);
217                if (!isMine(sConn, a))
218                   assignmentAccept(sConn, a);
219                a.activity().complete();
220             } catch (Exception JavaDoc e) {
221                throw new BaseException(e);
222             }
223          }
224       } catch (BaseException e) {
225          e.printStackTrace();
226          //throw e;
227
}
228    }
229
230    public static boolean isMine(SharkConnection sConn, String JavaDoc activityId) throws NotConnected, BaseException {
231       WfAssignment a = getAssignment(sConn, activityId);
232       return isMine(sConn, a);
233    }
234
235    public static boolean isMine(SharkConnection sConn, WfAssignment a) throws NotConnected, BaseException {
236       return a.get_accepted_status();
237    }
238
239    public static void assignmentAccept(SharkConnection sConn, String JavaDoc activityId) throws CannotAcceptSuspended, NotConnected, BaseException {
240       assignmentAccept(sConn, getAssignment(sConn, activityId));
241    }
242
243    private static void assignmentAccept(SharkConnection sConn, WfAssignment a) throws CannotAcceptSuspended, NotConnected, BaseException {
244       a.set_accepted_status(true);
245    }
246
247    public static WfAssignment getAssignment(SharkConnection sConn, String JavaDoc activityId) throws NotConnected, BaseException {
248       try {
249          WfAssignment[] ar = sConn.getResourceObject().get_sequence_work_item(0);
250          for (int i = 0; i < ar.length; ++i) {
251             if (activityId.equals(ar[i].activity().key())) {
252                return ar[i];
253             }
254          }
255          throw new BaseException("Activity:"
256                                     + activityId
257                                     +" not found in "
258                                     + sConn.getResourceObject().resource_key()
259                                     +"'s worklist");
260       } catch (Exception JavaDoc e) {
261          if (_debug_)
262             System.err.println("zvekseptsn");
263          e.printStackTrace();
264          throw new BaseException(e);
265       }
266    }
267
268    public static SharkConnection connect() throws ConnectFailed, BaseException {
269       if (_debug_)
270          System.err.println("#_connect_#");
271       SharkConnection sConn = Shark
272       .getInstance()
273       .getSharkConnection();
274       sConn.connect(userName, pwd, engineName, null);
275       return sConn;
276    }
277
278    public static SharkConnection connect(String JavaDoc user, String JavaDoc passwd) throws ConnectFailed, BaseException {
279       if (_debug_)
280          System.err.println("#_connect_#");
281       try {
282          String JavaDoc groupName = "idle";
283          UserGroupAdministration uga= Shark
284          .getInstance()
285          .getAdminInterface()
286          .getUserGroupAdministration();
287          if (!uga.doesGroupExist(groupName)) {
288             uga.createGroup(groupName,"test group");
289          }
290          if (!uga.doesUserExist(user)) {
291             uga.createUser(groupName, user, passwd, user, passwd,"test@together.at");
292          }
293       } catch (Throwable JavaDoc t) {}
294       SharkConnection sConn = Shark
295       .getInstance()
296       .getSharkConnection();
297       sConn.connect(user, passwd, engineName, null);
298       return sConn;
299    }
300
301    public static void disconnect(SharkConnection sConn) throws NotConnected, BaseException {
302       if (_debug_)
303          System.err.println("#_disconnect_#");
304       sConn.disconnect();
305    }
306
307    public static String JavaDoc[] getAllUsers() throws BaseException {
308       UserGroupAdministration uga= Shark
309       .getInstance()
310       .getAdminInterface()
311       .getUserGroupAdministration();
312
313       return uga.getAllUsers();
314    }
315
316    public static String JavaDoc getUserRealName(String JavaDoc uname) throws BaseException {
317       UserGroupAdministration uga= Shark
318       .getInstance()
319       .getAdminInterface()
320       .getUserGroupAdministration();
321
322       return uga.getUserRealName(uname);
323    }
324
325    public static String JavaDoc getUserEMailAddress(String JavaDoc uname) throws BaseException {
326       UserGroupAdministration uga= Shark
327       .getInstance()
328       .getAdminInterface()
329       .getUserGroupAdministration();
330
331       return uga.getUserEMailAddress(uname);
332    }
333
334    public static void variableSet(SharkConnection sConn,
335                                   String JavaDoc activityId,
336                                   String JavaDoc vName,
337                                   String JavaDoc vValue) throws NotConnected, BaseException, UpdateNotAllowed, InvalidData {
338       WfAssignment a = getAssignment(sConn, activityId);
339       if (!isMine(sConn, a))
340          throw new BaseException("I don't own activity "+ activityId);
341       Map _m = new HashMap();
342       Object JavaDoc c = a.activity().process_context().get(vName);
343       if (c instanceof Long JavaDoc) {
344          c = new Long JavaDoc(vValue);
345       } else if (c instanceof Boolean JavaDoc) {
346          c = Boolean.valueOf(vValue);
347       } else if (c instanceof Double JavaDoc) {
348          c = Double.valueOf(vValue);
349       } else {
350          c = vValue;
351       }
352       _m.put(vName, c);
353       a.activity().set_result(_m);
354    }
355    public static void s() {
356       WfAssignment a = null;
357       Map _m = null;
358       try {
359          SharkTransaction tr = Shark.getInstance().createTransaction();
360          try {
361             a.activity(tr).set_result(tr, _m);
362             a.activity(tr).complete(tr);
363             tr.commit();
364          } catch (RootException e) {
365             Shark.getInstance().emptyCaches(tr);
366             Shark.getInstance().unlockProcesses(tr);
367             tr.rollback();
368          }
369       } catch (TransactionException e) {
370       }
371    }
372    public static String JavaDoc[] xpdlsToLoad() {
373       try {
374          return Shark
375          .getInstance()
376          .getRepositoryManager()
377          .getPackagePaths();
378       } catch (BaseException e) {
379          e.printStackTrace();
380          return new String JavaDoc[]{};
381       }
382    }
383
384    public static String JavaDoc[] processesToStart() {
385       ExecutionAdministration ea = null;
386       try {
387          ea = Shark
388          .getInstance()
389          .getAdminInterface()
390          .getExecutionAdministration();
391          ea.connect(userName, pwd, engineName, null);
392          WfProcessMgr[] a = ea
393          .get_iterator_processmgr()
394          .get_next_n_sequence(0);
395          String JavaDoc [] ret = new String JavaDoc[a.length];
396          for (int i = 0; i < a.length; ++i) {
397             String JavaDoc n = a[i].name();
398             if (_debug_)
399                System.err.println("processName "+n);
400             ret[i]= n;
401          }
402          return ret;
403       } catch (Exception JavaDoc e) {
404          e.printStackTrace();
405       } finally {
406          try {
407             ea.disconnect();
408          } catch (BaseException e) {} catch (NotConnected e) {}
409       }
410       return new String JavaDoc[]{};
411    }
412
413    /**
414     * Replace all occurence of forReplace with replaceWith in input string.
415     * @param input represents input string
416     * @param forReplace represents substring for replace
417     * @param replaceWith represents replaced string value
418     * @return new string with replaced values
419     */

420    private static String JavaDoc replaceAll(String JavaDoc input,
421                                     String JavaDoc forReplace,
422                                     String JavaDoc replaceWith) {
423       if( input == null )
424          return null;
425       StringBuffer JavaDoc result = new StringBuffer JavaDoc();
426       boolean hasMore = true;
427       while (hasMore) {
428          int start = input.indexOf(forReplace);
429          int end = start + forReplace.length();
430          if (start != -1) {
431             result.append(input.substring(0, start) + replaceWith);
432             input = input.substring(end);
433          }
434          else {
435             hasMore = false;
436             result.append(input);
437          }
438       }
439       if (result.toString().equals(""))
440          return input; //nothing is changed
441
else
442          return result.toString();
443    }
444 }
445
446
Popular Tags