1 25 26 package org.objectweb.jonas.stests.manac; 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 ManacEC2 implements EntityBean { 44 45 static protected Logger logger = null; 46 protected static Logger history = null; 47 EntityContext ejbContext; 48 49 public abstract String getName(); 53 public abstract void setName(String n); 54 public abstract int getNum(); 55 public abstract void setNum(int n); 56 public abstract int getBalance(); 57 public abstract void setBalance(int b); 58 59 63 73 public void setEntityContext(EntityContext ctx) { 74 if (logger == null) { 75 logger = Log.getLogger(Log.JONAS_TESTS_PREFIX); 76 } 77 if (history == null) { 78 history = Log.getLogger("org.objectweb.jonas_tests.history"); 79 } 80 logger.log(BasicLevel.DEBUG, getName()); 81 ejbContext = ctx; 82 } 83 84 95 public void unsetEntityContext() { 96 logger.log(BasicLevel.DEBUG, getName()); 97 ejbContext = null; 98 } 99 100 113 public void ejbRemove() throws RemoveException { 114 logger.log(BasicLevel.DEBUG, getName()); 115 } 116 117 125 public void ejbLoad() { 126 String name = getName(); 127 int balance = getBalance(); 128 history.log(BasicLevel.INFO, name + "\tLOAD= " + balance); 129 logger.log(BasicLevel.DEBUG, name + " balance=" + balance); 130 if (balance < 0) { 131 logger.log(BasicLevel.WARN, name+" : Bad balance loaded"); 132 throw new EJBException ("ejbLoad: Balance "+name+" was negative ="+balance); 133 } 134 } 135 136 144 public void ejbStore() { 145 String name = getName(); 146 int balance = getBalance(); 147 history.log(BasicLevel.INFO, name + "\tSTORE= " + balance); 148 logger.log(BasicLevel.DEBUG, name + " balance=" + balance); 149 if (balance < 0) { 150 logger.log(BasicLevel.WARN, name+" : Bad balance stored"); 151 throw new EJBException ("ejbStore: Balance "+name+" was negative ="+balance); 152 } 153 } 154 155 160 public void ejbPostCreate(int num, int ib) throws CreateException { 161 logger.log(BasicLevel.DEBUG, getName()); 162 } 163 164 170 public java.lang.String ejbCreate(int num, int ib) throws CreateException , DuplicateKeyException { 171 172 setNum(num); 174 setName("a_"+(new Integer (num)).toString()); 175 setBalance(ib); 176 177 logger.log(BasicLevel.DEBUG, getName()); 178 179 return null; 181 } 182 183 187 public void ejbPassivate() { 188 setBalance(-80000); 191 logger.log(BasicLevel.DEBUG, getName()); 192 } 193 194 199 public void ejbActivate() { 200 logger.log(BasicLevel.DEBUG, getName()+" balance="+getBalance()); 201 } 202 203 207 210 public void credit(int v) { 211 String name = getName(); 212 if (getBalance() < 0) { 213 if (ejbContext.getRollbackOnly() == true) { 214 logger.log(BasicLevel.WARN, name+" : tx already rollbackonly"); 215 setBalance(-99000); 216 return; 217 } 218 logger.log(BasicLevel.WARN, name+" : Bad balance to credit"); 219 throw new EJBException ("credit: Balance "+name+" was negative ="+getBalance()); 220 } 221 int oldval = getBalance(); 222 setBalance(oldval+v); 223 logger.log(BasicLevel.DEBUG, name+" old balance="+oldval +" new balance="+getBalance()); 224 history.log(BasicLevel.INFO, name + "\tOLD= " + oldval + "\tNEW= " + getBalance()); 225 } 226 227 230 public void debit(int v) { 231 String name = getName(); 232 if (getBalance() < 0) { 233 if (ejbContext.getRollbackOnly() == true) { 234 logger.log(BasicLevel.WARN, name+" : tx already rollbackonly"); 235 setBalance(-99000); 236 return; 237 } 238 logger.log(BasicLevel.WARN, name+" : Bad balance to debit"); 239 throw new EJBException ("debit: Balance "+name+" was negative ="+getBalance()); 240 } 241 int oldval = getBalance(); 242 setBalance(oldval-v); 243 if (getBalance() < 0) { 244 logger.log(BasicLevel.WARN, name+" : set rollback only."); 245 ejbContext.setRollbackOnly(); 246 setBalance(-90000); } 248 logger.log(BasicLevel.DEBUG, name+" old balance="+oldval+" new balance="+getBalance()); 249 history.log(BasicLevel.INFO, name + "\tOLD= " + oldval + "\tNEW= " + getBalance()); 250 } 251 252 } 253 | Popular Tags |