FileDocCategorySizeDatePackage
EntityTestBean.javaAPI DocJBoss 4.2.16797Fri Jul 13 20:53:06 BST 2007org.jboss.ejb3.test.clusteredentity

EntityTestBean

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

Fields Summary
private EntityManager
manager
private String
cacheObjectName
static MyListener
listener
Constructors Summary
public EntityTestBean()

   
Methods Summary
public voidcleanup()

      try
      {         
         if (listener != null)
         {
            getCache().removeTreeCacheListener(listener);
         }
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      
      try
      {
         if (manager != null)
         {
            Customer c = findByCustomerId(1);
            if (c != null)
            {
               Set contacts = c.getContacts();
               for (Iterator it = contacts.iterator(); it.hasNext();)
                  manager.remove(it.next());
               c.setContacts(null);
               manager.remove(c);
            }
         }
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   
public CustomercreateCustomer()

      System.out.println("CREATE CUSTOMER");
      try
      {
         listener.clear();
         
         Customer customer = new Customer();
         customer.setId(1);
         customer.setName("JBoss");
         Set<Contact> contacts = new HashSet<Contact>();
         
         Contact kabir = new Contact();
         kabir.setId(1);
         kabir.setCustomer(customer);
         kabir.setName("Kabir");
         kabir.setTlf("1111");
         contacts.add(kabir);
         
         Contact bill = new Contact();
         bill.setId(2);
         bill.setCustomer(customer);
         bill.setName("Bill");
         bill.setTlf("2222");
         contacts.add(bill);

         customer.setContacts(contacts);

         manager.persist(customer);
         return customer;
      }
      catch (RuntimeException e)
      {
         throw e;
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }
      finally
      {
         System.out.println("CREATE CUSTOMER -  END");         
      }
   
public CustomerfindByCustomerId(java.lang.Integer id)

      System.out.println("FIND CUSTOMER");         
      listener.clear();
      try
      {
         Customer customer = manager.find(Customer.class, id);
         
         return customer;
      }
      catch (RuntimeException e)
      {
         throw e;
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }
      finally
      {
         System.out.println("FIND CUSTOMER -  END");         
      }
   
public voidgetCache(boolean optimistic)

      if (optimistic)
         cacheObjectName = "jboss.cache:service=OptimisticEJB3EntityTreeCache";
      else
         cacheObjectName = "jboss.cache:service=EJB3EntityTreeCache";

      try
      {
         //Just to initialise the cache with a listener
         TreeCache cache = getCache();
         if (listener == null)
         {
            listener = new MyListener();
            cache.addTreeCacheListener(listener);
         }
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }
   
private org.jboss.cache.TreeCachegetCache()

      MBeanServer server = MBeanServerLocator.locateJBoss();
      TreeCacheMBean proxy = (TreeCacheMBean)MBeanProxyExt.create(TreeCacheMBean.class, new ObjectName(cacheObjectName), server);
      TreeCache cache = proxy.getInstance();
      
      return cache;
   
public voidloadedFromCache()

      System.out.println("CHECK CACHE");         
      try
      {
         System.out.println("Visited: " + listener.visited);
         if (!listener.visited.contains("Customer#1"))
            throw new RuntimeException("Customer#1 was not in cache");
         if (!listener.visited.contains("Contact#1"))
            throw new RuntimeException("Contact#1 was not in cache");
         if (!listener.visited.contains("Contact#2"))
            throw new RuntimeException("Contact2#1 was not in cache");
         if (!listener.visited.contains("Customer.contacts#1"))
            throw new RuntimeException("Customer.contacts#1 was not in cache");
      }
      finally
      {
         System.out.println("CHECK CACHE -  END");         
      }