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.factory;
023:
024: import org.jboss.annotation.IgnoreDependency;
025:
026: import javax.ejb.EJB;
027: import javax.ejb.Remote;
028: import javax.ejb.Stateless;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031: import javax.persistence.EntityManager;
032: import javax.persistence.EntityManagerFactory;
033: import javax.persistence.PersistenceContext;
034: import javax.persistence.PersistenceUnit;
035:
036: /**
037: * Comment
038: *
039: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
040: * @version $Revision: 57207 $
041: */
042: @Stateless
043: @Remote(Session2.class)
044: public class Session2Bean implements Session2 {
045: @PersistenceContext(unitName="manager2")
046: EntityManager manager2;
047: @PersistenceContext(unitName="../session1.jar#manager1")
048: EntityManager manager1;
049: @PersistenceUnit(unitName="manager2")
050: EntityManagerFactory factory2;
051: @PersistenceUnit(unitName="../session1.jar#manager1")
052: EntityManagerFactory factory1;
053: @IgnoreDependency
054: @EJB
055: Session1 session1;
056:
057: //@Resource SessionContext ctx;
058:
059: public Entity1 find1FromManager(int id) {
060: EntityManager m1 = null;
061: try {
062: InitialContext ctx = new InitialContext();
063: m1 = (EntityManager) ctx.lookup("java:/Manager1");
064: } catch (NamingException e) {
065: throw new RuntimeException(e);
066: }
067: if (m1.find(Entity1.class, id) == null)
068: throw new RuntimeException("failed to find");
069: return manager1.find(Entity1.class, id);
070: }
071:
072: public void find1FromFactory(int id) {
073: EntityManagerFactory f1 = null;
074: try {
075: InitialContext ctx = new InitialContext();
076: f1 = (EntityManagerFactory) ctx
077: .lookup("java:/Manager1Factory");
078: } catch (NamingException e) {
079: throw new RuntimeException(e);
080: }
081: EntityManager m1 = f1.createEntityManager();
082: try {
083: if (m1.find(Entity1.class, id) == null)
084: throw new RuntimeException("failed to find");
085: } finally {
086: m1.close();
087: }
088:
089: EntityManager m = factory1.createEntityManager();
090: Entity1 one = m.find(Entity1.class, id);
091: try {
092: if (one == null)
093: throw new RuntimeException("NOT FOUND");
094: } finally {
095: m.close();
096: }
097:
098: }
099:
100: public Entity2 find2FromManager(int id) {
101: return manager2.find(Entity2.class, id);
102: }
103:
104: public void find2FromFactory(int id) {
105: EntityManager m = factory2.createEntityManager();
106: Entity2 two = m.find(Entity2.class, id);
107: if (two == null)
108: throw new RuntimeException("NOT FOUND");
109: // leak m.close();
110: }
111:
112: public Util findUtil1FromManager(int id) {
113: return manager1.find(Util.class, id);
114: }
115:
116: public Util findUtil2FromManager(int id) {
117: return manager2.find(Util.class, id);
118: }
119: }
|