001: package test;
002:
003: import java.io.File;
004: import java.io.FileFilter;
005: import java.io.FileInputStream;
006: import java.util.ArrayList;
007: import java.util.Arrays;
008: import java.util.Collection;
009: import java.util.Collections;
010: import java.util.HashMap;
011: import java.util.Iterator;
012: import java.util.List;
013: import java.util.Map;
014: import java.util.Properties;
015:
016: import javax.naming.InitialContext;
017: import javax.transaction.UserTransaction;
018:
019: import org.enhydra.shark.Shark;
020: import org.enhydra.shark.api.client.wfmc.wapi.WMConnectInfo;
021: import org.enhydra.shark.api.client.wfmc.wapi.WMFilter;
022: import org.enhydra.shark.api.client.wfmodel.WfActivity;
023: import org.enhydra.shark.api.client.wfmodel.WfAssignment;
024: import org.enhydra.shark.api.client.wfmodel.WfProcessIterator;
025: import org.enhydra.shark.api.client.wfmodel.WfProcessMgr;
026: import org.enhydra.shark.api.client.wfservice.AdminMisc;
027: import org.enhydra.shark.api.client.wfservice.PackageAdministration;
028: import org.enhydra.shark.api.client.wfservice.SharkConnection;
029: import org.enhydra.shark.api.client.wfservice.WMEntity;
030: import org.enhydra.shark.api.client.wfservice.XPDLBrowser;
031: import org.enhydra.shark.api.common.SharkConstants;
032: import org.enhydra.shark.utilities.MiscUtilities;
033: import org.enhydra.shark.utilities.WMEntityUtilities;
034: import org.enhydra.shark.xpdl.XMLUtil;
035:
036: public class JSPClientUtilities {
037: private static PackageFileFilter packageFileFilter = new PackageFileFilter();
038:
039: private static Properties p = new Properties();
040:
041: private static String EXTERNAL_PACKAGES_REPOSITORY = "repository/external";
042:
043: private static boolean _debug_ = false;
044:
045: private static boolean sharkConfigured = false;
046:
047: private static boolean ipc = false;
048:
049: private static String engineName = "SharkExampleJSP";
050:
051: private static WMConnectInfo wmconnInfo = new WMConnectInfo(
052: "admin", "enhydra", engineName, "");
053:
054: public static final String VARIABLE_TO_PROCESS_UPDATE = "VariableToProcess_UPDATE";
055:
056: public static final String VARIABLE_TO_PROCESS_VIEW = "VariableToProcess_VIEW";
057:
058: public static void initProperties(String realPath) throws Exception {
059: if (_debug_)
060: System.err.println("#_init_#");
061: if (!ipc) {
062: ipc = true;
063: try {
064: if (!realPath.endsWith("\\")) {
065: realPath = realPath + "\\";
066: }
067: realPath = replaceAll(realPath, "\\", "/");
068: p
069: .load(new FileInputStream(realPath
070: + "conf/Shark.conf"));
071: for (Iterator it = p.keySet().iterator(); it.hasNext();) {
072: String key = (String) it.next();
073: String value = p.getProperty(key);
074: if (0 <= value.indexOf("@@")) {
075: if (_debug_)
076: System.err.print("key is " + key
077: + ", old value is" + value);
078: value = replaceAll(value, "@@/", realPath);
079: p.setProperty(key, value);
080: if (_debug_)
081: System.err
082: .println(", new value is" + value);
083: }
084: }
085: p.load(new FileInputStream(realPath
086: + "conf/SharkJSPClient.conf"));
087: setPathToXPDLRepositoryFolder(realPath
088: + EXTERNAL_PACKAGES_REPOSITORY);
089: } catch (Exception e) {
090: System.out.println(e.getMessage());
091: e.printStackTrace();
092: }
093: p.setProperty("enginename", engineName);
094: }
095: }
096:
097: public static void init() throws Exception {
098: if (!sharkConfigured) {
099: Shark.configure(p);
100: sharkConfigured = true;
101: }
102: }
103:
104: public static String packageLoad(SharkConnection sc, String xpdlName)
105: throws Exception {
106: if (_debug_)
107: System.err.println("#_packageLoad_#");
108: String realPath = EXTERNAL_PACKAGES_REPOSITORY + "/" + xpdlName;
109: PackageAdministration pa = Shark.getInstance()
110: .getPackageAdministration();
111: String pkgId = XMLUtil.getIdFromFile(realPath);
112: if (!pa.isPackageOpened(sc.getSessionHandle(), pkgId)) {
113: try {
114: pa.openPackage(sc.getSessionHandle(), realPath);
115: } catch (Exception e) {
116: e.printStackTrace();
117: throw e;
118: }
119: }
120: return pkgId;
121: }
122:
123: public static void processStart(SharkConnection sc, String mgrName)
124: throws Exception {
125: if (_debug_)
126: System.err.println("#_processStartName_#");
127: try {
128: if (!isProcessRunning(sc, mgrName)) {
129: WfProcessMgr mgr = sc.getProcessMgr(mgrName);
130: mgr.create_process(null).start();
131: }
132: } catch (Exception e) {
133: e.printStackTrace();
134: throw e;
135: }
136: }
137:
138: public static boolean isProcessRunning(SharkConnection sc,
139: String mgrName) throws Exception {
140: // if (_debug_)
141: System.err.println("#_isProcessRunning_# (" + mgrName + ")");
142: try {
143: WfProcessMgr pMgr = sc.getProcessMgr(mgrName);
144: WfProcessIterator pit = pMgr.get_iterator_process();
145: pit.set_query_expression("state.equals(\""
146: + SharkConstants.STATE_OPEN_RUNNING + "\")");
147: if (_debug_) {
148: System.err.println("#_" + pit.how_many() + "_#");
149: System.err.println("#_"
150: + pit.get_next_n_sequence(0).length + "_#");
151: }
152: return 0 < pit.get_next_n_sequence(0).length;
153: } catch (Exception e) {
154: e.printStackTrace();
155: throw e;
156: }
157: }
158:
159: public static void activityComplete(SharkConnection sConn,
160: String activityId) throws Exception {
161: try {
162: if (null != activityId) {
163: try {
164: WfAssignment a = getAssignment(sConn, activityId);
165: if (!isMine(sConn, a))
166: assignmentAccept(sConn, a);
167: a.activity().complete();
168: } catch (Exception e) {
169: throw e;
170: }
171: }
172: } catch (Exception e) {
173: e.printStackTrace();
174: throw e;
175: }
176: }
177:
178: public static boolean isMine(SharkConnection sConn,
179: String activityId) throws Exception {
180: WfAssignment a = getAssignment(sConn, activityId);
181: return isMine(sConn, a);
182: }
183:
184: public static boolean isMine(SharkConnection sConn, WfAssignment a)
185: throws Exception {
186: return a.get_accepted_status();
187: }
188:
189: public static void assignmentAccept(SharkConnection sConn,
190: String activityId) throws Exception {
191: assignmentAccept(sConn, getAssignment(sConn, activityId));
192: }
193:
194: private static void assignmentAccept(SharkConnection sConn,
195: WfAssignment a) throws Exception {
196: a.set_accepted_status(true);
197: }
198:
199: public static WfAssignment getAssignment(SharkConnection sConn,
200: String activityId) throws Exception {
201: try {
202: WfAssignment[] ar = sConn.getResourceObject()
203: .get_sequence_work_item(0);
204: for (int i = 0; i < ar.length; ++i) {
205: if (activityId.equals(ar[i].activity().key())) {
206: return ar[i];
207: }
208: }
209: throw new Exception("Activity:" + activityId
210: + " not found in "
211: + sConn.getResourceObject().resource_key()
212: + "'s worklist");
213: } catch (Exception e) {
214: if (_debug_)
215: System.err.println("zvekseptsn");
216: e.printStackTrace();
217: throw e;
218: }
219: }
220:
221: public static SharkConnection connect() throws Exception {
222: if (_debug_)
223: System.err.println("#_connect_#");
224: SharkConnection sConn = Shark.getInstance()
225: .getSharkConnection();
226: sConn.connect(wmconnInfo);
227: return sConn;
228: }
229:
230: public static void disconnect(SharkConnection sConn)
231: throws Exception {
232: if (_debug_)
233: System.err.println("#_disconnect_#");
234: sConn.disconnect();
235: }
236:
237: public static void variableSet(SharkConnection sConn,
238: String activityId, String vName, String vValue)
239: throws Exception {
240: WfAssignment a = getAssignment(sConn, activityId);
241: if (!isMine(sConn, a))
242: throw new Exception("I don't own activity " + activityId);
243: Map _m = new HashMap();
244: Object c = a.activity().process_context().get(vName);
245: if (c instanceof Long) {
246: c = new Long(vValue);
247: } else if (c instanceof Boolean) {
248: c = Boolean.valueOf(vValue);
249: } else if (c instanceof Double) {
250: c = Double.valueOf(vValue);
251: } else {
252: c = vValue;
253: }
254: _m.put(vName, c);
255: a.activity().set_result(_m);
256: }
257:
258: public static List getVariableData(SharkConnection sConn,
259: WfActivity act) throws Exception {
260:
261: List ret = new ArrayList();
262:
263: Map _m = act.process_context();
264: String[][] eas = getExtAttribNVPairs(sConn, act);
265:
266: if (eas != null) {
267: for (int i = 0; i < eas.length; i++) {
268: String eaName = eas[i][0];
269: if (eaName.equalsIgnoreCase(VARIABLE_TO_PROCESS_UPDATE)
270: || eaName
271: .equalsIgnoreCase(VARIABLE_TO_PROCESS_VIEW)) {
272: String variableId = eas[i][1];
273: if (_m.containsKey(variableId)) {
274: Object c = _m.get(variableId);
275: if (c instanceof String || c instanceof Long
276: || c instanceof Boolean
277: || c instanceof Double) {
278: VariableData vd = new VariableData();
279: vd.setId(variableId);
280: vd
281: .setToUpdate(eaName
282: .equalsIgnoreCase(VARIABLE_TO_PROCESS_UPDATE));
283: vd.setVal(c);
284: vd.setJavaClass(c.getClass());
285:
286: WMEntity varEnt = Shark
287: .getInstance()
288: .getAdminMisc()
289: .getVariableDefinitionInfoByUniqueProcessDefinitionName(
290: sConn.getSessionHandle(),
291: act.container().manager()
292: .name(), variableId);
293: String varName = variableId;
294: if (varEnt != null) {
295: varName = varEnt.getName();
296: if (varName == null
297: || varName.equals("")) {
298: varName = variableId;
299: }
300: }
301: String varDesc = "";
302: if (varEnt != null) {
303: WMFilter filter = new WMFilter("Name",
304: WMFilter.EQ, "Description");
305: filter
306: .setFilterType(XPDLBrowser.SIMPLE_TYPE_XPDL);
307: varDesc = Shark
308: .getInstance()
309: .getXPDLBrowser()
310: .listAttributes(
311: sConn
312: .getSessionHandle(),
313: varEnt, filter, false)
314: .getArray()[0].getValue()
315: .toString();
316: }
317:
318: vd.setName(varName);
319: vd.setDescription(varDesc);
320: ret.add(vd);
321: }
322: }
323: }
324: }
325: }
326:
327: return ret;
328:
329: }
330:
331: public static String[] xpdlsToLoad() throws Exception {
332: List packageFiles = getDefinedPackageFiles(
333: EXTERNAL_PACKAGES_REPOSITORY, true);
334: Collection pfls = new ArrayList();
335: Iterator pfi = packageFiles.iterator();
336: Collections.sort(packageFiles);
337: while (pfi.hasNext()) {
338: File f = (File) pfi.next();
339: String fileName;
340: try {
341: fileName = f.getCanonicalPath();
342: } catch (Exception ex) {
343: fileName = f.getAbsolutePath();
344: }
345: fileName = fileName.substring(EXTERNAL_PACKAGES_REPOSITORY
346: .length() + 1);
347: pfls.add(fileName);
348: }
349: String[] pfs = new String[pfls.size()];
350: pfls.toArray(pfs);
351: return pfs;
352: }
353:
354: public static String[] processesToStart(SharkConnection sc)
355: throws Exception {
356: WfProcessMgr[] a = sc.get_iterator_processmgr()
357: .get_next_n_sequence(0);
358: String[] ret = new String[a.length];
359: for (int i = 0; i < a.length; ++i) {
360: String n = a[i].name();
361: if (_debug_)
362: System.err.println("processName " + n);
363: ret[i] = n;
364: }
365: return ret;
366: }
367:
368: /**
369: * Replace all occurence of forReplace with replaceWith in input string.
370: *
371: * @param input represents input string
372: * @param forReplace represents substring for replace
373: * @param replaceWith represents replaced string value
374: * @return new string with replaced values
375: */
376: private static String replaceAll(String input, String forReplace,
377: String replaceWith) {
378: if (input == null)
379: return null;
380: StringBuffer result = new StringBuffer();
381: boolean hasMore = true;
382: while (hasMore) {
383: int start = input.indexOf(forReplace);
384: int end = start + forReplace.length();
385: if (start != -1) {
386: result.append(input.substring(0, start) + replaceWith);
387: input = input.substring(end);
388: } else {
389: hasMore = false;
390: result.append(input);
391: }
392: }
393: if (result.toString().equals(""))
394: return input; // nothing is changed
395: else
396: return result.toString();
397: }
398:
399: static List getDefinedPackageFiles(String repository,
400: boolean traverse) {
401: File startingFolder = new File(repository);
402: List packageFiles = new ArrayList();
403: if (!startingFolder.exists()) {
404: System.out.println("Repository " + startingFolder
405: + " doesn't exist");
406: }
407: if (traverse) {
408: MiscUtilities.traverse(startingFolder, packageFiles, null);
409: } else {
410: packageFiles = Arrays.asList(startingFolder
411: .listFiles(packageFileFilter));
412: }
413:
414: return packageFiles;
415: }
416:
417: public static void setPathToXPDLRepositoryFolder(
418: String xpdlRepFolder) throws Exception {
419: EXTERNAL_PACKAGES_REPOSITORY = xpdlRepFolder;
420: System.err.println(xpdlRepFolder);
421: File f = new File(xpdlRepFolder);
422: System.err.println(f);
423: if (!f.isAbsolute()) {
424: System.err.println("isn't absolute");
425: f = f.getAbsoluteFile();
426: }
427: System.err.println(f);
428:
429: if (!f.exists()) {
430: throw new Exception("Folder " + xpdlRepFolder
431: + " does not exist");
432: }
433: try {
434: EXTERNAL_PACKAGES_REPOSITORY = f.getCanonicalPath();
435: } catch (Exception ex) {
436: EXTERNAL_PACKAGES_REPOSITORY = f.getAbsolutePath();
437: }
438: }
439:
440: // -1 -> do not show
441: // 0 -> show for reading
442: // 1 -> show for updating
443: protected int variableType(String varId, String[][] eas)
444: throws Exception {
445: int type = -1;
446:
447: if (eas != null) {
448: for (int i = 0; i < eas.length; i++) {
449: String eaName = eas[i][0];
450: String eaVal = eas[i][1];
451: if (eaVal.equals(varId)) {
452: if (eaName
453: .equalsIgnoreCase(VARIABLE_TO_PROCESS_UPDATE)) {
454: type = 1;
455: break;
456: } else if (eaName
457: .equalsIgnoreCase(VARIABLE_TO_PROCESS_VIEW)) {
458: type = 0;
459: break;
460: }
461: }
462: }
463: }
464:
465: return type;
466: }
467:
468: protected static String[][] getExtAttribNVPairs(SharkConnection sc,
469: WfActivity act) throws Exception {
470: XPDLBrowser xpdlb = Shark.getInstance().getXPDLBrowser();
471: AdminMisc am = Shark.getInstance().getAdminMisc();
472: WMEntity ent = am.getActivityDefinitionInfo(sc
473: .getSessionHandle(), act.container().key(), act.key());
474:
475: return WMEntityUtilities.getExtAttribNVPairs(sc
476: .getSessionHandle(), xpdlb, ent);
477: }
478:
479: public static UserTransaction getUserTransaction() throws Exception {
480: String lookupName = p
481: .getProperty("XaUserTransactionLookupName");
482: return (UserTransaction) new InitialContext()
483: .lookup(lookupName);
484: }
485:
486: }
487:
488: class PackageFileFilter implements FileFilter {
489: public boolean accept(File pathname) {
490: return !pathname.isDirectory();
491: }
492: }
|