1 25 26 package org.objectweb.jonas.jtests.beans.transacted; 27 28 import java.rmi.RemoteException ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 32 import javax.ejb.CreateException ; 33 import javax.ejb.EJBException ; 34 import javax.ejb.EntityBean ; 35 import javax.ejb.EntityContext ; 36 import javax.ejb.RemoveException ; 37 import javax.ejb.TimedObject ; 38 import javax.ejb.Timer ; 39 import javax.ejb.TimerHandle ; 40 import javax.ejb.TimerService ; 41 42 import org.objectweb.util.monolog.api.BasicLevel; 43 44 48 public abstract class SimpleEC2 extends SimpleCommon implements EntityBean , TimedObject { 49 50 private final int TOCANCEL = 10000; 51 52 protected EntityContext entityContext; 53 54 public abstract String getAccno(); 58 public abstract void setAccno(String accno); 59 public abstract String getCustomer(); 60 public abstract void setCustomer(String customer); 61 public abstract long getBalance(); 62 public abstract void setBalance(long balance); 63 public abstract int getTimerIdent(); 64 public abstract void setTimerIdent(int id); 65 public abstract int getTimerCount(); 66 public abstract void setTimerCount(int cnt); 67 68 72 public String ejbCreate() throws CreateException { 73 74 logger.log(BasicLevel.DEBUG, ""); 75 76 setAccno("000"); 78 setCustomer("Initial"); 79 setBalance(0); 80 setTimerIdent(0); 81 setTimerCount(0); 82 return getAccno(); 84 } 85 86 89 public void ejbPostCreate() { 90 logger.log(BasicLevel.DEBUG, ""); 91 } 92 93 96 public String ejbCreate(int i) throws CreateException { 97 98 logger.log(BasicLevel.DEBUG, ""); 99 100 if (isAssociated()) { 101 throw new EJBException ("ejbCreate(int i): should not be in a transaction"); 102 } 103 Integer v = new Integer ((int)i); 104 setAccno(v.toString()); 105 setCustomer("by int"); 106 setBalance(1000 + i); 107 setTimerIdent(0); 108 setTimerCount(0); 109 110 return getAccno(); 112 } 113 114 117 public void ejbPostCreate(int i) { 118 logger.log(BasicLevel.DEBUG, ""); 119 if (isAssociated()) { 120 throw new EJBException ("ejbPostCreate(int i): should not be in a transaction"); 121 } 122 } 123 124 127 public String ejbCreateForRequired(long i) throws CreateException { 128 logger.log(BasicLevel.DEBUG, ""); 129 if (!isAssociated()) { 130 throw new EJBException ("ejbCreate(long i): should be in a transaction"); 131 } 132 Integer v = new Integer ((int)i); 133 setAccno(v.toString()); 134 setCustomer("Required"); 135 setBalance(1000 + i); 136 setTimerIdent(0); 137 setTimerCount(0); 138 139 return getAccno(); 141 } 142 143 146 public void ejbPostCreateForRequired(long i) { 147 logger.log(BasicLevel.DEBUG, ""); 148 if (!isAssociated()) { 149 throw new EJBException ("ejbPostCreateForRequired(long i): should be in a transaction"); 150 } 151 } 152 153 156 public String ejbCreateForNever(short i) throws CreateException { 157 logger.log(BasicLevel.DEBUG, ""); 158 159 if (isAssociated()) { 160 throw new EJBException ("ejbCreateForNever(short i): should not be in a transaction"); 161 } 162 Integer v = new Integer ((int)i); 163 setAccno(v.toString()); 164 setCustomer("by short"); 165 setBalance(1000 + i); 166 setTimerIdent(0); 167 setTimerCount(0); 168 169 return getAccno(); 171 } 172 173 176 public String ejbCreateForRequiresNew(String i) throws CreateException { 177 logger.log(BasicLevel.DEBUG, ""); 178 179 if (!isAssociated()) { 180 throw new EJBException ("ejbCreate(String i): should be in a transaction"); 181 } 182 setAccno(i); 183 setCustomer("by string"); 184 setBalance(100); 185 setTimerIdent(0); 186 setTimerCount(0); 187 188 return getAccno(); 190 } 191 192 195 public void ejbPostCreateForRequiresNew(String i) { 196 logger.log(BasicLevel.DEBUG, ""); 197 198 if (!isAssociated()) { 199 throw new EJBException ("ejbPostCreateForRequiresNew(String i): should be in a transaction"); 200 } 201 } 202 203 206 public String ejbCreateForMandatory(char i) throws CreateException { 207 logger.log(BasicLevel.DEBUG, ""); 208 209 if (!isAssociated()) { 210 throw new EJBException ("ejbCreateForMandatory(char i): should be in a transaction"); 211 } 212 Integer v = new Integer ((int)i); 213 setAccno(v.toString()); 214 setCustomer("by char"); 215 setBalance(200); 216 setTimerIdent(0); 217 setTimerCount(0); 218 219 return getAccno(); 221 } 222 223 226 public void ejbPostCreateForMandatory(char i) { 227 logger.log(BasicLevel.DEBUG, ""); 228 229 if (!isAssociated()) { 230 throw new EJBException ("ejbPostCreateForMandatory(char i): should be in a transaction"); 231 } 232 } 233 234 237 public String ejbCreateForSupports(boolean intx) throws CreateException { 238 logger.log(BasicLevel.DEBUG, ""); 239 240 if (intx) { 241 setAccno("TRUE"); 242 setBalance(101); 243 if (!isAssociated()) { 244 throw new EJBException ("ejbCreateForSupports(true): should be in a transaction"); 245 } 246 } else { 247 setAccno("FALSE"); 248 setBalance(102); 249 if (isAssociated()) { 250 throw new EJBException ("ejbCreate(false): should not be in a transaction"); 251 } 252 } 253 setCustomer("by boolean"); 254 setTimerIdent(0); 255 setTimerCount(0); 256 257 return getAccno(); 259 } 260 261 264 public void ejbPostCreateForSupports(boolean intx) { 265 logger.log(BasicLevel.DEBUG, ""); 266 267 if (intx) { 268 if (!isAssociated()) { 269 throw new EJBException ("ejbPostCreateForSupports: should be in a transaction"); 270 } 271 } else { 272 if (isAssociated()) { 273 throw new EJBException ("ejbPostCreateForSupports: should not be in a transaction"); 274 } 275 } 276 } 277 278 public String ejbCreateWithTimer(int i, long dur) throws RemoteException , CreateException { 279 logger.log(BasicLevel.DEBUG, ""); 280 setAccno("001"); 282 setCustomer("Timer"); 283 setBalance(0); 284 setTimerIdent(0); 285 setTimerCount(0); 286 return getAccno(); 288 } 289 290 public void ejbPostCreateWithTimer(int i, long dur) throws RemoteException , CreateException { 291 logger.log(BasicLevel.DEBUG, ""); 292 TimerService timerservice = entityContext.getTimerService(); 293 int ret = getTimerIdent() + 1; 294 Timer mt = timerservice.createTimer(dur, new Integer (ret)); 295 } 296 297 300 public void ejbPostCreateForNever(short i) { 301 logger.log(BasicLevel.DEBUG, ""); 302 303 if (isAssociated()) { 304 throw new EJBException ("ejbPostCreateForNever(short i): should not be in a transaction"); 305 } 306 } 307 308 309 312 public boolean ejbHomeOpwith_notsupported() { 313 logger.log(BasicLevel.DEBUG, ""); 314 return isAssociated(); 315 } 316 317 320 public boolean ejbHomeOpwith_supports() { 321 logger.log(BasicLevel.DEBUG, ""); 322 return isAssociated(); 323 } 324 325 328 public boolean ejbHomeOpwith_required() { 329 logger.log(BasicLevel.DEBUG, ""); 330 return isAssociated(); 331 } 332 333 336 public boolean ejbHomeOpwith_requires_new() { 337 logger.log(BasicLevel.DEBUG, ""); 338 return isAssociated(); 339 } 340 341 344 public boolean ejbHomeOpwith_mandatory() { 345 logger.log(BasicLevel.DEBUG, ""); 346 return isAssociated(); 347 } 348 349 352 public boolean ejbHomeOpwith_never() { 353 logger.log(BasicLevel.DEBUG, ""); 354 return isAssociated(); 355 } 356 357 public void ejbRemove() throws RemoveException { 358 logger.log(BasicLevel.DEBUG, ""); 359 } 360 361 public void ejbPassivate() { 362 logger.log(BasicLevel.DEBUG, ""); 363 } 364 365 public void ejbActivate() { 366 logger.log(BasicLevel.DEBUG, ""); 367 } 368 369 public void ejbLoad() { 370 logger.log(BasicLevel.DEBUG, ""); 371 } 372 373 public void ejbStore() { 374 logger.log(BasicLevel.DEBUG, ""); 375 } 376 377 public void setEntityContext(EntityContext ctx) { 378 initLogger(); 379 logger.log(BasicLevel.DEBUG, ""); 380 this.entityContext = ctx; 381 } 382 383 public void unsetEntityContext() { 384 logger.log(BasicLevel.DEBUG, ""); 385 this.entityContext = null; 386 } 387 388 public int setTimer(int dur, int period) { 389 logger.log(BasicLevel.DEBUG, ""); 390 TimerService timerservice = entityContext.getTimerService(); 391 Timer mt = null; 392 int ret = getTimerIdent() + 1; 393 setTimerIdent(ret); 394 if (period > 0) { 395 mt = timerservice.createTimer(dur * 1000, period * 1000, new Integer (ret)); 396 } else if (period < 0) { 397 mt = timerservice.createTimer(dur * 1000, -period * 1000, new Integer (TOCANCEL)); 399 } else { 400 mt = timerservice.createTimer(dur * 1000, new Integer (ret)); 401 } 402 return ret; 403 } 404 405 public int setTimerGetHandle(int dur, int period) { 406 logger.log(BasicLevel.DEBUG, ""); 407 TimerService timerservice = entityContext.getTimerService(); 408 int ret = dur * 10 + period; 409 Timer t = null; 410 if (period > 0) { 411 t = timerservice.createTimer(dur * 1000, period * 1000, new Integer (ret)); 412 } else { 413 t = timerservice.createTimer(dur * 1000, new Integer (ret)); 414 } 415 TimerHandle hdl = t.getHandle(); 416 Timer t2 = hdl.getTimer(); 417 if (t != t2) { 418 logger.log(BasicLevel.ERROR, "Bad timer handle"); 419 throw new EJBException ("Bad timer handle"); 420 } 421 return ret; 422 } 423 424 public TimerHandle getTimerHandle(int ident) { 425 logger.log(BasicLevel.DEBUG, ""); 426 TimerHandle hdl = null; 427 TimerService timerservice = entityContext.getTimerService(); 428 Collection timerList = timerservice.getTimers(); 429 for (Iterator i = timerList.iterator(); i.hasNext(); ) { 430 Timer t = (Timer ) i.next(); 431 Integer id = (Integer ) t.getInfo(); 432 if (id.intValue() == ident) { 433 hdl = t.getHandle(); 434 break; 435 } 436 } 437 return hdl; 438 } 439 440 public void cancelTimer(int ident) { 441 logger.log(BasicLevel.DEBUG, ""); 442 TimerService timerservice = entityContext.getTimerService(); 443 Collection timerList = timerservice.getTimers(); 444 for (Iterator i = timerList.iterator(); i.hasNext(); ) { 445 Timer t = (Timer ) i.next(); 446 Integer id = (Integer ) t.getInfo(); 447 if (id.intValue() == ident) { 448 t.cancel(); 449 } 450 } 451 } 452 453 public long getTimeRemaining(int ident) { 454 logger.log(BasicLevel.DEBUG, ""); 455 TimerService timerservice = entityContext.getTimerService(); 456 Collection timerList = timerservice.getTimers(); 457 long ret = -1; 458 for (Iterator i = timerList.iterator(); i.hasNext(); ) { 459 Timer t = (Timer ) i.next(); 460 Integer id = (Integer ) t.getInfo(); 461 if (id.intValue() == ident) { 462 ret = t.getTimeRemaining(); 463 } 464 } 465 return ret; 466 } 467 468 public int getTimerNumber() { 469 logger.log(BasicLevel.DEBUG, ""); 470 TimerService timerservice = entityContext.getTimerService(); 471 Collection timerList = timerservice.getTimers(); 472 return timerList.size(); 473 } 474 475 478 public boolean supports_call_required() throws RemoteException { 479 logger.log(BasicLevel.DEBUG, ""); 480 Simple myentity = (Simple) entityContext.getEJBLocalObject(); 481 return myentity.opwith_required(); 482 } 483 484 488 491 public void ejbTimeout(Timer timer) { 492 logger.log(BasicLevel.DEBUG, ""); 493 TimerService timerservice = entityContext.getTimerService(); 494 Collection timerList = timerservice.getTimers(); 495 Integer id = (Integer ) timer.getInfo(); 496 if (id.intValue() == TOCANCEL) { 497 timer.cancel(); 498 } 499 setTimerCount(getTimerCount() + 1); 500 TimerHandle hdl = timer.getHandle(); 501 TimerHandle hdl2 = getDeserializedHandle(hdl); 502 if (! timersAreIdentical(hdl, hdl2)) { 503 logger.log(BasicLevel.ERROR, "Bad timer handle"); 504 throw new EJBException ("Bad timer handle"); 505 } 506 } 507 } 508 | Popular Tags |