KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > classloader > scoping > transaction > ejb > TestSessionBean


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.test.classloader.scoping.transaction.ejb;
23
24 import java.sql.Connection JavaDoc;
25
26 import javax.ejb.CreateException JavaDoc;
27 import javax.ejb.EJBException JavaDoc;
28 import javax.ejb.SessionBean JavaDoc;
29 import javax.ejb.SessionContext JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.sql.DataSource JavaDoc;
32 import javax.transaction.Transaction JavaDoc;
33 import javax.transaction.TransactionManager JavaDoc;
34
35 import org.jboss.resource.adapter.jdbc.WrappedConnection;
36 import org.jboss.test.classloader.scoping.transaction.interfaces.TestSession;
37
38 public class TestSessionBean
39    implements SessionBean JavaDoc
40 {
41    SessionContext JavaDoc context;
42
43    public void runTest()
44    {
45       try
46       {
47          Transaction JavaDoc tx = getTransaction();
48          if (tx == null)
49             throw new RuntimeException JavaDoc("No transaction");
50          String JavaDoc id = tx.toString();
51
52          TestSession next = (TestSession) context.getEJBObject();
53          int hashCode = next.invokeNext(id);
54          if (hashCode != getConnectionHashCode())
55             throw new RuntimeException JavaDoc("Not using same connection - assumes track by connection");
56       }
57       catch (Exception JavaDoc e)
58       {
59          throw new EJBException JavaDoc(e);
60       }
61    }
62
63    public int invokeNext(String JavaDoc id)
64    {
65       try
66       {
67          Transaction JavaDoc tx = getTransaction();
68          if (tx == null)
69             throw new RuntimeException JavaDoc("No transaction");
70          if (tx.toString().equals(id) == false)
71             throw new RuntimeException JavaDoc("Expected " + id + " got " + tx.toString());
72
73          return getConnectionHashCode();
74       }
75       catch (Exception JavaDoc e)
76       {
77          throw new EJBException JavaDoc(e);
78       }
79    }
80
81    public void ejbCreate()
82       throws CreateException JavaDoc
83    {
84    }
85    
86    public void setSessionContext( SessionContext JavaDoc context )
87    {
88       this.context = context;
89    }
90    
91    public void ejbActivate()
92    {
93    }
94    
95    public void ejbPassivate()
96    {
97    }
98    
99    public void ejbRemove()
100    {
101    }
102
103    private Transaction JavaDoc getTransaction()
104       throws Exception JavaDoc
105    {
106       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
107       TransactionManager JavaDoc tm = (TransactionManager JavaDoc) ctx.lookup("java:/TransactionManager"); // FIXME
108
return tm.getTransaction();
109    }
110
111    private int getConnectionHashCode()
112       throws Exception JavaDoc
113    {
114       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
115       DataSource JavaDoc ds = (DataSource JavaDoc) ctx.lookup("java:/DefaultDS"); // FIXME
116
Connection JavaDoc c = ds.getConnection();
117       try
118       {
119          WrappedConnection wc = (WrappedConnection) c;
120          return System.identityHashCode(wc.getUnderlyingConnection());
121       }
122       finally
123       {
124          c.close();
125       }
126    }
127 }
128
Popular Tags