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.test.classloader.scoping.transaction.ejb;
023:
024: import java.sql.Connection;
025:
026: import javax.ejb.CreateException;
027: import javax.ejb.EJBException;
028: import javax.ejb.SessionBean;
029: import javax.ejb.SessionContext;
030: import javax.naming.InitialContext;
031: import javax.sql.DataSource;
032: import javax.transaction.Transaction;
033: import javax.transaction.TransactionManager;
034:
035: import org.jboss.resource.adapter.jdbc.WrappedConnection;
036: import org.jboss.test.classloader.scoping.transaction.interfaces.TestSession;
037:
038: public class TestSessionBean implements SessionBean {
039: SessionContext context;
040:
041: public void runTest() {
042: try {
043: Transaction tx = getTransaction();
044: if (tx == null)
045: throw new RuntimeException("No transaction");
046: String id = tx.toString();
047:
048: TestSession next = (TestSession) context.getEJBObject();
049: int hashCode = next.invokeNext(id);
050: if (hashCode != getConnectionHashCode())
051: throw new RuntimeException(
052: "Not using same connection - assumes track by connection");
053: } catch (Exception e) {
054: throw new EJBException(e);
055: }
056: }
057:
058: public int invokeNext(String id) {
059: try {
060: Transaction tx = getTransaction();
061: if (tx == null)
062: throw new RuntimeException("No transaction");
063: if (tx.toString().equals(id) == false)
064: throw new RuntimeException("Expected " + id + " got "
065: + tx.toString());
066:
067: return getConnectionHashCode();
068: } catch (Exception e) {
069: throw new EJBException(e);
070: }
071: }
072:
073: public void ejbCreate() throws CreateException {
074: }
075:
076: public void setSessionContext(SessionContext context) {
077: this .context = context;
078: }
079:
080: public void ejbActivate() {
081: }
082:
083: public void ejbPassivate() {
084: }
085:
086: public void ejbRemove() {
087: }
088:
089: private Transaction getTransaction() throws Exception {
090: InitialContext ctx = new InitialContext();
091: TransactionManager tm = (TransactionManager) ctx
092: .lookup("java:/TransactionManager"); // FIXME
093: return tm.getTransaction();
094: }
095:
096: private int getConnectionHashCode() throws Exception {
097: InitialContext ctx = new InitialContext();
098: DataSource ds = (DataSource) ctx.lookup("java:/DefaultDS"); // FIXME
099: Connection c = ds.getConnection();
100: try {
101: WrappedConnection wc = (WrappedConnection) c;
102: return System
103: .identityHashCode(wc.getUnderlyingConnection());
104: } finally {
105: c.close();
106: }
107: }
108: }
|