001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.source;
018:
019: import org.apache.cocoon.ProcessingException;
020: import org.apache.cocoon.environment.ModifiableSource;
021: import org.apache.cocoon.environment.Source;
022: import org.apache.cocoon.xml.XMLizable;
023: import org.xml.sax.ContentHandler;
024: import org.xml.sax.InputSource;
025: import org.xml.sax.SAXException;
026:
027: import java.io.IOException;
028: import java.io.InputStream;
029:
030: /**
031: * A wrapper around a <code>Source</code> that reduces the number of calls to
032: * <code>Source.getLastModified()</code> which can be a costly operation.
033: *
034: * @deprecated by the Avalon Exalibur Source Resolving
035: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
036: * @version CVS $Id: DelayedRefreshSourceWrapper.java 433543 2006-08-22 06:22:54Z crossley $
037: */
038: public final class DelayedRefreshSourceWrapper implements Source,
039: ModifiableSource, XMLizable {
040:
041: private Source source;
042:
043: private long delay;
044:
045: private long nextCheckTime = 0;
046:
047: private long lastModified = 0;
048:
049: private boolean isModifiableSource;
050:
051: /**
052: * Creates a wrapper for a <code>Source</code> which ensures that
053: * <code>Source.getLastModified()</code> won't be called more than once per
054: * <code>delay</code> milliseconds period.
055: *
056: * @param source the wrapped <code>Source</code>
057: * @param delay the last-modified refresh delay, in milliseconds
058: */
059: public DelayedRefreshSourceWrapper(Source source, long delay) {
060: this .source = source;
061: this .delay = delay;
062: this .isModifiableSource = source instanceof ModifiableSource;
063: }
064:
065: /**
066: * Get the last modification time for the wrapped <code>Source</code>. The
067: * age of the returned information is guaranteed to be lower than or equal to
068: * the delay specified in the constructor.
069: * <p>
070: * This method is also thread-safe, even if the underlying Source is not.
071: *
072: * @return the last modification time.
073: */
074: public final long getLastModified() {
075:
076: // Do we have to refresh the source ?
077: if (System.currentTimeMillis() >= nextCheckTime) {
078: // Yes
079: this .refresh();
080: }
081:
082: return this .lastModified;
083: }
084:
085: /**
086: * Force the refresh of the wrapped <code>Source</code>, even if the refresh period
087: * isn't over, and starts a new period.
088: * <p>
089: * This method is thread-safe, even if the underlying Source is not.
090: */
091: public synchronized final void refresh() {
092:
093: this .nextCheckTime = System.currentTimeMillis() + this .delay;
094: // Refresh modifiable sources
095: if (this .isModifiableSource) {
096: ((ModifiableSource) this .source).refresh();
097: }
098:
099: // Keep the last modified date
100: this .lastModified = source.getLastModified();
101: }
102:
103: public final long getContentLength() {
104: return this .source.getContentLength();
105: }
106:
107: public final InputStream getInputStream()
108: throws ProcessingException, IOException {
109: return this .source.getInputStream();
110: }
111:
112: public final InputSource getInputSource()
113: throws ProcessingException, IOException {
114: return this .source.getInputSource();
115: }
116:
117: public final String getSystemId() {
118: return this .source.getSystemId();
119: }
120:
121: public final void recycle() {
122: this .source.recycle();
123: }
124:
125: public final void toSAX(ContentHandler handler) throws SAXException {
126: this.source.toSAX(handler);
127: }
128: }
|