1 43 package net.jforum.cache; 44 45 import java.util.ArrayList ; 46 import java.util.Collection ; 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 62 public class JBossCacheEngine implements CacheEngine 63 { 64 private Logger logger = Logger.getLogger(JBossCacheEngine.class); 65 private TreeCache cache; 66 67 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 e) { 80 throw new CacheException("Error while trying to configure jboss-cache: " + e); 81 } 82 } 83 84 87 public void stop() 88 { 89 this.cache.stopService(); 90 } 91 92 95 public void add(String key, Object value) 96 { 97 this.add(CacheEngine.DUMMY_FQN, key, value); 98 } 99 100 103 public void add(String fqn, String key, Object value) 104 { 105 try { 106 this.cache.put(Fqn.fromString(fqn), key, value); 107 } 108 catch (Exception e) { 109 throw new CacheException("Error adding a new entry to the cache: " + e); 110 } 111 } 112 113 116 public Object get(String fqn, String key) 117 { 118 try { 119 return this.cache.get(Fqn.fromString(fqn), key); 120 } 121 catch (Exception e) { 122 throw new CacheException("Error while trying to get an entry from the cache: " + e); 123 } 124 } 125 126 129 public Object get(String fqn) 130 { 131 try { 132 return this.cache.get(Fqn.fromString(fqn)); 133 } 134 catch (Exception e) { 135 throw new CacheException("Error while trying to get an entry from the cache: " + e); 136 } 137 } 138 139 142 public Collection getValues(String fqn) 143 { 144 Node node = (Node)this.get(fqn); 145 if (node == null) { 146 return new ArrayList (); 147 } 148 149 return node.getData().values(); 150 } 151 152 155 public void remove(String fqn, String 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 e) { 166 logger.warn("Error while removing a FQN from the cache: " + e); 167 } 168 } 169 170 173 public void remove(String fqn) 174 { 175 try { 176 this.cache.remove(Fqn.fromString(fqn)); 177 } 178 catch (Exception e) { 179 logger.warn("Error while removing a FQN from the cache: " + e); 180 } 181 } 182 183 } 184 | Popular Tags |