You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2004/06/11 14:18:23 UTC
cvs commit: cocoon-2.1/src/blocks/scratchpad/java/org/apache/cocoon/components/source/impl CachingSource.java CachingSourceFactory.java
vgritsenko 2004/06/11 05:18:23
Modified: src/blocks/scratchpad/java/org/apache/cocoon/components/source/impl
CachingSource.java CachingSourceFactory.java
Log:
line endings, trailing spaces, "unused" throws clause.
Revision Changes Path
1.12 +122 -119 cocoon-2.1/src/blocks/scratchpad/java/org/apache/cocoon/components/source/impl/CachingSource.java
Index: CachingSource.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/scratchpad/java/org/apache/cocoon/components/source/impl/CachingSource.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- CachingSource.java 15 Apr 2004 08:05:56 -0000 1.11
+++ CachingSource.java 11 Jun 2004 12:18:23 -0000 1.12
@@ -1,12 +1,12 @@
/*
* Copyright 1999-2004 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.
* 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.
@@ -54,7 +54,7 @@
* This class implements a proxy like source that uses another source
* to get the content. This implementation can cache the content for
* a given period of time
- *
+ *
* <h2>Syntax for Protocol</h2>
* <p>
* cached:http://www.apache.org/[?cocoon:cache-expires=60&cocoon:cache-name=main]
@@ -62,59 +62,59 @@
* <p>
* The above examples show 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
+ * 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.
* </p>
* <p>
- * The value of the expires parameter holds some additional semantics.
+ * The value of the expires parameter holds some additional semantics.
* Specifying <code>-1</code> will yield the cached response to be considered valid
- * always. <code>0</code> can be used to achieve the exact opposite. That is to say,
+ * always. <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>
* @version CVS $Id$
*/
public class CachingSource extends AbstractLogEnabled
implements Source, Serviceable, Initializable, XMLizable {
-
+
/** The ServiceManager */
protected ServiceManager manager;
-
+
/** The SourceResolver to resolve the wrapped Source */
protected SourceResolver resolver;
-
+
/** The current cache */
protected Cache cache;
-
+
/** The refresher for asynchronous updates */
protected Refresher refresher;
-
+
/** The source object for the real content */
protected Source source;
-
+
/** The cached response (if any) */
protected CachedSourceResponse response;
-
+
/** Did we just update meta info? */
protected boolean freshMeta;
-
+
/** The full location string */
final protected String uri;
-
+
/** The used protocol */
final protected String protocol;
-
+
/** The key used in the store */
final protected IdentifierCacheKey cacheKey;
-
+
/** number of seconds before cached object becomes invalid */
final protected int expires;
-
+
/** Parameters */
final protected Parameters parameters;
-
+
/** asynchronic refresh strategy ? */
final protected boolean async;
-
+
/**
* Construct a new object.
*/
@@ -130,7 +130,7 @@
this.expires = expires;
this.async = async;
this.parameters = parameters;
-
+
String key = "source:" + source.getURI();
String cacheName = parameters.getParameter("cache-name", null);
if (cacheName != null) {
@@ -138,19 +138,19 @@
}
this.cacheKey = new IdentifierCacheKey(key, false);
}
-
+
/**
* Set the ServiceManager.
*/
public void service(final ServiceManager manager) throws ServiceException {
this.manager = manager;
}
-
+
/**
* Initialize the Source.
*/
public void initialize() throws Exception {
-
+
boolean checkValidity = true;
if (this.expires == -1) {
if (getLogger().isDebugEnabled()) {
@@ -158,10 +158,11 @@
}
checkValidity = false;
}
-
+
if (this.async && this.expires != 0) {
if (getLogger().isDebugEnabled()) {
- getLogger().debug("Not invalidating cached response " +
"for asynch source " + getSourceURI());
+ getLogger().debug("Not invalidating cached response " +
+ "for asynch source " + getSourceURI());
}
checkValidity = false;
}
@@ -169,20 +170,22 @@
this.response = (CachedSourceResponse) this.cache.get(this.cacheKey);
if (this.response == null) {
if (getLogger().isDebugEnabled()) {
- getLogger().debug("No cached response found " +
"for source " + getSourceURI());
+ getLogger().debug("No cached response found " +
+ "for source " + getSourceURI());
}
checkValidity = false;
}
-
+
if (checkValidity) {
-
+
final ExpiresValidity cacheValidity = (ExpiresValidity) this.response.getValidityObjects()[0];
final SourceValidity sourceValidity = this.response.getValidityObjects()[1];
-
+
boolean remove = false;
if (this.expires == 0) {
if (getLogger().isDebugEnabled()) {
- getLogger().debug("Force invalidation of cached response" +
" of source " + getSourceURI());
+ getLogger().debug("Force invalidation of cached response" +
+ " of source " + getSourceURI());
}
remove = true;
}
@@ -190,7 +193,7 @@
boolean expired = cacheValidity.isValid() != SourceValidity.VALID;
if (expired) {
if (getLogger().isDebugEnabled()) {
- getLogger().debug("Cached response of source "
+ getLogger().debug("Cached response of source "
+ getSourceURI() + " is expired.");
}
boolean invalid = !isValid(sourceValidity, this.source);
@@ -203,7 +206,7 @@
}
else {
if (getLogger().isDebugEnabled()) {
- getLogger().debug("Cached response of source "
+ getLogger().debug("Cached response of source "
+ getSourceURI() + " is still valid.");
}
// set new expiration period
@@ -211,7 +214,7 @@
}
}
}
-
+
if (remove) {
this.response = null;
// remove it if it no longer exists
@@ -228,7 +231,7 @@
this.parameters);
}
}
-
+
/**
* Cleanup.
*/
@@ -241,10 +244,10 @@
this.resolver = null;
this.cache = null;
}
-
+
/**
* Initialize the cached response with meta info.
- *
+ *
* @throws IOException if an the binary response could not be initialized
*/
protected void initMetaResponse() throws IOException {
@@ -275,10 +278,10 @@
}
}
}
-
+
/**
* Initialize the cached response with binary contents.
- *
+ *
* @throws IOException if an the binary response could not be initialized
*/
protected void initBinaryResponse() throws IOException {
@@ -313,10 +316,10 @@
}
}
}
-
+
/**
* Initialize the cached response with XML contents.
- *
+ *
* @param refresh whether to force refresh.
* @throws SAXException if something happened during xml processing
* @throws IOException if an IO level error occured
@@ -355,9 +358,9 @@
}
}
}
-
+
// ---------------------------------------------------- Source implementation
-
+
/**
* Return the protocol identifier.
*/
@@ -400,7 +403,7 @@
}
return ((SourceMeta) this.response.getExtra()).getMimeType();
}
-
+
/**
* Return an <code>InputStream</code> object to read from the source.
*/
@@ -424,9 +427,9 @@
* @see org.apache.excalibur.source.Source#exists()
*/
public boolean exists() {
- return this.source.exists();
+ return this.source.exists();
}
-
+
/**
* Get the Validity object. This can either wrap the last modification
* date or the expires information or...
@@ -440,7 +443,7 @@
}
return null;
}
-
+
/**
* Refresh this object and update the last modified date
* and content length.
@@ -449,9 +452,9 @@
this.response = null;
this.source.refresh();
}
-
+
// ---------------------------------------------------- XMLizable implementation
-
+
/**
* Generates SAX events representing the object's state.
*/
@@ -476,46 +479,46 @@
this.manager.release(deserializer);
}
}
-
+
// ---------------------------------------------------- CachingSource specific accessors
-
+
/**
* Return the uri of the cached source.
*/
protected String getSourceURI() {
return this.source.getURI();
}
-
+
/**
* Return the used key.
*/
protected IdentifierCacheKey getCacheKey() {
return this.cacheKey;
}
-
+
/**
* Expires (in milli-seconds)
*/
protected long getExpiration() {
return this.expires * 1000;
}
-
+
/**
* Read XML content from source.
- *
- * @return content from source
- * @throws SAXException
- * @throws IOException
- * @throws CascadingIOException
- */
- protected static byte[] readXMLResponse(Source source, byte[] binary, ServiceManager manager)
+ *
+ * @return content from source
+ * @throws SAXException
+ * @throws IOException
+ * @throws CascadingIOException
+ */
+ protected static byte[] readXMLResponse(Source source, byte[] binary, ServiceManager manager)
throws SAXException, IOException, CascadingIOException {
XMLSerializer serializer = null;
XMLizer xmlizer = null;
byte[] result = null;
- try {
- serializer = (XMLSerializer) manager.lookup(XMLSerializer.ROLE);
-
+ try {
+ serializer = (XMLSerializer) manager.lookup(XMLSerializer.ROLE);
+
if (source instanceof XMLizable) {
((XMLizable) source).toSAX(serializer);
}
@@ -532,54 +535,54 @@
serializer);
}
}
- result = (byte[]) serializer.getSAXFragment();
- } catch (ServiceException se) {
- throw new CascadingIOException("Missing service dependency.", se);
- } finally {
+ result = (byte[]) serializer.getSAXFragment();
+ } catch (ServiceException se) {
+ throw new CascadingIOException("Missing service dependency.", se);
+ } finally {
manager.release(xmlizer);
- manager.release(serializer);
- }
- return result;
- }
+ manager.release(serializer);
+ }
+ return result;
+ }
- /**
+ /**
* Read binary content from source.
- *
- * @return content from source
- * @throws IOException
- * @throws SourceNotFoundException
- */
- protected static byte[] readBinaryResponse(Source source)
+ *
+ * @return content from source
+ * @throws IOException
+ * @throws SourceNotFoundException
+ */
+ protected static byte[] readBinaryResponse(Source source)
throws IOException, SourceNotFoundException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final byte[] buffer = new byte[2048];
- final InputStream inputStream = source.getInputStream();
- int length;
- while ((length = inputStream.read(buffer)) > -1) {
- baos.write(buffer, 0, length);
- }
- baos.flush();
- inputStream.close();
- return baos.toByteArray();
- }
-
+ final byte[] buffer = new byte[2048];
+ final InputStream inputStream = source.getInputStream();
+ int length;
+ while ((length = inputStream.read(buffer)) > -1) {
+ baos.write(buffer, 0, length);
+ }
+ baos.flush();
+ inputStream.close();
+ return baos.toByteArray();
+ }
+
/**
* Read meta data from source.
- *
+ *
* @return source meta data
* @throws IOException
*/
protected static SourceMeta readMeta(Source source) throws IOException {
SourceMeta meta;
-
+
if (source instanceof TraversableSource) {
-
+
final TraversableSourceMeta tmeta = new TraversableSourceMeta();
final TraversableSource tsource = (TraversableSource) source;
-
+
tmeta.setName(tsource.getName());
tmeta.setIsCollection(tsource.isCollection());
-
+
if (tmeta.isCollection()) {
final Collection children = tsource.getChildren();
if (children != null) {
@@ -594,13 +597,13 @@
tmeta.setChildren(names);
}
}
-
+
meta = tmeta;
}
else {
meta = new SourceMeta();
}
-
+
final long lastModified = source.getLastModified();
if (lastModified > 0) {
meta.setLastModified(lastModified);
@@ -609,77 +612,77 @@
meta.setLastModified(System.currentTimeMillis());
}
meta.setMimeType(source.getMimeType());
-
+
return meta;
}
-
+
protected static boolean isValid(SourceValidity validity, Source source) {
if (validity == null) return false;
- return validity.isValid() == SourceValidity.VALID ||
- (validity.isValid() == SourceValidity.UNKNOWN &&
+ return validity.isValid() == SourceValidity.VALID ||
+ (validity.isValid() == SourceValidity.UNKNOWN &&
validity.isValid(source.getValidity()) == SourceValidity.VALID);
}
-
+
/**
* Data holder for caching Source meta info.
*/
protected static class SourceMeta implements Serializable {
-
+
private String m_mimeType;
private long m_lastModified;
private boolean m_exists;
-
+
protected String getMimeType() {
return m_mimeType;
}
-
+
protected void setMimeType(String mimeType) {
m_mimeType = mimeType;
}
-
+
protected long getLastModified() {
return m_lastModified;
}
-
+
protected void setLastModified(long lastModified) {
m_lastModified = lastModified;
}
-
+
protected boolean exists() {
return m_exists;
}
-
+
protected void setExists(boolean exists) {
m_exists = exists;
}
-
+
}
-
+
protected static class TraversableSourceMeta extends SourceMeta {
private String m_name;
private boolean m_isCollection;
private String[] m_children;
-
+
protected String getName() {
return m_name;
}
-
+
protected void setName(String name) {
m_name = name;
}
-
+
protected boolean isCollection() {
return m_isCollection;
}
-
+
protected void setIsCollection(boolean isCollection) {
m_isCollection = isCollection;
}
-
+
protected String[] getChildren() {
return m_children;
}
-
+
protected void setChildren(String[] children) {
m_children = children;
}
1.11 +50 -51 cocoon-2.1/src/blocks/scratchpad/java/org/apache/cocoon/components/source/impl/CachingSourceFactory.java
Index: CachingSourceFactory.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/scratchpad/java/org/apache/cocoon/components/source/impl/CachingSourceFactory.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- CachingSourceFactory.java 7 May 2004 17:32:59 -0000 1.10
+++ CachingSourceFactory.java 11 Jun 2004 12:18:23 -0000 1.11
@@ -1,12 +1,12 @@
/*
* Copyright 1999-2004 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.
* 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.
@@ -43,36 +43,36 @@
/**
* This class implements a proxy like source caches the contents of the source
- * it wraps. This implementation can cache the content either
+ * it wraps. This implementation can cache the content either
* for a given period of time or until an external event invalidates
* the cached response.
* <p>
* When using the timeout approach you have a choice between two separate
* revalidation strategies.
* </p>
- * 1) Synchronously. This means that the cached contents are checked for validity
+ * 1) Synchronously. This means that the cached contents are checked for validity
* and thrown out on the current thread.<br>
* 2) Asynchronously. A cronjob is scheduled to invalidate and update the cached response
* in the backgound.<br><br>
- *
+ *
* <h2>Protocol syntax</h2>
* <p>
* The URL needs to contain the URL of the cached source, an expiration
- * period in seconds, and optionally a cache key:
+ * period in seconds, and optionally a cache key:
* <code>cached:http://www.apache.org/[?cocoon:cache-expires=60][&cocoon:cache-name=main]</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
+ * 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.
* </p>
* <p>
- * This factory creates either instances of {@link org.apache.cocoon.components.source.impl.CachingSource}
+ * This factory creates either instances of {@link org.apache.cocoon.components.source.impl.CachingSource}
* or {@link org.apache.cocoon.components.source.impl.TraversableCachingSource}
* depending on the whether the wrapped Source is an instance of TraversableSource.
* </p>
- *
+ *
* <h2>Parameters</h2>
* <table><tbody>
* <tr>
@@ -104,7 +104,7 @@
* <td><code>-1</code></td>
* </tr>
* </tbody></table>
- *
+ *
* @version CVS $Id$
* @since 2.1.1
*/
@@ -114,19 +114,19 @@
/** Protocol prefix / factory name */
private String scheme;
-
+
/** Asynchronous ? */
private boolean async;
-
+
/** The role of the cache */
private String cacheRole;
-
+
/** The role of the refresher */
private String refresherRole;
-
+
/** Default expires value */
private int defaultExpires;
-
+
/** Has the lazy initialization been done? */
private boolean isInitialized;
@@ -135,37 +135,38 @@
/** The {@link SourceResolver} */
protected SourceResolver resolver;
-
+
/** The refresher */
protected Refresher refresher;
-
+
/** The cache */
protected Cache cache;
-
+
// ---------------------------------------------------- Lifecycle
-
+
public CachingSourceFactory() {
}
-
- public void service(ServiceManager manager) throws ServiceException {
+
+ public void service(ServiceManager manager) {
this.manager = manager;
- // due to cyclic dependencies we can't lookup the resolver the refresher
- // or the cache until after the factory is initialized.
+ // Due to cyclic dependencies we can't lookup the resolver,
+ // the refresher or the cache until after the factory is
+ // initialized.
}
-
+
public void configure(Configuration configuration) throws ConfigurationException {
this.scheme = configuration.getAttribute("name");
Parameters parameters = Parameters.fromConfiguration(configuration);
-
- // 'async' parameter
- this.async = parameters.getParameterAsBoolean("async", false);
-
+
+ // 'async' parameter
+ this.async = parameters.getParameterAsBoolean("async", false);
+
// 'cache-role' parameter
this.cacheRole = parameters.getParameter("cache-role", Cache.ROLE);
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Using cache " + this.cacheRole);
}
-
+
// 'refresher-role' parameter
if (this.async) {
this.refresherRole = parameters.getParameter("refresher-role", Refresher.ROLE);
@@ -173,15 +174,14 @@
this.getLogger().debug("Using refresher " + this.refresherRole);
}
}
-
- this.defaultExpires = parameters.getParameterAsInteger("default-expires",-1);
-
+
+ this.defaultExpires = parameters.getParameterAsInteger("default-expires", -1);
}
-
+
/**
* Lazy initialization of resolver and refresher because of
* cyclic dependencies.
- *
+ *
* @throws SourceException
*/
private synchronized void lazyInitialize() throws SourceException {
@@ -214,7 +214,7 @@
}
this.isInitialized = true;
}
-
+
/* (non-Javadoc)
* @see org.apache.avalon.framework.activity.Disposable#dispose()
*/
@@ -227,32 +227,32 @@
this.resolver = null;
}
}
-
+
// ---------------------------------------------------- SourceFactory implementation
-
+
/**
* Get a <code>Source</code> object.
* @param parameters This is optional.
*/
public Source getSource(final String location, final Map parameters)
throws MalformedURLException, IOException {
-
+
if (this.getLogger().isDebugEnabled() ) {
this.getLogger().debug("Creating source object for " + location);
}
-
+
// we must do lazy initialization because of cyclic dependencies
if (!this.isInitialized) {
lazyInitialize();
}
-
+
// snip the cache protocol
int index = location.indexOf(':');
if (index == -1) {
throw new MalformedURLException("This Source requires a subprotocol to be specified.");
}
String uri = location.substring(index+1);
-
+
// parse the query string
SourceParameters sp = null;
String queryString = null;
@@ -262,7 +262,7 @@
uri = uri.substring(0,index);
sp = new SourceParameters(queryString);
}
-
+
// put caching source specific query string parameters
// into a Parameters object
final Parameters params = new Parameters();
@@ -280,14 +280,14 @@
uri += "?" + queryString;
}
}
-
+
int expires = params.getParameterAsInteger("cache-expires", -1);
if (expires == -1) {
expires = this.defaultExpires;
params.setParameter("cache-expires", String.valueOf(this.defaultExpires));
}
params.setParameter("cache-role", this.cacheRole);
-
+
final Source wrappedSource = this.resolver.resolveURI(uri);
CachingSource source;
if (wrappedSource instanceof TraversableSource) {
@@ -297,8 +297,7 @@
params,
expires,
this.async);
- }
- else {
+ } else {
source = new CachingSource(scheme,
location,
wrappedSource,
@@ -322,7 +321,7 @@
} catch (Exception e) {
throw new SourceException("Unable to initialize source.", e);
}
-
+
return source;
}
@@ -337,8 +336,8 @@
((CachingSource) source).dispose();
}
}
-
- // ---------------------------------------------------- URIAbsolutizer implementation
+
+ // ---------------------------------------------------- URIAbsolutizer implementation
/*
* (non-Javadoc)