📄 servicedispatcher.java
字号:
/* * $Id: ServiceDispatcher.java 7284 2006-04-12 18:39:42Z jaz $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */package org.ofbiz.service;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;import javax.transaction.Transaction;import javolution.util.FastList;import javolution.util.FastMap;import org.apache.commons.collections.map.LRUMap;import org.w3c.dom.Element;import org.ofbiz.base.config.GenericConfigException;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilTimer;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.base.util.UtilXml;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.transaction.DebugXaResource;import org.ofbiz.entity.transaction.GenericTransactionException;import org.ofbiz.entity.transaction.TransactionUtil;import org.ofbiz.security.Security;import org.ofbiz.security.SecurityConfigurationException;import org.ofbiz.security.SecurityFactory;import org.ofbiz.service.config.ServiceConfigUtil;import org.ofbiz.service.eca.ServiceEcaUtil;import org.ofbiz.service.engine.GenericEngine;import org.ofbiz.service.engine.GenericEngineFactory;import org.ofbiz.service.group.ServiceGroupReader;import org.ofbiz.service.jms.JmsListenerFactory;import org.ofbiz.service.job.JobManager;import org.ofbiz.service.job.JobManagerException;/** * Global Service Dispatcher * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 7284 $ * @since 2.0 */public class ServiceDispatcher { public static final String module = ServiceDispatcher.class.getName(); public static final int lruLogSize = 200; protected static Map runLog = new LRUMap(lruLogSize); protected static Map dispatchers = FastMap.newInstance(); protected static boolean enableJM = true; protected static boolean enableJMS = true; protected static boolean enableSvcs = true; protected GenericDelegator delegator = null; protected GenericEngineFactory factory = null; protected Security security = null; protected Map localContext = null; protected Map callbacks = null; protected JobManager jm = null; protected JmsListenerFactory jlf = null; public ServiceDispatcher(GenericDelegator delegator, boolean enableJM, boolean enableJMS, boolean enableSvcs) { Debug.logInfo("[ServiceDispatcher] : Creating new instance.", module); factory = new GenericEngineFactory(this); ServiceGroupReader.readConfig(); ServiceEcaUtil.readConfig(); this.delegator = delegator; this.localContext = FastMap.newInstance(); this.callbacks = FastMap.newInstance(); if (delegator != null) { try { this.security = SecurityFactory.getInstance(delegator); } catch (SecurityConfigurationException e) { Debug.logError(e, "[ServiceDispatcher.init] : No instance of security imeplemtation found.", module); } } // make sure we haven't disabled these features from running if (enableJM) { this.jm = new JobManager(this.delegator); } if (enableJMS) { this.jlf = new JmsListenerFactory(this); } if (enableSvcs) { this.runStartupServices(); } } public ServiceDispatcher(GenericDelegator delegator) { this(delegator, enableJM, enableJMS, enableSvcs); } /** * Returns a pre-registered instance of the ServiceDispatcher associated with this delegator. * @param delegator the local delegator * @return A reference to this global ServiceDispatcher */ public static ServiceDispatcher getInstance(String name, GenericDelegator delegator) { ServiceDispatcher sd = getInstance(null, null, delegator); if (!sd.containsContext(name)) { return null; } return sd; } /** * Returns an instance of the ServiceDispatcher associated with this delegator and registers the loader. * @param name the local dispatcher * @param context the context of the local dispatcher * @param delegator the local delegator * @return A reference to this global ServiceDispatcher */ public static ServiceDispatcher getInstance(String name, DispatchContext context, GenericDelegator delegator) { ServiceDispatcher sd = null; String dispatcherKey = delegator != null ? delegator.getDelegatorName() : "null"; sd = (ServiceDispatcher) dispatchers.get(dispatcherKey); if (sd == null) { synchronized (ServiceDispatcher.class) { if (Debug.verboseOn()) Debug.logVerbose("[ServiceDispatcher.getInstance] : No instance found (" + delegator.getDelegatorName() + ").", module); sd = (ServiceDispatcher) dispatchers.get(dispatcherKey); if (sd == null) { sd = new ServiceDispatcher(delegator); dispatchers.put(dispatcherKey, sd); } } } if (name != null && context != null) { sd.register(name, context); } return sd; } /** * Registers the loader with this ServiceDispatcher * @param name the local dispatcher * @param context the context of the local dispatcher */ public void register(String name, DispatchContext context) { if (Debug.infoOn()) Debug.logInfo("Registered dispatcher: " + context.getName(), module); this.localContext.put(name, context); } /** * De-Registers the loader with this ServiceDispatcher * @param local the LocalDispatcher to de-register */ public void deregister(LocalDispatcher local) { if (Debug.infoOn()) Debug.logInfo("De-Registering dispatcher: " + local.getName(), module); localContext.remove(local.getName()); if (localContext.size() == 1) { // 1 == the JMSDispatcher try { this.shutdown(); } catch (GenericServiceException e) { Debug.logError(e, "Trouble shutting down ServiceDispatcher!", module); } } } public synchronized void registerCallback(String serviceName, GenericServiceCallback cb) { List callBackList = (List) callbacks.get(serviceName); if (callBackList == null) { callBackList = FastList.newInstance(); } callBackList.add(cb); callbacks.put(serviceName, callBackList); } public List getCallbacks(String serviceName) { return (List) callbacks.get(serviceName); } /** * Run the service synchronously and return the result. * @param localName Name of the context to use. * @param service Service model object. * @param context Map of name, value pairs composing the context. * @return Map of name, value pairs composing the result. * @throws ServiceAuthException * @throws ServiceValidationException * @throws GenericServiceException */ public Map runSync(String localName, ModelService service, Map context) throws ServiceAuthException, ServiceValidationException, GenericServiceException { return runSync(localName, service, context, true); } /** * Run the service synchronously and IGNORE the result. * @param localName Name of the context to use. * @param service Service model object. * @param context Map of name, value pairs composing the context. * @throws ServiceAuthException * @throws ServiceValidationException * @throws GenericServiceException */ public void runSyncIgnore(String localName, ModelService service, Map context) throws ServiceAuthException, ServiceValidationException, GenericServiceException { runSync(localName, service, context, false); } /** * Run the service synchronously and return the result. * @param localName Name of the context to use. * @param modelService Service model object. * @param context Map of name, value pairs composing the context. * @param validateOut Validate OUT parameters * @return Map of name, value pairs composing the result. * @throws ServiceAuthException * @throws ServiceValidationException * @throws GenericServiceException */ public Map runSync(String localName, ModelService modelService, Map context, boolean validateOut) throws ServiceAuthException, ServiceValidationException, GenericServiceException { UtilTimer timer = null; if (Debug.timingOn()) { timer = new UtilTimer(localName + " / " + modelService.name, true); // not thread safe: UtilTimer.timerLog(localName + " / " + modelService.name, "Sync service started...", module); } boolean debugging = checkDebug(modelService, 1, true); if (Debug.verboseOn()) { Debug.logVerbose("[ServiceDispatcher.runSync] : invoking service " + modelService.name + " [" + modelService.location + "/" + modelService.invoke + "] (" + modelService.engineName + ")", module); } // setup the result map Map result = FastMap.newInstance(); boolean isFailure = false; boolean isError = false; // set up the running service log RunningService rs = this.logService(localName, modelService, GenericEngine.SYNC_MODE); // get eventMap once for all calls for speed, don't do event calls if it is null Map eventMap = ServiceEcaUtil.getServiceEventMap(modelService.name); // check the locale Locale locale = this.checkLocale(context); // setup the engine and context DispatchContext ctx = (DispatchContext) localContext.get(localName); GenericEngine engine = this.getGenericEngine(modelService.engineName); Map ecaContext = null; // for isolated transactions Transaction parentTransaction = null; // start the transaction boolean beganTrans = false; try { if (modelService.useTransaction) { beganTrans = TransactionUtil.begin(modelService.transactionTimeout); // isolate the transaction if defined if (modelService.requireNewTransaction && !beganTrans) { parentTransaction = TransactionUtil.suspend(); // now start a new transaction beganTrans = TransactionUtil.begin(modelService.transactionTimeout); } } // XAResource debugging if (beganTrans && TransactionUtil.debugResources) { DebugXaResource dxa = new DebugXaResource(modelService.name); try { dxa.enlist(); } catch (Exception e) { Debug.logError(e, module); } } try { // setup global transaction ECA listeners if (eventMap != null) ServiceEcaUtil.evalRules(modelService.name, eventMap, "global-rollback", ctx, context, result, false, false); if (eventMap != null) ServiceEcaUtil.evalRules(modelService.name, eventMap, "global-commit", ctx, context, result, false, false); // pre-auth ECA if (eventMap != null) ServiceEcaUtil.evalRules(modelService.name, eventMap, "auth", ctx, context, result, false, false); context = checkAuth(localName, context, modelService); Object userLogin = context.get("userLogin");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -