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/28 17:30:53 UTC
svn commit: r523370 -
/cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/
Author: reinhard
Date: Wed Mar 28 08:30:52 2007
New Revision: 523370
URL: http://svn.apache.org/viewvc?view=rev&rev=523370
Log:
refactoring of CachingSource: factor out validity calculation
Added:
cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSourceValidityStrategy.java (with props)
cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/EventAwareCachingSourceValidityStrategy.java (with props)
cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/ExpiresCachingSourceValidityStrategy.java (with props)
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=523370&r1=523369&r2=523370
==============================================================================
--- 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 Wed Mar 28 08:30:52 2007
@@ -31,7 +31,6 @@
import org.apache.excalibur.source.SourceException;
import org.apache.excalibur.source.SourceNotFoundException;
import org.apache.excalibur.source.SourceValidity;
-import org.apache.excalibur.source.impl.validity.ExpiresValidity;
import org.apache.excalibur.source.impl.validity.TimeStampValidity;
import org.apache.excalibur.xml.sax.XMLizable;
import org.apache.excalibur.xmlizer.XMLizer;
@@ -39,10 +38,8 @@
import org.apache.cocoon.CascadingIOException;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.caching.Cache;
-import org.apache.cocoon.caching.EventAware;
import org.apache.cocoon.caching.IdentifierCacheKey;
-import org.apache.cocoon.caching.validity.EventValidity;
-import org.apache.cocoon.caching.validity.NamedEvent;
+
import org.apache.cocoon.components.sax.XMLByteStreamCompiler;
import org.apache.cocoon.components.sax.XMLByteStreamInterpreter;
import org.apache.cocoon.xml.ContentHandlerWrapper;
@@ -58,7 +55,7 @@
*
* <h2>Syntax for Protocol</h2>
* <pre>
- * cached:http://www.apache.org/[?cocoon:cache-expires=60&cocoon:cache-name=main]
+ * cached:http://www.apache.org/[?cocoon:cache-expires=60&cocoon:cache-name=main][&cocoon:cache-fail=true]
* </pre>
*
* <p>The above examples show how the real source <code>http://www.apache.org</code>
@@ -70,6 +67,10 @@
* Specifying <code>-1</code> will yield the cached response to be considered valid
* always. Value <code>0</code> can be used to achieve the exact opposite. That is to say,
* the cached contents will be thrown out and updated immediately and unconditionally.<p>
+ *
+ * <p>The <code>cache-fail</code> argument lets subsequent syncronous requests, that have to be
+ * refreshed, fail, in the case that the wrapped source can't be reached. The default value
+ * for <code>cache-fail</code> is <code>true</code>.
*
* @version $Id$
*/
@@ -131,10 +132,12 @@
/** asynchronic refresh strategy ? */
final protected boolean async;
- final protected boolean eventAware;
+
+ private CachingSourceValidityStrategy validityStrategy;
/**
* Construct a new object.
+ * @param validityStrategy2
*/
public CachingSource(final String protocol,
final String uri,
@@ -143,7 +146,7 @@
final int expires,
final String cacheName,
final boolean async,
- final boolean eventAware,
+ final CachingSourceValidityStrategy validityStrategy,
final boolean fail) {
this.protocol = protocol;
this.uri = uri;
@@ -152,7 +155,7 @@
this.expires = expires;
this.cacheName = cacheName;
this.async = async;
- this.eventAware = eventAware;
+ this.validityStrategy = validityStrategy;
this.fail = fail;
String key = "source:" + getSourceURI();
@@ -580,56 +583,17 @@
if (this.response == null) {
return false;
}
-
- if (eventAware) {
- if (getLogger().isDebugEnabled()) {
- getLogger().debug("Cached response of source does not expire");
- }
- return true;
- }
-
- final SourceValidity[] validities = this.response.getValidityObjects();
- boolean valid = true;
-
- final ExpiresValidity expiresValidity = (ExpiresValidity) validities[0];
- final SourceValidity sourceValidity = validities[1];
-
- if (expiresValidity.isValid() != SourceValidity.VALID) {
- int validity = sourceValidity != null? sourceValidity.isValid() : SourceValidity.INVALID;
- if (validity == SourceValidity.INVALID ||
- validity == SourceValidity.UNKNOWN &&
- sourceValidity.isValid(source.getValidity()) != SourceValidity.VALID) {
- if (getLogger().isDebugEnabled()) {
- getLogger().debug("Response expired, invalid for " + getSourceURI());
- }
- valid = false;
- } else {
- if (getLogger().isDebugEnabled()) {
- getLogger().debug("Response expired, still valid for " + getSourceURI());
- }
- // set new expiration period
- validities[0] = new ExpiresValidity(getExpiration());
- }
- } else {
- if (getLogger().isDebugEnabled()) {
- getLogger().debug("Response not expired for " + getSourceURI());
- }
- }
-
- return valid;
+ return this.validityStrategy.checkValidity(this.response, this.source, this.getExpiration());
}
protected SourceValidity[] getCacheValidities() {
- if (this.cache instanceof EventAware) {
- // use event caching strategy, the associated event is the source uri
- return new SourceValidity[] { new EventValidity(new NamedEvent(this.source.getURI())) };
- } else {
- // we need to store both the cache expiration and the original source validity
- // the former is to determine whether to recheck the latter (see checkValidity)
- return new SourceValidity[] { new ExpiresValidity(getExpiration()), source.getValidity() };
- }
+ return validityStrategy.getCacheValidities(this, source);
}
+ public void setValidityStrategy(CachingSourceValidityStrategy s) {
+ this.validityStrategy = s;
+ }
+
/**
* Data holder for caching Source meta info.
*/
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=523370&r1=523369&r2=523370
==============================================================================
--- 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 Wed Mar 28 08:30:52 2007
@@ -64,13 +64,15 @@
* <p>
* The URL needs to contain the URL of the cached source, an expiration
* period in seconds, and optionally a cache key:
- * <code>cached:http://www.apache.org/[?cocoon:cache-expires=60][&cocoon:cache-name=main]</code>.
+ * <code>cached:http://www.apache.org/[?cocoon:cache-expires=60][&cocoon:cache-name=main][&cocoon:cache-fail=true]</code>.
* </p>
* <p>
* The above examples shows how the real source <code>http://www.apache.org/</code>
* is wrapped and the cached contents is used for <code>60</code> seconds.
* The second querystring parameter instructs that the cache key be extended with the string
- * <code>main</code>. This allows the use of multiple cache entries for the same source.
+ * <code>main</code>. This allows the use of multiple cache entries for the same source. The <code>cache-fail</code>
+ * argument lets subsequent syncronous requests, that have to be refreshed, fail, in the case
+ * that the wrapped source can't be reached. The default value for <code>cache-fail</code> is <code>true</code>.
* </p>
* <p>
* This factory creates either instances of {@link org.apache.cocoon.components.source.impl.CachingSource}
@@ -131,6 +133,7 @@
private static final String CACHE_ROLE_PARAM = "cache-role";
private static final String REFRESHER_ROLE_PARAM = "refresher-role";
private static final String DEFAULT_EXPIRES_PARAM = "default-expires";
+ private static final String DEFAULT_VALIDITY_STRATEGY = ExpiresCachingSourceValidityStrategy.class.getName();
// ---------------------------------------------------- Instance variables
@@ -143,6 +146,12 @@
/** Event aware ? */
private boolean eventAware;
+ /** Validity strategy implementation: class name */
+ private String validityStrategyClassName = DEFAULT_VALIDITY_STRATEGY;
+
+ /** Validity strategy implementation: class name */
+ private CachingSourceValidityStrategy validityStrategy;
+
/** The role of the cache */
private String cacheRole;
@@ -188,6 +197,18 @@
// 'event-aware' parameter
this.eventAware = parameters.getParameterAsBoolean(EVENT_AWARE_PARAM, false);
+
+ if(this.eventAware == true) {
+ this.validityStrategyClassName =
+ "org.apache.cocoon.components.source.impl.EventAwareCachingSourceValidityStrategy";
+ }
+ try {
+ this.validityStrategy = (CachingSourceValidityStrategy)
+ getClass().getClassLoader().loadClass(this.validityStrategyClassName).newInstance();
+ } catch(Exception cce) {
+ throw new ConfigurationException("Can't create class for '" +
+ this.validityStrategyClassName + "'.", cce);
+ }
// 'cache-role' parameter
this.cacheRole = parameters.getParameter(CACHE_ROLE_PARAM, Cache.ROLE);
@@ -343,6 +364,14 @@
throws SourceException {
CachingSource source;
+
+ CachingSourceValidityStrategy validityStrategy;
+ if(this.eventAware) {
+ validityStrategy = new EventAwareCachingSourceValidityStrategy();
+ } else {
+ validityStrategy = new ExpiresCachingSourceValidityStrategy();
+
+ }
if (wrappedSource instanceof TraversableSource) {
if (wrappedSource instanceof InspectableSource) {
@@ -354,7 +383,7 @@
expires,
cacheName,
isAsync(),
- eventAware,
+ validityStrategy,
fail);
} else {
source = new TraversableCachingSource(this,
@@ -365,7 +394,7 @@
expires,
cacheName,
isAsync(),
- eventAware,
+ validityStrategy,
fail);
}
} else {
@@ -376,7 +405,7 @@
expires,
cacheName,
isAsync(),
- eventAware,
+ validityStrategy,
fail);
}
@@ -429,4 +458,4 @@
return SourceUtil.absolutize(baseURI, location, true);
}
-}
+}
\ No newline at end of file
Added: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSourceValidityStrategy.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSourceValidityStrategy.java?view=auto&rev=523370
==============================================================================
--- cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSourceValidityStrategy.java (added)
+++ cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSourceValidityStrategy.java Wed Mar 28 08:30:52 2007
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.source.impl;
+
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceValidity;
+
+/**
+ * @version $Id$
+ */
+public interface CachingSourceValidityStrategy {
+
+ SourceValidity[] getCacheValidities(CachingSource cachingSource, Source source);
+
+ boolean checkValidity(CachedSourceResponse response, Source source, long expires);
+
+}
Propchange: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSourceValidityStrategy.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSourceValidityStrategy.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSourceValidityStrategy.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/EventAwareCachingSourceValidityStrategy.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/EventAwareCachingSourceValidityStrategy.java?view=auto&rev=523370
==============================================================================
--- cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/EventAwareCachingSourceValidityStrategy.java (added)
+++ cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/EventAwareCachingSourceValidityStrategy.java Wed Mar 28 08:30:52 2007
@@ -0,0 +1,30 @@
+package org.apache.cocoon.components.source.impl;
+
+import org.apache.cocoon.caching.validity.EventValidity;
+import org.apache.cocoon.caching.validity.NamedEvent;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceValidity;
+
+/**
+ * Use event caching strategy, the associated event is the source uri
+ *
+ * @version $Id$
+ */
+public class EventAwareCachingSourceValidityStrategy implements CachingSourceValidityStrategy {
+
+ private Log logger = LogFactory.getLog(getClass());
+
+ public SourceValidity[] getCacheValidities(CachingSource cachingSource, Source source) {
+ return new SourceValidity[] { new EventValidity(new NamedEvent(source.getURI())) };
+ }
+
+ public boolean checkValidity(CachedSourceResponse response, Source source, long expires) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("Cached response of source does not expire");
+ }
+ return true;
+ }
+
+}
Propchange: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/EventAwareCachingSourceValidityStrategy.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/EventAwareCachingSourceValidityStrategy.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/EventAwareCachingSourceValidityStrategy.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/ExpiresCachingSourceValidityStrategy.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/ExpiresCachingSourceValidityStrategy.java?view=auto&rev=523370
==============================================================================
--- cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/ExpiresCachingSourceValidityStrategy.java (added)
+++ cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/ExpiresCachingSourceValidityStrategy.java Wed Mar 28 08:30:52 2007
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.source.impl;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceValidity;
+import org.apache.excalibur.source.impl.validity.ExpiresValidity;
+
+/**
+ * We need to store both the cache expiration and the original source validity
+ * the former is to determine whether to recheck the latter (see checkValidity)
+ *
+ * @version $Id$
+ */
+public class ExpiresCachingSourceValidityStrategy implements CachingSourceValidityStrategy {
+
+ private Log logger = LogFactory.getLog(getClass());
+
+ public SourceValidity[] getCacheValidities(CachingSource cachingSource, Source source) {
+ return new SourceValidity[] {
+ new ExpiresValidity(cachingSource.getExpiration()), source.getValidity() };
+ }
+
+ public boolean checkValidity(CachedSourceResponse response, Source source, long expires) {
+ final SourceValidity[] validities = response.getValidityObjects();
+ boolean valid = true;
+
+ final ExpiresValidity expiresValidity = (ExpiresValidity) validities[0];
+ final SourceValidity sourceValidity = validities[1];
+
+ if (expiresValidity.isValid() != SourceValidity.VALID) {
+ int validity = sourceValidity != null? sourceValidity.isValid() : SourceValidity.INVALID;
+ if (validity == SourceValidity.INVALID ||
+ validity == SourceValidity.UNKNOWN &&
+ sourceValidity.isValid(source.getValidity()) != SourceValidity.VALID) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("Response expired, invalid for " + logger);
+ }
+ valid = false;
+ } else {
+ if (logger.isDebugEnabled()) {
+ logger.debug("Response expired, still valid for " + logger);
+ }
+ // set new expiration period
+ validities[0] = new ExpiresValidity(expires);
+ }
+ } else {
+ if (logger.isDebugEnabled()) {
+ logger.debug("Response not expired for " + logger);
+ }
+ }
+
+ return valid;
+ }
+
+}
Propchange: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/ExpiresCachingSourceValidityStrategy.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/ExpiresCachingSourceValidityStrategy.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/ExpiresCachingSourceValidityStrategy.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
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=523370&r1=523369&r2=523370
==============================================================================
--- 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 Wed Mar 28 08:30:52 2007
@@ -43,9 +43,9 @@
int expires,
String cacheName,
boolean async,
- boolean eventAware,
+ CachingSourceValidityStrategy validityStrategy,
boolean fail) {
- super(factory, protocol, uri, sourceUri, (TraversableSource) source, expires, cacheName, async, eventAware, fail);
+ super(factory, protocol, uri, sourceUri, (TraversableSource) source, expires, cacheName, async, validityStrategy, 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=523370&r1=523369&r2=523370
==============================================================================
--- 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 Wed Mar 28 08:30:52 2007
@@ -40,9 +40,9 @@
int expires,
String cacheName,
boolean async,
- boolean eventAware,
+ CachingSourceValidityStrategy validityStrategy,
boolean fail) {
- super(protocol, uri, sourceUri, source, expires, cacheName, async, eventAware, fail);
+ super(protocol, uri, sourceUri, source, expires, cacheName, async, validityStrategy, fail);
this.factory = factory;
}