KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > bank > BankBean


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.bank;
23
24 import java.io.Serializable JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26 import java.sql.Connection JavaDoc;
27 import javax.annotation.Resource;
28 import javax.ejb.EJBException JavaDoc;
29 import javax.ejb.Init JavaDoc;
30 import javax.ejb.Remove JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.sql.DataSource JavaDoc;
34 import javax.transaction.UserTransaction JavaDoc;
35
36 import org.jboss.logging.Logger;
37 import org.jboss.ejb3.Container;
38
39 /**
40  * @see <related>
41  * @author $Author: bill.burke@jboss.com $
42  * @version $Revision: 58478 $
43  */

44 public class BankBean implements Serializable JavaDoc, javax.ejb.SessionSynchronization JavaDoc
45 {
46    private static final Logger log = Logger.getLogger(BankBean.class);
47
48    transient public DataSource JavaDoc customerDb;
49
50    static final String JavaDoc ID = Container.ENC_CTX_NAME + "/env/org.jboss.ejb3.test.bank/id";
51
52    @Resource String JavaDoc id;
53
54    String JavaDoc customerId = "defaultId";
55
56    static long nextAccountId = System.currentTimeMillis();
57
58    static long nextCustomerId = System.currentTimeMillis();
59
60    String JavaDoc initialized = "";
61
62    private String JavaDoc activated = "";
63    
64    private String JavaDoc transactionState = "failed";
65    private String JavaDoc rollbackState;
66    private boolean beforeCalled = false;
67
68    public String JavaDoc getId()
69    {
70       return id;
71    }
72
73    public String JavaDoc getEnvEntryId() throws RemoteException JavaDoc, NamingException JavaDoc
74    {
75       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
76       String JavaDoc value = (String JavaDoc)jndiContext.lookup(ID);
77       return value;
78    }
79
80    public String JavaDoc createAccountId(Customer customer) throws RemoteException JavaDoc
81    {
82       return getId() + "." + customer.getName() + "." + (nextAccountId++);
83    }
84
85    public String JavaDoc createCustomerId()
86    {
87       return getId() + "." + (nextCustomerId++);
88    }
89
90    public void storeCustomerId(String JavaDoc customerId)
91    {
92       this.customerId = customerId;
93    }
94
95    public String JavaDoc retrieveCustomerId()
96    {
97       return customerId;
98    }
99
100    public String JavaDoc interceptCustomerId(String JavaDoc customerId)
101    {
102       return customerId;
103    }
104
105    public void testResource() throws Exception JavaDoc
106    {
107       if (customerDb == null) throw new Exception JavaDoc("customerDb resource not set");
108       Connection JavaDoc connection = customerDb.getConnection();
109       connection.close();
110    }
111
112    public void remove()
113    {
114    }
115
116    @Init JavaDoc
117    public void annotatedInit()
118    {
119       initialized += "YES";
120    }
121
122    public void init()
123    {
124       initialized += "YES";
125    }
126
127    public String JavaDoc isInitialized()
128    {
129       return initialized;
130    }
131
132    public String JavaDoc isActivated()
133    {
134       return activated;
135    }
136    
137    public String JavaDoc getTransactionState()
138    {
139       return transactionState;
140    }
141    
142    public void testTransactionTimeout()
143    {
144       try
145       {
146          Thread.sleep(2000);
147          transactionState = "ok";
148       }
149       catch (Exception JavaDoc e)
150       {
151          e.printStackTrace();
152       }
153    }
154    
155    public void afterBegin() throws EJBException JavaDoc, RemoteException JavaDoc
156    {
157       rollbackState = transactionState;
158    }
159
160    public void beforeCompletion() throws EJBException JavaDoc, RemoteException JavaDoc
161    {
162       beforeCalled = true;
163    }
164
165    public void afterCompletion(boolean committed) throws EJBException JavaDoc, RemoteException JavaDoc
166    {
167       if (!committed)
168          transactionState = rollbackState;
169    }
170 }
171
172 /*
173  * $Id: BankBean.java 58478 2006-11-17 00:27:34Z bill.burke@jboss.com $ Currently locked
174  * by:$Locker$ Revision: $Log$
175  * by:$Locker: $ Revision: Revision 1.17 2006/03/29 19:19:29 bdecoste
176  * by:$Locker: $ Revision: removed logging
177  * by:$Locker: $ Revision:
178  * by:$Locker$ Revision: Revision 1.16 2006/03/29 02:03:35 bdecoste
179  * by:$Locker$ Revision: injection for all bean types
180  * by:$Locker$ Revision:
181  * by:$Locker$ Revision: Revision 1.15 2005/10/30 00:06:46 starksm
182  * by:$Locker$ Revision: Update the jboss LGPL headers
183  * by:$Locker$ Revision:
184  * by:$Locker$ Revision: Revision 1.14 2005/10/16 16:45:22 bdecoste
185  * by:$Locker$ Revision: added SessionSynchronization
186  * by:$Locker$ Revision:
187  * by:$Locker$ Revision: Revision 1.13 2005/10/13 19:14:42 bdecoste
188  * by:$Locker$ Revision: added transaction timeouts via annotation or jboss.xml
189  * by:$Locker$ Revision:
190  * by:$Locker$ Revision: Revision 1.12 2005/09/24 02:28:31 bill
191  * by:$Locker$ Revision: injection compliance for ejb-refs and resource refs and env-entry
192  * by:$Locker$ Revision:
193  * by:$Locker$ Revision: Revision 1.11 2005/09/14 02:24:04 bill
194  * by:$Locker$ Revision: stateful bean should be serializable
195  * by:$Locker$ Revision:
196  * by:$Locker$ Revision: Revision 1.10 2005/08/24 20:30:43 bdecoste
197  * by:$Locker$ Revision: support for <env-entry>
198  * by:$Locker$ Revision:
199  * by:$Locker$ Revision: Revision 1.9 2005/06/02 23:25:14 bdecoste
200  * by:$Locker$ Revision: ejb3 jboss.xml support
201  * by:$Locker$ Revision:
202  * by:$Locker$ Revision: Revision 1.8 2005/05/26 02:24:09 bdecoste
203  * by:$Locker$ Revision: support for @Init
204  * by:$Locker$ Revision:
205  * by:$Locker$ Revision: Revision 1.7 2005/05/25 18:57:19 bdecoste
206  * by:$Locker$ Revision: added support for @Remove and @Exclude
207  * by:$Locker$ Revision:
208  * by:$Locker$ Revision: Revision 1.6 2005/05/17 22:37:42 bdecoste
209  * by:$Locker$ Revision: remove ejb2.1 rules
210  * by:$Locker$ Revision:
211  * by:$Locker$ Revision: Revision 1.5 2005/05/14 20:54:02 bdecoste
212  * by:$Locker$ Revision: added ejb3 dd support for Resource
213  * by:$Locker$ Revision:
214  * by:$Locker$ Revision: Revision 1.4 2005/05/12 20:31:46 bdecoste
215  * by:$Locker$ Revision: added ejb3 dd support for injection, callbacks, EJB
216  * by:$Locker$ Revision:
217  * by:$Locker$ Revision: Revision 1.3 2005/05/12 01:46:06 bdecoste
218  * by:$Locker$ Revision: added ejb3 dd support for RunAs and Inject
219  * by:$Locker$ Revision:
220  * by:$Locker$ Revision: Revision 1.2 2005/05/03 23:51:01 bdecoste
221  * by:$Locker$ Revision: fixed formatting
222  * by:$Locker$ Revision: Revision 1.1 2005/05/03
223  * 20:35:11 bdecoste test for ejb3 deployment descriptors Revision 1.5
224  * 2003/08/27 04:32:49 patriot1burke 4.0 rollback to 3.2 Revision 1.3 2002/02/15
225  * 06:15:50 user57 o replaced most System.out usage with Log4j. should really
226  * introduce some base classes to make this mess more maintainable... Revision
227  * 1.2 2001/01/07 23:14:34 peter Trying to get JAAS to work within test suite.
228  * Revision 1.1.1.1 2000/06/21 15:52:37 oberg Initial import of jBoss test. This
229  * module contains CTS tests, some simple examples, and small bean suites.
230  */

231
Popular Tags