Author: jonesde
Date: Sat Mar 24 02:52:10 2007 New Revision: 521999 URL: http://svn.apache.org/viewvc?view=rev&rev=521999 Log: This is a fairly complete rework of the UtilCache use and the loading code in the service engine; a reload of service definitions should now be possible by clearing the cache; also removed a bunch of the other caches that were either redundant and annoying, or dangerous because clearing them would break things, perhaps unknowingly; these are some problems and really messy weird code that has been around for quite a while, finally got sick of it and decided it needed to go Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/DispatchContext.java ofbiz/trunk/framework/service/src/org/ofbiz/service/ModelServiceReader.java ofbiz/trunk/framework/service/src/org/ofbiz/service/ServiceDispatcher.java ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaUtil.java ofbiz/trunk/framework/service/src/org/ofbiz/service/group/ServiceGroupReader.java Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/DispatchContext.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/DispatchContext.java?view=diff&rev=521999&r1=521998&r2=521999 ============================================================================== --- ofbiz/trunk/framework/service/src/org/ofbiz/service/DispatchContext.java (original) +++ ofbiz/trunk/framework/service/src/org/ofbiz/service/DispatchContext.java Sat Mar 24 02:52:10 2007 @@ -53,11 +53,11 @@ public static final String module = DispatchContext.class.getName(); protected static final String GLOBAL_KEY = "global.services"; - protected static UtilCache modelService = new UtilCache("service.ModelServices", 0, 0, false); + protected static UtilCache modelServiceMapByDispatcher = new UtilCache("service.ModelServiceMapByDispatcher", 0, 0, false); protected transient LocalDispatcher dispatcher; protected transient ClassLoader loader; - protected Collection readers; + protected Collection localReaders; protected Map attributes; protected String name; @@ -66,24 +66,17 @@ * @param readers a collection of reader URLs * @param loader the classloader to use for dispatched services */ - public DispatchContext(String name, Collection readers, ClassLoader loader, LocalDispatcher dispatcher) { + public DispatchContext(String name, Collection localReaders, ClassLoader loader, LocalDispatcher dispatcher) { this.name = name; - this.readers = readers; + this.localReaders = localReaders; this.loader = loader; this.dispatcher = dispatcher; this.attributes = FastMap.newInstance(); } public void loadReaders() { - Map localService = addReaders(readers); - if (localService != null) { - modelService.put(name, localService); - } - - Map globalService = addGlobal(); - if (globalService != null) { - modelService.put(GLOBAL_KEY, globalService); - } + this.getLocalServiceMap(); + this.getGlobalServiceMap(); } /** @@ -119,7 +112,7 @@ * @return Collection of reader URLs */ public Collection getReaders() { - return readers; + return localReaders; } /** @@ -163,7 +156,7 @@ } else if (mode.equalsIgnoreCase("out")) { modeInt = 2; } - + if (model == null) { throw new GenericServiceException("Model service is null! Should never happen."); } else { @@ -187,6 +180,7 @@ * @return GenericServiceModel that corresponds to the serviceName */ public ModelService getModelService(String serviceName) throws GenericServiceException { + //long timeStart = System.currentTimeMillis(); ModelService retVal = getLocalModelService(serviceName); if (retVal == null) { retVal = getGlobalModelService(serviceName); @@ -196,23 +190,12 @@ throw new GenericServiceException("Cannot locate service by name (" + serviceName + ")"); } + //Debug.logTiming("Got ModelService for name [" + serviceName + "] in [" + (System.currentTimeMillis() - timeStart) + "] milliseconds", module); return retVal; } private ModelService getLocalModelService(String serviceName) throws GenericServiceException { - Map serviceMap = (Map) modelService.get(name); - if (serviceMap == null) { - synchronized (this) { - serviceMap = (Map) modelService.get(name); - if (serviceMap == null) { - serviceMap = addReaders(readers); - if (serviceMap != null) { - modelService.put(name, serviceMap); - ServiceEcaUtil.reloadConfig(); - } - } - } - } + Map serviceMap = this.getLocalServiceMap(); ModelService retVal = null; if (serviceMap != null) { @@ -226,19 +209,7 @@ } private ModelService getGlobalModelService(String serviceName) throws GenericServiceException { - Map serviceMap = (Map) modelService.get(GLOBAL_KEY); - if (serviceMap == null) { - synchronized (this) { - serviceMap = (Map) modelService.get(GLOBAL_KEY); - if (serviceMap == null) { - serviceMap = addGlobal(); - if (serviceMap != null) { - modelService.put(GLOBAL_KEY, serviceMap); - ServiceEcaUtil.reloadConfig(); - } - } - } - } + Map serviceMap = this.getGlobalServiceMap(); ModelService retVal = null; if (serviceMap != null) { @@ -283,96 +254,91 @@ return dispatcher.getSecurity(); } - private Map addReaders(Collection readerURLs) { - Map serviceMap = FastMap.newInstance(); - - if (readerURLs == null) - return null; - Iterator urlIter = readerURLs.iterator(); - - while (urlIter.hasNext()) { - URL readerURL = (URL) urlIter.next(); - - serviceMap.putAll(addReader(readerURL)); - } - return serviceMap; - } - - private Map addReader(URL readerURL) { - if (readerURL == null) { - Debug.logError("Cannot add reader with a null reader URL", module); - return null; - } - - ModelServiceReader reader = ModelServiceReader.getModelServiceReader(readerURL, this); - - if (reader == null) { - Debug.logError("Could not load the reader for the reader URL " + readerURL, module); - return null; - } - - Map serviceMap = reader.getModelServices(); - - return serviceMap; - } - - private Map addReader(ResourceHandler handler) { - ModelServiceReader reader = ModelServiceReader.getModelServiceReader(handler, this); - - if (reader == null) { - Debug.logError("Could not load the reader for " + handler, module); - return null; + private Map getLocalServiceMap() { + Map serviceMap = (Map) modelServiceMapByDispatcher.get(name); + if (serviceMap == null) { + synchronized (this) { + serviceMap = (Map) modelServiceMapByDispatcher.get(name); + if (serviceMap == null) { + if (this.localReaders != null) { + serviceMap = FastMap.newInstance(); + Iterator urlIter = this.localReaders.iterator(); + while (urlIter.hasNext()) { + URL readerURL = (URL) urlIter.next(); + Map readerServiceMap = ModelServiceReader.getModelServiceMap(readerURL, this); + if (readerServiceMap != null) { + serviceMap.putAll(readerServiceMap); + } + } + } + if (serviceMap != null) { + modelServiceMapByDispatcher.put(name, serviceMap); + // NOTE: the current ECA per dispatcher for local services stuff is a bit broken, so now just doing this on the global def load: ServiceEcaUtil.reloadConfig(); + } + } + } } - - Map serviceMap = reader.getModelServices(); - + return serviceMap; } + + private Map getGlobalServiceMap() { + Map serviceMap = (Map) modelServiceMapByDispatcher.get(GLOBAL_KEY); + if (serviceMap == null) { + synchronized (this) { + serviceMap = (Map) modelServiceMapByDispatcher.get(GLOBAL_KEY); + if (serviceMap == null) { + serviceMap = FastMap.newInstance(); - private Map addGlobal() { - Map globalMap = FastMap.newInstance(); + Element rootElement = null; - Element rootElement = null; + try { + rootElement = ServiceConfigUtil.getXmlRootElement(); + } catch (GenericConfigException e) { + Debug.logError(e, "Error getting Service Engine XML root element", module); + return null; + } - try { - rootElement = ServiceConfigUtil.getXmlRootElement(); - } catch (GenericConfigException e) { - Debug.logError(e, "Error getting Service Engine XML root element", module); - return null; - } + List globalServicesElements = UtilXml.childElementList(rootElement, "global-services"); + Iterator gseIter = globalServicesElements.iterator(); + while (gseIter.hasNext()) { + Element globalServicesElement = (Element) gseIter.next(); + ResourceHandler handler = new MainResourceHandler( + ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME, globalServicesElement); + + Map servicesMap = ModelServiceReader.getModelServiceMap(handler, this); + if (servicesMap != null) { + serviceMap.putAll(servicesMap); + } + } + + // get all of the component resource model stuff, ie specified in each ofbiz-component.xml file + List componentResourceInfos = ComponentConfig.getAllServiceResourceInfos("model"); + Iterator componentResourceInfoIter = componentResourceInfos.iterator(); + while (componentResourceInfoIter.hasNext()) { + ComponentConfig.ServiceResourceInfo componentResourceInfo = (ComponentConfig.ServiceResourceInfo) componentResourceInfoIter.next(); + Map servicesMap = ModelServiceReader.getModelServiceMap(componentResourceInfo.createResourceHandler(), this); + if (servicesMap != null) { + serviceMap.putAll(servicesMap); + } + } - List globalServicesElements = UtilXml.childElementList(rootElement, "global-services"); - Iterator gseIter = globalServicesElements.iterator(); - while (gseIter.hasNext()) { - Element globalServicesElement = (Element) gseIter.next(); - ResourceHandler handler = new MainResourceHandler( - ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME, globalServicesElement); - - Map servicesMap = addReader(handler); - if (servicesMap != null) { - globalMap.putAll(servicesMap); - } - } - - // get all of the component resource model stuff, ie specified in each ofbiz-component.xml file - List componentResourceInfos = ComponentConfig.getAllServiceResourceInfos("model"); - Iterator componentResourceInfoIter = componentResourceInfos.iterator(); - while (componentResourceInfoIter.hasNext()) { - ComponentConfig.ServiceResourceInfo componentResourceInfo = (ComponentConfig.ServiceResourceInfo) componentResourceInfoIter.next(); - Map servicesMap = addReader(componentResourceInfo.createResourceHandler()); - if (servicesMap != null) { - globalMap.putAll(servicesMap); + if (serviceMap != null) { + modelServiceMapByDispatcher.put(GLOBAL_KEY, serviceMap); + ServiceEcaUtil.reloadConfig(); + } + } } } - return globalMap; + return serviceMap; } public Set getAllServiceNames() { Set serviceNames = new TreeSet(); - Map globalServices = (Map) modelService.get(GLOBAL_KEY); - Map localServices = (Map) modelService.get(name); + Map globalServices = (Map) modelServiceMapByDispatcher.get(GLOBAL_KEY); + Map localServices = (Map) modelServiceMapByDispatcher.get(name); if (globalServices != null) { serviceNames.addAll(globalServices.keySet()); } Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/ModelServiceReader.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/ModelServiceReader.java?view=diff&rev=521999&r1=521998&r2=521999 ============================================================================== --- ofbiz/trunk/framework/service/src/org/ofbiz/service/ModelServiceReader.java (original) +++ ofbiz/trunk/framework/service/src/org/ofbiz/service/ModelServiceReader.java Sat Mar 24 02:52:10 2007 @@ -26,23 +26,26 @@ import java.util.List; import java.util.Map; import java.util.Set; + import javax.xml.parsers.ParserConfigurationException; import javolution.util.FastList; import javolution.util.FastMap; +import org.apache.commons.collections.map.LinkedMap; import org.ofbiz.base.config.GenericConfigException; import org.ofbiz.base.config.ResourceHandler; -import org.ofbiz.base.util.*; -import org.ofbiz.base.util.cache.UtilCache; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.GeneralException; +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.model.ModelEntity; import org.ofbiz.entity.model.ModelField; import org.ofbiz.entity.model.ModelFieldType; import org.ofbiz.service.group.GroupModel; - -import org.apache.commons.collections.map.LinkedMap; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -57,9 +60,6 @@ public static final String module = ModelServiceReader.class.getName(); - protected static UtilCache readersUrl = new UtilCache("service.ModelServiceReader.ByURL", 0, 0); - protected static UtilCache readersLoader = new UtilCache("service.ModelServiceReader.ByResourceLoader", 0, 0); - /** is either from a URL or from a ResourceLoader (through the ResourceHandler) */ protected boolean isFromURL; protected URL readerURL = null; @@ -67,41 +67,32 @@ protected Map modelServices = null; protected DispatchContext dctx = null; - public static ModelServiceReader getModelServiceReader(URL readerURL, DispatchContext dctx) { - ModelServiceReader reader; + public static Map getModelServiceMap(URL readerURL, DispatchContext dctx) { + if (readerURL == null) { + Debug.logError("Cannot add reader with a null reader URL", module); + return null; + } - // if ( readersUrl.containsKey(readerURL) ) <-- this is unnecessary as it will return null below if not found - reader = (ModelServiceReader) readersUrl.get(readerURL); - if (reader == null) { // don't want to block here - synchronized (ModelServiceReader.class) { - // must check if null again as one of the blocked threads can still enter - reader = (ModelServiceReader) readersUrl.get(readerURL); - if (reader == null) { - // if (Debug.infoOn()) Debug.logInfo("[Creating reader]: " + readerURL.toExternalForm(), module); - reader = new ModelServiceReader(readerURL, dctx); - readersUrl.put(readerURL, reader); - } - } + ModelServiceReader reader = new ModelServiceReader(readerURL, dctx); + if (reader == null) { + Debug.logError("Could not load the reader for the reader URL " + readerURL, module); + return null; } - return reader; + + Map serviceMap = reader.getModelServices(); + return serviceMap; } - public static ModelServiceReader getModelServiceReader(ResourceHandler handler, DispatchContext dctx) { + public static Map getModelServiceMap(ResourceHandler handler, DispatchContext dctx) { ModelServiceReader reader; - - reader = (ModelServiceReader) readersLoader.get(handler); - if (reader == null) { // don't want to block here - synchronized (ModelServiceReader.class) { - // must check if null again as one of the blocked threads can still enter - reader = (ModelServiceReader) readersLoader.get(handler); - if (reader == null) { - // if (Debug.infoOn()) Debug.logInfo("[Creating reader]: " + handler, module); - reader = new ModelServiceReader(handler, dctx); - readersLoader.put(handler, reader); - } - } + reader = new ModelServiceReader(handler, dctx); + if (reader == null) { + Debug.logError("Could not load the reader for " + handler, module); + return null; } - return reader; + + Map serviceMap = reader.getModelServices(); + return serviceMap; } protected ModelServiceReader(URL readerURL, DispatchContext dctx) { Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/ServiceDispatcher.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/ServiceDispatcher.java?view=diff&rev=521999&r1=521998&r2=521999 ============================================================================== --- ofbiz/trunk/framework/service/src/org/ofbiz/service/ServiceDispatcher.java (original) +++ ofbiz/trunk/framework/service/src/org/ofbiz/service/ServiceDispatcher.java Sat Mar 24 02:52:10 2007 @@ -236,11 +236,7 @@ * @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); - } + long serviceStartTime = System.currentTimeMillis(); boolean debugging = checkDebug(modelService, 1, true); if (Debug.verboseOn()) { Debug.logVerbose("[ServiceDispatcher.runSync] : invoking service " + modelService.name + " [" + modelService.location + @@ -454,10 +450,14 @@ checkDebug(modelService, 0, debugging); rs.setEndStamp(); - if (timer != null) { - timer.setLog(true); - timer.timerString("Sync service finished", module); + + long timeToRun = System.currentTimeMillis() - serviceStartTime; + if (Debug.timingOn() && timeToRun > 50) { + Debug.logTiming("Sync service [" + localName + "/" + modelService.name + "] finished in [" + timeToRun + "] milliseconds", module); + } else if (timeToRun > 200) { + Debug.logInfo("Sync service [" + localName + "/" + modelService.name + "] finished in [" + timeToRun + "] milliseconds", module); } + return result; } Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaUtil.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaUtil.java?view=diff&rev=521999&r1=521998&r2=521999 ============================================================================== --- ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaUtil.java (original) +++ ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaUtil.java Sat Mar 24 02:52:10 2007 @@ -27,6 +27,8 @@ import java.util.TreeSet; import java.util.Collection; +import javolution.util.FastMap; + import org.ofbiz.base.component.ComponentConfig; import org.ofbiz.base.config.GenericConfigException; import org.ofbiz.base.config.MainResourceHandler; @@ -47,7 +49,8 @@ public static final String module = ServiceEcaUtil.class.getName(); - public static UtilCache ecaCache = new UtilCache("service.ServiceECAs", 0, 0, false); + // using a cache is dangerous here because if someone clears it the ECAs won't run: public static UtilCache ecaCache = new UtilCache("service.ServiceECAs", 0, 0, false); + public static Map ecaCache = FastMap.newInstance(); public static void reloadConfig() { ecaCache.clear(); @@ -115,13 +118,13 @@ numDefs++; } if (Debug.importantOn()) { - String resourceLocation = handler.getLocation(); - try { - resourceLocation = handler.getURL().toExternalForm(); - } catch (GenericConfigException e) { - Debug.logError(e, "Could not get resource URL", module); - } - Debug.logImportant("Loaded " + numDefs + " Service ECA definitions from " + resourceLocation, module); + String resourceLocation = handler.getLocation(); + try { + resourceLocation = handler.getURL().toExternalForm(); + } catch (GenericConfigException e) { + Debug.logError(e, "Could not get resource URL", module); + } + Debug.logImportant("Loaded " + numDefs + " Service ECA definitions from " + resourceLocation, module); } } Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/group/ServiceGroupReader.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/group/ServiceGroupReader.java?view=diff&rev=521999&r1=521998&r2=521999 ============================================================================== --- ofbiz/trunk/framework/service/src/org/ofbiz/service/group/ServiceGroupReader.java (original) +++ ofbiz/trunk/framework/service/src/org/ofbiz/service/group/ServiceGroupReader.java Sat Mar 24 02:52:10 2007 @@ -20,6 +20,9 @@ import java.util.Iterator; import java.util.List; +import java.util.Map; + +import javolution.util.FastMap; import org.ofbiz.base.component.ComponentConfig; import org.ofbiz.base.config.GenericConfigException; @@ -27,7 +30,6 @@ import org.ofbiz.base.config.ResourceHandler; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilXml; -import org.ofbiz.base.util.cache.UtilCache; import org.ofbiz.service.config.ServiceConfigUtil; import org.w3c.dom.Element; @@ -38,7 +40,8 @@ public static final String module = ServiceGroupReader.class.getName(); - public static UtilCache groupsCache = new UtilCache("service.ServiceGroups", 0, 0, false); + // using a cache is dangerous here because if someone clears it the groups won't work at all: public static UtilCache groupsCache = new UtilCache("service.ServiceGroups", 0, 0, false); + public static Map groupsCache = FastMap.newInstance(); public static void readConfig() { Element rootElement = null; |
Free forum by Nabble | Edit this page |