FileDocCategorySizeDatePackage
EntityTestBean.javaAPI DocJBoss 4.2.15238Fri Jul 13 20:55:06 BST 2007org.jboss.tutorial.clusteredentity.bean

EntityTestBean

public class EntityTestBean extends Object implements EntityTest
Comment
author
Bill Burke
version
$Revision$

Fields Summary
private EntityManager
manager
Constructors Summary
Methods Summary
public CustomercreateCustomer()

      Customer customer = new Customer();
      customer.setName("JBoss");

      Set<Contact> contacts = new HashSet<Contact>();
      Contact kabir = new Contact();
      kabir.setCustomer(customer);
      kabir.setName("Kabir");
      kabir.setTlf("1111");
      contacts.add(kabir);

      Contact bill = new Contact();
      bill.setCustomer(customer);
      bill.setName("Bill");
      bill.setTlf("2222");
      contacts.add(bill);

      customer.setContacts(contacts);
      manager.persist(customer);
      return customer;
   
public CustomerfindByCustomerId(java.lang.Long id)

      return manager.find(Customer.class, id);
   
private org.jboss.cache.TreeCachegetCache()

      MBeanServer server = MBeanServerLocator.locateJBoss();
      TreeCacheMBean proxy = (TreeCacheMBean)MBeanProxyExt.create(TreeCacheMBean.class, new ObjectName("jboss.cache:service=EJB3EntityTreeCache"), server);
      return proxy.getInstance();
   
public booleanisContactInCache(java.lang.Long id)

      try
      {
         TreeCache cache = getCache();
         String key = "/org/jboss/tutorial/clusteredentity/bean/Contact/org.jboss.tutorial.clusteredentity.bean.Contact#" + id;

         return isInCache(cache, key);
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }
   
public booleanisCustomerContactsInCache(java.lang.Long id)

      try
      {
         TreeCache cache = getCache();
         String key = "/org/jboss/tutorial/clusteredentity/bean/Customer/contacts/org.jboss.tutorial.clusteredentity.bean.Customer.contacts#" + id;

         return isInCache(cache, key);
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }
   
public booleanisCustomerInCache(java.lang.Long id)

      try
      {
         TreeCache cache = getCache();
         String key = "/org/jboss/tutorial/clusteredentity/bean/Customer/org.jboss.tutorial.clusteredentity.bean.Customer#" + id;
         return isInCache(cache, key);
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }
   
private booleanisInCache(org.jboss.cache.TreeCache cache, java.lang.String key)

      return isInCache(cache, null, key);
   
private booleanisInCache(org.jboss.cache.TreeCache cache, org.jboss.cache.Node node, java.lang.String key)

      //Not the best way to look up the cache entry, but how hibernate creates the cache entry
      //and fqn seems to be buried deep deep down inside hibernate...

      if (node == null)
      {
         node = cache.get("/");
      }

      Map map = node.getChildren();
      for(Object child : map.values())
      {
         Node childNode = (Node)child;

         Fqn fqn = childNode.getFqn();
         if (fqn.toString().equals(key))
         {
            Object entry = childNode.getData().get("item");
            return (entry != null) && (entry instanceof CacheEntry || entry instanceof CollectionCacheEntry);
         }

         Map children = childNode.getChildren();
         if (children != null && children.size() > 0)
         {
            if (isInCache(cache, childNode, key))
            {
               return true;
            }
         }
      }

      return false;