1 4 package org.objectweb.clusterDemo; 5 6 import java.rmi.RemoteException ; 7 import java.sql.Connection ; 8 import java.sql.SQLException ; 9 import java.sql.Statement ; 10 import javax.ejb.CreateException ; 11 import javax.ejb.EJBException ; 12 import javax.ejb.RemoveException ; 13 import javax.ejb.EJBObject ; 14 import javax.ejb.SessionBean ; 15 import javax.ejb.SessionContext ; 16 import javax.naming.Context ; 17 import javax.naming.InitialContext ; 18 import javax.naming.NamingException ; 19 import javax.sql.DataSource ; 20 21 import java.util.Vector ; 22 import java.util.Calendar ; 23 import java.util.GregorianCalendar ; 24 import java.util.Iterator ; 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 34 public class BankSessionSLR implements SessionBean { 35 36 static private Logger logger = null; 37 SessionContext ejbContext; 38 39 InitialContext ic = null; 40 private DataSource dataSource = null; 41 Connection conn = null; 42 Statement stmt; 43 44 48 59 public void setSessionContext(SessionContext 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 76 public void ejbRemove() { 77 logger.log(BasicLevel.DEBUG, ""); 78 } 79 80 85 public void ejbCreate() throws CreateException { 86 logger.log(BasicLevel.DEBUG, ""); 87 try { 88 ic = new InitialContext (); 89 } catch (Exception e) { 90 e.printStackTrace(); 91 } 92 } 93 94 95 99 public void ejbPassivate() { 100 logger.log(BasicLevel.DEBUG, ""); 101 } 102 103 108 public void ejbActivate() { 109 logger.log(BasicLevel.DEBUG, ""); 110 } 111 112 116 119 public void credit(Integer accountId, String clientName, int value, String 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 e) { 129 e.printStackTrace(); 130 } 131 132 } 133 134 137 public void debit(Integer accountId, String clientName, int value, String 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 e) { 147 e.printStackTrace(); 148 } 149 } 150 151 154 public Integer createAccount(String clientName, int value) { 155 Integer 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 e) { 162 e.printStackTrace(); 163 } 164 return accountId; 165 } 166 167 168 171 public String getDate() { 172 Calendar c = new GregorianCalendar (); 173 String 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 202 public String getBank() { 203 try { 204 return (String )ic.lookup("java:comp/env/bankname"); 205 } catch (Exception e) { 206 e.printStackTrace(); 207 return null; 208 } 209 } 210 211 } 212 | Popular Tags |