KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > helpers > IncludeCacheManagerSession


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.transformation.helpers;
17
18 import java.io.IOException JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23
24 import org.apache.avalon.framework.parameters.Parameters;
25 import org.apache.excalibur.source.Source;
26 import org.apache.excalibur.source.SourceResolver;
27 import org.apache.excalibur.source.SourceValidity;
28 import org.apache.excalibur.source.impl.validity.ExpiresValidity;
29
30 /**
31  * This object encapsulates a "caching session". A caching session has the
32  * duration of one single request.
33  * This object is used by the {@link IncludeCacheManager} and holds all required
34  * configuration for performing the caching of this request.
35  *
36  * The session can be configured during construction with the following parameters:
37  * - purge (boolean/false) : Turn on/off purging the cache
38  * - preemptive (boolean/false) : Turn on/off preemptive caching
39  * - parallel (boolean/false) : Turn on/off parallel processing
40  * - expires (long/0) : The lifetime of the cached content
41  *
42  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
43  * @version CVS $Id: IncludeCacheManagerSession.java 30932 2004-07-29 17:35:38Z vgritsenko $
44  * @since 2.1
45  */

46 public final class IncludeCacheManagerSession {
47
48     /** The expires information */
49     private long expires;
50     
51     /** Should we purge the cache */
52     private boolean purge;
53     
54     /** Should we load preemptive */
55     private boolean preemptive;
56     
57     /** Should we process everything in parallel */
58     private boolean parallel;
59
60     /** The used {@link IncludeCacheStorageProxy} */
61     private IncludeCacheStorageProxy storage;
62     
63     /** The list of all threads */
64     private Map JavaDoc threadList;
65     
66     /** Cache the expires validity object */
67     private SourceValidity validity;
68     
69     /** Cache the source objects */
70     private Map JavaDoc sourceList = new HashMap JavaDoc(10);
71     
72     /**
73      * Constructor
74      * @param configuration The parameters configuring this session
75      * @param proxy The proxy used to cache the data
76      */

77     IncludeCacheManagerSession(Parameters configuration,
78                         IncludeCacheStorageProxy proxy) {
79         this.expires = configuration.getParameterAsLong("expires", 0);
80         this.purge = configuration.getParameterAsBoolean("purge", false);
81         this.preemptive = configuration.getParameterAsBoolean("preemptive", false);
82         this.parallel = configuration.getParameterAsBoolean("parallel", false);
83         this.storage = proxy;
84     }
85     
86     /**
87      * Get the used storage proxy
88      */

89     IncludeCacheStorageProxy getCacheStorageProxy() {
90         return this.storage;
91     }
92
93     /**
94      * Get the expiration information
95      */

96     public long getExpires() {
97         return this.expires;
98     }
99
100     public SourceValidity getExpiresValidity() {
101         if ( this.expires > 0 && this.validity == null) {
102             this.validity = new ExpiresValidity( this.expires * 1000 ); // milliseconds
103
}
104         return this.validity;
105     }
106     
107     /**
108      * Is the cache purged?
109      */

110     public boolean isPurging() {
111         return this.purge;
112     }
113
114     /**
115      * Do we use preemptive caching?
116      */

117     public boolean isPreemptive() {
118         return this.preemptive;
119     }
120
121     /**
122      * Do we process the includes in parallel?
123      */

124     public boolean isParallel() {
125         return this.parallel;
126     }
127
128     /**
129      * Add another object to the thread list
130      * @param uri The absolute URI
131      * @param object The thread
132      */

133     void add(String JavaDoc uri, Object JavaDoc object) {
134         if ( null == this.threadList ) {
135             this.threadList = new HashMap JavaDoc(10);
136         }
137         this.threadList.put(uri, object);
138     }
139     
140     /**
141      * Get the thread object.
142      * @param uri The URI
143      * @return Object The thread.
144      */

145     Object JavaDoc get(String JavaDoc uri) {
146         if ( null != this.threadList ) {
147             return this.threadList.get( uri );
148         }
149         return null;
150     }
151     
152     /**
153      * Turn off/on preemptive caching
154      */

155     void setPreemptive(boolean value) {
156         this.preemptive = value;
157     }
158     
159     /**
160      * Lookup a source object and cache it
161      * @param uri Absolute URI
162      * @return Source The source obejct
163      */

164     public Source resolveURI(String JavaDoc uri, SourceResolver resolver)
165     throws IOException JavaDoc {
166         Source source = (Source)this.sourceList.get(uri);
167         if ( null == source ) {
168             source = resolver.resolveURI( uri );
169             this.sourceList.put( source.getURI(), source );
170         }
171         return source;
172     }
173     
174     /**
175      * Cleanup
176      * @param resolver The source resolver to release cached sources
177      */

178     void cleanup(SourceResolver resolver) {
179         Iterator JavaDoc iter = this.sourceList.values().iterator();
180         while ( iter.hasNext() ) {
181             final Source source = (Source) iter.next();
182             resolver.release( source );
183         }
184     }
185     
186     /**
187      * Print a representation of this object
188      */

189     public String JavaDoc toString() {
190         return "CacheManagerSession(" + this.hashCode() + ") -" +
191                 " expires: " + this.expires +
192                 " parallel: " + this.parallel +
193                 " preemptive: " + this.preemptive +
194                 " purge: " + this.purge;
195     }
196 }
197
Popular Tags