KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > connector > grizzly > FileCacheFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.web.connector.grizzly;
24
25 import java.util.concurrent.ConcurrentHashMap JavaDoc;
26 import java.util.concurrent.ConcurrentLinkedQueue JavaDoc;
27
28 import com.sun.enterprise.web.connector.grizzly.FileCache.FileCacheEntry;
29 /**
30  * A factory for creating <code>FileCache</code> instance.
31  *
32  * @author Jeanfrancois Arcand
33  */

34 public class FileCacheFactory{
35
36     
37      /**
38      * Timeout before remove the static resource from the cache.
39      */

40     public int secondsMaxAge = -1;
41     
42     
43     /**
44      * The maximum entries in the <code>fileCache</code>
45      */

46     public int maxCacheEntries = 1024;
47     
48  
49     /**
50      * The maximum size of a cached resources.
51      */

52     public long minEntrySize = 2048;
53             
54                
55     /**
56      * The maximum size of a cached resources.
57      */

58     public long maxEntrySize = 537600;
59     
60     
61     /**
62      * The maximum cached bytes
63      */

64     public long maxLargeFileCacheSize = 10485760;
65  
66     
67     /**
68      * The maximum cached bytes
69      */

70     public long maxSmallFileCacheSize = 1048576;
71     
72     
73     /**
74      * Is the FileCache enabled.
75      */

76     public static boolean isEnabled = true;
77     
78     
79     /**
80      * Is the large FileCache enabled.
81      */

82     public boolean isLargeFileCacheEnabled = true;
83     
84     
85     /**
86      * The port used
87      */

88     public int port = 8080;
89     
90     
91     /**
92      * Create a factory per port.
93      */

94     private static ConcurrentHashMap JavaDoc<Integer JavaDoc,FileCacheFactory> cache =
95             new ConcurrentHashMap JavaDoc<Integer JavaDoc,FileCacheFactory>();
96     
97     
98     /**
99      * The cache manager used by instance of <code>FileCache</code>
100      * created by this factory;
101      */

102     private ConcurrentLinkedQueue JavaDoc cacheManager;
103     
104     
105     /**
106      * Is monitoring enabled
107      */

108     private boolean isMonitoringEnabled = false;
109     
110     
111     /**
112      * A list of <code>FileCache</code> instance this Factory is owning.
113      */

114     protected FileCache fileCache;
115     
116     
117     // ---------------------------------------------------------------------//
118

119     
120     private FileCacheFactory(){
121     }
122
123     
124     /**
125      * Configure the factory.
126      */

127     private static FileCacheFactory newInstance(int currentPort){
128         FileCacheFactory fileCacheFactory= new FileCacheFactory();
129
130         fileCacheFactory.port = currentPort;
131         cache.put(currentPort, fileCacheFactory);
132
133         ConcurrentLinkedQueue JavaDoc<FileCacheEntry> cacheManager =
134             new ConcurrentLinkedQueue JavaDoc<FileCacheEntry>();
135         fileCacheFactory.cacheManager = cacheManager;
136
137         return fileCacheFactory;
138     }
139     
140     
141     /**
142      * Return an instance of this Factory.
143      */

144     public static FileCacheFactory getFactory(int currentPort){
145                 
146         FileCacheFactory fileCacheFactory = cache.get(currentPort);
147         if ( fileCacheFactory == null ){
148             fileCacheFactory = newInstance(currentPort);
149         }
150
151         return fileCacheFactory;
152     }
153     
154     
155     /**
156      * Return an instance of a <code>FileCache</code>
157      */

158     public FileCache getFileCache(){
159         if ( fileCache == null){
160             fileCache = new FileCache();
161             fileCache.setIsEnabled(isEnabled);
162             fileCache.setLargeFileCacheEnabled(isLargeFileCacheEnabled);
163             fileCache.setSecondsMaxAge(secondsMaxAge);
164             fileCache.setMaxCacheEntries(maxCacheEntries);
165             fileCache.setMinEntrySize(minEntrySize);
166             fileCache.setMaxEntrySize(maxEntrySize);
167             fileCache.setMaxLargeCacheSize(maxLargeFileCacheSize);
168             fileCache.setMaxSmallCacheSize(maxSmallFileCacheSize);
169             fileCache.setCacheManager(cacheManager);
170             fileCache.setIsMonitoringEnabled(isMonitoringEnabled);
171         }
172         
173         return fileCache;
174     }
175     
176     
177     private void setCacheManager(ConcurrentLinkedQueue JavaDoc cacheManager){
178         this.cacheManager = cacheManager;
179     }
180     
181     
182     /**
183      * Return the FileCache
184      */

185     public ConcurrentHashMap JavaDoc getCache(){
186         if ( fileCache != null ){
187             return fileCache.getCache();
188         } else {
189             return null;
190         }
191     }
192     // ---------------------------------------------------- Monitoring --------//
193

194     
195     /**
196      * Returns flag indicating whether file cache has been enabled
197      * @return 1 if file cache has been enabled, 0 otherwise
198      */

199     public int getFlagEnabled() {
200         return (isEnabled == true?1:0);
201     }
202     
203     
204     /**
205      * Return the maximum age of a valid cache entry
206      * @return cache entry maximum age
207      */

208     public int getSecondsMaxAge() {
209         return secondsMaxAge;
210     }
211     
212     
213     /**
214      * Return the number of current cache entries.
215      * @return current cache entries
216      */

217     public long getCountEntries() {
218         if (fileCache == null) return 0L;
219         return fileCache.getCountEntries();
220     }
221     
222     
223     /**
224      * Return the maximum number of cache entries
225      * @return maximum cache entries
226      */

227     public long getMaxEntries() {
228         if (fileCache == null) return 0L;
229         return maxCacheEntries;
230     }
231     
232     
233     /**
234      * The number of current open cache entries
235      * @return open cache entries
236      */

237     public long getCountOpenEntries() {
238         if (fileCache == null) return 0L;
239         return fileCache.getCountOpenEntries();
240     }
241     
242     
243     /**
244      * Return the maximum number of open cache entries
245      * @return maximum open cache entries
246      */

247     public long getMaxOpenEntries() {
248         if (fileCache == null) return 0L;
249         return fileCache.getMaxOpenEntries();
250     }
251     
252     
253     /**
254      * Return the heap space used for cache
255      * @return heap size
256      */

257     public long getSizeHeapCache() {
258         if (fileCache == null) return 0L;
259         return fileCache.getSizeHeapCache();
260     }
261     
262     
263     /**
264      * Return the maximum heap space used for cache
265      * @return maximum heap size
266      */

267     public long getMaxHeapCacheSize() {
268         if (fileCache == null) return 0L;
269         return fileCache.getMaxHeapCacheSize();
270     }
271     
272     
273     /**
274      * Return the size of Mapped memory used for caching
275      * @return Mapped memory size
276      */

277     public long getSizeMmapCache() {
278         if (fileCache == null) return 0L;
279         return fileCache.getSizeMmapCache();
280     }
281     
282     
283     /**
284      * Return the Maximum Memory Map size to be used for caching
285      * @return maximum Memory Map size
286      */

287     public long getMaxMmapCacheSize() {
288         if (fileCache == null) return 0L;
289         return fileCache.getMaxMmapCacheSize();
290     }
291     
292     
293     /**
294      * Return the Number of cache lookup hits
295      * @return cache hits
296      */

297     public long getCountHits() {
298         if (fileCache == null) return 0L;
299         return fileCache.getCountHits();
300     }
301     
302     
303     /**
304      * Return the Number of cache lookup misses
305      * @return cache misses
306      */

307     public long getCountMisses() {
308         if (fileCache == null) return 0L;
309         return fileCache.getCountMisses();
310     }
311     
312     
313     /**
314      * The Number of hits on cached file info
315      * @return hits on cached file info
316      */

317     public long getCountInfoHits() {
318         if (fileCache == null) return 0L;
319         return fileCache.getCountInfoHits();
320     }
321     
322     
323     /**
324      * Return the number of misses on cached file info
325      * @return misses on cache file info
326      */

327     public long getCountInfoMisses() {
328         if (fileCache == null) return 0L;
329         return fileCache.getCountInfoMisses();
330     }
331     
332     
333     /**
334      * Return the Number of hits on cached file content
335      * @return hits on cache file content
336      */

337     public long getCountContentHits() {
338         if (fileCache == null) return 0L;
339         return fileCache.getCountContentHits();
340     }
341     
342     
343     /**
344      * Return the Number of misses on cached file content
345      * @return missed on cached file content
346      */

347     public long getCountContentMisses() {
348         if (fileCache == null) return 0L;
349         return fileCache.getCountContentMisses();
350     }
351     
352     // ---------------------------------------------------- Properties ----- //
353

354     
355     /**
356      * Turn monitoring on/off
357      */

358     public void setIsMonitoringEnabled(boolean isMonitoringEnabled){
359         this.isMonitoringEnabled = isMonitoringEnabled;
360         FileCache.setIsMonitoringEnabled(isMonitoringEnabled);
361     }
362     
363     
364     /**
365      * The timeout in seconds before remove a <code>FileCacheEntry</code>
366      * from the <code>fileCache</code>
367      */

368     public void setSecondsMaxAge(int sMaxAges){
369         secondsMaxAge = sMaxAges;
370     }
371     
372     
373     /**
374      * Set the maximum entries this cache can contains.
375      */

376     public void setMaxCacheEntries(int mEntries){
377         maxCacheEntries = mEntries;
378     }
379
380     
381     /**
382      * Return the maximum entries this cache can contains.
383      */

384     public int getMaxCacheEntries(){
385         return maxCacheEntries;
386     }
387     
388     
389     /**
390      * Set the maximum size a <code>FileCacheEntry</code> can have.
391      */

392     public void setMinEntrySize(long mSize){
393         minEntrySize = mSize;
394     }
395     
396     
397     /**
398      * Get the maximum size a <code>FileCacheEntry</code> can have.
399      */

400     public long getMinEntrySize(){
401         return minEntrySize;
402     }
403      
404     
405     /**
406      * Set the maximum size a <code>FileCacheEntry</code> can have.
407      */

408     public void setMaxEntrySize(long mEntrySize){
409         maxEntrySize = mEntrySize;
410     }
411     
412     
413     /**
414      * Get the maximum size a <code>FileCacheEntry</code> can have.
415      */

416     public long getMaxEntrySize(){
417         return maxEntrySize;
418     }
419     
420     
421     /**
422      * Set the maximum cache size
423      */

424     public void setMaxLargeCacheSize(long mCacheSize){
425         maxLargeFileCacheSize = mCacheSize;
426     }
427
428     
429     /**
430      * Get the maximum cache size
431      */

432     public long getMaxLargeCacheSize(){
433         return maxLargeFileCacheSize;
434     }
435     
436     
437     /**
438      * Set the maximum cache size
439      */

440     public void setMaxSmallCacheSize(long mCacheSize){
441         maxSmallFileCacheSize = mCacheSize;
442     }
443     
444     
445     /**
446      * Get the maximum cache size
447      */

448     public long getMaxSmallCacheSize(){
449         return maxSmallFileCacheSize;
450     }
451
452     
453     /**
454      * Is the fileCache enabled.
455      */

456     public static boolean isEnabled(){
457         return isEnabled;
458     }
459
460     
461     /**
462      * Is the file caching mechanism enabled.
463      */

464     public static void setIsEnabled(boolean isE){
465         isEnabled = isE;
466     }
467    
468     
469     /**
470      * Is the large file cache support enabled.
471      */

472     public void setLargeFileCacheEnabled(boolean isLargeEnabled){
473         this.isLargeFileCacheEnabled = isLargeEnabled;
474     }
475    
476     
477     /**
478      * Is the large file cache support enabled.
479      */

480     public boolean getLargeFileCacheEnabled(){
481         return isLargeFileCacheEnabled;
482     }
483
484 }
485
Popular Tags