001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.test.clusteredentity;
023:
024: import java.util.HashSet;
025: import java.util.Iterator;
026: import java.util.Set;
027:
028: import javax.annotation.PreDestroy;
029: import javax.ejb.Remote;
030: import javax.ejb.Remove;
031: import javax.ejb.Stateless;
032: import javax.management.MBeanServer;
033: import javax.management.ObjectName;
034: import javax.persistence.EntityManager;
035: import javax.persistence.PersistenceContext;
036:
037: import org.jboss.cache.Fqn;
038: import org.jboss.cache.AbstractTreeCacheListener;
039: import org.jboss.cache.TreeCache;
040: import org.jboss.cache.TreeCacheMBean;
041: import org.jboss.mx.util.MBeanProxyExt;
042: import org.jboss.mx.util.MBeanServerLocator;
043:
044: /**
045: * Comment
046: *
047: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
048: * @version $Revision: 60695 $
049: */
050: @Stateless
051: @Remote(EntityTest.class)
052: public class EntityTestBean implements EntityTest {
053: @PersistenceContext
054: private EntityManager manager;
055:
056: private String cacheObjectName;
057:
058: static MyListener listener;
059:
060: public EntityTestBean() {
061: }
062:
063: public void getCache(boolean optimistic) {
064: if (optimistic)
065: cacheObjectName = "jboss.cache:service=OptimisticEJB3EntityTreeCache";
066: else
067: cacheObjectName = "jboss.cache:service=EJB3EntityTreeCache";
068:
069: try {
070: //Just to initialise the cache with a listener
071: TreeCache cache = getCache();
072: if (listener == null) {
073: listener = new MyListener();
074: cache.addTreeCacheListener(listener);
075: }
076: } catch (Exception e) {
077: throw new RuntimeException(e);
078: }
079: }
080:
081: public Customer createCustomer() {
082: System.out.println("CREATE CUSTOMER");
083: try {
084: listener.clear();
085:
086: Customer customer = new Customer();
087: customer.setId(1);
088: customer.setName("JBoss");
089: Set<Contact> contacts = new HashSet<Contact>();
090:
091: Contact kabir = new Contact();
092: kabir.setId(1);
093: kabir.setCustomer(customer);
094: kabir.setName("Kabir");
095: kabir.setTlf("1111");
096: contacts.add(kabir);
097:
098: Contact bill = new Contact();
099: bill.setId(2);
100: bill.setCustomer(customer);
101: bill.setName("Bill");
102: bill.setTlf("2222");
103: contacts.add(bill);
104:
105: customer.setContacts(contacts);
106:
107: manager.persist(customer);
108: return customer;
109: } catch (RuntimeException e) {
110: throw e;
111: } catch (Exception e) {
112: throw new RuntimeException(e);
113: } finally {
114: System.out.println("CREATE CUSTOMER - END");
115: }
116: }
117:
118: public Customer findByCustomerId(Integer id) {
119: System.out.println("FIND CUSTOMER");
120: listener.clear();
121: try {
122: Customer customer = manager.find(Customer.class, id);
123:
124: return customer;
125: } catch (RuntimeException e) {
126: throw e;
127: } catch (Exception e) {
128: throw new RuntimeException(e);
129: } finally {
130: System.out.println("FIND CUSTOMER - END");
131: }
132: }
133:
134: public void loadedFromCache() {
135: System.out.println("CHECK CACHE");
136: try {
137: System.out.println("Visited: " + listener.visited);
138: if (!listener.visited.contains("Customer#1"))
139: throw new RuntimeException(
140: "Customer#1 was not in cache");
141: if (!listener.visited.contains("Contact#1"))
142: throw new RuntimeException("Contact#1 was not in cache");
143: if (!listener.visited.contains("Contact#2"))
144: throw new RuntimeException(
145: "Contact2#1 was not in cache");
146: if (!listener.visited.contains("Customer.contacts#1"))
147: throw new RuntimeException(
148: "Customer.contacts#1 was not in cache");
149: } finally {
150: System.out.println("CHECK CACHE - END");
151: }
152:
153: }
154:
155: @PreDestroy
156: @Remove
157: public void cleanup() {
158: try {
159: if (listener != null) {
160: getCache().removeTreeCacheListener(listener);
161: }
162: } catch (Exception e) {
163: e.printStackTrace();
164: }
165:
166: try {
167: if (manager != null) {
168: Customer c = findByCustomerId(1);
169: if (c != null) {
170: Set contacts = c.getContacts();
171: for (Iterator it = contacts.iterator(); it
172: .hasNext();)
173: manager.remove(it.next());
174: c.setContacts(null);
175: manager.remove(c);
176: }
177: }
178: } catch (Exception e) {
179: e.printStackTrace();
180: }
181: }
182:
183: private TreeCache getCache() throws Exception {
184: MBeanServer server = MBeanServerLocator.locateJBoss();
185: TreeCacheMBean proxy = (TreeCacheMBean) MBeanProxyExt.create(
186: TreeCacheMBean.class, new ObjectName(cacheObjectName),
187: server);
188: TreeCache cache = proxy.getInstance();
189:
190: return cache;
191: }
192:
193: class MyListener extends AbstractTreeCacheListener {
194: HashSet visited = new HashSet();
195:
196: public void clear() {
197: visited.clear();
198: }
199:
200: @Override
201: public void nodeVisited(Fqn fqn) {
202: System.out.println("MyListener - Visiting node "
203: + fqn.toString());
204: String name = fqn.toString();
205: String token = ".clusteredentity.";
206: int index = name.indexOf(token);
207: if (index > -1) {
208: index += token.length();
209: name = name.substring(index);
210: System.out.println("MyListener - recording visit to "
211: + name);
212: visited.add(name);
213: }
214: }
215: }
216: }
|