001: /*
002: * Copyright (c) 2003, 2004 Rafael Steil
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on Jan 13, 2005 11:42:54 PM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.cache;
044:
045: import java.util.ArrayList;
046: import java.util.Collection;
047:
048: import net.jforum.exceptions.CacheException;
049: import net.jforum.util.preferences.ConfigKeys;
050: import net.jforum.util.preferences.SystemGlobals;
051:
052: import org.apache.log4j.Logger;
053: import org.jboss.cache.Fqn;
054: import org.jboss.cache.Node;
055: import org.jboss.cache.PropertyConfigurator;
056: import org.jboss.cache.TreeCache;
057:
058: /**
059: * @author Rafael Steil
060: * @version $Id: JBossCacheEngine.java,v 1.7 2005/09/25 02:40:28 rafaelsteil Exp $
061: */
062: public class JBossCacheEngine implements CacheEngine {
063: private Logger logger = Logger.getLogger(JBossCacheEngine.class);
064: private TreeCache cache;
065:
066: /**
067: * @see net.jforum.cache.CacheEngine#init()
068: */
069: public void init() {
070: try {
071: this .cache = new TreeCache();
072: PropertyConfigurator config = new PropertyConfigurator();
073: config.configure(this .cache, SystemGlobals
074: .getValue(ConfigKeys.JBOSS_CACHE_PROPERTIES));
075:
076: this .cache.startService();
077: } catch (Exception e) {
078: throw new CacheException(
079: "Error while trying to configure jboss-cache: " + e);
080: }
081: }
082:
083: /**
084: * @see net.jforum.cache.CacheEngine#stop()
085: */
086: public void stop() {
087: this .cache.stopService();
088: }
089:
090: /**
091: * @see net.jforum.cache.CacheEngine#add(java.lang.String, java.lang.Object)
092: */
093: public void add(String key, Object value) {
094: this .add(CacheEngine.DUMMY_FQN, key, value);
095: }
096:
097: /**
098: * @see net.jforum.cache.CacheEngine#add(java.lang.String, java.lang.String, java.lang.Object)
099: */
100: public void add(String fqn, String key, Object value) {
101: try {
102: this .cache.put(Fqn.fromString(fqn), key, value);
103: } catch (Exception e) {
104: throw new CacheException(
105: "Error adding a new entry to the cache: " + e);
106: }
107: }
108:
109: /**
110: * @see net.jforum.cache.CacheEngine#get(java.lang.String, java.lang.String)
111: */
112: public Object get(String fqn, String key) {
113: try {
114: return this .cache.get(Fqn.fromString(fqn), key);
115: } catch (Exception e) {
116: throw new CacheException(
117: "Error while trying to get an entry from the cache: "
118: + e);
119: }
120: }
121:
122: /**
123: * @see net.jforum.cache.CacheEngine#get(java.lang.String)
124: */
125: public Object get(String fqn) {
126: try {
127: return this .cache.get(Fqn.fromString(fqn));
128: } catch (Exception e) {
129: throw new CacheException(
130: "Error while trying to get an entry from the cache: "
131: + e);
132: }
133: }
134:
135: /**
136: * @see net.jforum.cache.CacheEngine#getValues(java.lang.String)
137: */
138: public Collection getValues(String fqn) {
139: Node node = (Node) this .get(fqn);
140: if (node == null) {
141: return new ArrayList();
142: }
143:
144: return node.getData().values();
145: }
146:
147: /**
148: * @see net.jforum.cache.CacheEngine#remove(java.lang.String, java.lang.String)
149: */
150: public void remove(String fqn, String key) {
151: try {
152: if (key == null) {
153: this .cache.remove(Fqn.fromString(fqn));
154: } else {
155: this .cache.remove(Fqn.fromString(fqn), key);
156: }
157: } catch (Exception e) {
158: logger.warn("Error while removing a FQN from the cache: "
159: + e);
160: }
161: }
162:
163: /**
164: * @see net.jforum.cache.CacheEngine#remove(java.lang.String)
165: */
166: public void remove(String fqn) {
167: try {
168: this .cache.remove(Fqn.fromString(fqn));
169: } catch (Exception e) {
170: logger.warn("Error while removing a FQN from the cache: "
171: + e);
172: }
173: }
174:
175: }
|