KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > cache > DotCache


1 /*
2  * Created on May 31, 2005
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package com.dotmarketing.cache;
8
9 import java.util.Properties JavaDoc;
10
11 import com.dotmarketing.util.Logger;
12 import net.sf.hibernate.cache.Cache;
13 import net.sf.hibernate.cache.CacheException;
14 import net.sf.hibernate.cache.CacheProvider;
15
16 /**
17  * @author Salvador
18  *
19  * TODO To change the template for this generated type comment go to Window -
20  * Preferences - Java - Code Style - Code Templates
21  */

22 public class DotCache implements Cache {
23
24     //The provider used to create the cache
25
private CacheProvider provider;
26
27     //The cache "instance" to store the data;
28
private Cache cache;
29
30     //Default provider
31
private String JavaDoc defaultProvider = "net.sf.ehcache.hibernate.Provider";
32
33     //Staic method that initialize the cache when the JVM go up
34
public DotCache(String JavaDoc cacheProviderClassName,String JavaDoc regionName, Properties JavaDoc properties) {
35         //providerClassName is null the provider will be the default provider
36
cacheProviderClassName = (cacheProviderClassName != null ? cacheProviderClassName : defaultProvider);
37         //If the properties come null, there is a default properties
38
if (properties == null)
39         {
40             properties = new Properties JavaDoc();
41         }
42        
43         //Reflection to load the cache Class
44
try
45         {
46             Class JavaDoc classDefinition = Class.forName(cacheProviderClassName);
47             provider = (CacheProvider) classDefinition.newInstance();
48             cache = provider.buildCache(regionName, properties);
49         }
50         catch (InstantiationException JavaDoc e)
51         {
52             Logger.debug(DotCache.class,e.toString());
53         }
54         catch (IllegalAccessException JavaDoc e)
55         {
56             Logger.debug(DotCache.class,e.toString());
57         }
58         catch (ClassNotFoundException JavaDoc e)
59         {
60             Logger.debug(DotCache.class,e.toString());
61             try
62             {
63                 Class JavaDoc classDefinition = Class.forName(defaultProvider);
64                 provider = (CacheProvider) classDefinition.newInstance();
65                 cache = provider.buildCache(regionName, properties);
66             }
67             catch(Exception JavaDoc ex)
68             {
69                 Logger.error(DotCache.class,ex.toString());
70             }
71         }
72         catch (CacheException cacheEx)
73         {
74             Logger.debug(DotCache.class,cacheEx.toString());
75         }
76     }
77     
78     //Methods implemented in the interface
79
public void clear()
80     {
81         try
82         {
83             cache.clear();
84         }
85         catch (Exception JavaDoc cacheEx)
86         {
87             Logger.debug(DotCache.class,cacheEx.getMessage());
88         }
89     }
90
91     public void destroy()
92     {
93         try
94         {
95         cache.destroy();
96         }
97         catch (Exception JavaDoc cacheEx)
98         {
99             Logger.debug(DotCache.class,cacheEx.getMessage());
100         }
101     }
102
103     public Object JavaDoc get(Object JavaDoc key)
104     {
105         Object JavaDoc returnValue = null;
106         try
107         {
108             returnValue = cache.get(key);
109         }
110         catch (Exception JavaDoc cacheEx)
111         {
112             Logger.debug(DotCache.class,cacheEx.getMessage());
113         }
114         return returnValue;
115     }
116
117     public int getTimeout()
118     {
119         return cache.getTimeout();
120     }
121
122     public void lock(Object JavaDoc key)
123     {
124         try
125         {
126             cache.lock(key);
127         }
128         catch (Exception JavaDoc cacheEx)
129         {
130             Logger.debug(DotCache.class,cacheEx.getMessage());
131         }
132     }
133
134     public long nextTimestamp()
135     {
136         return cache.nextTimestamp();
137     }
138
139     public void put(Object JavaDoc key, Object JavaDoc value)
140     {
141         try
142         {
143             cache.put(key,value);
144         }
145         catch (Exception JavaDoc cacheEx)
146         {
147             Logger.debug(DotCache.class,cacheEx.getMessage());
148         }
149     }
150
151     public void remove(Object JavaDoc key)
152     {
153         try
154         {
155             cache.remove(key);
156         }
157         catch (Exception JavaDoc cacheEx)
158         {
159             Logger.debug(DotCache.class,cacheEx.getMessage());
160         }
161     }
162
163     public void unlock(Object JavaDoc key)
164     {
165         try
166         {
167             cache.unlock(key);
168         }
169         catch (Exception JavaDoc cacheEx)
170         {
171             Logger.debug(DotCache.class,cacheEx.getMessage());
172         }
173     }
174     //END Methods implemented in the interface
175
}
176
Popular Tags