1 22 package org.jboss.test.cache.bean; 23 24 import org.jboss.cache.Cache; 25 import org.jboss.cache.CacheException; 26 import org.jboss.cache.Fqn; 27 import org.jboss.cache.Node; 28 import org.jboss.cache.TreeCache; 29 import org.jboss.cache.config.Configuration; 30 import org.jboss.cache.factories.DefaultCacheFactory; 31 import org.jboss.cache.lock.IsolationLevel; 32 33 import javax.ejb.CreateException ; 34 import javax.ejb.SessionBean ; 35 import javax.ejb.SessionContext ; 36 37 import java.util.List ; 38 import java.util.Map ; 39 import java.util.Set ; 40 import java.util.Vector ; 41 42 43 54 public class TreeCacheTesterBean implements SessionBean 55 { 56 Cache cache = null; 57 58 62 public void ejbCreate() throws CreateException 63 { 64 log("I'm being created"); 65 } 66 67 74 public void ejbCreate(String cluster_name, 75 String props, 76 int caching_mode) throws CreateException 77 { 78 try { 79 Configuration config = new Configuration(); 81 config.setClusterName(cluster_name); 82 config.setClusterConfig(props); 83 config.setCacheMode(Configuration.legacyModeToCacheMode(caching_mode)); 84 config.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup"); 85 86 cache = DefaultCacheFactory.createCache(config); 87 88 } catch (Exception e) { 89 throw new CreateException (e.toString()); 90 } 91 } 92 93 94 119 public void ejbActivate() 120 { 121 } 122 123 public void ejbPassivate() 124 { 125 } 126 127 public void ejbRemove() 128 { 129 log("I'm being removed"); 130 if (cache != null) { 131 cache.stop(); 132 cache.destroy(); 133 cache = null; 134 } 135 } 136 137 public void setSessionContext(SessionContext ctx) 138 { 139 } 140 141 142 146 public Vector getMembers() 147 { 148 List members = cache.getMembers(); 149 return members == null ? null : new Vector (members); 150 } 151 152 153 157 public void setCacheMode(int mode) throws Exception 158 { 159 cache.getConfiguration().setCacheMode(Configuration.legacyModeToCacheMode(mode)); 160 } 161 162 163 167 public void setIsolationLevel(IsolationLevel level) 168 { 169 cache.getConfiguration().setIsolationLevel(level); 170 } 171 172 173 178 public Set getKeys(String fqn) throws CacheException 179 { 180 Node node = cache.getChild(Fqn.fromString(fqn)); 181 return node == null ? null : node.getKeys(); 182 } 183 184 190 public Object get(String fqn, String key) throws CacheException { 191 return cache.get(Fqn.fromString(fqn), key); 192 } 193 194 199 public boolean exists(String fqn) 200 { 201 return cache.hasChild(Fqn.fromString(fqn)); 202 } 203 204 210 public void put(String fqn, Map data) throws Exception 211 { 212 cache.put(fqn, data); 213 } 214 215 223 public Object put(String fqn, String key, Object value) throws Exception 224 { 225 return cache.put(Fqn.fromString(fqn), key, value); 226 } 227 228 233 public void remove(String fqn) throws Exception 234 { 235 cache.remove(fqn); 236 } 237 238 245 public Object remove(String fqn, String key) throws Exception 246 { 247 return cache.remove(Fqn.fromString(fqn), key); 248 } 249 250 269 274 public Set getChildrenNames(String fqn) throws CacheException 275 { 276 Node node = cache.getChild(Fqn.fromString(fqn)); 277 return (node == null ? null : node.getChildrenNames()); 278 } 279 280 298 309 316 328 335 private void log(String msg) 336 { 337 System.out.println("-- [" + Thread.currentThread().getName() + "]: " + msg); 338 } 339 340 } 341 | Popular Tags |