You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ti...@apache.org on 2004/02/05 22:29:17 UTC
cvs commit: cocoon-2.1/src/blocks/woody/conf woody-cache.xroles
tim 2004/02/05 13:29:17
Modified: src/blocks/woody/java/org/apache/cocoon/woody
DefaultFormManager.java
src/blocks/woody/java/org/apache/cocoon/woody/binding
JXPathBindingManager.java
Added: src/blocks/woody/java/org/apache/cocoon/woody
CacheManager.java DefaultCacheManager.java
src/blocks/woody/conf woody-cache.xroles
Log:
Make the Woody form definitions and bindings use a shared cache.
Revision Changes Path
1.13 +7 -46 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/DefaultFormManager.java
Index: DefaultFormManager.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/DefaultFormManager.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- DefaultFormManager.java 5 Feb 2004 20:53:20 -0000 1.12
+++ DefaultFormManager.java 5 Feb 2004 21:29:17 -0000 1.13
@@ -63,12 +63,11 @@
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.woody.CacheManager;
import org.apache.cocoon.woody.formmodel.*;
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.xml.sax.InputSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -80,13 +79,15 @@
extends AbstractLogEnabled
implements FormManager, ThreadSafe, Serviceable, Disposable, Configurable, Component, Initializable {
+ protected static final String PREFIX = "WoodyForm:";
protected ServiceManager manager;
protected Configuration configuration;
protected SimpleServiceSelector widgetDefinitionBuilderSelector;
- protected FastHashMap formDefinitionCache = new FastHashMap();
+ protected CacheManager cacheManager;
public void service(ServiceManager serviceManager) throws ServiceException {
this.manager = serviceManager;
+ this.cacheManager = (CacheManager)serviceManager.lookup(CacheManager.ROLE);
}
/**
@@ -129,7 +130,7 @@
}
public FormDefinition getFormDefinition(Source source) throws Exception {
- FormDefinition formDefinition = getStoredFormDefinition(source);
+ FormDefinition formDefinition = (FormDefinition)this.cacheManager.get(source, PREFIX);
if (formDefinition == null) {
Document formDocument;
try {
@@ -149,57 +150,17 @@
FormDefinitionBuilder formDefinitionBuilder = (FormDefinitionBuilder)widgetDefinitionBuilderSelector.select("form");
formDefinition = (FormDefinition)formDefinitionBuilder.buildWidgetDefinition(formElement);
- storeFormDefinition(formDefinition, source);
+ this.cacheManager.set(formDefinition, source, PREFIX);
}
return formDefinition;
}
- protected FormDefinition getStoredFormDefinition(Source source) {
- String key = "WoodyForm:" + source.getURI();
- SourceValidity newValidity = source.getValidity();
-
- if (newValidity == null) {
- formDefinitionCache.remove(key);
- return null;
- }
-
- Object[] formDefinitionAndValidity = (Object[])formDefinitionCache.get(key);
- if (formDefinitionAndValidity == null)
- return null;
-
- SourceValidity storedValidity = (SourceValidity)formDefinitionAndValidity[1];
- int valid = storedValidity.isValid();
- boolean isValid;
- if (valid == 0) {
- valid = storedValidity.isValid(newValidity);
- isValid = (valid == 1);
- } else {
- isValid = (valid == 1);
- }
-
- if (!isValid) {
- formDefinitionCache.remove(key);
- return null;
- }
-
- return (FormDefinition)formDefinitionAndValidity[0];
- }
-
- protected void storeFormDefinition(FormDefinition formDefinition, Source source) throws IOException {
- String key = "WoodyForm:" + source.getURI();
- SourceValidity validity = source.getValidity();
- if (validity != null) {
- Object[] formDefinitionAndValidity = {formDefinition, validity};
- formDefinitionCache.put(key, formDefinitionAndValidity);
- }
- }
-
/**
* Disposable
*/
public void dispose() {
widgetDefinitionBuilderSelector.dispose();
this.manager = null;
- this.formDefinitionCache = null;
+ this.cacheManager = null;
}
}
1.1 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/CacheManager.java
Index: CacheManager.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.woody;
import java.io.IOException;
import org.apache.excalibur.source.Source;
/**
* Work interface for the component that caches objects for Woody.
*/
public interface CacheManager {
String ROLE = CacheManager.class.getName();
/**
* Retrieves an object from the cache.
*/
Object get(Source source, String prefix);
/**
* Saves an object in the cache.
*/
void set(Object object, Source source, String prefix) throws IOException;
}
1.1 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/DefaultCacheManager.java
Index: DefaultCacheManager.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.woody;
import java.io.IOException;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceValidity;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.commons.collections.FastHashMap;
/**
* Component implementing the {@link CacheManager} role.
*/
public class DefaultCacheManager
extends AbstractLogEnabled
implements CacheManager, ThreadSafe, Serviceable, Disposable, Configurable, Component {
protected ServiceManager manager;
protected Configuration configuration;
protected FastHashMap cache = new FastHashMap();
public void service(ServiceManager serviceManager) throws ServiceException {
this.manager = serviceManager;
}
/**
* Configurable
*/
public void configure(Configuration configuration) throws ConfigurationException {
this.configuration = configuration;
}
public Object get(Source source, String prefix) {
String key = prefix + source.getURI();
SourceValidity newValidity = source.getValidity();
if (newValidity == null) {
cache.remove(key);
return null;
}
Object[] objectAndValidity = (Object[])cache.get(key);
if (objectAndValidity == null)
return null;
SourceValidity storedValidity = (SourceValidity)objectAndValidity[1];
int valid = storedValidity.isValid();
boolean isValid;
if (valid == 0) {
valid = storedValidity.isValid(newValidity);
isValid = (valid == 1);
} else {
isValid = (valid == 1);
}
if (!isValid) {
cache.remove(key);
return null;
}
return objectAndValidity[0];
}
public void set(Object object, Source source, String prefix) throws IOException {
String key = prefix + source.getURI();
SourceValidity validity = source.getValidity();
if (validity != null) {
Object[] objectAndValidity = {object, validity};
cache.put(key, objectAndValidity);
}
}
/**
* Disposable
*/
public void dispose() {
this.manager = null;
this.cache = null;
}
}
1.18 +17 -67 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.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- JXPathBindingManager.java 3 Feb 2004 12:26:21 -0000 1.17
+++ JXPathBindingManager.java 5 Feb 2004 21:29:17 -0000 1.18
@@ -57,14 +57,14 @@
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.woody.CacheManager;
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;
@@ -82,17 +82,17 @@
implements BindingManager, Serviceable, Disposable,
Initializable, Configurable, ThreadSafe {
+ private static final String PREFIX = "WoodyBinding:";
private ServiceManager serviceManager;
private DatatypeManager datatypeManager;
private Configuration configuration;
private SimpleServiceSelector bindingBuilderSelector;
-
- protected static final String bindingKeyPrefix = "WoodyBinding:";
- protected FastHashMap definitionCache = new FastHashMap();
+ private CacheManager cacheManager;
public void service(ServiceManager serviceManager) throws ServiceException {
this.serviceManager = serviceManager;
this.datatypeManager = (DatatypeManager)serviceManager.lookup(DatatypeManager.ROLE);
+ this.cacheManager = (CacheManager)serviceManager.lookup(CacheManager.ROLE);
}
public void configure(Configuration configuration) throws ConfigurationException {
@@ -106,12 +106,8 @@
}
public Binding createBinding(Source source) throws BindingException {
- return getBindingDefinition(source);
- }
-
- public Binding getBindingDefinition(Source source) throws BindingException {
- Binding bindingDefinition = getStoredBindingDefinition(source);
- if (bindingDefinition == null) {
+ Binding binding = (Binding)this.cacheManager.get(source, PREFIX);
+ if (binding == null) {
try {
InputSource is = new InputSource(source.getInputStream());
is.setSystemId(source.getURI());
@@ -119,66 +115,20 @@
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);
+ binding = getBuilderAssistant().getBindingForConfigurationElement(rootElm);
+ ((JXPathBindingBase)binding).enableLogging(getLogger());
+ getLogger().debug("Creation of new Binding finished. " + binding);
} else {
getLogger().debug("Root Element of said binding file is in wrong namespace.");
}
- storeBindingDefinition(bindingDefinition, source);
+ this.cacheManager.set(binding, source, PREFIX);
} catch (BindingException e) {
throw e;
} catch (Exception e) {
throw new BindingException("Error creating binding from " + source.getURI(), e);
}
}
- return bindingDefinition;
- }
-
- protected Binding getStoredBindingDefinition(Source source) {
- return 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);
- }
+ return binding;
}
private Assistant getBuilderAssistant() {
@@ -190,7 +140,7 @@
this.bindingBuilderSelector = null;
this.serviceManager.release(this.datatypeManager);
this.datatypeManager = null;
- this.definitionCache = null;
+ this.cacheManager = null;
}
/**
@@ -198,12 +148,12 @@
* childBindings to recursively
*
* This patterns was chosen to prevent Inversion Of Control between
- * this factory and its builder classes (that could be provided by third
- * parties)
+ * this factory and its builder classes (that could be provided by
+ * third parties.)
*/
/* NOTE: To get access to the logger in this inner class you must not call
* getLogger() as with JDK 1.3 this gives a NoSuchMethod error. You need to
- * implement an explicite access method for the logger in the outer class.
+ * implement an explicit access method for the logger in the outer class.
*/
public class Assistant {
1.1 cocoon-2.1/src/blocks/woody/conf/woody-cache.xroles
Index: woody-cache.xroles
===================================================================
<?xml version="1.0"?>
<xroles xpath="/role-list" unless="role[@name='org.apache.cocoon.woody.CacheManager']">
<role name="org.apache.cocoon.woody.CacheManager"
shorthand="woody-cachemanager"
default-class="org.apache.cocoon.woody.DefaultCacheManager"/>
</xroles>