001: /* RequesterImpl.java */
002:
003: package org.enhydra.shark.asap;
004:
005: import java.io.File;
006: import java.io.FileInputStream;
007: import java.io.FileOutputStream;
008: import java.io.ObjectInputStream;
009: import java.io.ObjectOutputStream;
010: import java.io.Serializable;
011: import java.util.HashMap;
012: import java.util.HashSet;
013: import java.util.Map;
014: import java.util.Set;
015:
016: import org.apache.axis.types.URI;
017: import org.enhydra.shark.api.client.wfmodel.InvalidPerformer;
018: import org.enhydra.shark.api.client.wfmodel.WfEventAudit;
019: import org.enhydra.shark.api.client.wfmodel.WfProcess;
020: import org.enhydra.shark.api.client.wfmodel.WfProcessIterator;
021: import org.enhydra.shark.api.client.wfmodel.WfRequester;
022: import org.enhydra.shark.api.client.wfmodel.WfStateEventAudit;
023:
024: /**
025: * @author V.Puskas, S.Bojanic
026: */
027: public class RequesterImpl implements WfRequester {
028:
029: public RequesterImpl() {
030: }
031:
032: public static void addObserver(String procId, URI instance,
033: URI observer) {
034: System.out.println("Adding observer " + observer
035: + " for process URI " + instance);
036: getObservers(procId).add(
037: new ObsInfo(instance.toString(), observer.toString()));
038: persistChanges();
039: }
040:
041: public static void removeObserver(String procId, URI instance,
042: URI observer) {
043: System.out.println("Removing observer " + observer
044: + " for process URI " + instance);
045: getObservers(procId).remove(
046: new ObsInfo(instance.toString(), observer.toString()));
047: persistChanges();
048: }
049:
050: public static void removeObservers(String procId) {
051: System.out.println("Removing observers for process " + procId);
052: getObserverMap().remove(procId);
053: persistChanges();
054: }
055:
056: public static Set getObservers(String procId) {
057: Set observers = (Set) getObserverMap().get(procId);
058: if (null == observers) {
059: observers = new HashSet();
060: getObserverMap().put(procId, observers);
061: }
062: return observers;
063: }
064:
065: public int how_many_performer() throws Exception {
066: // TODO Auto-generated method stub
067: return 0;
068: }
069:
070: public WfProcessIterator get_iterator_performer() throws Exception {
071: // TODO Auto-generated method stub
072: return null;
073: }
074:
075: public WfProcess[] get_sequence_performer(int max_number)
076: throws Exception {
077: // TODO Auto-generated method stub
078: return null;
079: }
080:
081: public boolean is_member_of_performer(WfProcess member)
082: throws Exception {
083: // TODO Auto-generated method stub
084: return false;
085: }
086:
087: public void receive_event(final WfEventAudit event)
088: throws Exception, InvalidPerformer {
089: System.out.println("Web Service Requester received an event.");
090:
091: if (!(event instanceof WfStateEventAudit))
092: return;
093:
094: WfProcess proc = null;
095: // UserTransaction ut = null;
096: try {
097: // ut = org.enhydra.shark.asap.SharkServiceImpl.getUserTransaction();
098: // ut.begin();
099: proc = (WfProcess) event.source();
100: String procId = event.process_key();
101: String new_state = ((WfStateEventAudit) event).new_state();
102: String old_state = ((WfStateEventAudit) event).old_state();
103: Map result = proc.result();
104: System.out.println("Notifying observers for process "
105: + procId);
106: AsapBindingUtilitiesImpl.notifyObservers(procId, result,
107: new_state, old_state, getObservers(procId));
108: // ut.commit();
109: } catch (Exception e) {
110: e.printStackTrace();
111: // try {
112: // if (ut.getStatus() != Status.STATUS_NO_TRANSACTION) {
113: // ut.rollback();
114: // }
115: // } catch (Exception _) {
116: // }
117: throw e;
118: }
119: }
120:
121: private static Map obs = null;
122: private static String FILELOC = null;
123:
124: protected static Map getObserverMap() {
125: if (obs == null) {
126: readObservers();
127: }
128: return obs;
129: }
130:
131: protected static void persistChanges() {
132: if (FILELOC == null) {
133: FILELOC = System.getProperty("user.home")
134: + File.separatorChar + ".wfxmlsharkobservers";
135: }
136: try {
137: FileOutputStream ostream = new FileOutputStream(FILELOC);
138: ObjectOutputStream p = new ObjectOutputStream(ostream);
139: if (obs == null) {
140: obs = new HashMap();
141: }
142: p.writeObject(obs);
143:
144: p.flush();
145: ostream.close();
146: } catch (Throwable _) {
147: _.printStackTrace();
148: }
149: }
150:
151: protected static void readObservers() {
152: if (FILELOC == null) {
153: FILELOC = System.getProperty("user.home")
154: + File.separatorChar + ".wfxmlsharkobservers";
155: }
156:
157: try {
158: System.out.println("Requester Storage File Location -> "
159: + FILELOC);
160: if (!new File(FILELOC).exists()) {
161: persistChanges();
162: }
163: FileInputStream istream = new FileInputStream(FILELOC);
164: ObjectInputStream p = new ObjectInputStream(istream);
165:
166: obs = (Map) p.readObject();
167:
168: istream.close();
169: } catch (Throwable _) {
170: obs = new HashMap();
171: _.printStackTrace();
172: }
173: }
174: }
175:
176: class ObsInfo implements Serializable {
177: private String procURI;
178: private String obsURI;
179:
180: public ObsInfo(String procURI, String obsURI) {
181: this .procURI = procURI;
182: this .obsURI = obsURI;
183: }
184:
185: public String getProcURI() {
186: return procURI;
187: }
188:
189: public String getObsURI() {
190: return obsURI;
191: }
192:
193: public boolean equals(Object obj) {
194: if (!(obj instanceof ObsInfo)) {
195: return false;
196: }
197: ObsInfo oi = (ObsInfo) obj;
198: if (this .getProcURI().equals(oi.getProcURI())
199: && this .getObsURI().equals(oi.getObsURI())) {
200: return true;
201: }
202: return false;
203: }
204:
205: public int hashCode() {
206: return procURI.hashCode() + obsURI.hashCode();
207: }
208: }
|