1 25 package org.ofbiz.shark.requester; 26 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 import org.ofbiz.base.util.Debug; 31 import org.ofbiz.entity.GenericValue; 32 import org.ofbiz.service.DispatchContext; 33 import org.ofbiz.service.GenericServiceException; 34 import org.ofbiz.service.LocalDispatcher; 35 import org.ofbiz.service.ModelService; 36 import org.ofbiz.shark.container.SharkContainer; 37 38 import org.enhydra.shark.api.SharkTransaction; 39 import org.enhydra.shark.api.client.wfbase.BaseException; 40 import org.enhydra.shark.api.client.wfmodel.InvalidPerformer; 41 import org.enhydra.shark.api.client.wfmodel.SourceNotAvailable; 42 import org.enhydra.shark.api.client.wfmodel.WfEventAudit; 43 44 50 public class ServiceRequester extends AbstractRequester { 51 52 public static final String module = ServiceRequester.class.getName(); 53 public static final int ASYNC = 0; 54 public static final int SYNC = 1; 55 56 protected Map initialContext = new HashMap (); 57 protected String serviceName = null; 58 protected String eventType = null; 59 protected int serviceMode = 1; 60 61 public ServiceRequester(GenericValue userLogin, String eventType) { 63 super(userLogin); 64 this.setEventType(eventType); 65 } 66 67 public ServiceRequester(GenericValue userLogin) { 68 super(userLogin); 69 } 70 71 75 public void receive_event(WfEventAudit event) throws BaseException, InvalidPerformer { 76 if (this.getEventType() == null || this.getEventType().equals(event.event_type())) { 77 try { 78 this.run(event); 79 } catch (GenericServiceException e) { 80 Debug.logError(e, module); 81 throw new BaseException(e); 82 } 83 } 84 } 85 86 public void receive_event(SharkTransaction trans, WfEventAudit event) throws BaseException, InvalidPerformer { 87 receive_event(event); 88 } 89 90 94 95 public void setEventType(String eventType) { 96 this.eventType = eventType; 97 } 98 99 public String getEventType() { 100 return this.eventType; 101 } 102 103 public void setService(String serviceName, int serviceMode) { 104 this.serviceName = serviceName; 105 this.serviceMode = serviceMode; 106 } 107 108 public void setService(String serviceName) { 109 this.setService(serviceName, ServiceRequester.ASYNC); 110 } 111 112 public String getServiceName() { 113 return this.serviceName; 114 } 115 116 public int getServiceMode() { 117 return this.serviceMode; 118 } 119 120 public void setInitalContextValues(Map initialContext) { 121 this.initialContext = new HashMap (initialContext); 122 } 123 124 private void run(WfEventAudit event) throws GenericServiceException { 125 LocalDispatcher dispatcher = SharkContainer.getDispatcher(); 127 if (dispatcher == null) { 128 throw new GenericServiceException("Cannot run service with null dispatcher"); 129 } 130 131 Map serviceContext = makeServiceContext(event, dispatcher); 133 134 String serviceName = this.getServiceName(); 136 if (serviceName != null) { 137 int mode = this.getServiceMode(); 138 if (mode == ServiceRequester.SYNC) { 139 dispatcher.runSyncIgnore(serviceName, serviceContext); 140 } else { 141 dispatcher.runAsync(serviceName, serviceContext); 142 } 143 } else { 144 Debug.logWarning("ServiceRequester -> receive_event() called with no service defined!", module); 145 } 146 } 147 148 private Map makeServiceContext(WfEventAudit event, LocalDispatcher dispatcher) throws GenericServiceException { 149 DispatchContext dctx = dispatcher.getDispatchContext(); 150 try { 151 return dctx.getModelService(this.getServiceName()).makeValid(getWRD(event, initialContext), ModelService.IN_PARAM); 152 } catch (BaseException e) { 153 throw new GenericServiceException(e); 154 } 155 } 156 } 157 | Popular Tags |