001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: SimpleEC2.java 8456 2006-06-13 08:28:03Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.transacted;
027:
028: import java.rmi.RemoteException;
029: import java.util.Collection;
030: import java.util.Iterator;
031:
032: import javax.ejb.CreateException;
033: import javax.ejb.EJBException;
034: import javax.ejb.EntityBean;
035: import javax.ejb.EntityContext;
036: import javax.ejb.RemoveException;
037: import javax.ejb.TimedObject;
038: import javax.ejb.Timer;
039: import javax.ejb.TimerHandle;
040: import javax.ejb.TimerService;
041:
042: import org.objectweb.util.monolog.api.BasicLevel;
043:
044: /**
045: * Entity bean with container managed persistence version 2.
046: * @author Helene Joanin
047: */
048: public abstract class SimpleEC2 extends SimpleCommon implements
049: EntityBean, TimedObject {
050:
051: private final int TOCANCEL = 10000;
052:
053: protected EntityContext entityContext;
054:
055: // ------------------------------------------------------------------
056: // Get and Set accessor methods of the bean's abstract schema
057: // ------------------------------------------------------------------
058: public abstract String getAccno();
059:
060: public abstract void setAccno(String accno);
061:
062: public abstract String getCustomer();
063:
064: public abstract void setCustomer(String customer);
065:
066: public abstract long getBalance();
067:
068: public abstract void setBalance(long balance);
069:
070: public abstract int getTimerIdent();
071:
072: public abstract void setTimerIdent(int id);
073:
074: public abstract int getTimerCount();
075:
076: public abstract void setTimerCount(int cnt);
077:
078: /**
079: * Create without arg
080: * transaction attribute = supports (by default in xml file)
081: */
082: public String ejbCreate() throws CreateException {
083:
084: logger.log(BasicLevel.DEBUG, "");
085:
086: // Must init fields in any case.
087: setAccno("000");
088: setCustomer("Initial");
089: setBalance(0);
090: setTimerIdent(0);
091: setTimerCount(0);
092: // In BMP, return PK.
093: return getAccno();
094: }
095:
096: /**
097: * transaction attribute = default (= supports)
098: */
099: public void ejbPostCreate() {
100: logger.log(BasicLevel.DEBUG, "");
101: }
102:
103: /**
104: * transaction attribute = notsupported
105: */
106: public String ejbCreate(int i) throws CreateException {
107:
108: logger.log(BasicLevel.DEBUG, "");
109:
110: if (isAssociated()) {
111: throw new EJBException(
112: "ejbCreate(int i): should not be in a transaction");
113: }
114: Integer v = new Integer((int) i);
115: setAccno(v.toString());
116: setCustomer("by int");
117: setBalance(1000 + i);
118: setTimerIdent(0);
119: setTimerCount(0);
120:
121: // In BMP, return PK.
122: return getAccno();
123: }
124:
125: /**
126: * transaction attribute = notsupported
127: */
128: public void ejbPostCreate(int i) {
129: logger.log(BasicLevel.DEBUG, "");
130: if (isAssociated()) {
131: throw new EJBException(
132: "ejbPostCreate(int i): should not be in a transaction");
133: }
134: }
135:
136: /**
137: * transaction attribute = required
138: */
139: public String ejbCreateForRequired(long i) throws CreateException {
140: logger.log(BasicLevel.DEBUG, "");
141: if (!isAssociated()) {
142: throw new EJBException(
143: "ejbCreate(long i): should be in a transaction");
144: }
145: Integer v = new Integer((int) i);
146: setAccno(v.toString());
147: setCustomer("Required");
148: setBalance(1000 + i);
149: setTimerIdent(0);
150: setTimerCount(0);
151:
152: // In BMP, return PK.
153: return getAccno();
154: }
155:
156: /**
157: * transaction attribute = required
158: */
159: public void ejbPostCreateForRequired(long i) {
160: logger.log(BasicLevel.DEBUG, "");
161: if (!isAssociated()) {
162: throw new EJBException(
163: "ejbPostCreateForRequired(long i): should be in a transaction");
164: }
165: }
166:
167: /**
168: * transaction attribute = never
169: */
170: public String ejbCreateForNever(short i) throws CreateException {
171: logger.log(BasicLevel.DEBUG, "");
172:
173: if (isAssociated()) {
174: throw new EJBException(
175: "ejbCreateForNever(short i): should not be in a transaction");
176: }
177: Integer v = new Integer((int) i);
178: setAccno(v.toString());
179: setCustomer("by short");
180: setBalance(1000 + i);
181: setTimerIdent(0);
182: setTimerCount(0);
183:
184: // In BMP, return PK.
185: return getAccno();
186: }
187:
188: /**
189: * transaction attribute = requiresnew
190: */
191: public String ejbCreateForRequiresNew(String i)
192: throws CreateException {
193: logger.log(BasicLevel.DEBUG, "");
194:
195: if (!isAssociated()) {
196: throw new EJBException(
197: "ejbCreate(String i): should be in a transaction");
198: }
199: setAccno(i);
200: setCustomer("by string");
201: setBalance(100);
202: setTimerIdent(0);
203: setTimerCount(0);
204:
205: // In BMP, return PK.
206: return getAccno();
207: }
208:
209: /**
210: * transaction attribute = requiresnew
211: */
212: public void ejbPostCreateForRequiresNew(String i) {
213: logger.log(BasicLevel.DEBUG, "");
214:
215: if (!isAssociated()) {
216: throw new EJBException(
217: "ejbPostCreateForRequiresNew(String i): should be in a transaction");
218: }
219: }
220:
221: /**
222: * transaction attribute = mandatory
223: */
224: public String ejbCreateForMandatory(char i) throws CreateException {
225: logger.log(BasicLevel.DEBUG, "");
226:
227: if (!isAssociated()) {
228: throw new EJBException(
229: "ejbCreateForMandatory(char i): should be in a transaction");
230: }
231: Integer v = new Integer((int) i);
232: setAccno(v.toString());
233: setCustomer("by char");
234: setBalance(200);
235: setTimerIdent(0);
236: setTimerCount(0);
237:
238: // In BMP, return PK.
239: return getAccno();
240: }
241:
242: /**
243: * transaction attribute = mandatory
244: */
245: public void ejbPostCreateForMandatory(char i) {
246: logger.log(BasicLevel.DEBUG, "");
247:
248: if (!isAssociated()) {
249: throw new EJBException(
250: "ejbPostCreateForMandatory(char i): should be in a transaction");
251: }
252: }
253:
254: /**
255: * transaction attribute = supports
256: */
257: public String ejbCreateForSupports(boolean intx)
258: throws CreateException {
259: logger.log(BasicLevel.DEBUG, "");
260:
261: if (intx) {
262: setAccno("TRUE");
263: setBalance(101);
264: if (!isAssociated()) {
265: throw new EJBException(
266: "ejbCreateForSupports(true): should be in a transaction");
267: }
268: } else {
269: setAccno("FALSE");
270: setBalance(102);
271: if (isAssociated()) {
272: throw new EJBException(
273: "ejbCreate(false): should not be in a transaction");
274: }
275: }
276: setCustomer("by boolean");
277: setTimerIdent(0);
278: setTimerCount(0);
279:
280: // In BMP, return PK.
281: return getAccno();
282: }
283:
284: /**
285: * transaction attribute = supports
286: */
287: public void ejbPostCreateForSupports(boolean intx) {
288: logger.log(BasicLevel.DEBUG, "");
289:
290: if (intx) {
291: if (!isAssociated()) {
292: throw new EJBException(
293: "ejbPostCreateForSupports: should be in a transaction");
294: }
295: } else {
296: if (isAssociated()) {
297: throw new EJBException(
298: "ejbPostCreateForSupports: should not be in a transaction");
299: }
300: }
301: }
302:
303: public String ejbCreateWithTimer(int i, long dur)
304: throws RemoteException, CreateException {
305: logger.log(BasicLevel.DEBUG, "");
306: // Must init fields in any case.
307: setAccno("001");
308: setCustomer("Timer");
309: setBalance(0);
310: setTimerIdent(0);
311: setTimerCount(0);
312: // In BMP, return PK.
313: return getAccno();
314: }
315:
316: public void ejbPostCreateWithTimer(int i, long dur)
317: throws RemoteException, CreateException {
318: logger.log(BasicLevel.DEBUG, "");
319: TimerService timerservice = entityContext.getTimerService();
320: int ret = getTimerIdent() + 1;
321: Timer mt = timerservice.createTimer(dur, new Integer(ret));
322: }
323:
324: /**
325: * transaction attribute = never
326: */
327: public void ejbPostCreateForNever(short i) {
328: logger.log(BasicLevel.DEBUG, "");
329:
330: if (isAssociated()) {
331: throw new EJBException(
332: "ejbPostCreateForNever(short i): should not be in a transaction");
333: }
334: }
335:
336: /**
337: * This method return true if there is an association of a transaction with this thread
338: */
339: public boolean ejbHomeOpwith_notsupported() {
340: logger.log(BasicLevel.DEBUG, "");
341: return isAssociated();
342: }
343:
344: /**
345: * This method return true if there is an association of a transaction with this thread
346: */
347: public boolean ejbHomeOpwith_supports() {
348: logger.log(BasicLevel.DEBUG, "");
349: return isAssociated();
350: }
351:
352: /**
353: * This method return true if there is an association of a transaction with this thread
354: */
355: public boolean ejbHomeOpwith_required() {
356: logger.log(BasicLevel.DEBUG, "");
357: return isAssociated();
358: }
359:
360: /**
361: * This method return true if there is an association of a transaction with this thread
362: */
363: public boolean ejbHomeOpwith_requires_new() {
364: logger.log(BasicLevel.DEBUG, "");
365: return isAssociated();
366: }
367:
368: /**
369: * This method return true if there is an association of a transaction with this thread
370: */
371: public boolean ejbHomeOpwith_mandatory() {
372: logger.log(BasicLevel.DEBUG, "");
373: return isAssociated();
374: }
375:
376: /**
377: * This method return true if there is an association of a transaction with this thread
378: */
379: public boolean ejbHomeOpwith_never() {
380: logger.log(BasicLevel.DEBUG, "");
381: return isAssociated();
382: }
383:
384: public void ejbRemove() throws RemoveException {
385: logger.log(BasicLevel.DEBUG, "");
386: }
387:
388: public void ejbPassivate() {
389: logger.log(BasicLevel.DEBUG, "");
390: }
391:
392: public void ejbActivate() {
393: logger.log(BasicLevel.DEBUG, "");
394: }
395:
396: public void ejbLoad() {
397: logger.log(BasicLevel.DEBUG, "");
398: }
399:
400: public void ejbStore() {
401: logger.log(BasicLevel.DEBUG, "");
402: }
403:
404: public void setEntityContext(EntityContext ctx) {
405: initLogger();
406: logger.log(BasicLevel.DEBUG, "");
407: this .entityContext = ctx;
408: }
409:
410: public void unsetEntityContext() {
411: logger.log(BasicLevel.DEBUG, "");
412: this .entityContext = null;
413: }
414:
415: public int setTimer(int dur, int period) {
416: logger.log(BasicLevel.DEBUG, "");
417: TimerService timerservice = entityContext.getTimerService();
418: Timer mt = null;
419: int ret = getTimerIdent() + 1;
420: setTimerIdent(ret);
421: if (period > 0) {
422: mt = timerservice.createTimer(dur * 1000, period * 1000,
423: new Integer(ret));
424: } else if (period < 0) {
425: // special test for cancel inside ejbTimeout
426: mt = timerservice.createTimer(dur * 1000, -period * 1000,
427: new Integer(TOCANCEL));
428: } else {
429: mt = timerservice.createTimer(dur * 1000, new Integer(ret));
430: }
431: return ret;
432: }
433:
434: public int setTimer(java.util.Date date, int period) {
435: logger.log(BasicLevel.DEBUG, "");
436: TimerService timerservice = entityContext.getTimerService();
437: Timer mt = null;
438: int ret = getTimerIdent() + 1;
439: setTimerIdent(ret);
440: if (period > 0) {
441: mt = timerservice.createTimer(date, period * 1000,
442: new Integer(ret));
443: } else if (period < 0) {
444: // special test for cancel inside ejbTimeout
445: mt = timerservice.createTimer(date, -period * 1000,
446: new Integer(TOCANCEL));
447: } else {
448: mt = timerservice.createTimer(date, new Integer(ret));
449: }
450: return ret;
451: }
452:
453: public int setTimerGetHandle(int dur, int period) {
454: logger.log(BasicLevel.DEBUG, "");
455: TimerService timerservice = entityContext.getTimerService();
456: int ret = dur * 10 + period;
457: Timer t = null;
458: if (period > 0) {
459: t = timerservice.createTimer(dur * 1000, period * 1000,
460: new Integer(ret));
461: } else {
462: t = timerservice.createTimer(dur * 1000, new Integer(ret));
463: }
464: TimerHandle hdl = t.getHandle();
465: Timer t2 = hdl.getTimer();
466: if (t != t2) {
467: logger.log(BasicLevel.ERROR, "Bad timer handle");
468: logger.log(BasicLevel.ERROR, "Original=" + t);
469: logger.log(BasicLevel.ERROR, "Recomputed=" + t2);
470: throw new EJBException("Bad timer handle");
471: }
472: return ret;
473: }
474:
475: public TimerHandle getTimerHandle(int ident) {
476: logger.log(BasicLevel.DEBUG, "");
477: TimerHandle hdl = null;
478: TimerService timerservice = entityContext.getTimerService();
479: Collection timerList = timerservice.getTimers();
480: for (Iterator i = timerList.iterator(); i.hasNext();) {
481: Timer t = (Timer) i.next();
482: Integer id = (Integer) t.getInfo();
483: if (id.intValue() == ident) {
484: hdl = t.getHandle();
485: break;
486: }
487: }
488: return hdl;
489: }
490:
491: public void cancelTimer(int ident) {
492: logger.log(BasicLevel.DEBUG, "");
493: TimerService timerservice = entityContext.getTimerService();
494: Collection timerList = timerservice.getTimers();
495: for (Iterator i = timerList.iterator(); i.hasNext();) {
496: Timer t = (Timer) i.next();
497: Integer id = (Integer) t.getInfo();
498: if (id.intValue() == ident) {
499: t.cancel();
500: }
501: }
502: }
503:
504: public void cancelTimers() {
505: logger.log(BasicLevel.DEBUG, "");
506: TimerService timerservice = entityContext.getTimerService();
507: Collection timerList = timerservice.getTimers();
508: for (Iterator i = timerList.iterator(); i.hasNext();) {
509: Timer t = (Timer) i.next();
510: t.cancel();
511: }
512: }
513:
514: public long getTimeRemaining(int ident) {
515: logger.log(BasicLevel.DEBUG, "");
516: TimerService timerservice = entityContext.getTimerService();
517: Collection timerList = timerservice.getTimers();
518: long ret = -1;
519: for (Iterator i = timerList.iterator(); i.hasNext();) {
520: Timer t = (Timer) i.next();
521: Integer id = (Integer) t.getInfo();
522: if (id.intValue() == ident) {
523: ret = t.getTimeRemaining();
524: }
525: }
526: return ret;
527: }
528:
529: public int getTimerNumber() {
530: logger.log(BasicLevel.DEBUG, "");
531: TimerService timerservice = entityContext.getTimerService();
532: Collection timerList = timerservice.getTimers();
533: return timerList.size();
534: }
535:
536: /**
537: * This support method calls a required method
538: */
539: public boolean supports_call_required() throws RemoteException {
540: logger.log(BasicLevel.DEBUG, "");
541: Simple myentity = (Simple) entityContext.getEJBLocalObject();
542: return myentity.opwith_required();
543: }
544:
545: /**
546: * Cancels all timers, that are associated with this entity, and starts a
547: * new timer.
548: */
549: public void startInfoTimer(int dur, String inform)
550: throws RemoteException {
551: logger.log(BasicLevel.DEBUG, "");
552: TimerService timerservice = entityContext.getTimerService();
553: // Remove all timers with this Info first
554: for (Iterator timers = timerservice.getTimers().iterator(); timers
555: .hasNext();) {
556: Timer timer = (Timer) timers.next();
557: if (inform.equals(timer.getInfo())) {
558: timer.cancel();
559: }
560: }
561: // Now, create the Timer
562: timerservice.createTimer(dur * 1000, inform);
563: }
564:
565: // -----------------------------------------------------------
566: // TimedObject implementation
567: // -----------------------------------------------------------
568:
569: /**
570: * A timer is expired.
571: */
572: public void ejbTimeout(Timer timer) {
573: logger.log(BasicLevel.DEBUG, "");
574: TimerService timerservice = entityContext.getTimerService();
575: Collection timerList = timerservice.getTimers();
576: Integer id = (Integer) timer.getInfo();
577: if (id.intValue() == TOCANCEL) {
578: timer.cancel();
579: }
580: setTimerCount(getTimerCount() + 1);
581: TimerHandle hdl = timer.getHandle();
582: TimerHandle hdl2 = getDeserializedHandle(hdl);
583: if (!timersAreIdentical(hdl, hdl2)) {
584: logger.log(BasicLevel.ERROR, "Bad timer handle");
585: throw new EJBException("Bad timer handle");
586: }
587: }
588: }
|