KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clusterDemo > BankSessionSLR


1 // BankSessionSLR.java
2
// Stateless Session bean
3

4 package org.objectweb.clusterDemo;
5
6 import java.rmi.RemoteException JavaDoc;
7 import java.sql.Connection JavaDoc;
8 import java.sql.SQLException JavaDoc;
9 import java.sql.Statement JavaDoc;
10 import javax.ejb.CreateException JavaDoc;
11 import javax.ejb.EJBException JavaDoc;
12 import javax.ejb.RemoveException JavaDoc;
13 import javax.ejb.EJBObject JavaDoc;
14 import javax.ejb.SessionBean JavaDoc;
15 import javax.ejb.SessionContext JavaDoc;
16 import javax.naming.Context JavaDoc;
17 import javax.naming.InitialContext JavaDoc;
18 import javax.naming.NamingException JavaDoc;
19 import javax.sql.DataSource JavaDoc;
20
21 import java.util.Vector JavaDoc;
22 import java.util.Calendar JavaDoc;
23 import java.util.GregorianCalendar JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.objectweb.jonas.common.Log;
27 import org.objectweb.util.monolog.api.Logger;
28 import org.objectweb.util.monolog.api.BasicLevel;
29
30
31 /**
32  *
33  */

34 public class BankSessionSLR implements SessionBean JavaDoc {
35
36     static private Logger logger = null;
37     SessionContext JavaDoc ejbContext;
38     
39     InitialContext JavaDoc ic = null;
40     private DataSource JavaDoc dataSource = null;
41     Connection JavaDoc conn = null;
42     Statement JavaDoc stmt;
43
44     // ------------------------------------------------------------------
45
// SessionBean implementation
46
// ------------------------------------------------------------------
47

48     /**
49      * Set the associated session context. The container calls this method
50      * after the instance creation.
51      * The enterprise Bean instance should store the reference to the context
52      * object in an instance variable.
53      * This method is called with no transaction context.
54      *
55      * @param sessionContext A SessionContext interface for the instance.
56      * @throws EJBException Thrown by the method to indicate a failure caused by
57      * a system-level error.
58      */

59     public void setSessionContext(SessionContext JavaDoc ctx) {
60         if (logger == null) {
61         logger = Log.getLogger("org.objectweb.jonas_tests");
62     }
63     logger.log(BasicLevel.DEBUG, "");
64     ejbContext = ctx;
65     }
66     
67     /**
68      * A container invokes this method before it ends the life of the session object.
69      * This happens as a result of a client's invoking a remove operation, or when a
70      * container decides to terminate the session object after a timeout.
71      * This method is called with no transaction context.
72      *
73      * @throws EJBException Thrown by the method to indicate a failure caused by
74      * a system-level error.
75      */

76     public void ejbRemove() {
77     logger.log(BasicLevel.DEBUG, "");
78     }
79     
80     /**
81      * The Session bean must define 1 or more ejbCreate methods.
82      *
83      * @throws CreateException Failure to create a session EJB object.
84      */

85     public void ejbCreate() throws CreateException JavaDoc {
86     logger.log(BasicLevel.DEBUG, "");
87     try {
88             ic = new InitialContext JavaDoc();
89         } catch (Exception JavaDoc e) {
90             e.printStackTrace();
91         }
92     }
93
94
95     /**
96      * A container invokes this method on an instance before the instance
97      * becomes disassociated with a specific EJB object.
98      */

99     public void ejbPassivate() {
100     logger.log(BasicLevel.DEBUG, "");
101     }
102
103     /**
104      * A container invokes this method when the instance is taken out of
105      * the pool of available instances to become associated with a specific
106      * EJB object.
107      */

108     public void ejbActivate() {
109     logger.log(BasicLevel.DEBUG, "");
110     }
111     
112     // ------------------------------------------------------------------
113
// BankSession implementation
114
// ------------------------------------------------------------------
115

116     /**
117      * credit method
118      */

119     public void credit(Integer JavaDoc accountId, String JavaDoc clientName, int value, String JavaDoc reason) {
120     try {
121         logger.log(BasicLevel.DEBUG, "");
122         BankAccountLocalHome accountHome = (BankAccountLocalHome)ic.lookup("java:comp/env/ejb/account");
123         BankAccountLocal account = (BankAccountLocal)accountHome.findByPrimaryKey(accountId);
124         account.putMoney(value);
125         BankOperationLocalHome operationHome = (BankOperationLocalHome)ic.lookup("java:comp/env/ejb/operation");
126         operationHome.create(value, reason+ " in bank " + getBank(), getDate());
127         System.out.println(">>>>>> account " + clientName + " credited");
128     } catch (Exception JavaDoc e) {
129         e.printStackTrace();
130     }
131
132     }
133
134     /**
135      * debit method
136      */

137     public void debit(Integer JavaDoc accountId, String JavaDoc clientName, int value, String JavaDoc reason) {
138     try {
139         logger.log(BasicLevel.DEBUG, "");
140         BankAccountLocalHome accountHome = (BankAccountLocalHome)ic.lookup("java:comp/env/ejb/account");
141         BankAccountLocal account = (BankAccountLocal)accountHome.findByPrimaryKey(accountId);
142         account.getMoney(value);
143         BankOperationLocalHome operationHome = (BankOperationLocalHome)ic.lookup("java:comp/env/ejb/operation");
144         operationHome.create(value, reason + " in bank " + getBank(), getDate());
145         System.out.println(">>>>>> account " + clientName + " debited");
146     } catch (Exception JavaDoc e) {
147         e.printStackTrace();
148     }
149     }
150
151     /**
152      * create new account
153      */

154     public Integer JavaDoc createAccount(String JavaDoc clientName, int value) {
155     Integer JavaDoc accountId = null;
156     try {
157         logger.log(BasicLevel.DEBUG, "");
158         BankAccountLocalHome accountHome = (BankAccountLocalHome)ic.lookup("java:comp/env/ejb/account");
159         BankAccountLocal account = accountHome.create(value,clientName);
160         System.out.println(">>>>>> account " + clientName + " created");
161     } catch (Exception JavaDoc e) {
162         e.printStackTrace();
163     }
164     return accountId;
165     }
166
167
168     /**
169      * get Date method for primary key generation
170      */

171     public String JavaDoc getDate() {
172     Calendar JavaDoc c = new GregorianCalendar JavaDoc();
173     String JavaDoc result = c.get(Calendar.YEAR) + "/" +
174         c.get(Calendar.MONTH) + "/" +
175         c.get(Calendar.DAY_OF_MONTH) + " ";
176     int hour = c.get(Calendar.HOUR_OF_DAY);
177     if (hour < 10) {
178         result += "0"+hour+"h";
179     } else {
180         result += ""+hour+"h";
181     }
182     int minute = c.get(Calendar.MINUTE);
183     if (minute < 10) {
184         
185         result += "0"+minute+"m";
186     } else {
187         result += ""+minute+"m";
188     }
189
190     int second = c.get(Calendar.SECOND);
191     if (second < 10) {
192         result += "0"+second+"s";
193     } else {
194         result += ""+second+"s";
195     }
196     return result + " " + getBank();
197     }
198
199     /**
200      * get Bank value from env value
201      */

202     public String JavaDoc getBank() {
203     try {
204         return (String JavaDoc)ic.lookup("java:comp/env/bankname");
205     } catch (Exception JavaDoc e) {
206         e.printStackTrace();
207         return null;
208     }
209     }
210
211 }
212
Popular Tags