KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tutorial > clusteredentity > bean > EntityTestBean


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.tutorial.clusteredentity.bean;
8
9
10 import java.util.HashSet JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Set JavaDoc;
13 import javax.ejb.Remote JavaDoc;
14 import javax.ejb.Stateless JavaDoc;
15 import javax.management.MBeanServer JavaDoc;
16 import javax.management.ObjectName JavaDoc;
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 /**
31  * Comment
32  *
33  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
34  * @version $Revision$
35  */

36 @Stateless JavaDoc
37 @Remote JavaDoc(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 JavaDoc<Contact> contacts = new HashSet JavaDoc<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 JavaDoc id)
69    {
70       return manager.find(Customer.class, id);
71    }
72
73
74    public boolean isCustomerInCache(Long JavaDoc id)
75    {
76       try
77       {
78          TreeCache cache = getCache();
79          String JavaDoc key = "/org/jboss/tutorial/clusteredentity/bean/Customer/org.jboss.tutorial.clusteredentity.bean.Customer#" + id;
80          return isInCache(cache, key);
81       }
82       catch (Exception JavaDoc e)
83       {
84          throw new RuntimeException JavaDoc(e);
85       }
86    }
87
88    public boolean isContactInCache(Long JavaDoc id)
89    {
90       try
91       {
92          TreeCache cache = getCache();
93          String JavaDoc key = "/org/jboss/tutorial/clusteredentity/bean/Contact/org.jboss.tutorial.clusteredentity.bean.Contact#" + id;
94
95          return isInCache(cache, key);
96       }
97       catch (Exception JavaDoc e)
98       {
99          throw new RuntimeException JavaDoc(e);
100       }
101    }
102
103    public boolean isCustomerContactsInCache(Long JavaDoc id)
104    {
105       try
106       {
107          TreeCache cache = getCache();
108          String JavaDoc 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 JavaDoc e)
113       {
114          throw new RuntimeException JavaDoc(e);
115       }
116    }
117
118    private TreeCache getCache() throws Exception JavaDoc
119    {
120       MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
121       TreeCacheMBean proxy = (TreeCacheMBean)MBeanProxy.get(TreeCacheMBean.class, new ObjectName JavaDoc("jboss.cache:service=EJB3EntityTreeCache"), server);
122       return proxy.getInstance();
123    }
124
125    private boolean isInCache(TreeCache cache, String JavaDoc key)throws CacheException
126    {
127       return isInCache(cache, null, key);
128    }
129
130    private boolean isInCache(TreeCache cache, Node node, String JavaDoc key) throws CacheException
131    {
132       //Not the best way to look up the cache entry, but how hibernate creates the cache entry
133
//and fqn seems to be buried deep deep down inside hibernate...
134

135       if (node == null)
136       {
137          node = cache.get("/");
138       }
139
140       Map JavaDoc map = node.getChildren();
141       for(Object JavaDoc child : map.values())
142       {
143          Node childNode = (Node)child;
144
145          Fqn fqn = childNode.getFqn();
146          if (fqn.toString().equals(key))
147          {
148             Object JavaDoc 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