001: /*
002: * $Id: GenericDispatcher.java,v 1.5 2004/02/19 18:52:35 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.service;
026:
027: import java.util.Map;
028:
029: import org.ofbiz.entity.GenericDelegator;
030: import org.ofbiz.base.util.Debug;
031:
032: /**
033: * Generic Services Local Dispatcher
034: *
035: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
036: * @version $Revision: 1.5 $
037: * @since 2.0
038: */
039: public class GenericDispatcher extends GenericAbstractDispatcher {
040:
041: public static final String module = GenericDispatcher.class
042: .getName();
043:
044: public GenericDispatcher() {
045: }
046:
047: public GenericDispatcher(String name, GenericDelegator delegator) {
048: this (name, delegator, null);
049: }
050:
051: public GenericDispatcher(String name, GenericDelegator delegator,
052: ClassLoader loader) {
053: if (loader == null) {
054: try {
055: loader = Thread.currentThread().getContextClassLoader();
056: } catch (SecurityException e) {
057: loader = this .getClass().getClassLoader();
058: }
059: }
060: DispatchContext dc = new DispatchContext(name, null, loader,
061: null);
062: init(name, delegator, dc);
063: }
064:
065: public GenericDispatcher(DispatchContext ctx,
066: GenericDelegator delegator) {
067: init(ctx.getName(), delegator, ctx);
068: }
069:
070: public GenericDispatcher(DispatchContext ctx,
071: ServiceDispatcher dispatcher) {
072: this .dispatcher = dispatcher;
073: this .ctx = ctx;
074: this .name = ctx.getName();
075:
076: ctx.setDispatcher(this );
077: ctx.loadReaders();
078: dispatcher.register(name, ctx);
079: }
080:
081: protected void init(String name, GenericDelegator delegator,
082: DispatchContext ctx) {
083: if (name == null || name.length() == 0)
084: throw new IllegalArgumentException(
085: "The name of a LocalDispatcher cannot be a null or empty String");
086:
087: this .name = name;
088: this .ctx = ctx;
089: this .dispatcher = ServiceDispatcher.getInstance(name, ctx,
090: delegator);
091:
092: ctx.setDispatcher(this );
093: ctx.loadReaders();
094: if (Debug.infoOn())
095: Debug.logInfo(
096: "[LocalDispatcher] : Created Dispatcher for: "
097: + name, module);
098: }
099:
100: public static LocalDispatcher getLocalDispatcher(String name,
101: GenericDelegator delegator) throws GenericServiceException {
102: ServiceDispatcher sd = ServiceDispatcher.getInstance(name,
103: delegator);
104: LocalDispatcher this Dispatcher = null;
105: if (sd != null) {
106: this Dispatcher = sd.getLocalDispatcher(name);
107: }
108: if (this Dispatcher == null) {
109: this Dispatcher = new GenericDispatcher(name, delegator);
110: }
111:
112: if (this Dispatcher != null) {
113: return this Dispatcher;
114: } else {
115: throw new GenericServiceException(
116: "Unable to load dispatcher for name : " + name
117: + " with delegator : "
118: + delegator.getDelegatorName());
119: }
120: }
121:
122: /**
123: * @see org.ofbiz.service.LocalDispatcher#runSync(java.lang.String, java.util.Map)
124: */
125: public Map runSync(String serviceName, Map context)
126: throws ServiceValidationException, GenericServiceException {
127: ModelService service = ctx.getModelService(serviceName);
128: return dispatcher.runSync(this .name, service, context);
129: }
130:
131: /**
132: * @see org.ofbiz.service.LocalDispatcher#runSync(java.lang.String, java.util.Map, int, boolean)
133: */
134: public Map runSync(String serviceName, Map context,
135: int transactionTimeout, boolean requireNewTransaction)
136: throws ServiceAuthException, ServiceValidationException,
137: GenericServiceException {
138: ModelService service = ctx.getModelService(serviceName);
139: // clone the model service for updates
140: ModelService cloned = new ModelService(service);
141: cloned.requireNewTransaction = requireNewTransaction;
142: cloned.transactionTimeout = transactionTimeout;
143: return dispatcher.runSync(this .name, cloned, context);
144: }
145:
146: /**
147: * @see org.ofbiz.service.LocalDispatcher#runSyncIgnore(java.lang.String, java.util.Map)
148: */
149: public void runSyncIgnore(String serviceName, Map context)
150: throws GenericServiceException {
151: ModelService service = ctx.getModelService(serviceName);
152: dispatcher.runSyncIgnore(this .name, service, context);
153: }
154:
155: /**
156: * @see org.ofbiz.service.LocalDispatcher#runSyncIgnore(java.lang.String, java.util.Map)
157: */
158: public void runSyncIgnore(String serviceName, Map context,
159: int transactionTimeout, boolean requireNewTransaction)
160: throws ServiceAuthException, ServiceValidationException,
161: GenericServiceException {
162: ModelService service = ctx.getModelService(serviceName);
163: // clone the model service for updates
164: ModelService cloned = new ModelService(service);
165: cloned.requireNewTransaction = requireNewTransaction;
166: cloned.transactionTimeout = transactionTimeout;
167: dispatcher.runSyncIgnore(this .name, cloned, context);
168: }
169:
170: /**
171: * @see org.ofbiz.service.LocalDispatcher#runAsync(java.lang.String, java.util.Map, org.ofbiz.service.GenericRequester, boolean, int, boolean)
172: */
173: public void runAsync(String serviceName, Map context,
174: GenericRequester requester, boolean persist,
175: int transactionTimeout, boolean requireNewTransaction)
176: throws ServiceAuthException, ServiceValidationException,
177: GenericServiceException {
178: ModelService service = ctx.getModelService(serviceName);
179: // clone the model service for updates
180: ModelService cloned = new ModelService(service);
181: cloned.requireNewTransaction = requireNewTransaction;
182: cloned.transactionTimeout = transactionTimeout;
183: dispatcher.runAsync(this .name, cloned, context, requester,
184: persist);
185: }
186:
187: /**
188: * @see org.ofbiz.service.LocalDispatcher#runAsync(java.lang.String, java.util.Map, org.ofbiz.service.GenericRequester, boolean)
189: */
190: public void runAsync(String serviceName, Map context,
191: GenericRequester requester, boolean persist)
192: throws ServiceAuthException, ServiceValidationException,
193: GenericServiceException {
194: ModelService service = ctx.getModelService(serviceName);
195: dispatcher.runAsync(this .name, service, context, requester,
196: persist);
197: }
198:
199: /**
200: * @see org.ofbiz.service.LocalDispatcher#runAsync(java.lang.String, java.util.Map, org.ofbiz.service.GenericRequester)
201: */
202: public void runAsync(String serviceName, Map context,
203: GenericRequester requester) throws ServiceAuthException,
204: ServiceValidationException, GenericServiceException {
205: runAsync(serviceName, context, requester, true);
206: }
207:
208: /**
209: * @see org.ofbiz.service.LocalDispatcher#runAsync(java.lang.String, java.util.Map, boolean)
210: */
211: public void runAsync(String serviceName, Map context,
212: boolean persist) throws ServiceAuthException,
213: ServiceValidationException, GenericServiceException {
214: ModelService service = ctx.getModelService(serviceName);
215: dispatcher.runAsync(this .name, service, context, persist);
216: }
217:
218: /**
219: * @see org.ofbiz.service.LocalDispatcher#runAsync(java.lang.String, java.util.Map)
220: */
221: public void runAsync(String serviceName, Map context)
222: throws ServiceAuthException, ServiceValidationException,
223: GenericServiceException {
224: runAsync(serviceName, context, true);
225: }
226:
227: /**
228: * @see org.ofbiz.service.LocalDispatcher#runAsyncWait(java.lang.String, java.util.Map, boolean)
229: */
230: public GenericResultWaiter runAsyncWait(String serviceName,
231: Map context, boolean persist) throws ServiceAuthException,
232: ServiceValidationException, GenericServiceException {
233: GenericResultWaiter waiter = new GenericResultWaiter();
234: this .runAsync(serviceName, context, waiter, persist);
235: return waiter;
236: }
237:
238: /**
239: * @see org.ofbiz.service.LocalDispatcher#runAsyncWait(java.lang.String, java.util.Map)
240: */
241: public GenericResultWaiter runAsyncWait(String serviceName,
242: Map context) throws ServiceAuthException,
243: ServiceValidationException, GenericServiceException {
244: return runAsyncWait(serviceName, context, true);
245: }
246: }
|