KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > interoperability > WfXMLInteroperabilityImpl


1 /* WfXMLInteroperabilityImpl.java */
2 package org.enhydra.shark.interoperability;
3
4 import java.net.InetAddress JavaDoc;
5 import java.util.*;
6
7 import org.apache.axis.encoding.ser.DateDeserializer;
8 import org.apache.axis.types.URI;
9 import org.apache.axis.types.URI.MalformedURIException;
10 import org.enhydra.shark.Shark;
11 import org.enhydra.shark.api.RootException;
12 import org.enhydra.shark.api.SharkTransaction;
13 import org.enhydra.shark.api.client.wfbase.BaseException;
14 import org.enhydra.shark.api.client.wfservice.AdminMisc;
15 import org.enhydra.shark.api.common.SharkConstants;
16 import org.enhydra.shark.api.internal.interoperability.InteroperabilityException;
17 import org.enhydra.shark.api.internal.interoperability.WfEngineInteroperability;
18 import org.enhydra.shark.api.internal.working.CallbackUtilities;
19 import org.enhydra.shark.asap.AsapBindingUtilitiesImpl;
20 import org.enhydra.shark.asap.types.*;
21 import org.enhydra.shark.asap.types.holders.*;
22 import org.enhydra.shark.utilities.MiscUtilities;
23 import org.enhydra.shark.wfxml.SharkWebServiceLocator;
24
25 /**
26  * WfXMLInteroperabilityImpl
27  *
28  * @author V.Puskas
29  * @version 0.11
30  */

31 public class WfXMLInteroperabilityImpl implements WfEngineInteroperability {
32
33    protected SharkWebServiceLocator locator;
34
35    protected String JavaDoc _meh;
36
37    protected String JavaDoc namePrefix;
38
39    protected String JavaDoc description;
40
41    protected static String JavaDoc DEFAULT_DESCRIPTION = "Enhydra Shark engine [${1}]"
42                                                  + "at address ${2} on behalf"
43                                                  + " of activity ${3} in "
44                                                  + "process instance ${4} made"
45                                                  + " this request.";
46
47    protected static final String JavaDoc PARAM_HOST = "Interoperability.Host";
48
49    protected static final String JavaDoc PARAM_PORT = "Interoperability.Port";
50
51    protected static final String JavaDoc PARAM_DEFAULT_PORT = "8080";
52
53    public static final String JavaDoc EXTENDED_ATTRIBUTE_NAME_PM = "ParamMapping";
54    public static final String JavaDoc EXTENDED_ATTRIBUTE_NAME_SL = "ForeignSchemaLocation";
55
56    public void configure(CallbackUtilities cus) throws RootException {
57       this.namePrefix = cus.getProperty("enginename") + "/";
58       this.locator = new SharkWebServiceLocator();
59       try {
60          URI _me = new URI(locator.getasapObserverBindingAddress());
61          _me.setHost(cus.getProperty(PARAM_HOST, InetAddress.getLocalHost()
62             .getCanonicalHostName()));
63          _me.setPort(Integer.parseInt(cus.getProperty(PARAM_PORT,
64                                                       PARAM_DEFAULT_PORT)));
65          this._meh = _me.toString();
66          this.description = cus.getProperty("Interoperability.Description",
67                                             DEFAULT_DESCRIPTION);
68          this.description = MiscUtilities.replaceAll(this.description,
69                                                      "${1}",
70                                                      cus.getProperty("enginename"));
71          this.description = MiscUtilities.replaceAll(this.description,
72                                                      "${2}",
73                                                      _meh);
74       } catch (Exception JavaDoc e) {
75          throw new RootException(e);
76       }
77    }
78
79    public String JavaDoc start(SharkTransaction st,
80                        String JavaDoc remoteEngine,
81                        String JavaDoc processInstanceId,
82                        String JavaDoc workitemId,
83                        boolean isSynchronized,
84                        Map parameters) throws InteroperabilityException {
85       try {
86          Map _parameters = prepareInParams(st,
87                                            processInstanceId,
88                                            workitemId,
89                                            parameters);
90          URI _me = getURI(isSynchronized, processInstanceId, workitemId);
91          String JavaDoc desc = MiscUtilities.replaceAll(this.description,
92                                                 "${3}",
93                                                 workitemId);
94          desc = MiscUtilities.replaceAll(desc, "${4}", processInstanceId);
95
96          CreateInstanceRsHolder response = new CreateInstanceRsHolder();
97          AdminMisc a = Shark.getInstance().getAdminInterface().getAdminMisc();
98          workitemId = org.enhydra.shark.Shark.getInstance()
99             .getAdminInterface()
100             .getAdminMisc()
101             .getAssignmentActivityId(processInstanceId, workitemId);
102
103          String JavaDoc schemaLocation = a.getActivitiesExtendedAttributeValue(st,
104                                                                        processInstanceId,
105                                                                        workitemId,
106                                                                        EXTENDED_ATTRIBUTE_NAME_SL);
107
108          locator.getwfxmlFactoryBinding(new java.net.URL JavaDoc(remoteEngine))
109             .createInstance(new Request(_me,
110                                         new URI(remoteEngine),
111                                         YesNoIfError.Yes,
112                                         null),
113                             new CreateInstanceRq(true,
114                                                  _me,
115                                                  namePrefix + processInstanceId,
116                                                  workitemId,
117                                                  desc,
118                                                  AsapBindingUtilitiesImpl.getContextData(_parameters,
119                                                                                          schemaLocation)),
120                             new ResponseHolder(),
121                             response);
122          return response.value.getInstanceKey().toString();
123       } catch (Exception JavaDoc e) {
124          throw new InteroperabilityException(e);
125       }
126    }
127
128    public void suspend(SharkTransaction st,
129                        String JavaDoc remoteInstanceUri,
130                        String JavaDoc processInstanceId,
131                        String JavaDoc workitemId) throws InteroperabilityException {
132       changeState(remoteInstanceUri,
133                   processInstanceId,
134                   workitemId,
135                   SharkConstants.STATE_OPEN_NOT_RUNNING_SUSPENDED);
136    }
137
138    public void resume(SharkTransaction st,
139                       String JavaDoc remoteInstanceUri,
140                       String JavaDoc processInstanceId,
141                       String JavaDoc workitemId) throws InteroperabilityException {
142       changeState(remoteInstanceUri,
143                   processInstanceId,
144                   workitemId,
145                   SharkConstants.STATE_OPEN_RUNNING);
146    }
147
148    public void terminate(SharkTransaction st,
149                          String JavaDoc remoteInstanceUri,
150                          String JavaDoc processInstanceId,
151                          String JavaDoc workitemId) throws InteroperabilityException {
152       changeState(remoteInstanceUri,
153                   processInstanceId,
154                   workitemId,
155                   SharkConstants.STATE_CLOSED_TERMINATED);
156    }
157
158    public void abort(SharkTransaction st,
159                      String JavaDoc remoteInstanceUri,
160                      String JavaDoc processInstanceId,
161                      String JavaDoc workitemId) throws InteroperabilityException {
162       changeState(remoteInstanceUri,
163                   processInstanceId,
164                   workitemId,
165                   SharkConstants.STATE_CLOSED_ABORTED);
166    }
167
168    /**
169     * @param isSynchronized
170     * @param processInstanceId
171     * @param workitemId
172     * @return @throws BaseException
173     * @throws MalformedURIException
174     */

175    protected URI getURI(boolean isSynchronized,
176                         String JavaDoc processInstanceId,
177                         String JavaDoc workitemId) throws MalformedURIException,
178                                           BaseException {
179       URI ret = new URI(_meh);
180       if (isSynchronized) {
181          ret.setQueryString("procId="
182                             + processInstanceId
183                             + "&actId="
184                             + org.enhydra.shark.Shark.getInstance()
185                                .getAdminInterface()
186                                .getAdminMisc()
187                                .getAssignmentActivityId(processInstanceId,
188                                                         workitemId));
189       }
190       return ret;
191    }
192
193    /**
194     * @param remoteInstanceUri
195     * @param processInstanceId
196     * @param workitemId
197     * @param requestedNewState
198     * @throws InteroperabilityException
199     */

200    protected void changeState(String JavaDoc remoteInstanceUri,
201                               String JavaDoc processInstanceId,
202                               String JavaDoc workitemId,
203                               String JavaDoc requestedNewState) throws InteroperabilityException {
204       try {
205          URI _me = getURI(true, processInstanceId, workitemId);
206          ChangeStateRsHolder response = new ChangeStateRsHolder();
207          locator.getasapInstanceBinding(new java.net.URL JavaDoc(remoteInstanceUri))
208             .changeState(new Request(_me,
209                                      new URI(remoteInstanceUri),
210                                      YesNoIfError.Yes,
211                                      ""),
212                          new ChangeStateRq(AsapBindingUtilitiesImpl.parseState(requestedNewState)),
213                          new ResponseHolder(),
214                          response);
215       } catch (Exception JavaDoc e) {
216          throw new InteroperabilityException(e);
217       }
218
219    }
220
221    protected Map prepareInParams(SharkTransaction st,
222                                  String JavaDoc processInstanceId,
223                                  String JavaDoc workitemId,
224                                  Map parameters) throws BaseException,
225                                                 InteroperabilityException {
226       AdminMisc a = Shark.getInstance().getAdminInterface().getAdminMisc();
227       workitemId = org.enhydra.shark.Shark.getInstance()
228          .getAdminInterface()
229          .getAdminMisc()
230          .getAssignmentActivityId(processInstanceId, workitemId);
231
232       String JavaDoc extendedAttributes = a.getActivitiesExtendedAttributeValue(st,
233                                                                         processInstanceId,
234                                                                         workitemId,
235                                                                         EXTENDED_ATTRIBUTE_NAME_PM);
236       Map m = prepParamsMapping(extendedAttributes);
237       for (Iterator it = m.keySet().iterator(); it.hasNext();) {
238          String JavaDoc localName = (String JavaDoc) it.next();
239          String JavaDoc remoteName = (String JavaDoc) m.get(localName);
240          if (parameters.containsKey(localName)) {
241             Object JavaDoc localValue = parameters.remove(localName);
242             parameters.put(remoteName, localValue);
243          }
244       }
245       System.err.println("###"
246                          + processInstanceId + "\n#" + workitemId + "\n:"
247                          + extendedAttributes + ":");
248       return parameters;
249    }
250
251    /**
252     * @param extendedAttributes
253     * @return @throws InteroperabilityException
254     */

255    private Map prepParamsMapping(String JavaDoc extendedAttributes) throws InteroperabilityException {
256       Map ret = new HashMap();
257       String JavaDoc[] arr = MiscUtilities.tokenize(extendedAttributes, ";");
258       for (int i = 0; i < arr.length; i++) {
259          int index = arr[i].indexOf(',');
260          if (0 > index) throw new InteroperabilityException("wrong contents of extended attribute "
261                                                             + EXTENDED_ATTRIBUTE_NAME_PM
262                                                             + "=" + arr[i]);
263          String JavaDoc localName = arr[i].substring(0, index).trim();
264          String JavaDoc remoteName = arr[i].substring(1 + index).trim();
265          ret.put(remoteName, localName);
266       }
267       return ret;
268    }
269
270    public Map parseOutParams(SharkTransaction st,
271                              String JavaDoc processInstanceId,
272                              String JavaDoc workitemId,
273                              Map parameters,
274                              Map cSig) throws InteroperabilityException {
275       String JavaDoc extendedAttributes = "";
276       HashMap ret = new HashMap();
277       try {
278          AdminMisc a = Shark.getInstance().getAdminInterface().getAdminMisc();
279          workitemId = org.enhydra.shark.Shark.getInstance()
280             .getAdminInterface()
281             .getAdminMisc()
282             .getAssignmentActivityId(processInstanceId, workitemId);
283
284          extendedAttributes = a.getActivitiesExtendedAttributeValue(st,
285                                                                     processInstanceId,
286                                                                     workitemId,
287                                                                     EXTENDED_ATTRIBUTE_NAME_PM);
288
289       } catch (BaseException e) {
290          throw new InteroperabilityException(e);
291       }
292       Map m = prepParamsMapping(extendedAttributes);
293       for (Iterator it = parameters.keySet().iterator(); it.hasNext();) {
294          String JavaDoc remoteName = it.next().toString();
295          String JavaDoc localName = (String JavaDoc) m.get(remoteName);
296          if (null == localName) {
297             if (!cSig.containsKey(remoteName)) continue;
298             localName = remoteName;
299          }
300          String JavaDoc stringRep = parameters.get(remoteName).toString();
301          String JavaDoc localType = (String JavaDoc) cSig.get(localName);
302          Object JavaDoc localValue = null;
303          if (!"java.lang.String".equals(localType)) {
304             if (null == stringRep) {
305                stringRep = "0";
306             }
307             if (null != localType) {
308                if (localType.equals("java.lang.Long")) {
309                   localValue = new Long JavaDoc(stringRep);
310                } else if (localType.equals("java.lang.Boolean")) {
311                   localValue = new Boolean JavaDoc(stringRep);
312                } else if (localType.equals("java.lang.Double")) {
313                   localValue = new Double JavaDoc(stringRep);
314                } else if (localType.equals("java.util.Date")) {
315                   try {
316                      DateDeserializer a = new DateDeserializer(Class.forName(localType),
317                                                                new javax.xml.namespace.QName JavaDoc("http://www.w3.org/2001/XMLSchema",
318                                                                                              "date"));
319                      localValue = a.makeValue(stringRep);
320                      // localValue = DateFormat.getDateInstance()
321
// .parse(stringRep);
322
} catch (Exception JavaDoc _) {
323                      localValue = new Date(0L);
324                   }
325                }
326             }
327          } else {
328             localValue = stringRep;
329             if (null == stringRep) localValue = "";
330          }
331          ret.put(localName, localValue);
332       }
333       // for (Iterator it = cSig.keySet().iterator(); it.hasNext();) {
334
// String localName = (String) it.next();
335
// String localType = (String) cSig.get(localName);
336
// if (!"java.lang.String".equals(localType)) {
337
// Object localValue = parameters.get(localName);
338
// if (null == localValue) {
339
// localValue = "0";
340
// }
341
// if (null != localType) {
342
// if (localType.equals("java.lang.Long")) {
343
// localValue = new Long((String)localValue);
344
// } else if (localType.equals("java.lang.Boolean")) {
345
// localValue = new Boolean((String)localValue);
346
// } else if (localType.equals("java.lang.Double")) {
347
// localValue = new Double((String)localValue);
348
// } else if (localType.equals("java.util.Date")) {
349
// try {
350
// localValue =
351
// DateFormat.getDateInstance().parse((String)localValue);
352
// } catch (Exception _) {
353
// localValue = new Date(0L);
354
// }
355
// }
356
// }
357
// parameters.put(localName, localValue);
358
// }
359
// }
360
System.err.println("###"
361                          + processInstanceId + "\n#" + workitemId + "\n:"
362                          + ret + ":");
363       return ret;
364    }
365 }
366
Popular Tags