1 2 package org.enhydra.shark.interoperability; 3 4 import java.net.InetAddress ; 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 31 public class WfXMLInteroperabilityImpl implements WfEngineInteroperability { 32 33 protected SharkWebServiceLocator locator; 34 35 protected String _meh; 36 37 protected String namePrefix; 38 39 protected String description; 40 41 protected static String 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 PARAM_HOST = "Interoperability.Host"; 48 49 protected static final String PARAM_PORT = "Interoperability.Port"; 50 51 protected static final String PARAM_DEFAULT_PORT = "8080"; 52 53 public static final String EXTENDED_ATTRIBUTE_NAME_PM = "ParamMapping"; 54 public static final String 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 e) { 75 throw new RootException(e); 76 } 77 } 78 79 public String start(SharkTransaction st, 80 String remoteEngine, 81 String processInstanceId, 82 String 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 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 schemaLocation = a.getActivitiesExtendedAttributeValue(st, 104 processInstanceId, 105 workitemId, 106 EXTENDED_ATTRIBUTE_NAME_SL); 107 108 locator.getwfxmlFactoryBinding(new java.net.URL (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 e) { 124 throw new InteroperabilityException(e); 125 } 126 } 127 128 public void suspend(SharkTransaction st, 129 String remoteInstanceUri, 130 String processInstanceId, 131 String 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 remoteInstanceUri, 140 String processInstanceId, 141 String 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 remoteInstanceUri, 150 String processInstanceId, 151 String 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 remoteInstanceUri, 160 String processInstanceId, 161 String workitemId) throws InteroperabilityException { 162 changeState(remoteInstanceUri, 163 processInstanceId, 164 workitemId, 165 SharkConstants.STATE_CLOSED_ABORTED); 166 } 167 168 175 protected URI getURI(boolean isSynchronized, 176 String processInstanceId, 177 String 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 200 protected void changeState(String remoteInstanceUri, 201 String processInstanceId, 202 String workitemId, 203 String requestedNewState) throws InteroperabilityException { 204 try { 205 URI _me = getURI(true, processInstanceId, workitemId); 206 ChangeStateRsHolder response = new ChangeStateRsHolder(); 207 locator.getasapInstanceBinding(new java.net.URL (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 e) { 216 throw new InteroperabilityException(e); 217 } 218 219 } 220 221 protected Map prepareInParams(SharkTransaction st, 222 String processInstanceId, 223 String 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 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 localName = (String ) it.next(); 239 String remoteName = (String ) m.get(localName); 240 if (parameters.containsKey(localName)) { 241 Object 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 255 private Map prepParamsMapping(String extendedAttributes) throws InteroperabilityException { 256 Map ret = new HashMap(); 257 String [] 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 localName = arr[i].substring(0, index).trim(); 264 String 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 processInstanceId, 272 String workitemId, 273 Map parameters, 274 Map cSig) throws InteroperabilityException { 275 String 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 remoteName = it.next().toString(); 295 String localName = (String ) m.get(remoteName); 296 if (null == localName) { 297 if (!cSig.containsKey(remoteName)) continue; 298 localName = remoteName; 299 } 300 String stringRep = parameters.get(remoteName).toString(); 301 String localType = (String ) cSig.get(localName); 302 Object 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 (stringRep); 310 } else if (localType.equals("java.lang.Boolean")) { 311 localValue = new Boolean (stringRep); 312 } else if (localType.equals("java.lang.Double")) { 313 localValue = new Double (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 ("http://www.w3.org/2001/XMLSchema", 318 "date")); 319 localValue = a.makeValue(stringRep); 320 } catch (Exception _) { 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 System.err.println("###" 361 + processInstanceId + "\n#" + workitemId + "\n:" 362 + ret + ":"); 363 return ret; 364 } 365 } 366 | Popular Tags |