📄 servicedispatcher.java
字号:
/*
* $Id: ServiceDispatcher.java,v 1.15 2004/02/19 18:52:35 ajzeneski Exp $
*
* Copyright (c) 2001, 2002 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.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.transaction.InvalidTransactionException;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.transaction.GenericTransactionException;
import org.ofbiz.entity.transaction.TransactionFactory;
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;
/**
* Global Service Dispatcher
*
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
* @version $Revision: 1.15 $
* @since 2.0
*/
public class ServiceDispatcher {
public static final String module = ServiceDispatcher.class.getName();
protected static Map dispatchers = new HashMap();
protected GenericDelegator delegator = null;
protected GenericEngineFactory factory = null;
protected Security security = null;
protected Map localContext = null;
protected JobManager jm = null;
protected JmsListenerFactory jlf = null;
public ServiceDispatcher(GenericDelegator delegator) {
Debug.logInfo("[ServiceDispatcher] : Creating new instance.", module);
factory = new GenericEngineFactory(this);
ServiceGroupReader.readConfig();
ServiceEcaUtil.readConfig();
this.delegator = delegator;
this.localContext = new HashMap();
if (delegator != null) {
try {
this.security = SecurityFactory.getInstance(delegator);
} catch (SecurityConfigurationException e) {
Debug.logError(e, "[ServiceDispatcher.init] : No instance of security imeplemtation found.", module);
}
}
this.jm = new JobManager(this.delegator);
this.jlf = new JmsListenerFactory(this);
}
/**
* 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("[ServiceDispatcher.register] : 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("[ServiceDispatcher.deregister] : 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);
}
}
}
/**
* 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 {
boolean debugging = checkDebug(modelService, 1, true);
if (Debug.verboseOn()) {
Debug.logVerbose("[ServiceDispatcher.runSync] : invoking service " + modelService.name + " [" + modelService.location +
"/" + modelService.invoke + "] (" + modelService.engineName + ")", module);
}
// check the locale
this.checkLocale(context);
// for isolated transactions
TransactionManager tm = TransactionFactory.getTransactionManager();
Transaction parentTransaction = null;
// start the transaction
boolean beganTrans = false;
if (modelService.useTransaction) {
try {
beganTrans = TransactionUtil.begin(modelService.transactionTimeout);
} catch (GenericTransactionException te) {
throw new GenericServiceException("Cannot start the transaction.", te.getNested());
}
// isolate the transaction if defined
if (modelService.requireNewTransaction && !beganTrans) {
try {
parentTransaction = tm.suspend();
} catch (SystemException se) {
Debug.logError(se, "Problems suspending current transaction", module);
throw new GenericServiceException("Problems suspending transaction, see logs");
}
// now start a new transaction
try {
beganTrans = TransactionUtil.begin(modelService.transactionTimeout);
} catch (GenericTransactionException gte) {
throw new GenericServiceException("Cannot start the transaction.", gte.getNested());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -