You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by an...@apache.org on 2005/06/04 00:57:36 UTC

svn commit: r179906 - /cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DefaultSelectionListBuilder.java /cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DynamicSelectionList.java

Author: antonio
Date: Fri Jun  3 15:57:34 2005
New Revision: 179906

URL: http://svn.apache.org/viewcvs?rev=179906&view=rev
Log:
Improved dynamic selection list performance inside repeaters. Deprecate @dynamic in <fd:selection-list> in favor of @cache. Posible @cache values are: request, static, none. @cache='static' is equivalent to @dynamic='false'. @cache='none' is equivalent to @dynamic='true'. @cache='request' is new, similar to @dynamic='true' + caching the selection list per request.

Modified:
    cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DefaultSelectionListBuilder.java
    cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DynamicSelectionList.java

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DefaultSelectionListBuilder.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DefaultSelectionListBuilder.java?rev=179906&r1=179905&r2=179906&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DefaultSelectionListBuilder.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DefaultSelectionListBuilder.java Fri Jun  3 15:57:34 2005
@@ -19,6 +19,9 @@
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
 import org.w3c.dom.Node;
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.avalon.framework.service.Serviceable;
@@ -27,6 +30,7 @@
 import org.apache.cocoon.forms.datatype.convertor.DefaultFormatCache;
 import org.apache.cocoon.forms.datatype.convertor.ConversionResult;
 import org.apache.cocoon.forms.util.DomHelper;
+import org.apache.cocoon.util.Deprecation;
 import org.apache.excalibur.source.Source;
 import org.apache.excalibur.source.SourceResolver;
 import org.apache.excalibur.xml.sax.XMLizable;
@@ -45,9 +49,14 @@
  *
  * @version $Id$
  */
-public class DefaultSelectionListBuilder implements SelectionListBuilder, Serviceable {
+public class DefaultSelectionListBuilder implements SelectionListBuilder, Serviceable, Contextualizable {
 
     private ServiceManager serviceManager;
+    private Context context;
+
+    public void contextualize(Context context) throws ContextException {
+        this.context = context;
+    }
 
     public void service(ServiceManager manager) throws ServiceException {
         this.serviceManager = manager;
@@ -57,18 +66,35 @@
         SelectionList selectionList;
         String src = selectionListElement.getAttribute("src");
         if (src.length() > 0) {
-            boolean dynamic = DomHelper.getAttributeAsBoolean(selectionListElement, "dynamic", false);
-            if (!dynamic) {
-                selectionListElement = readSelectionList(src);
-                selectionList = buildStaticList(selectionListElement, datatype);
+            boolean dynamic = false;
+            boolean usePerRequestCache = false;
+            String cacheType = DomHelper.getAttribute(selectionListElement, "cache", null);
+
+            // Read @cache 
+            if ("request".equals(cacheType)) { // Dynamic SelectionList cached per request
+                dynamic = true;
+                usePerRequestCache = true;
+            } else if ("none".equals(cacheType)){ // Dynamic SelectionList non cached
+                dynamic = true;
+            } else if ("static".equals(cacheType)) {
+                // Static SelectionList (default values)
+            } else { // Checking for deprecated @dynamic
+                if (DomHelper.getAttribute(selectionListElement, "dynamic", null) != null) {
+                    Deprecation.logger.warn("'@dynamic' is deprecated in <fd:selection-list> and replaced by '@cache' at " + DomHelper.getLocation(selectionListElement));                    
+                }
+                dynamic = DomHelper.getAttributeAsBoolean(selectionListElement, "dynamic", false);
+            }
+            // Create SelectionList
+            if (dynamic) {
+                selectionList = new DynamicSelectionList(datatype, src, usePerRequestCache, serviceManager, context);
             } else {
-                selectionList = new DynamicSelectionList(datatype, src, serviceManager);
+                selectionListElement = readSelectionList(src);
+                selectionList = buildStaticList(selectionListElement, datatype);                
             }
         } else {
             // selection list is defined inline
             selectionList = buildStaticList(selectionListElement, datatype);
         }
-
         return selectionList;
     }
 

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DynamicSelectionList.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DynamicSelectionList.java?rev=179906&r1=179905&r2=179906&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DynamicSelectionList.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/datatype/DynamicSelectionList.java Fri Jun  3 15:57:34 2005
@@ -18,22 +18,28 @@
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
 import org.xml.sax.Attributes;
+import org.apache.avalon.framework.context.Context;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.excalibur.source.SourceResolver;
 import org.apache.excalibur.source.Source;
 import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.components.ContextHelper;
 import org.apache.cocoon.components.source.SourceUtil;
+import org.apache.cocoon.environment.Request;
 import org.apache.cocoon.forms.Constants;
 import org.apache.cocoon.forms.datatype.convertor.Convertor;
 import org.apache.cocoon.forms.datatype.convertor.DefaultFormatCache;
 import org.apache.cocoon.forms.datatype.convertor.ConversionResult;
 import org.apache.cocoon.xml.AttributesImpl;
 import org.apache.cocoon.xml.AbstractXMLPipe;
+import org.apache.cocoon.xml.SaxBuffer;
 import org.apache.cocoon.xml.XMLUtils;
 import org.apache.cocoon.xml.dom.DOMBuilder;
 import org.w3c.dom.Element;
 
 import java.io.IOException;
+import java.rmi.server.UID;
+import java.util.Enumeration;
 import java.util.Locale;
 
 /**
@@ -48,10 +54,35 @@
  */
 public class DynamicSelectionList implements SelectionList {
     private String src;
+    private boolean usePerRequestCache;
     private Datatype datatype;
     private ServiceManager serviceManager;
+    private Context context;
 
+    /**
+     * @param datatype
+     * @param src
+     * @param usePerRequestCache 
+     * @param serviceManager
+     * @param context
+     */
+    public DynamicSelectionList(Datatype datatype, String src, boolean usePerRequestCache, ServiceManager serviceManager, Context context) {
+        this.datatype = datatype;
+        this.src = src;
+        this.serviceManager = serviceManager;
+        this.usePerRequestCache = usePerRequestCache;
+        this.context = context;
+    }
+
+    /**
+     * Creates a DynamicSelectionList without caching
+     * @param datatype - 
+     * @param src - 
+     * @param serviceManager -
+     */
     public DynamicSelectionList(Datatype datatype, String src, ServiceManager serviceManager) {
+        this.usePerRequestCache = false;
+        this.context = null;
         this.datatype = datatype;
         this.src = src;
         this.serviceManager = serviceManager;
@@ -71,8 +102,11 @@
         handler.setContentHandler(contentHandler);
         SourceUtil.toSAX(serviceManager, source, null, handler);
     }
-
-    public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
+    
+    /*
+     * This method generate SaxFragment directly from source.
+     */
+    private void generateSaxFragmentFromSrc(ContentHandler contentHandler, Locale locale) throws SAXException {
         SourceResolver sourceResolver = null;
         Source source = null;
         try {
@@ -85,10 +119,43 @@
             throw new SAXException("Error while generating selection list: " + e.getMessage(), e);
         } finally {
             if (sourceResolver != null) {
-                if (source != null)
+                if (source != null) {
                     try { sourceResolver.release(source); } catch (Exception e) {}
+                }
                 serviceManager.release(sourceResolver);
             }
+        }
+    }
+
+    public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
+
+        if (usePerRequestCache) {
+            // Search the cacheID in request attributes
+            Request request = ContextHelper.getRequest(this.context);
+            Enumeration enumeration = request.getAttributeNames();
+            boolean cacheFound = false;
+            String name = null;
+            while (enumeration.hasMoreElements()) {
+                name = (String)enumeration.nextElement();
+                if (name.startsWith(src)) {
+                    cacheFound = true;
+                    break;
+                }
+            }
+            SaxBuffer saxBuffer;
+            if (cacheFound) {
+                saxBuffer = (SaxBuffer)request.getAttribute(name);
+            } else {
+                // Generate the usePerRequestCache and store in a request attribute.
+                saxBuffer = new SaxBuffer();
+                generateSaxFragmentFromSrc(saxBuffer, locale);
+                String cacheID = (new UID()).toString();
+                request.setAttribute(src + cacheID, saxBuffer);
+            }
+            // Output the stored saxBuffer to the contentHandler
+            saxBuffer.toSAX(contentHandler);
+        } else { // We don't use usePerRequestCache => re-read from the source.
+            generateSaxFragmentFromSrc(contentHandler, locale);
         }
     }