KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > testbean > bean > BMTStatelessBean


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.testbean.bean;
23
24 import java.rmi.*;
25 import javax.ejb.*;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.Context JavaDoc;
28 import org.jboss.test.testbean.interfaces.BMTStateful;
29 import javax.transaction.UserTransaction JavaDoc;
30 import javax.transaction.Status JavaDoc;
31
32
33 public class BMTStatelessBean implements SessionBean {
34    org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass());
35    
36   private SessionContext sessionContext;
37
38   public void ejbCreate() throws RemoteException, CreateException {
39   log.debug("BMTStatelessBean.ejbCreate() called");
40   }
41
42   public void ejbActivate() throws RemoteException {
43     log.debug("BMTStatelessBean.ejbActivate() called");
44   }
45
46   public void ejbPassivate() throws RemoteException {
47       log.debug("BMTStatelessBean.ejbPassivate() called");
48   }
49
50   public void ejbRemove() throws RemoteException {
51      log.debug("BMTStatelessBean.ejbRemove() called");
52   }
53
54   public void setSessionContext(SessionContext context) throws RemoteException {
55     sessionContext = context;
56   }
57
58     public String JavaDoc txExists() throws RemoteException {
59         String JavaDoc result = "";
60         try {
61             UserTransaction JavaDoc ut1 = sessionContext.getUserTransaction();
62             result += "Got UserTransaction via sessionContext.getUserTransaction()\n";
63         
64             UserTransaction JavaDoc ut2 = (UserTransaction JavaDoc)new InitialContext JavaDoc().lookup("java:comp/UserTransaction");
65             result += "Got UserTransaction via lookup(java:comp/UserTransaction)\n";
66             return result;
67         } catch (Exception JavaDoc e) {
68             throw new RemoteException(e.getMessage());
69         }
70     }
71         
72         
73     public String JavaDoc txCommit() throws RemoteException {
74         try {
75             UserTransaction JavaDoc tx = sessionContext.getUserTransaction();
76         
77             String JavaDoc result = "Got transaction : " + statusName(tx.getStatus()) + "\n";
78             tx.begin();
79             result += "tx.begin(): " + statusName(tx.getStatus()) + "\n";
80             tx.commit();
81             result += "tx.commit(): " + statusName(tx.getStatus()) + "\n";
82         
83             return result;
84             
85         } catch (Exception JavaDoc e) {
86             log.debug("failed", e);
87             throw new RemoteException(e.getMessage());
88         }
89         
90     }
91         
92     public String JavaDoc txRollback() throws RemoteException {
93         try {
94             UserTransaction JavaDoc tx = sessionContext.getUserTransaction();
95         
96             String JavaDoc result = "Got transaction : " + statusName(tx.getStatus()) + "\n";
97             tx.begin();
98             result += "tx.begin(): " + statusName(tx.getStatus()) + "\n";
99             tx.rollback();
100             result += "tx.rollback(): " + statusName(tx.getStatus()) + "\n";
101         
102             return result;
103             
104         } catch (Exception JavaDoc e) {
105             throw new RemoteException(e.getMessage());
106         }
107     }
108         
109       
110
111     // this should not be allowed by the container
112
public String JavaDoc txBegin() throws RemoteException {
113         try {
114             UserTransaction JavaDoc tx = sessionContext.getUserTransaction();
115         
116             tx.begin();
117             return "status: " + statusName(tx.getStatus());
118         } catch (Exception JavaDoc e) {
119             throw new RemoteException(e.getMessage());
120         }
121         
122     }
123
124     private String JavaDoc statusName(int s) {
125         switch (s) {
126             case Status.STATUS_ACTIVE: return "STATUS_ACTIVE";
127             case Status.STATUS_COMMITTED: return "STATUS_COMMITED";
128             case Status.STATUS_COMMITTING: return "STATUS_COMMITTING";
129             case Status.STATUS_MARKED_ROLLBACK: return "STATUS_MARKED_ROLLBACK";
130             case Status.STATUS_NO_TRANSACTION: return "STATUS_NO_TRANSACTION";
131             case Status.STATUS_PREPARED: return "STATUS_PREPARED";
132             case Status.STATUS_PREPARING: return "STATUS_PREPARING";
133             case Status.STATUS_ROLLEDBACK: return "STATUS_ROLLEDBACK";
134             case Status.STATUS_ROLLING_BACK: return "STATUS_ROLLING_BACK";
135             case Status.STATUS_UNKNOWN: return "STATUS_UNKNOWN";
136        }
137        return "REALLY_UNKNOWN";
138     }
139
140 }
141
Popular Tags