1 25 26 package org.objectweb.jonas.jtests.beans.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 int balance = getBalance(); 124 history.log(BasicLevel.DEBUG, name + "\tLOAD= " + balance); 125 if (balance < 0) { 126 history.log(BasicLevel.WARN, name + " : Bad balance loaded"); 127 throw new EJBException ("ejbLoad: Balance "+name+" was negative ="+balance); 128 } 129 } 130 131 139 public void ejbStore() { 140 String name = getName(); 141 int balance = getBalance(); 142 history.log(BasicLevel.DEBUG, name + "\tSTORE= " + balance); 143 if (balance < 0) { 144 history.log(BasicLevel.WARN, name + " : Bad balance stored"); 145 throw new EJBException ("ejbStore: Balance "+name+" was negative ="+balance); 146 } 147 } 148 149 154 public void ejbPostCreate(int num, int ib) throws CreateException { 155 history.log(BasicLevel.DEBUG, getName()); 156 } 157 158 164 public java.lang.String ejbCreate(int num, int ib) throws CreateException , DuplicateKeyException { 165 166 setNum(num); 168 setName("a_"+(new Integer (num)).toString()); 169 setBalance(ib); 170 171 history.log(BasicLevel.DEBUG, getName()); 172 173 return null; 175 } 176 177 181 public void ejbPassivate() { 182 history.log(BasicLevel.DEBUG, getName()); 187 } 188 189 194 public void ejbActivate() { 195 history.log(BasicLevel.DEBUG, getName() + " balance=" + getBalance()); 196 } 197 198 202 205 public void credit(int v) { 206 String name = getName(); 207 if (getBalance() < 0) { 208 if (ejbContext.getRollbackOnly() == true) { 209 history.log(BasicLevel.WARN, name + " : tx already rollbackonly"); 210 setBalance(-99000); 211 return; 212 } 213 history.log(BasicLevel.WARN, name + " : Bad balance to credit"); 214 throw new EJBException ("credit: Balance "+name+" was negative ="+getBalance()); 215 } 216 int oldval = getBalance(); 217 setBalance(oldval + v); 218 history.log(BasicLevel.DEBUG, name + "\told= " + oldval + "\tnew= " + getBalance()); 219 } 220 221 224 public void debit(int v) { 225 String name = getName(); 226 if (getBalance() < 0) { 227 if (ejbContext.getRollbackOnly() == true) { 228 history.log(BasicLevel.WARN, name + " : tx already rollbackonly"); 229 setBalance(-99000); 230 return; 231 } 232 history.log(BasicLevel.WARN, name + " : Bad balance to debit"); 233 throw new EJBException ("debit: Balance "+name+" was negative ="+getBalance()); 234 } 235 int oldval = getBalance(); 236 setBalance(oldval - v); 237 if (getBalance() < 0) { 238 history.log(BasicLevel.WARN, name + " : set rollback only."); 239 ejbContext.setRollbackOnly(); 240 setBalance(-90000); } 242 history.log(BasicLevel.DEBUG, name + "\tOLD= " + oldval + "\tNEW= " + getBalance()); 243 } 244 245 } 246 | Popular Tags |