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.transformation.helpers;
018:
019: import java.io.IOException;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import org.apache.avalon.framework.parameters.Parameters;
025: import org.apache.excalibur.source.Source;
026: import org.apache.excalibur.source.SourceResolver;
027: import org.apache.excalibur.source.SourceValidity;
028: import org.apache.excalibur.source.impl.validity.ExpiresValidity;
029:
030: /**
031: * This object encapsulates a "caching session". A caching session has the
032: * duration of one single request.
033: * This object is used by the {@link IncludeCacheManager} and holds all required
034: * configuration for performing the caching of this request.
035: *
036: * The session can be configured during construction with the following parameters:
037: * - purge (boolean/false) : Turn on/off purging the cache
038: * - preemptive (boolean/false) : Turn on/off preemptive caching
039: * - parallel (boolean/false) : Turn on/off parallel processing
040: * - expires (long/0) : The lifetime of the cached content
041: *
042: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
043: * @version CVS $Id: IncludeCacheManagerSession.java 433543 2006-08-22 06:22:54Z crossley $
044: * @since 2.1
045: */
046: public final class IncludeCacheManagerSession {
047:
048: /** The expires information */
049: private long expires;
050:
051: /** Should we purge the cache */
052: private boolean purge;
053:
054: /** Should we load preemptive */
055: private boolean preemptive;
056:
057: /** Should we process everything in parallel */
058: private boolean parallel;
059:
060: /** The used {@link IncludeCacheStorageProxy} */
061: private IncludeCacheStorageProxy storage;
062:
063: /** The list of all threads */
064: private Map threadList;
065:
066: /** Cache the expires validity object */
067: private SourceValidity validity;
068:
069: /** Cache the source objects */
070: private Map sourceList = new HashMap(10);
071:
072: /**
073: * Constructor
074: * @param configuration The parameters configuring this session
075: * @param proxy The proxy used to cache the data
076: */
077: IncludeCacheManagerSession(Parameters configuration,
078: IncludeCacheStorageProxy proxy) {
079: this .expires = configuration.getParameterAsLong("expires", 0);
080: this .purge = configuration
081: .getParameterAsBoolean("purge", false);
082: this .preemptive = configuration.getParameterAsBoolean(
083: "preemptive", false);
084: this .parallel = configuration.getParameterAsBoolean("parallel",
085: false);
086: this .storage = proxy;
087: }
088:
089: /**
090: * Get the used storage proxy
091: */
092: IncludeCacheStorageProxy getCacheStorageProxy() {
093: return this .storage;
094: }
095:
096: /**
097: * Get the expiration information
098: */
099: public long getExpires() {
100: return this .expires;
101: }
102:
103: public SourceValidity getExpiresValidity() {
104: if (this .expires > 0 && this .validity == null) {
105: this .validity = new ExpiresValidity(this .expires * 1000); // milliseconds
106: }
107: return this .validity;
108: }
109:
110: /**
111: * Is the cache purged?
112: */
113: public boolean isPurging() {
114: return this .purge;
115: }
116:
117: /**
118: * Do we use preemptive caching?
119: */
120: public boolean isPreemptive() {
121: return this .preemptive;
122: }
123:
124: /**
125: * Do we process the includes in parallel?
126: */
127: public boolean isParallel() {
128: return this .parallel;
129: }
130:
131: /**
132: * Add another object to the thread list
133: * @param uri The absolute URI
134: * @param object The thread
135: */
136: void add(String uri, Object object) {
137: if (null == this .threadList) {
138: this .threadList = new HashMap(10);
139: }
140: this .threadList.put(uri, object);
141: }
142:
143: /**
144: * Get the thread object.
145: * @param uri The URI
146: * @return Object The thread.
147: */
148: Object get(String uri) {
149: if (null != this .threadList) {
150: return this .threadList.get(uri);
151: }
152: return null;
153: }
154:
155: /**
156: * Turn off/on preemptive caching
157: */
158: void setPreemptive(boolean value) {
159: this .preemptive = value;
160: }
161:
162: /**
163: * Lookup a source object and cache it
164: * @param uri Absolute URI
165: * @return Source The source obejct
166: */
167: public Source resolveURI(String uri, SourceResolver resolver)
168: throws IOException {
169: Source source = (Source) this .sourceList.get(uri);
170: if (null == source) {
171: source = resolver.resolveURI(uri);
172: this .sourceList.put(source.getURI(), source);
173: }
174: return source;
175: }
176:
177: /**
178: * Cleanup
179: * @param resolver The source resolver to release cached sources
180: */
181: void cleanup(SourceResolver resolver) {
182: Iterator iter = this .sourceList.values().iterator();
183: while (iter.hasNext()) {
184: final Source source = (Source) iter.next();
185: resolver.release(source);
186: }
187: }
188:
189: /**
190: * Print a representation of this object
191: */
192: public String toString() {
193: return "CacheManagerSession(" + this .hashCode() + ") -"
194: + " expires: " + this .expires + " parallel: "
195: + this .parallel + " preemptive: " + this .preemptive
196: + " purge: " + this.purge;
197: }
198: }
|