KickJava   Java API By Example, From Geeks To Geeks.

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


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

42 public class WorkerBean
43    extends SessionSupport
44 {
45    private static transient Logger log =
46          Logger.getLogger(WorkerBean.class);
47    
48    private String JavaDoc testName;
49    private Account account1, account2;
50
51    public void ejbCreate(String JavaDoc name)
52    {
53       this.testName = name;
54       log = Logger.getLogger(WorkerBean.class.getName() + "#" + testName);
55       log.debug("ejbCreate(" + name + "), ctx=" + sessionCtx);
56       initAccounts();
57    }
58
59    public void setBalances(int value1, int value2)
60    {
61       try
62       {
63          account1.setBalance(value1);
64          account2.setBalance(value2);
65       }
66       catch (RemoteException JavaDoc e)
67       {
68          throw new EJBException JavaDoc(e);
69       }
70    }
71    
72    public int[] getBalances()
73    {
74       try
75       {
76          int[] balances = new int[2];
77          balances[0] = account1.getBalance();
78          balances[1] = account2.getBalance();
79          return balances;
80       }
81       catch (RemoteException JavaDoc e)
82       {
83          throw new EJBException JavaDoc(e);
84       }
85    }
86    
87    public void addToBalances(int value)
88    {
89       try
90       {
91          account1.addToBalance(value);
92          account2.addToBalance(value);
93       }
94       catch (RemoteException JavaDoc e)
95       {
96          throw new EJBException JavaDoc(e);
97       }
98    }
99
100    public void setRollbackOnly()
101    {
102       sessionCtx.setRollbackOnly();
103    }
104    
105    public void tellFirstAccountToSetRollbackOnly()
106    {
107       try
108       {
109          account1.setRollbackOnly();
110       }
111       catch (RemoteException JavaDoc e)
112       {
113          throw new EJBException JavaDoc(e);
114       }
115    }
116    
117    public void tellSecondAccountToSetRollbackOnly()
118    {
119       try
120       {
121          account2.setRollbackOnly();
122       }
123       catch (RemoteException JavaDoc e)
124       {
125          throw new EJBException JavaDoc(e);
126       }
127    }
128    
129    private void initAccounts()
130    {
131       try
132       {
133          Object JavaDoc objref;
134          AccountHome home;
135          Context JavaDoc iniCtx = new InitialContext JavaDoc();
136          
137          objref = iniCtx.lookup("java:comp/env/ejb/Account1");
138          home = (AccountHome)
139             PortableRemoteObject.narrow(objref, AccountHome.class);
140          account1 = home.create("account1", 0);
141          
142          objref = iniCtx.lookup("java:comp/env/ejb/Account2");
143          home = (AccountHome)
144             PortableRemoteObject.narrow(objref, AccountHome.class);
145          account2 = home.create("account2", 0);
146          
147       }
148       catch (Exception JavaDoc e)
149       {
150          throw new EJBException JavaDoc(e);
151       }
152    }
153
154 }
155
Popular Tags