1 25 26 package org.objectweb.jonas.stests.bank; 27 28 import javax.ejb.CreateException ; 29 import javax.ejb.DuplicateKeyException ; 30 import javax.ejb.EJBException ; 31 import javax.ejb.EntityBean ; 32 import javax.ejb.EntityContext ; 33 import javax.ejb.RemoveException ; 34 35 import org.objectweb.jonas.common.Log; 36 import org.objectweb.util.monolog.api.Logger; 37 import org.objectweb.util.monolog.api.BasicLevel; 38 39 43 public abstract class AccountEC2 implements EntityBean { 44 45 protected static Logger history = null; 46 EntityContext ejbContext; 47 48 public abstract String getName(); 52 public abstract void setName(String n); 53 public abstract int getNum(); 54 public abstract void setNum(int n); 55 public abstract int getBalance(); 56 public abstract void setBalance(int b); 57 58 62 72 public void setEntityContext(EntityContext ctx) { 73 if (history == null) { 74 history = Log.getLogger("org.objectweb.jonas_tests.history"); 75 } 76 history.log(BasicLevel.DEBUG, getName()); 77 ejbContext = ctx; 78 } 79 80 91 public void unsetEntityContext() { 92 history.log(BasicLevel.DEBUG, getName()); 93 ejbContext = null; 94 } 95 96 109 public void ejbRemove() throws RemoveException { 110 history.log(BasicLevel.DEBUG, getName()); 111 } 112 113 121 public void ejbLoad() { 122 String name = getName(); 123 history.log(BasicLevel.DEBUG, name + "\tLOAD= "); 124 int balance = getBalance(); 125 history.log(BasicLevel.DEBUG, name + "\tLOAD= " + balance); 126 if (balance < 0) { 127 history.log(BasicLevel.WARN, name + " : Bad balance loaded"); 128 throw new EJBException ("ejbLoad: Balance "+name+" was negative ="+balance); 129 } 130 } 131 132 140 public void ejbStore() { 141 String name = getName(); 142 history.log(BasicLevel.DEBUG, name + "\tSTORE= "); 143 int balance = getBalance(); 144 history.log(BasicLevel.DEBUG, name + "\tSTORE= " + balance); 145 if (balance < 0) { 146 history.log(BasicLevel.WARN, name + " : Bad balance stored"); 147 throw new EJBException ("ejbStore: Balance "+name+" was negative ="+balance); 148 } 149 } 150 151 156 public void ejbPostCreate(int num, int ib) throws CreateException { 157 history.log(BasicLevel.DEBUG, getName()); 158 } 159 160 166 public java.lang.String ejbCreate(int num, int ib) throws CreateException , DuplicateKeyException { 167 168 setNum(num); 170 setName("a_"+(new Integer (num)).toString()); 171 setBalance(ib); 172 173 history.log(BasicLevel.DEBUG, getName()); 174 175 return null; 177 } 178 179 183 public void ejbPassivate() { 184 setBalance(-80000); 187 history.log(BasicLevel.DEBUG, getName()); 188 } 189 190 195 public void ejbActivate() { 196 history.log(BasicLevel.DEBUG, getName() + " balance=" + getBalance()); 197 } 198 199 203 206 public void credit(int v) { 207 String name = getName(); 208 if (getBalance() < 0) { 209 if (ejbContext.getRollbackOnly() == true) { 210 history.log(BasicLevel.WARN, name + " : tx already rollbackonly"); 211 setBalance(-99000); 212 return; 213 } 214 history.log(BasicLevel.WARN, name + " : Bad balance to credit"); 215 throw new EJBException ("credit: Balance "+name+" was negative ="+getBalance()); 216 } 217 int oldval = getBalance(); 218 setBalance(oldval + v); 219 history.log(BasicLevel.DEBUG, name + "\told= " + oldval + "\tnew= " + getBalance()); 220 } 221 222 225 public void debit(int v) { 226 String name = getName(); 227 if (getBalance() < 0) { 228 if (ejbContext.getRollbackOnly() == true) { 229 history.log(BasicLevel.WARN, name + " : tx already rollbackonly"); 230 setBalance(-99000); 231 return; 232 } 233 history.log(BasicLevel.WARN, name + " : Bad balance to debit"); 234 throw new EJBException ("debit: Balance "+name+" was negative ="+getBalance()); 235 } 236 int oldval = getBalance(); 237 setBalance(oldval - v); 238 if (getBalance() < 0) { 239 history.log(BasicLevel.WARN, name + " : set rollback only."); 240 ejbContext.setRollbackOnly(); 241 setBalance(-90000); } 243 history.log(BasicLevel.DEBUG, name + "\tOLD= " + oldval + "\tNEW= " + getBalance()); 244 } 245 246 } 247 | Popular Tags |