001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.service;
019:
020: import java.util.Map;
021:
022: import javolution.util.FastMap;
023:
024: import org.ofbiz.entity.GenericDelegator;
025: import org.ofbiz.entity.GenericEntityException;
026: import org.ofbiz.base.util.Debug;
027:
028: /**
029: * Generic Services Local Dispatcher
030: */
031: public class GenericDispatcher extends GenericAbstractDispatcher {
032:
033: public static final String module = GenericDispatcher.class
034: .getName();
035:
036: protected static Map dispatcherCache = FastMap.newInstance();
037:
038: public static LocalDispatcher getLocalDispatcher(
039: String dispatcherName, GenericDelegator delegator) {
040: return getLocalDispatcher(dispatcherName, delegator, null, null);
041: }
042:
043: public static LocalDispatcher getLocalDispatcher(
044: String dispatcherName, GenericDelegator delegator,
045: ClassLoader loader) {
046: return getLocalDispatcher(dispatcherName, delegator, loader,
047: null);
048: }
049:
050: public static LocalDispatcher getLocalDispatcher(
051: String dispatcherName, GenericDelegator delegator,
052: ClassLoader loader, ServiceDispatcher serviceDispatcher) {
053: if (dispatcherName == null) {
054: dispatcherName = "default";
055: Debug
056: .logWarning(
057: "Got a getGenericDelegator call with a null dispatcherName, assuming default for the name.",
058: module);
059: }
060: LocalDispatcher dispatcher = (LocalDispatcher) dispatcherCache
061: .get(dispatcherName);
062:
063: if (dispatcher == null) {
064: synchronized (GenericDelegator.class) {
065: // must check if null again as one of the blocked threads can still enter
066: dispatcher = (GenericDispatcher) dispatcherCache
067: .get(dispatcherName);
068: if (dispatcher == null) {
069: if (Debug.infoOn())
070: Debug.logInfo("Creating new dispatcher ["
071: + dispatcherName + "] ("
072: + Thread.currentThread().getName()
073: + ")", module);
074: //Debug.logInfo(new Exception(), "Showing stack where new dispatcher is being created...", module);
075:
076: if (delegator == null && serviceDispatcher != null) {
077: delegator = serviceDispatcher.getDelegator();
078: }
079:
080: if (loader == null) {
081: loader = GenericDispatcher.class
082: .getClassLoader();
083: }
084:
085: ServiceDispatcher sd = serviceDispatcher != null ? serviceDispatcher
086: : ServiceDispatcher.getInstance(
087: dispatcherName, delegator);
088: LocalDispatcher this Dispatcher = null;
089: if (sd != null) {
090: dispatcher = sd
091: .getLocalDispatcher(dispatcherName);
092: }
093: if (this Dispatcher == null) {
094: dispatcher = new GenericDispatcher(
095: dispatcherName, delegator, loader, sd);
096: }
097:
098: if (dispatcher != null) {
099: dispatcherCache.put(dispatcherName, dispatcher);
100: } else {
101: Debug
102: .logError(
103: "Could not create dispatcher with name "
104: + dispatcherName
105: + ", constructor failed (got null value) not sure why/how.",
106: module);
107: }
108: }
109: }
110: }
111: return dispatcher;
112: }
113:
114: /** special method to obtain a new 'unique' reference with a variation on parameters */
115: public static LocalDispatcher newInstance(String name,
116: GenericDelegator delegator, boolean enableJM,
117: boolean enableJMS, boolean enableSvcs)
118: throws GenericServiceException {
119: ServiceDispatcher sd = new ServiceDispatcher(delegator,
120: enableJM, enableJMS, enableSvcs);
121: ClassLoader loader = null;
122: try {
123: loader = Thread.currentThread().getContextClassLoader();
124: } catch (SecurityException e) {
125: loader = GenericDispatcher.class.getClassLoader();
126: }
127: return new GenericDispatcher(name, delegator, loader, sd);
128: }
129:
130: protected GenericDispatcher() {
131: }
132:
133: protected GenericDispatcher(String name,
134: GenericDelegator delegator, ClassLoader loader,
135: ServiceDispatcher serviceDispatcher) {
136: if (serviceDispatcher != null) {
137: this .dispatcher = serviceDispatcher;
138: }
139: if (loader == null) {
140: try {
141: loader = Thread.currentThread().getContextClassLoader();
142: } catch (SecurityException e) {
143: loader = this .getClass().getClassLoader();
144: }
145: }
146: DispatchContext dc = new DispatchContext(name, null, loader,
147: null);
148: init(name, delegator, dc);
149: }
150:
151: protected void init(String name, GenericDelegator delegator,
152: DispatchContext ctx) {
153: if (name == null || name.length() == 0)
154: throw new IllegalArgumentException(
155: "The name of a LocalDispatcher cannot be a null or empty String");
156:
157: this .name = name;
158: this .ctx = ctx;
159: this .dispatcher = ServiceDispatcher.getInstance(name, ctx,
160: delegator);
161:
162: ctx.setDispatcher(this );
163: ctx.loadReaders();
164: if (Debug.infoOn())
165: Debug.logInfo(
166: "[LocalDispatcher] : Created Dispatcher for: "
167: + name, module);
168: }
169:
170: /**
171: * @see org.ofbiz.service.LocalDispatcher#runSync(java.lang.String, java.util.Map)
172: */
173: public Map runSync(String serviceName, Map context)
174: throws ServiceValidationException, GenericServiceException {
175: ModelService service = ctx.getModelService(serviceName);
176: return dispatcher.runSync(this .name, service, context);
177: }
178:
179: /**
180: * @see org.ofbiz.service.LocalDispatcher#runSync(java.lang.String, java.util.Map, int, boolean)
181: */
182: public Map runSync(String serviceName, Map context,
183: int transactionTimeout, boolean requireNewTransaction)
184: throws ServiceAuthException, ServiceValidationException,
185: GenericServiceException {
186: ModelService service = ctx.getModelService(serviceName);
187: // clone the model service for updates
188: ModelService cloned = new ModelService(service);
189: cloned.requireNewTransaction = requireNewTransaction;
190: if (transactionTimeout != -1) {
191: cloned.transactionTimeout = transactionTimeout;
192: }
193: return dispatcher.runSync(this .name, cloned, context);
194: }
195:
196: /**
197: * @see org.ofbiz.service.LocalDispatcher#runSyncIgnore(java.lang.String, java.util.Map)
198: */
199: public void runSyncIgnore(String serviceName, Map context)
200: throws GenericServiceException {
201: ModelService service = ctx.getModelService(serviceName);
202: dispatcher.runSyncIgnore(this .name, service, context);
203: }
204:
205: /**
206: * @see org.ofbiz.service.LocalDispatcher#runSyncIgnore(java.lang.String, java.util.Map)
207: */
208: public void runSyncIgnore(String serviceName, Map context,
209: int transactionTimeout, boolean requireNewTransaction)
210: throws ServiceAuthException, ServiceValidationException,
211: GenericServiceException {
212: ModelService service = ctx.getModelService(serviceName);
213: // clone the model service for updates
214: ModelService cloned = new ModelService(service);
215: cloned.requireNewTransaction = requireNewTransaction;
216: if (transactionTimeout != -1) {
217: cloned.transactionTimeout = transactionTimeout;
218: }
219: dispatcher.runSyncIgnore(this .name, cloned, context);
220: }
221:
222: /**
223: * @see org.ofbiz.service.LocalDispatcher#runAsync(java.lang.String, java.util.Map, org.ofbiz.service.GenericRequester, boolean, int, boolean)
224: */
225: public void runAsync(String serviceName, Map context,
226: GenericRequester requester, boolean persist,
227: int transactionTimeout, boolean requireNewTransaction)
228: throws ServiceAuthException, ServiceValidationException,
229: GenericServiceException {
230: ModelService service = ctx.getModelService(serviceName);
231: // clone the model service for updates
232: ModelService cloned = new ModelService(service);
233: cloned.requireNewTransaction = requireNewTransaction;
234: if (transactionTimeout != -1) {
235: cloned.transactionTimeout = transactionTimeout;
236: }
237: dispatcher.runAsync(this .name, cloned, context, requester,
238: persist);
239: }
240:
241: /**
242: * @see org.ofbiz.service.LocalDispatcher#runAsync(java.lang.String, java.util.Map, org.ofbiz.service.GenericRequester, boolean)
243: */
244: public void runAsync(String serviceName, Map context,
245: GenericRequester requester, boolean persist)
246: throws ServiceAuthException, ServiceValidationException,
247: GenericServiceException {
248: ModelService service = ctx.getModelService(serviceName);
249: dispatcher.runAsync(this .name, service, context, requester,
250: persist);
251: }
252:
253: /**
254: * @see org.ofbiz.service.LocalDispatcher#runAsync(java.lang.String, java.util.Map, org.ofbiz.service.GenericRequester)
255: */
256: public void runAsync(String serviceName, Map context,
257: GenericRequester requester) throws ServiceAuthException,
258: ServiceValidationException, GenericServiceException {
259: runAsync(serviceName, context, requester, true);
260: }
261:
262: /**
263: * @see org.ofbiz.service.LocalDispatcher#runAsync(java.lang.String, java.util.Map, boolean)
264: */
265: public void runAsync(String serviceName, Map context,
266: boolean persist) throws ServiceAuthException,
267: ServiceValidationException, GenericServiceException {
268: ModelService service = ctx.getModelService(serviceName);
269: dispatcher.runAsync(this .name, service, context, persist);
270: }
271:
272: /**
273: * @see org.ofbiz.service.LocalDispatcher#runAsync(java.lang.String, java.util.Map)
274: */
275: public void runAsync(String serviceName, Map context)
276: throws ServiceAuthException, ServiceValidationException,
277: GenericServiceException {
278: runAsync(serviceName, context, true);
279: }
280:
281: /**
282: * @see org.ofbiz.service.LocalDispatcher#runAsyncWait(java.lang.String, java.util.Map, boolean)
283: */
284: public GenericResultWaiter runAsyncWait(String serviceName,
285: Map context, boolean persist) throws ServiceAuthException,
286: ServiceValidationException, GenericServiceException {
287: GenericResultWaiter waiter = new GenericResultWaiter();
288: this .runAsync(serviceName, context, waiter, persist);
289: return waiter;
290: }
291:
292: /**
293: * @see org.ofbiz.service.LocalDispatcher#runAsyncWait(java.lang.String, java.util.Map)
294: */
295: public GenericResultWaiter runAsyncWait(String serviceName,
296: Map context) throws ServiceAuthException,
297: ServiceValidationException, GenericServiceException {
298: return runAsyncWait(serviceName, context, true);
299: }
300: }
|