1 25 package org.ofbiz.shark.service; 26 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import javax.transaction.Transaction ; 33 34 import org.ofbiz.base.util.StringUtil; 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.entity.GenericValue; 37 import org.ofbiz.entity.transaction.TransactionUtil; 38 import org.ofbiz.entity.transaction.GenericTransactionException; 39 import org.ofbiz.service.GenericRequester; 40 import org.ofbiz.service.GenericResultWaiter; 41 import org.ofbiz.service.GenericServiceException; 42 import org.ofbiz.service.ModelService; 43 import org.ofbiz.service.ServiceDispatcher; 44 import org.ofbiz.service.engine.AbstractEngine; 45 import org.ofbiz.shark.container.SharkContainer; 46 import org.ofbiz.shark.requester.SimpleRequester; 47 48 import org.enhydra.shark.api.client.wfbase.BaseException; 49 import org.enhydra.shark.api.client.wfmodel.*; 50 import org.enhydra.shark.api.client.wfservice.AdminInterface; 51 import org.enhydra.shark.api.client.wfservice.ConnectFailed; 52 import org.enhydra.shark.api.client.wfservice.ExecutionAdministration; 53 import org.enhydra.shark.api.client.wfservice.NotConnected; 54 55 62 public class SharkServiceEngine extends AbstractEngine { 63 64 public static final String module = SharkServiceEngine.class.getName(); 65 66 public SharkServiceEngine(ServiceDispatcher dispatcher) { 67 super(dispatcher); 68 } 69 70 80 public Map runSync(String localName, ModelService modelService, Map context) throws GenericServiceException { 81 GenericResultWaiter waiter = new GenericResultWaiter(); 82 this.runAsync(localName, modelService, context, waiter, false); 83 return waiter.waitForResult(); 84 } 85 86 95 public void runSyncIgnore(String localName, ModelService modelService, Map context) throws GenericServiceException { 96 this.runSync(localName, modelService, context); 97 } 98 99 110 public void runAsync(String localName, ModelService modelService, Map context, GenericRequester requester, boolean persist) throws GenericServiceException { 111 this.runWf(modelService, context, requester); 112 } 113 114 124 public void runAsync(String localName, ModelService modelService, Map context, boolean persist) throws GenericServiceException { 125 this.runAsync(localName, modelService, context, new GenericResultWaiter(), persist); 126 } 127 128 private GenericRequester runWf(ModelService model, Map context, GenericRequester waiter) throws GenericServiceException { 129 GenericValue userLogin = (GenericValue) context.get("userLogin"); 130 if (userLogin == null) { 131 userLogin = SharkContainer.getAdminUser(); 132 } 133 134 AdminInterface admin = SharkContainer.getAdminInterface(); 135 ExecutionAdministration exec = admin.getExecutionAdministration(); 136 137 boolean beganTrans = false; 138 boolean hasError = false; 139 Transaction trans = null; 140 141 try { 142 beganTrans = TransactionUtil.begin(); 143 if (!beganTrans) { 144 trans = TransactionUtil.suspend(); 145 beganTrans = TransactionUtil.begin(); 146 } 147 148 try { 149 try { 151 exec.connect(userLogin.getString("userLoginId"), userLogin.getString("currentPassword"), null, null); 152 } catch (BaseException e) { 153 throw new GenericServiceException(e); 154 } catch (ConnectFailed e) { 155 throw new GenericServiceException(e); 156 } 157 158 try { 159 WfRequester req = new SimpleRequester(userLogin, model, waiter); 161 WfProcessMgr mgr = null; 162 String location = this.getLocation(model); 163 String version = null; 164 165 if (location.indexOf("::") != -1) { 167 List splitList = StringUtil.split(location, "::"); 168 location = (String ) splitList.get(0); 169 version = (String ) splitList.get(1); 170 } 171 172 try { 174 if (version == null) { 175 mgr = exec.getProcessMgr(location, model.invoke); 176 } else { 177 mgr = exec.getProcessMgr(location, version, model.invoke); 178 } 179 } catch (BaseException e) { 180 throw new GenericServiceException(e); 181 } catch (NotConnected e) { 182 throw new GenericServiceException(e); 183 } 184 185 if (mgr == null) { 187 throw new GenericServiceException("Unable to obtain Process Manager for : " + this.getLocation(model) + " / " + model.invoke); 188 } 189 190 WfProcess proc = null; 192 try { 193 proc = mgr.create_process(req); 194 } catch (BaseException e) { 195 throw new GenericServiceException(e); 196 } catch (NotEnabled e) { 197 throw new GenericServiceException(e); 198 } catch (InvalidRequester e) { 199 throw new GenericServiceException(e); 200 } catch (RequesterRequired e) { 201 throw new GenericServiceException(e); 202 } 203 204 Map contextSig = null; 205 try { 206 contextSig = mgr.context_signature(); 207 } catch (BaseException e) { 208 throw new GenericServiceException(e); 209 } 210 211 if (contextSig != null) { 212 Iterator sigKeys = contextSig.keySet().iterator(); 213 Map formalParams = new HashMap (); 214 while (sigKeys.hasNext()) { 215 String key = (String ) sigKeys.next(); 216 formalParams.put(key, context.get(key)); 217 } 218 219 try { 221 proc.set_process_context(formalParams); 222 } catch (BaseException e) { 223 throw new GenericServiceException(e); 224 } catch (InvalidData e) { 225 throw new GenericServiceException(e); 226 } catch (UpdateNotAllowed e) { 227 throw new GenericServiceException(e); 228 } 229 } 230 231 try { 233 proc.start(); 234 } catch (BaseException e) { 235 throw new GenericServiceException(e); 236 } catch (CannotStart e) { 237 throw new GenericServiceException(e); 238 } catch (AlreadyRunning e) { 239 throw new GenericServiceException(e); 240 } 241 } catch (GenericServiceException e) { 242 throw e; 243 } finally { 244 try { 246 exec.disconnect(); 247 } catch (NotConnected e) { 248 throw new GenericServiceException(e); 249 } catch (BaseException e) { 250 throw new GenericServiceException(e); 251 } 252 } 253 } catch (GenericServiceException e) { 254 hasError = true; 255 throw e; 256 } finally { 257 if (hasError) { 258 try { 259 TransactionUtil.rollback(beganTrans, "Transaction rollback from Shark", null); 260 } catch (GenericTransactionException e) { 261 Debug.logError(e, "Could not rollback transaction", module); 262 } 263 } else { 264 try { 265 TransactionUtil.commit(beganTrans); 266 } catch (GenericTransactionException e) { 267 Debug.logError(e, "Could not commit transaction", module); 268 throw new GenericServiceException("Commit transaction failed"); 269 } 270 } 271 } 272 } catch (GenericTransactionException e) { 273 throw new GenericServiceException(e); 274 } finally { 275 if (trans != null) { 276 try { 277 TransactionUtil.resume(trans); 278 } catch (GenericTransactionException e) { 279 throw new GenericServiceException(e); 280 } 281 } 282 } 283 284 return waiter; 285 } 286 } 287 | Popular Tags |