JBossCacheEngine.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 5k
Category:

Java Develop

Development Platform:

Java

  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. import java.util.ArrayList;
  45. import java.util.Collection;
  46. import net.jforum.exceptions.CacheException;
  47. import net.jforum.util.preferences.ConfigKeys;
  48. import net.jforum.util.preferences.SystemGlobals;
  49. import org.apache.log4j.Logger;
  50. import org.jboss.cache.Fqn;
  51. import org.jboss.cache.Node;
  52. import org.jboss.cache.PropertyConfigurator;
  53. import org.jboss.cache.TreeCache;
  54. /**
  55.  * @author Rafael Steil
  56.  * @version $Id: JBossCacheEngine.java,v 1.7 2005/09/25 02:40:28 rafaelsteil Exp $
  57.  */
  58. public class JBossCacheEngine implements CacheEngine
  59. {
  60. private Logger logger = Logger.getLogger(JBossCacheEngine.class);
  61. private TreeCache cache;
  62. /**
  63.  * @see net.jforum.cache.CacheEngine#init()
  64.  */
  65. public void init()
  66. {
  67. try {
  68. this.cache = new TreeCache();
  69. PropertyConfigurator config = new PropertyConfigurator();
  70. config.configure(this.cache, SystemGlobals.getValue(ConfigKeys.JBOSS_CACHE_PROPERTIES));
  71. this.cache.startService();
  72. }
  73. catch (Exception e) {
  74. throw new CacheException("Error while trying to configure jboss-cache: " + e);
  75. }
  76. }
  77. /**
  78.  * @see net.jforum.cache.CacheEngine#stop()
  79.  */
  80. public void stop()
  81. {
  82. this.cache.stopService();
  83. }
  84. /**
  85.  * @see net.jforum.cache.CacheEngine#add(java.lang.String, java.lang.Object)
  86.  */
  87. public void add(String key, Object value)
  88. {
  89. this.add(CacheEngine.DUMMY_FQN, key, value);
  90. }
  91. /**
  92.  * @see net.jforum.cache.CacheEngine#add(java.lang.String, java.lang.String, java.lang.Object)
  93.  */
  94. public void add(String fqn, String key, Object value)
  95. {
  96. try {
  97. this.cache.put(Fqn.fromString(fqn), key, value);
  98. }
  99. catch (Exception e) {
  100. throw new CacheException("Error adding a new entry to the cache: " + e);
  101. }
  102. }
  103. /**
  104.  * @see net.jforum.cache.CacheEngine#get(java.lang.String, java.lang.String)
  105.  */
  106. public Object get(String fqn, String key)
  107. {
  108. try {
  109. return this.cache.get(Fqn.fromString(fqn), key);
  110. }
  111. catch (Exception e) {
  112. throw new CacheException("Error while trying to get an entry from the cache: " + e);
  113. }
  114. }
  115. /**
  116.  * @see net.jforum.cache.CacheEngine#get(java.lang.String)
  117.  */
  118. public Object get(String fqn)
  119. {
  120. try {
  121. return this.cache.get(Fqn.fromString(fqn));
  122. }
  123. catch (Exception e) {
  124. throw new CacheException("Error while trying to get an entry from the cache: " + e);
  125. }
  126. }
  127. /**
  128.  * @see net.jforum.cache.CacheEngine#getValues(java.lang.String)
  129.  */
  130. public Collection getValues(String fqn)
  131. {
  132. Node node = (Node)this.get(fqn);
  133. if (node == null) {
  134. return new ArrayList();
  135. }
  136. return node.getData().values();
  137. }
  138. /**
  139.  * @see net.jforum.cache.CacheEngine#remove(java.lang.String, java.lang.String)
  140.  */
  141. public void remove(String fqn, String key)
  142. {
  143. try {
  144. if (key == null) {
  145. this.cache.remove(Fqn.fromString(fqn));
  146. }
  147. else {
  148. this.cache.remove(Fqn.fromString(fqn), key);
  149. }
  150. }
  151. catch (Exception e) {
  152. logger.warn("Error while removing a FQN from the cache: " + e);
  153. }
  154. }
  155. /**
  156.  * @see net.jforum.cache.CacheEngine#remove(java.lang.String)
  157.  */
  158. public void remove(String fqn)
  159. {
  160. try {
  161. this.cache.remove(Fqn.fromString(fqn));
  162. }
  163. catch (Exception e) {
  164. logger.warn("Error while removing a FQN from the cache: " + e);
  165. }
  166. }
  167. }