KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > cache > TreeCacheAdapter


1 /*
2  * Copyright (C) 2006 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.cache;
18
19 import java.io.Serializable JavaDoc;
20
21 import org.alfresco.error.AlfrescoRuntimeException;
22 import org.jboss.cache.Fqn;
23 import org.jboss.cache.TreeCache;
24
25 /**
26  * A thin adapter for <b>TreeCache</b> support.
27  *
28  * @author Derek Hulley
29  */

30 public class TreeCacheAdapter<K extends Serializable JavaDoc, V extends Serializable JavaDoc>
31         implements SimpleCache<K, V>
32 {
33     private TreeCache cache;
34     private Fqn regionFqn;
35     
36     public TreeCacheAdapter()
37     {
38     }
39
40     /**
41      * @param cache the backing Ehcache instance
42      */

43     public void setCache(TreeCache cache)
44     {
45         this.cache = cache;
46     }
47
48     /**
49      * Set the uniquely named region of the cache within which all object must be cached
50      *
51      * @param regionName the cache region
52      */

53     public void setRegionName(String JavaDoc regionName)
54     {
55         this.regionFqn = new Fqn(regionName);
56     }
57
58     public boolean contains(K key)
59     {
60         try
61         {
62             return cache.exists(regionFqn, key);
63         }
64         catch (Throwable JavaDoc e)
65         {
66             throw new AlfrescoRuntimeException("contains failed", e);
67         }
68     }
69
70     @SuppressWarnings JavaDoc("unchecked")
71     public V get(K key)
72     {
73         try
74         {
75             Object JavaDoc element = cache.get(regionFqn, key);
76             if (element != null)
77             {
78                 return (V) element;
79             }
80             else
81             {
82                 return null;
83             }
84         }
85         catch (Throwable JavaDoc e)
86         {
87             throw new AlfrescoRuntimeException("Failed to get from TreeCache: \n" +
88                     " key: " + key,
89                     e);
90         }
91     }
92
93     public void put(K key, V value)
94     {
95         try
96         {
97             cache.put(regionFqn, key, value);
98         }
99         catch (Throwable JavaDoc e)
100         {
101             throw new AlfrescoRuntimeException("Failed to put into TreeCache: \n" +
102                     " key: " + key + "\n" +
103                     " value: " + value,
104                     e);
105         }
106     }
107
108     public void remove(K key)
109     {
110         try
111         {
112             cache.remove(regionFqn, key);
113         }
114         catch (Throwable JavaDoc e)
115         {
116             throw new AlfrescoRuntimeException("Failed to remove from TreeCache: \n" +
117                     " key: " + key,
118                     e);
119         }
120     }
121
122     public void clear()
123     {
124         try
125         {
126             cache.remove(regionFqn);
127         }
128         catch (Throwable JavaDoc e)
129         {
130             throw new AlfrescoRuntimeException("Failed to clear cache", e);
131         }
132     }
133 }
134
Popular Tags