KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > cache > AbstractCache


1 /*
2  * $Id: AbstractCache.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.entity.cache;
26
27 import org.ofbiz.base.util.cache.UtilCache;
28 import org.ofbiz.entity.GenericDelegator;
29
30 public abstract class AbstractCache {
31
32     protected String JavaDoc delegatorName, id;
33
34     protected AbstractCache(String JavaDoc delegatorName, String JavaDoc id) {
35         this.delegatorName = delegatorName;
36         this.id = id;
37     }
38
39     public GenericDelegator getDelegator() {
40         return GenericDelegator.getGenericDelegator(delegatorName);
41     }
42
43     public void remove(String JavaDoc entityName) {
44         UtilCache.clearCache(getCacheName(entityName));
45     }
46
47     public void clear() {
48         UtilCache.clearCachesThatStartWith(getCacheNamePrefix());
49     }
50
51     public String JavaDoc getCacheNamePrefix() {
52         return "entitycache." + id + "." + delegatorName + ".";
53     }
54
55     public String JavaDoc[] getCacheNamePrefixes() {
56         return new String JavaDoc[] {
57             "entitycache." + id + ".${delegator-name}.",
58             "entitycache." + id + "." + delegatorName + "."
59         };
60     }
61
62     public String JavaDoc getCacheName(String JavaDoc entityName) {
63         return getCacheNamePrefix() + entityName;
64     }
65
66     public String JavaDoc[] getCacheNames(String JavaDoc entityName) {
67         String JavaDoc[] prefixes = getCacheNamePrefixes();
68         String JavaDoc[] names = new String JavaDoc[prefixes.length * 2];
69         for (int i = 0; i < prefixes.length; i++) {
70             names[i] = prefixes[i] + "${entity-name}";
71         }
72         for (int i = prefixes.length, j = 0; j < prefixes.length; i++, j++) {
73             names[i] = prefixes[j] + entityName;
74         }
75         return names;
76     }
77
78     protected UtilCache getCache(String JavaDoc entityName) {
79         synchronized (UtilCache.utilCacheTable) {
80             return (UtilCache) UtilCache.utilCacheTable.get(getCacheName(entityName));
81         }
82     }
83
84     protected UtilCache getOrCreateCache(String JavaDoc entityName) {
85         synchronized (UtilCache.utilCacheTable) {
86             String JavaDoc name = getCacheName(entityName);
87             UtilCache cache = (UtilCache) UtilCache.utilCacheTable.get(name);
88             if (cache == null) {
89                 cache = new UtilCache(name, 0, 0, true);
90                 String JavaDoc[] names = getCacheNames(entityName);
91                 cache.setPropertiesParams(names);
92             }
93             return cache;
94         }
95     }
96 }
97
Popular Tags