001: // BankSessionSLR.java
002: // Stateless Session bean
003:
004: package org.objectweb.clusterDemo;
005:
006: import java.rmi.RemoteException;
007: import java.sql.Connection;
008: import java.sql.SQLException;
009: import java.sql.Statement;
010: import javax.ejb.CreateException;
011: import javax.ejb.EJBException;
012: import javax.ejb.RemoveException;
013: import javax.ejb.EJBObject;
014: import javax.ejb.SessionBean;
015: import javax.ejb.SessionContext;
016: import javax.naming.Context;
017: import javax.naming.InitialContext;
018: import javax.naming.NamingException;
019: import javax.sql.DataSource;
020:
021: import java.util.Vector;
022: import java.util.Calendar;
023: import java.util.GregorianCalendar;
024: import java.util.Iterator;
025:
026: import org.objectweb.jonas.common.Log;
027: import org.objectweb.util.monolog.api.Logger;
028: import org.objectweb.util.monolog.api.BasicLevel;
029:
030: /**
031: *
032: */
033: public class BankSessionSLR implements SessionBean {
034:
035: static private Logger logger = null;
036: SessionContext ejbContext;
037:
038: InitialContext ic = null;
039: private DataSource dataSource = null;
040: Connection conn = null;
041: Statement stmt;
042:
043: // ------------------------------------------------------------------
044: // SessionBean implementation
045: // ------------------------------------------------------------------
046:
047: /**
048: * Set the associated session context. The container calls this method
049: * after the instance creation.
050: * The enterprise Bean instance should store the reference to the context
051: * object in an instance variable.
052: * This method is called with no transaction context.
053: *
054: * @param sessionContext A SessionContext interface for the instance.
055: * @throws EJBException Thrown by the method to indicate a failure caused by
056: * a system-level error.
057: */
058: public void setSessionContext(SessionContext ctx) {
059: if (logger == null) {
060: logger = Log.getLogger("org.objectweb.jonas_tests");
061: }
062: logger.log(BasicLevel.DEBUG, "");
063: ejbContext = ctx;
064: }
065:
066: /**
067: * A container invokes this method before it ends the life of the session object.
068: * This happens as a result of a client's invoking a remove operation, or when a
069: * container decides to terminate the session object after a timeout.
070: * This method is called with no transaction context.
071: *
072: * @throws EJBException Thrown by the method to indicate a failure caused by
073: * a system-level error.
074: */
075: public void ejbRemove() {
076: logger.log(BasicLevel.DEBUG, "");
077: }
078:
079: /**
080: * The Session bean must define 1 or more ejbCreate methods.
081: *
082: * @throws CreateException Failure to create a session EJB object.
083: */
084: public void ejbCreate() throws CreateException {
085: logger.log(BasicLevel.DEBUG, "");
086: try {
087: ic = new InitialContext();
088: } catch (Exception e) {
089: e.printStackTrace();
090: }
091: }
092:
093: /**
094: * A container invokes this method on an instance before the instance
095: * becomes disassociated with a specific EJB object.
096: */
097: public void ejbPassivate() {
098: logger.log(BasicLevel.DEBUG, "");
099: }
100:
101: /**
102: * A container invokes this method when the instance is taken out of
103: * the pool of available instances to become associated with a specific
104: * EJB object.
105: */
106: public void ejbActivate() {
107: logger.log(BasicLevel.DEBUG, "");
108: }
109:
110: // ------------------------------------------------------------------
111: // BankSession implementation
112: // ------------------------------------------------------------------
113:
114: /**
115: * credit method
116: */
117: public void credit(Integer accountId, String clientName, int value,
118: String reason) {
119: try {
120: logger.log(BasicLevel.DEBUG, "");
121: BankAccountLocalHome accountHome = (BankAccountLocalHome) ic
122: .lookup("java:comp/env/ejb/account");
123: BankAccountLocal account = (BankAccountLocal) accountHome
124: .findByPrimaryKey(accountId);
125: account.putMoney(value);
126: BankOperationLocalHome operationHome = (BankOperationLocalHome) ic
127: .lookup("java:comp/env/ejb/operation");
128: operationHome.create(value, reason + " in bank "
129: + getBank(), getDate());
130: System.out.println(">>>>>> account " + clientName
131: + " credited");
132: } catch (Exception e) {
133: e.printStackTrace();
134: }
135:
136: }
137:
138: /**
139: * debit method
140: */
141: public void debit(Integer accountId, String clientName, int value,
142: String reason) {
143: try {
144: logger.log(BasicLevel.DEBUG, "");
145: BankAccountLocalHome accountHome = (BankAccountLocalHome) ic
146: .lookup("java:comp/env/ejb/account");
147: BankAccountLocal account = (BankAccountLocal) accountHome
148: .findByPrimaryKey(accountId);
149: account.getMoney(value);
150: BankOperationLocalHome operationHome = (BankOperationLocalHome) ic
151: .lookup("java:comp/env/ejb/operation");
152: operationHome.create(value, reason + " in bank "
153: + getBank(), getDate());
154: System.out.println(">>>>>> account " + clientName
155: + " debited");
156: } catch (Exception e) {
157: e.printStackTrace();
158: }
159: }
160:
161: /**
162: * create new account
163: */
164: public Integer createAccount(String clientName, int value) {
165: Integer accountId = null;
166: try {
167: logger.log(BasicLevel.DEBUG, "");
168: BankAccountLocalHome accountHome = (BankAccountLocalHome) ic
169: .lookup("java:comp/env/ejb/account");
170: BankAccountLocal account = accountHome.create(value,
171: clientName);
172: System.out.println(">>>>>> account " + clientName
173: + " created");
174: } catch (Exception e) {
175: e.printStackTrace();
176: }
177: return accountId;
178: }
179:
180: /**
181: * get Date method for primary key generation
182: */
183: public String getDate() {
184: Calendar c = new GregorianCalendar();
185: String result = c.get(Calendar.YEAR) + "/"
186: + c.get(Calendar.MONTH) + "/"
187: + c.get(Calendar.DAY_OF_MONTH) + " ";
188: int hour = c.get(Calendar.HOUR_OF_DAY);
189: if (hour < 10) {
190: result += "0" + hour + "h";
191: } else {
192: result += "" + hour + "h";
193: }
194: int minute = c.get(Calendar.MINUTE);
195: if (minute < 10) {
196:
197: result += "0" + minute + "m";
198: } else {
199: result += "" + minute + "m";
200: }
201:
202: int second = c.get(Calendar.SECOND);
203: if (second < 10) {
204: result += "0" + second + "s";
205: } else {
206: result += "" + second + "s";
207: }
208: return result + " " + getBank();
209: }
210:
211: /**
212: * get Bank value from env value
213: */
214: public String getBank() {
215: try {
216: return (String) ic.lookup("java:comp/env/bankname");
217: } catch (Exception e) {
218: e.printStackTrace();
219: return null;
220: }
221: }
222:
223: }
|