Return-Path: Delivered-To: apmail-cocoon-cvs-archive@www.apache.org Received: (qmail 59404 invoked from network); 2 Feb 2004 21:29:56 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 2 Feb 2004 21:29:56 -0000 Received: (qmail 80595 invoked by uid 500); 2 Feb 2004 21:29:43 -0000 Delivered-To: apmail-cocoon-cvs-archive@cocoon.apache.org Received: (qmail 80538 invoked by uid 500); 2 Feb 2004 21:29:43 -0000 Mailing-List: contact cvs-help@cocoon.apache.org; run by ezmlm Precedence: bulk Reply-To: dev@cocoon.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list cvs@cocoon.apache.org Received: (qmail 80514 invoked by uid 500); 2 Feb 2004 21:29:43 -0000 Delivered-To: apmail-cocoon-2.1-cvs@apache.org Received: (qmail 80508 invoked from network); 2 Feb 2004 21:29:43 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 2 Feb 2004 21:29:43 -0000 Received: (qmail 59355 invoked by uid 1788); 2 Feb 2004 21:29:54 -0000 Date: 2 Feb 2004 21:29:54 -0000 Message-ID: <20040202212954.59354.qmail@minotaur.apache.org> From: tim@apache.org To: cocoon-2.1-cvs@apache.org Subject: cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding JXPathBindingManager.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N tim 2004/02/02 13:29:54 Modified: src/blocks/woody/java/org/apache/cocoon/woody/binding JXPathBindingManager.java Log: Cache woody form binding definitions. Revision Changes Path 1.15 +84 -27 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/JXPathBindingManager.java Index: JXPathBindingManager.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/JXPathBindingManager.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- JXPathBindingManager.java 28 Jan 2004 12:55:25 -0000 1.14 +++ JXPathBindingManager.java 2 Feb 2004 21:29:54 -0000 1.15 @@ -50,6 +50,7 @@ */ package org.apache.cocoon.woody.binding; +import java.io.IOException; import org.apache.avalon.framework.activity.Disposable; import org.apache.avalon.framework.activity.Initializable; import org.apache.avalon.framework.configuration.Configurable; @@ -63,7 +64,9 @@ import org.apache.cocoon.woody.datatype.DatatypeManager; import org.apache.cocoon.woody.util.DomHelper; import org.apache.cocoon.woody.util.SimpleServiceSelector; +import org.apache.commons.collections.FastHashMap; import org.apache.excalibur.source.Source; +import org.apache.excalibur.source.SourceValidity; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.InputSource; @@ -79,13 +82,14 @@ implements BindingManager, Serviceable, Disposable, Initializable, Configurable, ThreadSafe { - // TODO caching of the Bindings. - private ServiceManager serviceManager; private DatatypeManager datatypeManager; private Configuration configuration; private SimpleServiceSelector bindingBuilderSelector; + protected static final String bindingKeyPrefix = "WoodyBinding:"; + protected FastHashMap definitionCache = new FastHashMap(); + public void service(ServiceManager serviceManager) throws ServiceException { this.serviceManager = serviceManager; this.datatypeManager = (DatatypeManager)serviceManager.lookup(DatatypeManager.ROLE); @@ -101,27 +105,79 @@ bindingBuilderSelector.configure(configuration.getChild("bindings")); } - public Binding createBinding(Source bindSrc) - throws BindingException { - try { - InputSource is = new InputSource(bindSrc.getInputStream()); - is.setSystemId(bindSrc.getURI()); - - Document doc = DomHelper.parse(is); - Element rootElm = doc.getDocumentElement(); - JXPathBindingBase newBinding = null; - if (BindingManager.NAMESPACE.equals(rootElm.getNamespaceURI())) { - newBinding = getBuilderAssistant().getBindingForConfigurationElement(rootElm); - newBinding.enableLogging(getLogger()); - getLogger().debug("Creation of new Binding finished. " + newBinding); - } else { - getLogger().debug("Root Element of said binding file is in wrong namespace."); + public Binding createBinding(Source source) throws BindingException { + return getBindingDefinition(source); + } + + public Binding getBindingDefinition(Source source) throws BindingException { + Binding bindingDefinition = getStoredBindingDefinition(source); + if (bindingDefinition == null) { + try { + InputSource is = new InputSource(source.getInputStream()); + is.setSystemId(source.getURI()); + + Document doc = DomHelper.parse(is); + Element rootElm = doc.getDocumentElement(); + if (BindingManager.NAMESPACE.equals(rootElm.getNamespaceURI())) { + bindingDefinition = getBuilderAssistant().getBindingForConfigurationElement(rootElm); + ((JXPathBindingBase)bindingDefinition).enableLogging(getLogger()); + getLogger().debug("Creation of new Binding finished. " + bindingDefinition); + } else { + getLogger().debug("Root Element of said binding file is in wrong namespace."); + } + storeBindingDefinition(bindingDefinition, source); + } catch (BindingException e) { + throw e; + } catch (Exception e) { + throw new BindingException("Error creating binding from " + source.getURI(), e); } - return newBinding; - } catch (BindingException e) { - throw e; - } catch (Exception e) { - throw new BindingException("Error creating binding from " + bindSrc.getURI(), e); + } + return bindingDefinition; + } + + protected Binding getStoredBindingDefinition(Source source) { + return (Binding)getStoredDefinition(source, bindingKeyPrefix + source.getURI()); + } + + protected void storeBindingDefinition(Binding bindingDefinition, Source source) throws IOException { + storeDefinition(bindingDefinition, source, bindingKeyPrefix + source.getURI()); + } + + protected Binding getStoredDefinition(Source source, String key) { + SourceValidity newValidity = source.getValidity(); + + if (newValidity == null) { + definitionCache.remove(key); + return null; + } + + Object[] definitionAndValidity = (Object[])definitionCache.get(key); + if (definitionAndValidity == null) + return null; + + SourceValidity storedValidity = (SourceValidity)definitionAndValidity[1]; + int valid = storedValidity.isValid(); + boolean isValid; + if (valid == 0) { + valid = storedValidity.isValid(newValidity); + isValid = (valid == 1); + } else { + isValid = (valid == 1); + } + + if (!isValid) { + definitionCache.remove(key); + return null; + } + + return (Binding)definitionAndValidity[0]; + } + + protected void storeDefinition(Object definition, Source source, String key) throws IOException { + SourceValidity validity = source.getValidity(); + if (validity != null) { + Object[] definitionAndValidity = {definition, validity}; + definitionCache.put(key, definitionAndValidity); } } @@ -130,10 +186,11 @@ } public void dispose() { - bindingBuilderSelector.dispose(); - bindingBuilderSelector = null; - serviceManager.release(datatypeManager); - datatypeManager = null; + this.bindingBuilderSelector.dispose(); + this.bindingBuilderSelector = null; + this.serviceManager.release(this.datatypeManager); + this.datatypeManager = null; + this.definitionCache = null; } /**