KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > factory > Session2Bean


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.factory;
23
24 import org.jboss.annotation.IgnoreDependency;
25
26 import javax.ejb.EJB JavaDoc;
27 import javax.ejb.Remote JavaDoc;
28 import javax.ejb.Stateless JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.persistence.EntityManager;
32 import javax.persistence.EntityManagerFactory;
33 import javax.persistence.PersistenceContext;
34 import javax.persistence.PersistenceUnit;
35
36 /**
37  * Comment
38  *
39  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
40  * @version $Revision: 45473 $
41  */

42 @Stateless JavaDoc
43 @Remote JavaDoc(Session2.class)
44 public class Session2Bean implements Session2
45 {
46    @PersistenceContext(unitName="manager2") EntityManager manager2;
47    @PersistenceContext(unitName="../session1.jar#manager1") EntityManager manager1;
48    @PersistenceUnit(unitName="manager2") EntityManagerFactory factory2;
49    @PersistenceUnit(unitName="../session1.jar#manager1") EntityManagerFactory factory1;
50    @IgnoreDependency @EJB JavaDoc Session1 session1;
51
52    //@Resource SessionContext ctx;
53

54    public Entity1 find1FromManager(int id)
55    {
56       EntityManager m1 = null;
57       try
58       {
59          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
60          m1 = (EntityManager)ctx.lookup("java:/Manager1");
61       }
62       catch (NamingException JavaDoc e)
63       {
64          throw new RuntimeException JavaDoc(e);
65       }
66       if (m1.find(Entity1.class, id) == null) throw new RuntimeException JavaDoc("failed to find");
67       return manager1.find(Entity1.class, id);
68    }
69    public void find1FromFactory(int id)
70    {
71       EntityManagerFactory f1 = null;
72       try
73       {
74          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
75          f1 = (EntityManagerFactory)ctx.lookup("java:/Manager1Factory");
76       }
77       catch (NamingException JavaDoc e)
78       {
79          throw new RuntimeException JavaDoc(e);
80       }
81       EntityManager m1 = f1.createEntityManager();
82       try
83       {
84          if (m1.find(Entity1.class, id) == null) throw new RuntimeException JavaDoc("failed to find");
85       }
86       finally
87       {
88          m1.close();
89       }
90
91       EntityManager m = factory1.createEntityManager();
92       Entity1 one = m.find(Entity1.class, id);
93       try
94       {
95          if (one == null) throw new RuntimeException JavaDoc("NOT FOUND");
96       }
97       finally
98       {
99          m.close();
100       }
101
102    }
103    public Entity2 find2FromManager(int id)
104    {
105       return manager2.find(Entity2.class, id);
106    }
107    public void find2FromFactory(int id)
108    {
109       EntityManager m = factory2.createEntityManager();
110       Entity2 two = m.find(Entity2.class, id);
111       if (two == null) throw new RuntimeException JavaDoc("NOT FOUND");
112       // leak m.close();
113
}
114
115    public Util findUtil1FromManager(int id)
116    {
117       return manager1.find(Util.class, id);
118    }
119
120    public Util findUtil2FromManager(int id)
121    {
122       return manager2.find(Util.class, id);
123    }
124 }
125
Popular Tags