KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > dtm > ejb > FrontEndBean


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.dtm.ejb;
23
24 import java.rmi.RemoteException JavaDoc;
25
26 import javax.ejb.EJBException JavaDoc;
27 import javax.ejb.FinderException JavaDoc;
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.rmi.PortableRemoteObject JavaDoc;
31
32 import org.jboss.logging.Logger;
33 import org.jboss.test.dtm.interfaces.Account;
34 import org.jboss.test.dtm.interfaces.AccountHome;
35 import org.jboss.test.dtm.interfaces.AccountEntityHome;
36 import org.jboss.test.util.ejb.SessionSupport;
37
38 /**
39  * Implementation class for the FrontEnd stateful session bean.
40  *
41  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
42  * @version $Revision: 58115 $
43  */

44 public class FrontEndBean
45    extends SessionSupport
46 {
47    private static transient Logger log =
48          Logger.getLogger(FrontEndBean.class);
49    
50    private String JavaDoc testName;
51    private Account account1, account2, account3;
52
53    public void ejbCreate(String JavaDoc name)
54    {
55       ejbCreate(name, false);
56    }
57
58    public void ejbCreate(String JavaDoc name, boolean reuseAccounts)
59    {
60       this.testName = name;
61       log = Logger.getLogger(FrontEndBean.class.getName() + "#" + testName);
62       log.debug("ejbCreate(" + name + "), ctx=" + sessionCtx);
63       if (reuseAccounts)
64          findAccounts();
65       else
66          initAccounts();
67
68    }
69
70    public void setBalances(int value1, int value2, int value3)
71    {
72       try
73       {
74          account1.setBalance(value1);
75          account2.setBalance(value2);
76          account3.setBalance(value3);
77       }
78       catch (RemoteException JavaDoc e)
79       {
80          throw new EJBException JavaDoc(e);
81       }
82    }
83    
84    public int[] getBalances()
85    {
86       try
87       {
88          int[] balances = new int[3];
89          balances[0] = account1.getBalance();
90          balances[1] = account2.getBalance();
91          balances[2] = account3.getBalance();
92          return balances;
93       }
94       catch (RemoteException JavaDoc e)
95       {
96          throw new EJBException JavaDoc(e);
97       }
98    }
99    
100    public void addToBalances(int value)
101    {
102       try
103       {
104          account1.addToBalance(value);
105          account2.addToBalance(value);
106          account3.addToBalance(value);
107       }
108       catch (RemoteException JavaDoc e)
109       {
110          throw new EJBException JavaDoc(e);
111       }
112    }
113
114    public void setRollbackOnly()
115    {
116       sessionCtx.setRollbackOnly();
117    }
118    
119    public void tellFirstAccountToSetRollbackOnly()
120    {
121       try
122       {
123          account1.setRollbackOnly();
124       }
125       catch (RemoteException JavaDoc e)
126       {
127          throw new EJBException JavaDoc(e);
128       }
129    }
130    
131    public void tellSecondAccountToSetRollbackOnly()
132    {
133       try
134       {
135          account2.setRollbackOnly();
136       }
137       catch (RemoteException JavaDoc e)
138       {
139          throw new EJBException JavaDoc(e);
140       }
141    }
142    
143    public void tellThirdAccountToSetRollbackOnly()
144    {
145       try
146       {
147          account3.setRollbackOnly();
148       }
149       catch (RemoteException JavaDoc e)
150       {
151          throw new EJBException JavaDoc(e);
152       }
153    }
154    
155    private void initAccounts()
156    {
157       try
158       {
159          Object JavaDoc objref;
160          AccountHome home;
161          Context JavaDoc iniCtx = new InitialContext JavaDoc();
162          
163          objref = iniCtx.lookup("java:comp/env/ejb/Account1");
164          home = (AccountHome)
165             PortableRemoteObject.narrow(objref, AccountHome.class);
166          account1 = home.create(testName + "#account1", 0);
167          
168          objref = iniCtx.lookup("java:comp/env/ejb/Account2");
169          home = (AccountHome)
170             PortableRemoteObject.narrow(objref, AccountHome.class);
171          account2 = home.create(testName + "#account2", 0);
172          
173          objref = iniCtx.lookup("java:comp/env/ejb/Account3");
174          home = (AccountHome)
175             PortableRemoteObject.narrow(objref, AccountHome.class);
176          account3 = home.create(testName + "#account3", 0);
177
178       }
179       catch (Exception JavaDoc e)
180       {
181          throw new EJBException JavaDoc(e);
182       }
183    }
184
185    private void findAccounts()
186    {
187       try
188       {
189          Object JavaDoc objref;
190          AccountEntityHome home;
191          Context JavaDoc iniCtx = new InitialContext JavaDoc();
192          
193          objref = iniCtx.lookup("java:comp/env/ejb/Account1");
194          home = (AccountEntityHome)
195             PortableRemoteObject.narrow(objref, AccountEntityHome.class);
196          try
197          {
198             account1 = home.findByPrimaryKey(testName + "#account1");
199          }
200          catch (FinderException JavaDoc e)
201          {
202             account1 = home.create(testName + "#account1", 0);
203          }
204          
205          objref = iniCtx.lookup("java:comp/env/ejb/Account2");
206          home = (AccountEntityHome)
207             PortableRemoteObject.narrow(objref, AccountEntityHome.class);
208          try
209          {
210             account2 = home.findByPrimaryKey(testName + "#account2");
211          }
212          catch (FinderException JavaDoc e)
213          {
214             account2 = home.create(testName + "#account2", 0);
215          }
216          
217          
218          objref = iniCtx.lookup("java:comp/env/ejb/Account3");
219          home = (AccountEntityHome)
220             PortableRemoteObject.narrow(objref, AccountEntityHome.class);
221          try
222          {
223             account3 = home.findByPrimaryKey(testName + "#account3");
224          }
225          catch (FinderException JavaDoc e)
226          {
227             account3 = home.create(testName + "#account3", 0);
228          }
229       }
230       catch (Exception JavaDoc e)
231       {
232          throw new EJBException JavaDoc(e);
233       }
234    }
235
236 }
237
Popular Tags