You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by re...@apache.org on 2007/03/27 18:09:15 UTC

svn commit: r522980 - /cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/

Author: reinhard
Date: Tue Mar 27 09:09:14 2007
New Revision: 522980

URL: http://svn.apache.org/viewvc?view=rev&rev=522980
Log:
- first shot on making the CachingSource fail-safe after it worked once
  (currently it's only tested for XMLizeable sources)

Modified:
    cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSource.java
    cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSourceFactory.java
    cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/InspectableTraversableCachingSource.java
    cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/TraversableCachingSource.java

Modified: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSource.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSource.java?view=diff&rev=522980&r1=522979&r2=522980
==============================================================================
--- cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSource.java (original)
+++ cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSource.java Tue Mar 27 09:09:14 2007
@@ -83,6 +83,7 @@
 
     public static final String CACHE_EXPIRES_PARAM = "cache-expires";
     public static final String CACHE_NAME_PARAM = "cache-name";
+    public static final String CACHE_FAIL_PARAM = "cache-fail";
 
     private static final SourceMeta DUMMY = new SourceMeta();
 
@@ -100,16 +101,17 @@
     /** The source object for the real content */
     protected Source source;
 
-
     /** The ServiceManager */
     protected ServiceManager manager;
 
     /** The current cache */
     protected Cache cache;
 
-
     /** The cached response (if any) */
     private CachedSourceResponse response;
+    
+    /** The cached response (if any) */
+    private CachedSourceResponse previousResponse;
 
     /** Did we just update meta info? */
     private boolean freshMeta;
@@ -117,8 +119,11 @@
     /** The key used in the store */
     final protected IdentifierCacheKey cacheKey;
 
-    /** number of seconds before cached object becomes invalid */
+    /** Number of seconds before cached object becomes invalid */
     final protected int expires;
+    
+    /** Fail because of a syncronous refresh excpetion */
+    final protected boolean fail;
 
     /** cache key extension */
     final protected String cacheName;
@@ -138,7 +143,8 @@
                          final int expires,
                          final String cacheName,
                          final boolean async,
-                         final boolean eventAware) {
+                         final boolean eventAware,
+                         final boolean fail) {
         this.protocol = protocol;
         this.uri = uri;
         this.sourceUri = sourceUri;
@@ -147,6 +153,7 @@
         this.cacheName = cacheName;
         this.async = async;
         this.eventAware = eventAware;
+        this.fail = fail;
 
         String key = "source:" + getSourceURI();
         if (cacheName != null) {
@@ -177,6 +184,10 @@
         }
 
         this.response = (CachedSourceResponse) this.cache.get(this.cacheKey);
+        // save response if subsequent request should never fail
+        if(!fail) {
+            this.previousResponse = this.response;
+        }
 
         if (this.response == null) {
             if (getLogger().isDebugEnabled()) {
@@ -202,6 +213,7 @@
      */
     public void dispose() {
         this.response = null;
+        this.previousResponse = null;
         this.source = null;
         this.manager = null;
         this.cache = null;
@@ -230,7 +242,9 @@
 
     private void clearResponse() {
         this.response = null;
-        this.cache.remove(this.cacheKey);
+        if(!fail) {
+            this.cache.remove(this.cacheKey);
+        }
     }
 
     /**
@@ -283,19 +297,34 @@
     protected byte[] getXMLResponse() throws SAXException, IOException, CascadingIOException {
         CachedSourceResponse response = getResponse();
 
-        if (response.getXMLResponse() == null) {
-            if (!this.freshMeta) {
-                /* always refresh meta in this case */
-                response.setExtra(readMeta(this.source));
-                this.freshMeta = true;
+        try {
+            if (response.getXMLResponse() == null) {
+                if (!this.freshMeta) {
+                    /* always refresh meta in this case */
+                    response.setExtra(readMeta(this.source));
+                    this.freshMeta = true;
+                }
+                if (((SourceMeta) response.getExtra()).exists()) {
+                    if (response.getBinaryResponse() == null) {
+                        response.setBinaryResponse(readBinaryResponse(this.source));
+                    }
+                    response.setXMLResponse(readXMLResponse(this.source, response.getBinaryResponse(), this.manager));
+                }
+                setResponse(response);
             }
-            if (((SourceMeta) response.getExtra()).exists()) {
-                if (response.getBinaryResponse() == null) {
-                    response.setBinaryResponse(readBinaryResponse(this.source));
+        } catch (Exception e) {
+            if(!fail && this.previousResponse != null) {
+                response = cloneResponse(this.previousResponse);
+                setResponse(cloneResponse(response));       
+            } else { // rethrow exception
+                if (e instanceof SAXException) {
+                    throw (SAXException) e;
+                } else if (e instanceof IOException) {
+                    throw (IOException) e;
+                } else if (e instanceof CascadingIOException) {
+                    throw (IOException) e;
                 }
-                response.setXMLResponse(readXMLResponse(this.source, response.getBinaryResponse(), this.manager));
             }
-            setResponse(response);
         }
 
         return response.getXMLResponse();
@@ -308,6 +337,15 @@
             // Could not initialize meta. Return default meta values.
             return DUMMY;
         }
+    }
+    
+    private CachedSourceResponse cloneResponse(final CachedSourceResponse orig) {
+        CachedSourceResponse c = new CachedSourceResponse(this.getCacheValidities());
+        c.setBinaryResponse(orig.getBinaryResponse());
+        c.setContentType(orig.getContentType());
+        c.setExtra(orig.getExtra());
+        c.setXMLResponse(orig.getXMLResponse());
+        return c;
     }
 
     // ---------------------------------------------------- Source implementation

Modified: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSourceFactory.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSourceFactory.java?view=diff&rev=522980&r1=522979&r2=522980
==============================================================================
--- cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSourceFactory.java (original)
+++ cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSourceFactory.java Tue Mar 27 09:09:14 2007
@@ -325,9 +325,10 @@
 
         int expires = params.getParameterAsInteger(CachingSource.CACHE_EXPIRES_PARAM, defaultExpires);
         String cacheName = params.getParameter(CachingSource.CACHE_NAME_PARAM, null);
+        boolean fail = params.getParameterAsBoolean(CachingSource.CACHE_FAIL_PARAM, false);
 
         Source source = this.resolver.resolveURI(uri);
-        return createCachingSource(location, uri, source, expires, cacheName);
+        return createCachingSource(location, uri, source, expires, cacheName, fail);
     }
 
     /**
@@ -337,7 +338,8 @@
                                                 String wrappedUri,
                                                 Source wrappedSource,
                                                 int expires,
-                                                String cacheName)
+                                                String cacheName,
+                                                boolean fail)
     throws SourceException {
 
         CachingSource source;
@@ -352,7 +354,8 @@
                                                                  expires,
                                                                  cacheName,
                                                                  isAsync(),
-                                                                 eventAware);
+                                                                 eventAware,
+                                                                 fail);
             } else {
                 source = new TraversableCachingSource(this,
                                                       getScheme(),
@@ -362,7 +365,8 @@
                                                       expires,
                                                       cacheName,
                                                       isAsync(),
-                                                      eventAware);
+                                                      eventAware,
+                                                      fail);
             }
         } else {
             source = new CachingSource(getScheme(),
@@ -372,7 +376,8 @@
                                        expires,
                                        cacheName,
                                        isAsync(),
-                                       eventAware);
+                                       eventAware,
+                                       fail);
         }
 
         // set the required components directly for speed

Modified: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/InspectableTraversableCachingSource.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/InspectableTraversableCachingSource.java?view=diff&rev=522980&r1=522979&r2=522980
==============================================================================
--- cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/InspectableTraversableCachingSource.java (original)
+++ cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/InspectableTraversableCachingSource.java Tue Mar 27 09:09:14 2007
@@ -43,8 +43,9 @@
                                                int expires,
                                                String cacheName,
                                                boolean async,
-                                               boolean eventAware) {
-        super(factory, protocol, uri, sourceUri, (TraversableSource) source, expires, cacheName, async, eventAware);
+                                               boolean eventAware, 
+                                               boolean fail) {
+        super(factory, protocol, uri, sourceUri, (TraversableSource) source, expires, cacheName, async, eventAware, fail);
     }
 
     private InspectableSource getInspectableSource() {

Modified: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/TraversableCachingSource.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/TraversableCachingSource.java?view=diff&rev=522980&r1=522979&r2=522980
==============================================================================
--- cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/TraversableCachingSource.java (original)
+++ cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/TraversableCachingSource.java Tue Mar 27 09:09:14 2007
@@ -40,8 +40,9 @@
                                     int expires,
                                     String cacheName,
                                     boolean async,
-                                    boolean eventAware) {
-        super(protocol, uri, sourceUri, source, expires, cacheName, async, eventAware);
+                                    boolean eventAware, 
+                                    boolean fail) {
+        super(protocol, uri, sourceUri, source, expires, cacheName, async, eventAware, fail);
         this.factory = factory;
     }
 
@@ -154,7 +155,7 @@
 
     protected final TraversableCachingSource createSource(String uri, String wrappedUri, Source wrapped)
     throws SourceException {
-        return (TraversableCachingSource) this.factory.createCachingSource(uri, wrappedUri, wrapped, expires, cacheName);
+        return (TraversableCachingSource) this.factory.createCachingSource(uri, wrappedUri, wrapped, expires, cacheName, fail);
     }
 
     protected SourceMeta readMeta(Source source) throws SourceException {