📄 servicedispatcher.java
字号:
Debug.logError(ise, "Trouble resuming parent transaction", module);
throw new GenericServiceException("Resume transaction exception, see logs");
} catch (SystemException se) {
Debug.logError(se, "Trouble resuming parent transaction", module);
throw new GenericServiceException("Resume transaction exception, see logs");
}
}
checkDebug(service, 0, debugging);
} catch (Throwable t) {
Debug.logError(t, "Service [" + service.name + "] threw an unexpected exception/error", module);
try {
TransactionUtil.rollback(beganTrans);
} catch (GenericTransactionException te) {
Debug.logError(te, "Cannot rollback transaction", module);
}
checkDebug(service, 0, debugging);
if (t instanceof ServiceAuthException) {
throw (ServiceAuthException) t;
} else if (t instanceof ServiceValidationException) {
throw (ServiceValidationException) t;
} else if (t instanceof GenericServiceException) {
throw (GenericServiceException) t;
} else {
throw new GenericServiceException("Service [" + service.name + "] Failed" + service.debugInfo() , t);
}
}
}
/**
* Run the service asynchronously 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.
* @param persist True for store/run; False for run.
* @throws ServiceAuthException
* @throws ServiceValidationException
* @throws GenericServiceException
*/
public void runAsync(String localName, ModelService service, Map context, boolean persist) throws ServiceAuthException, ServiceValidationException, GenericServiceException {
this.runAsync(localName, service, context, null, persist);
}
/**
* Gets the GenericEngine instance that corresponds to the given name
* @param engineName Name of the engine
* @return GenericEngine instance that corresponds to the engineName
*/
public GenericEngine getGenericEngine(String engineName) throws GenericServiceException {
return factory.getGenericEngine(engineName);
}
/**
* Gets the JobManager associated with this dispatcher
* @return JobManager that is associated with this dispatcher
*/
public JobManager getJobManager() {
return this.jm;
}
/**
* Gets the JmsListenerFactory which holds the message listeners.
* @return JmsListenerFactory
*/
public JmsListenerFactory getJMSListenerFactory() {
return this.jlf;
}
/**
* Gets the GenericDelegator associated with this dispatcher
* @return GenericDelegator associated with this dispatcher
*/
public GenericDelegator getDelegator() {
return this.delegator;
}
/**
* Gets the Security object associated with this dispatcher
* @return Security object associated with this dispatcher
*/
public Security getSecurity() {
return this.security;
}
/**
* Gets the local context from a name
* @param name of the context to find.
*/
public DispatchContext getLocalContext(String name) {
return (DispatchContext) localContext.get(name);
}
/**
* Gets the local dispatcher from a name
* @param name of the LocalDispatcher to find.
* @return LocalDispatcher matching the loader name
*/
public LocalDispatcher getLocalDispatcher(String name) {
return ((DispatchContext) localContext.get(name)).getDispatcher();
}
/**
* Test if this dispatcher instance contains the local context.
* @param name of the local context
* @return true if the local context is found in this dispatcher.
*/
public boolean containsContext(String name) {
return localContext.containsKey(name);
}
protected void shutdown() throws GenericServiceException {
Debug.logImportant("Shutting down the service engine...", module);
// shutdown JMS listeners
jlf.closeListeners();
// shutdown the job scheduler
jm.finalize();
}
// checks if parameters were passed for authentication
private Map checkAuth(String localName, Map context, ModelService origService) throws ServiceAuthException, GenericServiceException {
String service = ServiceConfigUtil.getElementAttr("authorization", "service-name");
if (service == null) {
throw new GenericServiceException("No Authentication Service Defined");
}
if (service.equals(origService.name)) {
// manually calling the auth service, don't continue...
return context;
}
if (context.containsKey("login.username")) {
// check for a username/password, if there log the user in and make the userLogin object
String username = (String) context.get("login.username");
if (context.containsKey("login.password")) {
String password = (String) context.get("login.password");
context.put("userLogin", getLoginObject(service, localName, username, password));
context.remove("login.password");
} else {
context.put("userLogin", getLoginObject(service, localName, username, null));
}
context.remove("login.username");
} else {
// if a userLogin object is there, make sure the given username/password exists in our local database
GenericValue userLogin = (GenericValue) context.get("userLogin");
if (userLogin != null) {
GenericValue newUserLogin = getLoginObject(service, localName, userLogin.getString("userLoginId"), userLogin.getString("currentPassword"));
if (newUserLogin == null) {
// uh oh, couldn't validate that one...
// we'll have to remove it from the incoming context which will cause an auth error later if auth is required
context.remove("userLogin");
}
}
}
// evaluate permissions for the service or throw exception if fail.
DispatchContext dctx = this.getLocalContext(localName);
GenericValue userLogin = (GenericValue) context.get("userLogin");
if (!origService.evalPermissions(dctx.getSecurity(), userLogin)) {
throw new ServiceAuthException("You do not have permission to invoke this service");
}
return context;
}
// gets a value object from name/password pair
private GenericValue getLoginObject(String service, String localName, String username, String password) throws GenericServiceException {
Map context = UtilMisc.toMap("login.username", username, "login.password", password, "isServiceAuth", new Boolean(true));
if (Debug.verboseOn()) Debug.logVerbose("[ServiceDispathcer.authenticate] : Invoking UserLogin Service", module);
// get the dispatch context and service model
DispatchContext dctx = getLocalContext(localName);
ModelService model = dctx.getModelService(service);
// get the service engine
GenericEngine engine = getGenericEngine(model.engineName);
// invoke the service and get the UserLogin value object
Map result = engine.runSync(localName, model, context);
GenericValue value = (GenericValue) result.get("userLogin");
return value;
}
// checks the locale object in the context
private void checkLocale(Map context) {
Object locale = context.get("locale");
Locale newLocale = null;
if (locale != null) {
if (locale instanceof Locale) {
return;
} else if (locale instanceof String) {
// en_US = lang_COUNTRY
newLocale = UtilMisc.parseLocale((String) locale);
}
}
if (newLocale == null) {
newLocale = Locale.getDefault();
}
context.put("locale", newLocale);
}
// mode 1 = beginning (turn on) mode 0 = end (turn off)
private boolean checkDebug(ModelService model, int mode, boolean enable) {
boolean debugOn = Debug.verboseOn();
switch (mode) {
case 0:
if (model.debug && enable && debugOn) {
// turn it off
Debug.set(Debug.VERBOSE, false);
Debug.logInfo("Verbose logging turned OFF", module);
return true;
}
break;
case 1:
if (model.debug && enable && !debugOn) {
// turn it on
Debug.set(Debug.VERBOSE, true);
Debug.logInfo("Verbose logging turned ON", module);
return true;
}
break;
default:
Debug.logError("Invalid mode for checkDebug should be (0 or 1)", module);
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -