1 7 package org.jboss.tutorial.clusteredentity.bean; 8 9 10 import java.util.HashSet ; 11 import java.util.Map ; 12 import java.util.Set ; 13 import javax.ejb.Remote ; 14 import javax.ejb.Stateless ; 15 import javax.management.MBeanServer ; 16 import javax.management.ObjectName ; 17 import javax.persistence.EntityManager; 18 import javax.persistence.PersistenceContext; 19 import javax.persistence.PersistenceContext; 20 import org.hibernate.cache.entry.CacheEntry; 21 import org.hibernate.cache.entry.CollectionCacheEntry; 22 import org.jboss.cache.CacheException; 23 import org.jboss.cache.Fqn; 24 import org.jboss.cache.Node; 25 import org.jboss.cache.TreeCache; 26 import org.jboss.cache.TreeCacheMBean; 27 import org.jboss.mx.util.MBeanProxy; 28 import org.jboss.mx.util.MBeanServerLocator; 29 30 36 @Stateless 37 @Remote (EntityTest.class) 38 public class EntityTestBean implements EntityTest 39 { 40 @PersistenceContext 41 private EntityManager manager; 42 43 private Fqn testFqn; 44 45 public Customer createCustomer() 46 { 47 Customer customer = new Customer(); 48 customer.setName("JBoss"); 49 50 Set <Contact> contacts = new HashSet <Contact>(); 51 Contact kabir = new Contact(); 52 kabir.setCustomer(customer); 53 kabir.setName("Kabir"); 54 kabir.setTlf("1111"); 55 contacts.add(kabir); 56 57 Contact bill = new Contact(); 58 bill.setCustomer(customer); 59 bill.setName("Bill"); 60 bill.setTlf("2222"); 61 contacts.add(bill); 62 63 customer.setContacts(contacts); 64 manager.persist(customer); 65 return customer; 66 } 67 68 public Customer findByCustomerId(Long id) 69 { 70 return manager.find(Customer.class, id); 71 } 72 73 74 public boolean isCustomerInCache(Long id) 75 { 76 try 77 { 78 TreeCache cache = getCache(); 79 String key = "/org/jboss/tutorial/clusteredentity/bean/Customer/org.jboss.tutorial.clusteredentity.bean.Customer#" + id; 80 return isInCache(cache, key); 81 } 82 catch (Exception e) 83 { 84 throw new RuntimeException (e); 85 } 86 } 87 88 public boolean isContactInCache(Long id) 89 { 90 try 91 { 92 TreeCache cache = getCache(); 93 String key = "/org/jboss/tutorial/clusteredentity/bean/Contact/org.jboss.tutorial.clusteredentity.bean.Contact#" + id; 94 95 return isInCache(cache, key); 96 } 97 catch (Exception e) 98 { 99 throw new RuntimeException (e); 100 } 101 } 102 103 public boolean isCustomerContactsInCache(Long id) 104 { 105 try 106 { 107 TreeCache cache = getCache(); 108 String key = "/org/jboss/tutorial/clusteredentity/bean/Customer/contacts/org.jboss.tutorial.clusteredentity.bean.Customer.contacts#" + id; 109 110 return isInCache(cache, key); 111 } 112 catch (Exception e) 113 { 114 throw new RuntimeException (e); 115 } 116 } 117 118 private TreeCache getCache() throws Exception 119 { 120 MBeanServer server = MBeanServerLocator.locateJBoss(); 121 TreeCacheMBean proxy = (TreeCacheMBean)MBeanProxy.get(TreeCacheMBean.class, new ObjectName ("jboss.cache:service=EJB3EntityTreeCache"), server); 122 return proxy.getInstance(); 123 } 124 125 private boolean isInCache(TreeCache cache, String key)throws CacheException 126 { 127 return isInCache(cache, null, key); 128 } 129 130 private boolean isInCache(TreeCache cache, Node node, String key) throws CacheException 131 { 132 135 if (node == null) 136 { 137 node = cache.get("/"); 138 } 139 140 Map map = node.getChildren(); 141 for(Object child : map.values()) 142 { 143 Node childNode = (Node)child; 144 145 Fqn fqn = childNode.getFqn(); 146 if (fqn.toString().equals(key)) 147 { 148 Object entry = childNode.getData().get("item"); 149 return (entry != null) && (entry instanceof CacheEntry || entry instanceof CollectionCacheEntry); 150 } 151 152 if (childNode.hasChildren()) 153 { 154 if (isInCache(cache, childNode, key)) 155 { 156 return true; 157 } 158 } 159 } 160 161 return false; 162 } 163 } 164 | Popular Tags |