📄 servicedispatcher.java
字号:
} /** * 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, (Locale) context.get("locale"))); context.remove("login.password"); } else { context.put("userLogin", getLoginObject(service, localName, username, null, (Locale) context.get("locale"))); } 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) { // Because of encrypted passwords we can't just pass in the encrypted version of the password from the data, so we'll do something different and not run the userLogin service... //The old way: GenericValue newUserLogin = getLoginObject(service, localName, userLogin.getString("userLoginId"), userLogin.getString("currentPassword"), (Locale) context.get("locale")); GenericValue newUserLogin = null; try { newUserLogin = this.getDelegator().findByPrimaryKeyCache("UserLogin", UtilMisc.toMap("userLoginId", userLogin.get("userLoginId"))); } catch (GenericEntityException e) { Debug.logError(e, "Error looking up service authentication UserLogin: " + e.toString(), module); // leave newUserLogin null, will be handled below } 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 Debug.logInfo("Service auth failed for userLoginId [" + userLogin.get("userLoginId") + "] because UserLogin record not found.", module); context.remove("userLogin"); } else if (newUserLogin.getString("currentPassword") != null && !newUserLogin.getString("currentPassword").equals(userLogin.getString("currentPassword"))) { // passwords didn't match, remove the userLogin for failed auth Debug.logInfo("Service auth failed for userLoginId [" + userLogin.get("userLoginId") + "] because UserLogin record currentPassword fields did not match; note that the UserLogin object passed into a service may need to have the currentPassword encrypted.", module); 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, Locale locale) throws GenericServiceException { Map context = UtilMisc.toMap("login.username", username, "login.password", password, "isServiceAuth", new Boolean(true), "locale", locale); 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 Locale checkLocale(Map context) { Object locale = context.get("locale"); Locale newLocale = null; if (locale != null) { if (locale instanceof Locale) { return (Locale) locale; } else if (locale instanceof String) { // en_US = lang_COUNTRY newLocale = UtilMisc.parseLocale((String) locale); } } if (newLocale == null) { newLocale = Locale.getDefault(); } context.put("locale", newLocale); return 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; } // run startup services private synchronized int runStartupServices() { if (jm == null) return 0; Element root = null; try { root = ServiceConfigUtil.getXmlRootElement(); } catch (GenericConfigException e) { Debug.logError(e, module); return 0; } int servicesScheduled = 0; List startupServices = UtilXml.childElementList(root, "startup-service"); if (startupServices != null && startupServices.size() > 0) { Iterator i = startupServices.iterator(); while (i.hasNext()) { Element ss = (Element) i.next(); String serviceName = ss.getAttribute("name"); String runtimeDataId = ss.getAttribute("runtime-data-id"); String delayStr = ss.getAttribute("runtime-delay"); String sendToPool = ss.getAttribute("run-in-pool"); if (UtilValidate.isEmpty(sendToPool)) { sendToPool = ServiceConfigUtil.getSendPool(); } long runtimeDelay = 0; try { runtimeDelay = Long.parseLong(delayStr); } catch (Exception e) { Debug.logError(e, "Unable to parse runtime-delay value; using 0", module); runtimeDelay = 0; } // current time + 1 sec delay + extended delay long runtime = System.currentTimeMillis() + 1000 + runtimeDelay; try { jm.schedule(sendToPool, serviceName, runtimeDataId, runtime); } catch (JobManagerException e) { Debug.logError(e, "Unable to schedule service [" + serviceName + "]", module); } } } return servicesScheduled; } private RunningService logService(String localName, ModelService modelService, int mode) { // set up the running service log RunningService rs = new RunningService(localName, modelService, mode); if (runLog == null) { runLog = new LRUMap(lruLogSize); } try { runLog.put(rs, this); } catch (Throwable t) { Debug.logWarning("LRUMap problem; resetting LRU [" + runLog.size() + "]", module); runLog = new LRUMap(lruLogSize); try { runLog.put(rs, this); } catch (Throwable t2) { Debug.logError(t2, "Unable to put() in reset LRU map!", module); } } return rs; } /** * Enabled/Disables the Job Manager/Scheduler globally * (this will not effect any dispatchers already running) * @param enable */ public static void enableJM(boolean enable) { ServiceDispatcher.enableJM = enable; } /** * Enabled/Disables the JMS listeners globally * (this will not effect any dispatchers already running) * @param enable */ public static void enableJMS(boolean enable) { ServiceDispatcher.enableJMS = enable; } /** * Enabled/Disables the startup services globally * (this will not effect any dispatchers already running) * @param enable */ public static void enableSvcs(boolean enable) { ServiceDispatcher.enableSvcs = enable; } public static Map getServiceLogMap() { return runLog; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -