KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > cache > JBossCacheEngine


1 /*
2  * Copyright (c) 2003, 2004 Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * Created on Jan 13, 2005 11:42:54 PM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.cache;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.Collection JavaDoc;
47
48 import net.jforum.exceptions.CacheException;
49 import net.jforum.util.preferences.ConfigKeys;
50 import net.jforum.util.preferences.SystemGlobals;
51
52 import org.apache.log4j.Logger;
53 import org.jboss.cache.Fqn;
54 import org.jboss.cache.Node;
55 import org.jboss.cache.PropertyConfigurator;
56 import org.jboss.cache.TreeCache;
57
58 /**
59  * @author Rafael Steil
60  * @version $Id: JBossCacheEngine.java,v 1.7 2005/09/25 02:40:28 rafaelsteil Exp $
61  */

62 public class JBossCacheEngine implements CacheEngine
63 {
64     private Logger logger = Logger.getLogger(JBossCacheEngine.class);
65     private TreeCache cache;
66
67     /**
68      * @see net.jforum.cache.CacheEngine#init()
69      */

70     public void init()
71     {
72         try {
73             this.cache = new TreeCache();
74             PropertyConfigurator config = new PropertyConfigurator();
75             config.configure(this.cache, SystemGlobals.getValue(ConfigKeys.JBOSS_CACHE_PROPERTIES));
76             
77             this.cache.startService();
78         }
79         catch (Exception JavaDoc e) {
80             throw new CacheException("Error while trying to configure jboss-cache: " + e);
81         }
82     }
83     
84     /**
85      * @see net.jforum.cache.CacheEngine#stop()
86      */

87     public void stop()
88     {
89         this.cache.stopService();
90     }
91
92     /**
93      * @see net.jforum.cache.CacheEngine#add(java.lang.String, java.lang.Object)
94      */

95     public void add(String JavaDoc key, Object JavaDoc value)
96     {
97         this.add(CacheEngine.DUMMY_FQN, key, value);
98     }
99
100     /**
101      * @see net.jforum.cache.CacheEngine#add(java.lang.String, java.lang.String, java.lang.Object)
102      */

103     public void add(String JavaDoc fqn, String JavaDoc key, Object JavaDoc value)
104     {
105         try {
106             this.cache.put(Fqn.fromString(fqn), key, value);
107         }
108         catch (Exception JavaDoc e) {
109             throw new CacheException("Error adding a new entry to the cache: " + e);
110         }
111     }
112
113     /**
114      * @see net.jforum.cache.CacheEngine#get(java.lang.String, java.lang.String)
115      */

116     public Object JavaDoc get(String JavaDoc fqn, String JavaDoc key)
117     {
118         try {
119             return this.cache.get(Fqn.fromString(fqn), key);
120         }
121         catch (Exception JavaDoc e) {
122             throw new CacheException("Error while trying to get an entry from the cache: " + e);
123         }
124     }
125
126     /**
127      * @see net.jforum.cache.CacheEngine#get(java.lang.String)
128      */

129     public Object JavaDoc get(String JavaDoc fqn)
130     {
131         try {
132             return this.cache.get(Fqn.fromString(fqn));
133         }
134         catch (Exception JavaDoc e) {
135             throw new CacheException("Error while trying to get an entry from the cache: " + e);
136         }
137     }
138     
139     /**
140      * @see net.jforum.cache.CacheEngine#getValues(java.lang.String)
141      */

142     public Collection JavaDoc getValues(String JavaDoc fqn)
143     {
144         Node node = (Node)this.get(fqn);
145         if (node == null) {
146             return new ArrayList JavaDoc();
147         }
148         
149         return node.getData().values();
150     }
151
152     /**
153      * @see net.jforum.cache.CacheEngine#remove(java.lang.String, java.lang.String)
154      */

155     public void remove(String JavaDoc fqn, String JavaDoc key)
156     {
157         try {
158             if (key == null) {
159                 this.cache.remove(Fqn.fromString(fqn));
160             }
161             else {
162                 this.cache.remove(Fqn.fromString(fqn), key);
163             }
164         }
165         catch (Exception JavaDoc e) {
166             logger.warn("Error while removing a FQN from the cache: " + e);
167         }
168     }
169
170     /**
171      * @see net.jforum.cache.CacheEngine#remove(java.lang.String)
172      */

173     public void remove(String JavaDoc fqn)
174     {
175         try {
176             this.cache.remove(Fqn.fromString(fqn));
177         }
178         catch (Exception JavaDoc e) {
179             logger.warn("Error while removing a FQN from the cache: " + e);
180         }
181     }
182
183 }
184
Popular Tags