cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r232770 - in /cocoon: blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/ blocks/portal/trunk/samples/coplets/docs/ blocks/portal/trunk/samples/profiles/copletdata/ trunk/
Date Mon, 15 Aug 2005 08:22:44 GMT
Author: cziegeler
Date: Mon Aug 15 01:22:33 2005
New Revision: 232770

URL: http://svn.apache.org/viewcvs?rev=232770&view=rev
Log:
Portal block: Add global caching to caching uri adapter.

Modified:
    cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/AbstractCopletAdapter.java
    cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/ApplicationCopletAdapter.java
    cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/CachingURICopletAdapter.java
    cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/URICopletAdapter.java
    cocoon/blocks/portal/trunk/samples/coplets/docs/portal-demo.xml
    cocoon/blocks/portal/trunk/samples/profiles/copletdata/portal.xml
    cocoon/trunk/status.xml

Modified: cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/AbstractCopletAdapter.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/AbstractCopletAdapter.java?rev=232770&r1=232769&r2=232770&view=diff
==============================================================================
--- cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/AbstractCopletAdapter.java
(original)
+++ cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/AbstractCopletAdapter.java
Mon Aug 15 01:22:33 2005
@@ -94,6 +94,22 @@
     }
     
     /**
+     * Get a configuration value
+     * First the coplet data is queried and if it doesn't provide an
+     * attribute with the given name, the coplet base data is used.
+     * If no value is found the default value is returned.
+     */
+    protected Object getConfiguration(CopletInstanceData coplet,
+                                      String key,
+                                      Object defaultValue) {
+        Object data = this.getConfiguration(coplet, key);
+        if ( data == null ) {
+            data = defaultValue;
+        }
+        return data;
+    }
+
+    /**
      * Implement this and not toSAX()
      */
     public abstract void streamContent(CopletInstanceData coplet, 

Modified: cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/ApplicationCopletAdapter.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/ApplicationCopletAdapter.java?rev=232770&r1=232769&r2=232770&view=diff
==============================================================================
--- cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/ApplicationCopletAdapter.java
(original)
+++ cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/ApplicationCopletAdapter.java
Mon Aug 15 01:22:33 2005
@@ -43,7 +43,7 @@
 
 /**
  * This coplet adapter is used to connect to external applications that are 
- * plugged into the portal
+ * plugged into the portal.
  *
  * @author <a href="mailto:gerald.kahrer@rizit.at">Gerald Kahrer</a>
  * 
@@ -51,19 +51,21 @@
  */
 public class ApplicationCopletAdapter extends CachingURICopletAdapter {
 
-    public void streamContent(
-        final CopletInstanceData coplet,
-        final String uri,
-        final ContentHandler contentHandler)
+    /**
+     * @see org.apache.cocoon.portal.coplet.adapter.impl.URICopletAdapter#streamContent(org.apache.cocoon.portal.coplet.CopletInstanceData,
java.lang.String, org.xml.sax.ContentHandler)
+     */
+    public void streamContent(final CopletInstanceData coplet,
+                              final String uri,
+                              final ContentHandler contentHandler)
     throws SAXException {
         try {
             super.streamContent(coplet, uri, contentHandler);
         } catch (SAXException se) {
-            getLogger().error(
+            this.getLogger().error(
                 "ApplicationCopletAdapter: Exception while getting coplet resource",
                 se);
 
-            renderErrorContent(coplet, contentHandler);
+            this.renderErrorContent(coplet, contentHandler);
         }
     }
 
@@ -91,8 +93,8 @@
                 // this is a normal link event, so save the url in the instance data
                 // for ProxyTransformer
                 String linkValue = event.getLink();
-                Boolean addParams = (Boolean)this.getConfiguration(coplet, "appendParameters");
-                if ( addParams != null && addParams.booleanValue() ) {
+                Boolean addParams = (Boolean)this.getConfiguration(coplet, "appendParameters",
Boolean.FALSE);
+                if ( addParams.booleanValue() ) {
                     final StringBuffer uri = new StringBuffer(event.getLink());
                     boolean hasParams = (uri.toString().indexOf("?") != -1);
                     

Modified: cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/CachingURICopletAdapter.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/CachingURICopletAdapter.java?rev=232770&r1=232769&r2=232770&view=diff
==============================================================================
--- cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/CachingURICopletAdapter.java
(original)
+++ cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/CachingURICopletAdapter.java
Mon Aug 15 01:22:33 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 1999-2002,2004 The Apache Software Foundation.
+ * Copyright 1999-2002,2004-2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -15,173 +15,135 @@
  */
 package org.apache.cocoon.portal.coplet.adapter.impl;
 
-import org.apache.avalon.framework.parameters.Parameterizable;
-import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.caching.Cache;
+import org.apache.cocoon.caching.CachedResponse;
 import org.apache.cocoon.components.sax.XMLByteStreamCompiler;
 import org.apache.cocoon.components.sax.XMLByteStreamInterpreter;
 import org.apache.cocoon.portal.PortalService;
 import org.apache.cocoon.portal.coplet.CopletInstanceData;
 import org.apache.cocoon.portal.event.CopletInstanceEvent;
+import org.apache.excalibur.source.SourceValidity;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
 
 /**
- * This is the adapter to use pipelines as coplets. The result of the called 
- * pipeline is cached until a 
- * {@link org.apache.cocoon.portal.event.CopletInstanceEvent}
- * for that coplet is received. Configuration options of super
- * classes apply.
+ * This adapter extends the {@link org.apache.cocoon.portal.coplet.adapter.impl.URICopletAdapter}
+ * by a caching mechanism. The result of the called uri/pipeline is cached until a 
+ * {@link org.apache.cocoon.portal.event.CopletInstanceEvent} for that coplet instance
+ * is received.
+ * The content can eiter be cached in the user session or globally. The default is
+ * the user session.
  *
  * @author <a href="mailto:gerald.kahrer@rizit.at">Gerald Kahrer</a>
  * 
  * @version CVS $Id$
  */
 public class CachingURICopletAdapter
-    extends URICopletAdapter
-    implements Parameterizable {
+    extends URICopletAdapter {
     
-    /**
-     * The cache for saving the coplet data
-     */
+    /** The attribute name for the storing the cached coplet content. */
     public static final String CACHE = "cacheData";
 
-    /**
-     * Marks the validity of the cached data
-     */
-    public static final String CACHE_VALIDITY = "cacheValidity";
-
-    /**
-     * Tells the adapter to not cache the current response
-     */
+    /** This attribute can be set on the instance to not cache the current response. */
     public static final String DO_NOT_CACHE = "doNotCache";
     
-    /**
-     * Marks cache valid.
-     */
-    public static final String CACHE_VALID = "1";
+    /** The cache to use for global caching. */
+    protected Cache cache;
 
     /**
-     * Marks cache invalid
+     * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
      */
-    public static final String CACHE_INVALID = "0";
-
-    /**
-     * Caching can be basically disabled with this parameter
-     */
-    public static final String PARAMETER_DISABLE_CACHING = "disable_caching";
+    public void service(ServiceManager manager) throws ServiceException {
+        super.service(manager);
+        this.cache = (Cache)this.manager.lookup(Cache.ROLE);
+    }
 
     /**
-     * instance variable, that shows, if caching is disabled
-     */
-    private boolean disableCaching = false;
-
-    /* (non-Javadoc)
-     * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
+     * @see org.apache.avalon.framework.activity.Disposable#dispose()
      */
-    public void parameterize(Parameters parameters) {
-        if (parameters != null) {
-            this.disableCaching = parameters.getParameterAsBoolean(PARAMETER_DISABLE_CACHING,
false);
-            if (this.disableCaching) {
-                getLogger().info(this.getClass().getName() + " Caching is disabled.");
-            } else {
-                getLogger().info(this.getClass().getName() + " Caching is enabled.");
-            }
+    public void dispose() {
+        if ( this.manager != null ) {
+            this.manager.release(this.cache);
+            this.cache = null;
         }
+        super.dispose();
     }
 
-    /* (non-Javadoc)
+    /**
      * @see org.apache.cocoon.portal.coplet.adapter.impl.AbstractCopletAdapter#streamContent(org.apache.cocoon.portal.coplet.CopletInstanceData,
org.xml.sax.ContentHandler)
      */
     public void streamContent(CopletInstanceData coplet, ContentHandler contentHandler)
     throws SAXException {
-        this.streamContent( coplet, (String) coplet.getCopletData().getAttribute("uri"),
contentHandler);
+        this.streamContent( coplet, 
+                            (String) coplet.getCopletData().getAttribute("uri"),
+                            contentHandler);
     }
 
-    /* (non-Javadoc)
+    /**
      * @see org.apache.cocoon.portal.coplet.adapter.impl.URICopletAdapter#streamContent(org.apache.cocoon.portal.coplet.CopletInstanceData,
java.lang.String, org.xml.sax.ContentHandler)
      */
     public void streamContent( final CopletInstanceData coplet,
                                final String uri,
                                final ContentHandler contentHandler)
     throws SAXException {
-        if (this.isValidCache(coplet)) {
-            this.toSAXFromCache(coplet, contentHandler);
-        } else {
-            XMLByteStreamCompiler bc = new XMLByteStreamCompiler();
-
-            super.streamContent(coplet, uri, bc);
-
-            if ( coplet.getAttribute(DO_NOT_CACHE) != null ) {
-                coplet.removeAttribute(DO_NOT_CACHE);
-                this.setCacheInvalid(coplet);
-                XMLByteStreamInterpreter bi = new XMLByteStreamInterpreter();
-                bi.setContentHandler(contentHandler);
-
-                bi.deserialize(bc.getSAXFragment());
+        // Is caching enabled?
+        boolean cachingEnabled = ((Boolean)this.getConfiguration(coplet, "cache-enabled",
Boolean.TRUE)).booleanValue();
+        // do we cache globally?
+        boolean cacheGlobal = ((Boolean)this.getConfiguration(coplet, "cache-global", Boolean.FALSE)).booleanValue();
+
+        Object data = null;
+        // If caching is enabed and the cache is still valid, then use the cache
+        if (cachingEnabled) {
+            if ( cacheGlobal ) {
+                final String key = this.getCacheKey(coplet, uri);
+                CachedResponse response = this.cache.get(key);
+                if (response != null ) {
+                    data = response.getResponse();
+                }
             } else {
-                this.toCache(coplet, bc.getSAXFragment());
-
-                this.toSAXFromCache(coplet, contentHandler);
+                data = coplet.getAttribute(CACHE);
             }
         }
-    }
-
-    /**
-     * Caches the data of the coplet resource in the coplet instance.
-     * @param coplet the coplet instance data
-     * @param data the data of the coplet resource
-     */
-    private void toCache(CopletInstanceData coplet, Object data) {
-        coplet.setAttribute(CACHE, data);
-
-        this.setCacheValid(coplet);
-    }
-
-    /**
-     * Creates SAX events from the cached coplet data.
-     * @param coplet the coplet instance data
-     * @param contentHandler the handler, that should receive the SAX events
-     * @throws SAXException
-     */
-    private void toSAXFromCache(CopletInstanceData coplet,
-                                ContentHandler contentHandler)
-    throws SAXException {
-        XMLByteStreamInterpreter bi = new XMLByteStreamInterpreter();
-        bi.setContentHandler(contentHandler);
+        if (data == null) {
+            // if caching is permanently or temporary disabled, flush the cache and invoke
coplet
+            if ( !cachingEnabled || coplet.getAttribute(DO_NOT_CACHE) != null ) {
+                coplet.removeAttribute(DO_NOT_CACHE);
+                if ( cacheGlobal ) {
+                    final String key = this.getCacheKey(coplet, uri);
+                    this.cache.remove(key); 
+                } else {
+                    coplet.removeAttribute(CACHE);
+                }
+                super.streamContent(coplet, uri, contentHandler);                
+            } else {
 
-        bi.deserialize(coplet.getAttribute(CACHE));
-    }
+                XMLByteStreamCompiler bc = new XMLByteStreamCompiler();
 
-    /**
-     * Tests the cache for validity.
-     * @param coplet the coplet instance data
-     */
-    public boolean isValidCache(CopletInstanceData coplet) {
-        if (disableCaching) {
-            return false;
+                super.streamContent(coplet, uri, bc);
+                data = bc.getSAXFragment();
+                if ( cacheGlobal ) {
+                    CachedResponse response = new CachedResponse((SourceValidity[])null,
(byte[])data);
+                    try {
+                        final String key = this.getCacheKey(coplet, uri);
+                        this.cache.store(key, response);
+                    } catch (ProcessingException pe) {
+                        // we ignore this
+                        this.getLogger().warn("Exception during storing response into cache.",
pe);
+                    }
+                } else {
+                    coplet.setAttribute(CACHE, data);
+                }
+            }
         }
-        String cacheValidity = (String) coplet.getAttribute(CACHE_VALIDITY);
-
-        if (cacheValidity == null) {
-            return false;
+        // and now stream the data
+        if ( data != null ) {
+            XMLByteStreamInterpreter bi = new XMLByteStreamInterpreter();
+            bi.setContentHandler(contentHandler);
+            bi.deserialize(data);
         }
-        return CACHE_VALID.equals(cacheValidity);
-    }
-
-    /**
-     * Sets the cache valid.
-     * @param coplet the coplet instance data
-     */
-    public void setCacheValid(CopletInstanceData coplet) {
-        coplet.setAttribute(CACHE_VALIDITY, CACHE_VALID);
-    }
-
-    /**
-     * Sets the cache invalid.
-     * @param coplet the coplet instance data
-     */
-    public void setCacheInvalid(CopletInstanceData coplet) {
-        coplet.setAttribute(CACHE_VALIDITY, CACHE_INVALID);
     }
 
     /**
@@ -201,7 +163,21 @@
     public void handleCopletInstanceEvent(CopletInstanceEvent event) {
         final CopletInstanceData coplet = (CopletInstanceData) event.getTarget();
 
-        this.setCacheInvalid(coplet);
+        // do we cache globally?
+        boolean cacheGlobal = ((Boolean)this.getConfiguration(coplet, "cache-global", Boolean.FALSE)).booleanValue();
+        if ( cacheGlobal ) {
+            final String key = this.getCacheKey(coplet,
+                                                (String) coplet.getCopletData().getAttribute("uri"));
+            this.cache.remove(key);
+        } else {
+            coplet.removeAttribute(CACHE);
+        }
     }
 
+    /**
+     * Build the key for the global cache.
+     */
+    protected String getCacheKey(CopletInstanceData coplet, String uri) {
+        return "coplet:" + coplet.getCopletData().getId() + '/' + uri;
+    }
 }

Modified: cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/URICopletAdapter.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/URICopletAdapter.java?rev=232770&r1=232769&r2=232770&view=diff
==============================================================================
--- cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/URICopletAdapter.java
(original)
+++ cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/coplet/adapter/impl/URICopletAdapter.java
Mon Aug 15 01:22:33 2005
@@ -63,14 +63,14 @@
     /** The application context */
     protected Context context;
     
-    /* (non-Javadoc)
+    /**
      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
      */
     public void contextualize(Context context) throws ContextException {
         this.context = context;
     }
     
-    /* (non-Javadoc)
+    /**
      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
      */
     public void service(ServiceManager manager) throws ServiceException {
@@ -100,11 +100,11 @@
 			if (uri.startsWith("cocoon:")) {
                 portalService = (PortalService)this.manager.lookup(PortalService.ROLE);
 
-                Boolean handlePars = (Boolean)this.getConfiguration( coplet, "handleParameters");
+                Boolean handlePars = (Boolean)this.getConfiguration( coplet, "handleParameters",
Boolean.FALSE);
                 
                 String sourceUri = uri;
                 
-                if ( handlePars != null && handlePars.booleanValue() ) {
+                if ( handlePars.booleanValue() ) {
                     List list = (List) portalService.getTemporaryAttribute(URICopletAdapter.class.getName());
                     if ( list != null && list.contains( coplet )) {
                         // add parameters

Modified: cocoon/blocks/portal/trunk/samples/coplets/docs/portal-demo.xml
URL: http://svn.apache.org/viewcvs/cocoon/blocks/portal/trunk/samples/coplets/docs/portal-demo.xml?rev=232770&r1=232769&r2=232770&view=diff
==============================================================================
--- cocoon/blocks/portal/trunk/samples/coplets/docs/portal-demo.xml (original)
+++ cocoon/blocks/portal/trunk/samples/coplets/docs/portal-demo.xml Mon Aug 15 01:22:33 2005
@@ -14,7 +14,7 @@
   See the License for the specific language governing permissions and
   limitations under the License.
 -->
-<!-- SVN $Id:$ -->
+<!-- SVN $Id$ -->
 <document> 
   <header> 
 	 <title>The Cocoon Portal</title>
@@ -43,12 +43,12 @@
         </p>
         <p>For more information <fork href="http://cocoon.apache.org">visit the
Cocoon Homepage</fork>.</p>
      </s1> 
-     <s1 title="The JSR-168">
-        <p>The Cocoon portal supports portlets conforming to the JSR-168. The JSR-168
contains 
-           a sample that uses the testsuite from the <fork href="http://jakarta.apache.org/pluto">Jakarta-Pluto
project</fork>.</p>
+     <s1 title="The JSR 168">
+        <p>The Cocoon portal supports portlets conforming to the JSR 168. The JSR 168
tab contains 
+           a sample using the testsuite from the <fork href="http://portals.apache.org/pluto">Pluto
project</fork>.</p>
         <p>These samples only work if you follow these installation instructions:</p>
         <ul>
-         <li>Get the jakarta pluto project and install it into Tomcat (Test Pluto now).</li>
+         <li>Get the Pluto project and install it into Tomcat (Test Pluto now).</li>
          <li>Install Cocoon as a web application in Tomcat and remove the Pluto webapp.
Please note,
              that it is currently not possible to start Cocoon directly from a war file;
it has 
              to be expanded.</li>

Modified: cocoon/blocks/portal/trunk/samples/profiles/copletdata/portal.xml
URL: http://svn.apache.org/viewcvs/cocoon/blocks/portal/trunk/samples/profiles/copletdata/portal.xml?rev=232770&r1=232769&r2=232770&view=diff
==============================================================================
--- cocoon/blocks/portal/trunk/samples/profiles/copletdata/portal.xml (original)
+++ cocoon/blocks/portal/trunk/samples/profiles/copletdata/portal.xml Mon Aug 15 01:22:33
2005
@@ -14,7 +14,7 @@
   See the License for the specific language governing permissions and
   limitations under the License.
 -->
-<!-- SVN $Id:$ -->
+<!-- SVN $Id$ -->
 <coplets>
    <!--
      + Different news coplets
@@ -140,10 +140,14 @@
    </coplet-data>
    <coplet-data id="Portal-Demo" name="standard">
       <title>Introduction</title>
-      <coplet-base-data>URICoplet</coplet-base-data>
+      <coplet-base-data>CachingURICoplet</coplet-base-data>
       <attribute>
          <name>uri</name>
          <value xsi:type="java:java.lang.String" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">cocoon:/coplets/docs/portal-demo.html</value>
+      </attribute>
+      <attribute>
+        <name>cache-global</name>
+        <value xsi:type="java:java.lang.Boolean" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">true</value>
       </attribute>
       <aspect>
         <name>mandatory</name>

Modified: cocoon/trunk/status.xml
URL: http://svn.apache.org/viewcvs/cocoon/trunk/status.xml?rev=232770&r1=232769&r2=232770&view=diff
==============================================================================
--- cocoon/trunk/status.xml (original)
+++ cocoon/trunk/status.xml Mon Aug 15 01:22:33 2005
@@ -518,6 +518,9 @@
    </action>
   </release>
   <release version="2.1.8" date="TBD">
+    <action dev="CZ" type="add">
+      Portal block: Add global caching to caching uri adapter.
+    </action>
     <action dev="AG" type="update">
       Updated xalan to 2.7.0.
     </action>



Mime
View raw message