1 25 package org.ofbiz.service; 26 27 import java.util.Map ; 28 29 import org.ofbiz.entity.GenericDelegator; 30 import org.ofbiz.base.util.Debug; 31 32 39 public class GenericDispatcher extends GenericAbstractDispatcher { 40 41 public static final String module = GenericDispatcher.class.getName(); 42 43 public GenericDispatcher() {} 44 45 public GenericDispatcher(String name, GenericDelegator delegator) { 46 this(name, delegator, null); 47 } 48 49 public GenericDispatcher(String name, GenericDelegator delegator, ClassLoader loader) { 50 if (loader == null) { 51 try { 52 loader = Thread.currentThread().getContextClassLoader(); 53 } catch (SecurityException e) { 54 loader = this.getClass().getClassLoader(); 55 } 56 } 57 DispatchContext dc = new DispatchContext(name, null, loader, null); 58 init(name, delegator, dc); 59 } 60 61 public GenericDispatcher(DispatchContext ctx, GenericDelegator delegator) { 62 init(ctx.getName(), delegator, ctx); 63 } 64 65 public GenericDispatcher(DispatchContext ctx, ServiceDispatcher dispatcher) { 66 this.dispatcher = dispatcher; 67 this.ctx = ctx; 68 this.name = ctx.getName(); 69 70 ctx.setDispatcher(this); 71 ctx.loadReaders(); 72 dispatcher.register(name, ctx); 73 } 74 75 protected void init(String name, GenericDelegator delegator, DispatchContext ctx) { 76 if (name == null || name.length() == 0) 77 throw new IllegalArgumentException ("The name of a LocalDispatcher cannot be a null or empty String"); 78 79 this.name = name; 80 this.ctx = ctx; 81 this.dispatcher = ServiceDispatcher.getInstance(name, ctx, delegator); 82 83 ctx.setDispatcher(this); 84 ctx.loadReaders(); 85 if (Debug.infoOn()) Debug.logInfo("[LocalDispatcher] : Created Dispatcher for: " + name, module); 86 } 87 88 public static LocalDispatcher getLocalDispatcher(String name, GenericDelegator delegator) throws GenericServiceException { 89 ServiceDispatcher sd = ServiceDispatcher.getInstance(name, delegator); 90 LocalDispatcher thisDispatcher = null; 91 if (sd != null) { 92 thisDispatcher = sd.getLocalDispatcher(name); 93 } 94 if (thisDispatcher == null) { 95 thisDispatcher = new GenericDispatcher(name, delegator); 96 } 97 98 if (thisDispatcher != null) { 99 return thisDispatcher; 100 } else { 101 throw new GenericServiceException("Unable to load dispatcher for name : " + name + " with delegator : " + delegator.getDelegatorName()); 102 } 103 } 104 105 public static LocalDispatcher newInstance(String name, GenericDelegator delegator, boolean enableJM, boolean enableJMS, boolean enableSvcs) throws GenericServiceException { 107 ServiceDispatcher sd = new ServiceDispatcher(delegator, enableJM, enableJMS, enableSvcs); 108 ClassLoader loader = null; 109 try { 110 loader = Thread.currentThread().getContextClassLoader(); 111 } catch (SecurityException e) { 112 loader = GenericDispatcher.class.getClassLoader(); 113 } 114 DispatchContext dc = new DispatchContext(name, null, loader, null); 115 return new GenericDispatcher(dc, sd); 116 } 117 118 121 public Map runSync(String serviceName, Map context) throws ServiceValidationException, GenericServiceException { 122 ModelService service = ctx.getModelService(serviceName); 123 return dispatcher.runSync(this.name, service, context); 124 } 125 126 129 public Map runSync(String serviceName, Map context, int transactionTimeout, boolean requireNewTransaction) throws ServiceAuthException, ServiceValidationException, GenericServiceException { 130 ModelService service = ctx.getModelService(serviceName); 131 ModelService cloned = new ModelService(service); 133 cloned.requireNewTransaction = requireNewTransaction; 134 cloned.transactionTimeout = transactionTimeout; 135 return dispatcher.runSync(this.name, cloned, context); 136 } 137 138 141 public void runSyncIgnore(String serviceName, Map context) throws GenericServiceException { 142 ModelService service = ctx.getModelService(serviceName); 143 dispatcher.runSyncIgnore(this.name, service, context); 144 } 145 146 149 public void runSyncIgnore(String serviceName, Map context, int transactionTimeout, boolean requireNewTransaction) throws ServiceAuthException, ServiceValidationException, GenericServiceException { 150 ModelService service = ctx.getModelService(serviceName); 151 ModelService cloned = new ModelService(service); 153 cloned.requireNewTransaction = requireNewTransaction; 154 cloned.transactionTimeout = transactionTimeout; 155 dispatcher.runSyncIgnore(this.name, cloned, context); 156 } 157 158 161 public void runAsync(String serviceName, Map context, GenericRequester requester, boolean persist, int transactionTimeout, boolean requireNewTransaction) throws ServiceAuthException, ServiceValidationException, GenericServiceException { 162 ModelService service = ctx.getModelService(serviceName); 163 ModelService cloned = new ModelService(service); 165 cloned.requireNewTransaction = requireNewTransaction; 166 cloned.transactionTimeout = transactionTimeout; 167 dispatcher.runAsync(this.name, cloned, context, requester, persist); 168 } 169 170 173 public void runAsync(String serviceName, Map context, GenericRequester requester, boolean persist) throws ServiceAuthException, ServiceValidationException, GenericServiceException { 174 ModelService service = ctx.getModelService(serviceName); 175 dispatcher.runAsync(this.name, service, context, requester, persist); 176 } 177 178 181 public void runAsync(String serviceName, Map context, GenericRequester requester) throws ServiceAuthException, ServiceValidationException, GenericServiceException { 182 runAsync(serviceName, context, requester, true); 183 } 184 185 188 public void runAsync(String serviceName, Map context, boolean persist) throws ServiceAuthException, ServiceValidationException, GenericServiceException { 189 ModelService service = ctx.getModelService(serviceName); 190 dispatcher.runAsync(this.name, service, context, persist); 191 } 192 193 196 public void runAsync(String serviceName, Map context) throws ServiceAuthException, ServiceValidationException, GenericServiceException { 197 runAsync(serviceName, context, true); 198 } 199 200 203 public GenericResultWaiter runAsyncWait(String serviceName, Map context, boolean persist) throws ServiceAuthException, ServiceValidationException, GenericServiceException { 204 GenericResultWaiter waiter = new GenericResultWaiter(); 205 this.runAsync(serviceName, context, waiter, persist); 206 return waiter; 207 } 208 209 212 public GenericResultWaiter runAsyncWait(String serviceName, Map context) throws ServiceAuthException, ServiceValidationException, GenericServiceException { 213 return runAsyncWait(serviceName, context, true); 214 } 215 } 216 217 | Popular Tags |