KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > clusteredentity > EntityTestBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.clusteredentity;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import javax.ejb.Remote JavaDoc;
28 import javax.ejb.Stateless JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.persistence.EntityManager;
32 import javax.persistence.PersistenceContext;
33
34 import org.jboss.cache.AbstractCacheListener;
35 import org.jboss.cache.Fqn;
36 import org.jboss.cache.Cache;
37 import org.jboss.cache.CacheListener;
38 import org.jboss.cache.jmx.CacheJmxWrapperMBean;
39 import org.jboss.mx.util.MBeanProxyExt;
40 import org.jboss.mx.util.MBeanServerLocator;
41 import org.jgroups.View;
42
43 /**
44  * Comment
45  *
46  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
47  * @version $Revision: 58553 $
48  */

49 @Stateless JavaDoc
50 @Remote JavaDoc(EntityTest.class)
51 public class EntityTestBean implements EntityTest
52 {
53    @PersistenceContext
54    private EntityManager manager;
55    
56    static MyListener listener;
57
58    public EntityTestBean()
59    {
60       try
61       {
62          //Just to initialise the cache with a listener
63
Cache cache = getCache();
64          if (listener == null)
65          {
66             listener = new MyListener();
67             cache.addCacheListener(listener);
68          }
69       }
70       catch (Exception JavaDoc e)
71       {
72          throw new RuntimeException JavaDoc(e);
73       }
74    }
75    
76    public Customer createCustomer()
77    {
78       System.out.println("CREATE CUSTOMER");
79       try
80       {
81          listener.clear();
82          
83          Customer customer = new Customer();
84          customer.setId(1);
85          customer.setName("JBoss");
86          Set JavaDoc<Contact> contacts = new HashSet JavaDoc<Contact>();
87          
88          Contact kabir = new Contact();
89          kabir.setId(1);
90          kabir.setCustomer(customer);
91          kabir.setName("Kabir");
92          kabir.setTlf("1111");
93          contacts.add(kabir);
94          
95          Contact bill = new Contact();
96          bill.setId(2);
97          bill.setCustomer(customer);
98          bill.setName("Bill");
99          bill.setTlf("2222");
100          contacts.add(bill);
101
102          customer.setContacts(contacts);
103
104          manager.persist(customer);
105          return customer;
106       }
107       catch (Exception JavaDoc e)
108       {
109          throw new RuntimeException JavaDoc(e); // TODO Auto-generated catch block
110
}
111       finally
112       {
113          System.out.println("CREATE CUSTOMER - END");
114       }
115    }
116
117    public Customer findByCustomerId(Integer JavaDoc id)
118    {
119       System.out.println("FIND CUSTOMER");
120       listener.clear();
121       try
122       {
123          Customer customer = manager.find(Customer.class, id);
124          
125          return customer;
126       }
127       catch (Exception JavaDoc e)
128       {
129          throw new RuntimeException JavaDoc(e); // TODO Auto-generated catch block
130
}
131       finally
132       {
133          System.out.println("FIND CUSTOMER - END");
134       }
135    }
136    
137    public void loadedFromCache()
138    {
139       System.out.println("CHECK CACHE");
140       try
141       {
142          System.out.println("Visited: " + listener.visited);
143          if (!listener.visited.contains("Customer#1"))
144             throw new RuntimeException JavaDoc("Customer#1 was not in cache");
145          if (!listener.visited.contains("Contact#1"))
146             throw new RuntimeException JavaDoc("Contact#1 was not in cache");
147          if (!listener.visited.contains("Contact#2"))
148             throw new RuntimeException JavaDoc("Contact2#1 was not in cache");
149          if (!listener.visited.contains("Customer.contacts#1"))
150             throw new RuntimeException JavaDoc("Customer.contacts#1 was not in cache");
151       }
152       finally
153       {
154          System.out.println("CHECK CACHE - END");
155       }
156       
157    }
158
159    private Cache getCache() throws Exception JavaDoc
160    {
161       MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
162       CacheJmxWrapperMBean proxy = (CacheJmxWrapperMBean)MBeanProxyExt.create(CacheJmxWrapperMBean.class, new ObjectName JavaDoc("jboss.cache:service=EJB3EntityTreeCache"), server);
163       Cache cache = proxy.getCache();
164       
165       return cache;
166    }
167
168    class MyListener extends AbstractCacheListener
169    {
170       HashSet JavaDoc visited = new HashSet JavaDoc();
171       
172       public void clear()
173       {
174          visited.clear();
175       }
176       
177       @Override JavaDoc
178       public void nodeVisited(Fqn fqn, boolean pre)
179       {
180          if (!pre)
181          {
182             System.out.println("MyListener - Visiting node " + fqn.toString());
183             String JavaDoc name = fqn.toString();
184             String JavaDoc token = ".clusteredentity.";
185             int index = name.indexOf(token);
186             index += token.length();
187             name = name.substring(index);
188             System.out.println(name);
189             visited.add(name);
190          }
191       }
192    }
193 }
194
Popular Tags