KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > transacted > SimpleEC2


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: SimpleEC2.java,v 1.11 2004/10/20 10:03:11 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.transacted;
27
28 import java.rmi.RemoteException JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.ejb.CreateException JavaDoc;
33 import javax.ejb.EJBException JavaDoc;
34 import javax.ejb.EntityBean JavaDoc;
35 import javax.ejb.EntityContext JavaDoc;
36 import javax.ejb.RemoveException JavaDoc;
37 import javax.ejb.TimedObject JavaDoc;
38 import javax.ejb.Timer JavaDoc;
39 import javax.ejb.TimerHandle JavaDoc;
40 import javax.ejb.TimerService JavaDoc;
41
42 import org.objectweb.util.monolog.api.BasicLevel;
43
44 /**
45  * Entity bean with container managed persistence version 2.
46  * @author Helene Joanin
47  */

48 public abstract class SimpleEC2 extends SimpleCommon implements EntityBean JavaDoc, TimedObject JavaDoc {
49
50     private final int TOCANCEL = 10000;
51
52     protected EntityContext JavaDoc entityContext;
53
54     // ------------------------------------------------------------------
55
// Get and Set accessor methods of the bean's abstract schema
56
// ------------------------------------------------------------------
57
public abstract String JavaDoc getAccno();
58     public abstract void setAccno(String JavaDoc accno);
59     public abstract String JavaDoc getCustomer();
60     public abstract void setCustomer(String JavaDoc 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     /**
69      * Create without arg
70      * transaction attribute = supports (by default in xml file)
71      */

72     public String JavaDoc ejbCreate() throws CreateException JavaDoc {
73
74         logger.log(BasicLevel.DEBUG, "");
75
76         // Must init fields in any case.
77
setAccno("000");
78         setCustomer("Initial");
79         setBalance(0);
80         setTimerIdent(0);
81         setTimerCount(0);
82         // In BMP, return PK.
83
return getAccno();
84     }
85
86     /**
87      * transaction attribute = default (= supports)
88      */

89     public void ejbPostCreate() {
90         logger.log(BasicLevel.DEBUG, "");
91     }
92
93     /**
94      * transaction attribute = notsupported
95      */

96     public String JavaDoc ejbCreate(int i) throws CreateException JavaDoc {
97
98         logger.log(BasicLevel.DEBUG, "");
99
100         if (isAssociated()) {
101             throw new EJBException JavaDoc("ejbCreate(int i): should not be in a transaction");
102         }
103         Integer JavaDoc v = new Integer JavaDoc((int)i);
104         setAccno(v.toString());
105         setCustomer("by int");
106         setBalance(1000 + i);
107         setTimerIdent(0);
108         setTimerCount(0);
109     
110         // In BMP, return PK.
111
return getAccno();
112     }
113
114     /**
115      * transaction attribute = notsupported
116      */

117     public void ejbPostCreate(int i) {
118         logger.log(BasicLevel.DEBUG, "");
119         if (isAssociated()) {
120             throw new EJBException JavaDoc("ejbPostCreate(int i): should not be in a transaction");
121         }
122     }
123
124     /**
125      * transaction attribute = required
126      */

127     public String JavaDoc ejbCreateForRequired(long i) throws CreateException JavaDoc {
128         logger.log(BasicLevel.DEBUG, "");
129         if (!isAssociated()) {
130             throw new EJBException JavaDoc("ejbCreate(long i): should be in a transaction");
131         }
132         Integer JavaDoc v = new Integer JavaDoc((int)i);
133         setAccno(v.toString());
134         setCustomer("Required");
135         setBalance(1000 + i);
136         setTimerIdent(0);
137         setTimerCount(0);
138     
139         // In BMP, return PK.
140
return getAccno();
141     }
142
143     /**
144      * transaction attribute = required
145      */

146     public void ejbPostCreateForRequired(long i) {
147         logger.log(BasicLevel.DEBUG, "");
148         if (!isAssociated()) {
149             throw new EJBException JavaDoc("ejbPostCreateForRequired(long i): should be in a transaction");
150         }
151     }
152
153     /**
154      * transaction attribute = never
155      */

156     public String JavaDoc ejbCreateForNever(short i) throws CreateException JavaDoc {
157         logger.log(BasicLevel.DEBUG, "");
158
159         if (isAssociated()) {
160             throw new EJBException JavaDoc("ejbCreateForNever(short i): should not be in a transaction");
161         }
162         Integer JavaDoc v = new Integer JavaDoc((int)i);
163         setAccno(v.toString());
164         setCustomer("by short");
165         setBalance(1000 + i);
166         setTimerIdent(0);
167         setTimerCount(0);
168     
169         // In BMP, return PK.
170
return getAccno();
171     }
172
173     /**
174      * transaction attribute = requiresnew
175      */

176     public String JavaDoc ejbCreateForRequiresNew(String JavaDoc i) throws CreateException JavaDoc {
177         logger.log(BasicLevel.DEBUG, "");
178
179         if (!isAssociated()) {
180             throw new EJBException JavaDoc("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         // In BMP, return PK.
189
return getAccno();
190     }
191
192     /**
193      * transaction attribute = requiresnew
194      */

195     public void ejbPostCreateForRequiresNew(String JavaDoc i) {
196         logger.log(BasicLevel.DEBUG, "");
197
198         if (!isAssociated()) {
199             throw new EJBException JavaDoc("ejbPostCreateForRequiresNew(String i): should be in a transaction");
200         }
201     }
202
203     /**
204      * transaction attribute = mandatory
205      */

206     public String JavaDoc ejbCreateForMandatory(char i) throws CreateException JavaDoc {
207         logger.log(BasicLevel.DEBUG, "");
208
209         if (!isAssociated()) {
210             throw new EJBException JavaDoc("ejbCreateForMandatory(char i): should be in a transaction");
211         }
212         Integer JavaDoc v = new Integer JavaDoc((int)i);
213         setAccno(v.toString());
214         setCustomer("by char");
215         setBalance(200);
216         setTimerIdent(0);
217         setTimerCount(0);
218     
219         // In BMP, return PK.
220
return getAccno();
221     }
222
223     /**
224      * transaction attribute = mandatory
225      */

226     public void ejbPostCreateForMandatory(char i) {
227         logger.log(BasicLevel.DEBUG, "");
228
229         if (!isAssociated()) {
230             throw new EJBException JavaDoc("ejbPostCreateForMandatory(char i): should be in a transaction");
231         }
232     }
233
234     /**
235      * transaction attribute = supports
236      */

237     public String JavaDoc ejbCreateForSupports(boolean intx) throws CreateException JavaDoc {
238         logger.log(BasicLevel.DEBUG, "");
239
240         if (intx) {
241             setAccno("TRUE");
242             setBalance(101);
243             if (!isAssociated()) {
244                 throw new EJBException JavaDoc("ejbCreateForSupports(true): should be in a transaction");
245             }
246         } else {
247             setAccno("FALSE");
248             setBalance(102);
249             if (isAssociated()) {
250                 throw new EJBException JavaDoc("ejbCreate(false): should not be in a transaction");
251             }
252         }
253         setCustomer("by boolean");
254         setTimerIdent(0);
255         setTimerCount(0);
256     
257         // In BMP, return PK.
258
return getAccno();
259     }
260
261     /**
262      * transaction attribute = supports
263      */

264     public void ejbPostCreateForSupports(boolean intx) {
265         logger.log(BasicLevel.DEBUG, "");
266
267         if (intx) {
268             if (!isAssociated()) {
269                 throw new EJBException JavaDoc("ejbPostCreateForSupports: should be in a transaction");
270             }
271         } else {
272             if (isAssociated()) {
273                 throw new EJBException JavaDoc("ejbPostCreateForSupports: should not be in a transaction");
274             }
275         }
276     }
277
278     public String JavaDoc ejbCreateWithTimer(int i, long dur) throws RemoteException JavaDoc, CreateException JavaDoc {
279         logger.log(BasicLevel.DEBUG, "");
280         // Must init fields in any case.
281
setAccno("001");
282         setCustomer("Timer");
283         setBalance(0);
284         setTimerIdent(0);
285         setTimerCount(0);
286         // In BMP, return PK.
287
return getAccno();
288     }
289
290     public void ejbPostCreateWithTimer(int i, long dur) throws RemoteException JavaDoc, CreateException JavaDoc {
291         logger.log(BasicLevel.DEBUG, "");
292         TimerService JavaDoc timerservice = entityContext.getTimerService();
293         int ret = getTimerIdent() + 1;
294         Timer JavaDoc mt = timerservice.createTimer(dur, new Integer JavaDoc(ret));
295     }
296
297     /**
298      * transaction attribute = never
299      */

300     public void ejbPostCreateForNever(short i) {
301         logger.log(BasicLevel.DEBUG, "");
302
303         if (isAssociated()) {
304             throw new EJBException JavaDoc("ejbPostCreateForNever(short i): should not be in a transaction");
305         }
306     }
307
308
309     /**
310      * This method return true if there is an association of a transaction with this thread
311      */

312     public boolean ejbHomeOpwith_notsupported() {
313         logger.log(BasicLevel.DEBUG, "");
314         return isAssociated();
315     }
316
317     /**
318      * This method return true if there is an association of a transaction with this thread
319      */

320     public boolean ejbHomeOpwith_supports() {
321         logger.log(BasicLevel.DEBUG, "");
322         return isAssociated();
323     }
324    
325     /**
326      * This method return true if there is an association of a transaction with this thread
327      */

328     public boolean ejbHomeOpwith_required() {
329         logger.log(BasicLevel.DEBUG, "");
330         return isAssociated();
331     }
332
333     /**
334      * This method return true if there is an association of a transaction with this thread
335      */

336     public boolean ejbHomeOpwith_requires_new() {
337         logger.log(BasicLevel.DEBUG, "");
338         return isAssociated();
339     }
340
341     /**
342      * This method return true if there is an association of a transaction with this thread
343      */

344     public boolean ejbHomeOpwith_mandatory() {
345         logger.log(BasicLevel.DEBUG, "");
346         return isAssociated();
347     }
348   
349     /**
350      * This method return true if there is an association of a transaction with this thread
351      */

352     public boolean ejbHomeOpwith_never() {
353         logger.log(BasicLevel.DEBUG, "");
354         return isAssociated();
355     }
356
357     public void ejbRemove() throws RemoveException JavaDoc {
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 JavaDoc 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 JavaDoc timerservice = entityContext.getTimerService();
391         Timer JavaDoc mt = null;
392         int ret = getTimerIdent() + 1;
393         setTimerIdent(ret);
394         if (period > 0) {
395             mt = timerservice.createTimer(dur * 1000, period * 1000, new Integer JavaDoc(ret));
396         } else if (period < 0) {
397             // special test for cancel inside ejbTimeout
398
mt = timerservice.createTimer(dur * 1000, -period * 1000, new Integer JavaDoc(TOCANCEL));
399         } else {
400             mt = timerservice.createTimer(dur * 1000, new Integer JavaDoc(ret));
401         }
402         return ret;
403     }
404
405     public int setTimerGetHandle(int dur, int period) {
406         logger.log(BasicLevel.DEBUG, "");
407         TimerService JavaDoc timerservice = entityContext.getTimerService();
408         int ret = dur * 10 + period;
409         Timer JavaDoc t = null;
410         if (period > 0) {
411             t = timerservice.createTimer(dur * 1000, period * 1000, new Integer JavaDoc(ret));
412         } else {
413             t = timerservice.createTimer(dur * 1000, new Integer JavaDoc(ret));
414         }
415         TimerHandle JavaDoc hdl = t.getHandle();
416         Timer JavaDoc t2 = hdl.getTimer();
417         if (t != t2) {
418             logger.log(BasicLevel.ERROR, "Bad timer handle");
419             throw new EJBException JavaDoc("Bad timer handle");
420         }
421         return ret;
422     }
423
424     public TimerHandle JavaDoc getTimerHandle(int ident) {
425         logger.log(BasicLevel.DEBUG, "");
426         TimerHandle JavaDoc hdl = null;
427         TimerService JavaDoc timerservice = entityContext.getTimerService();
428         Collection JavaDoc timerList = timerservice.getTimers();
429         for (Iterator JavaDoc i = timerList.iterator(); i.hasNext(); ) {
430             Timer JavaDoc t = (Timer JavaDoc) i.next();
431             Integer JavaDoc id = (Integer JavaDoc) 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 JavaDoc timerservice = entityContext.getTimerService();
443         Collection JavaDoc timerList = timerservice.getTimers();
444         for (Iterator JavaDoc i = timerList.iterator(); i.hasNext(); ) {
445             Timer JavaDoc t = (Timer JavaDoc) i.next();
446             Integer JavaDoc id = (Integer JavaDoc) 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 JavaDoc timerservice = entityContext.getTimerService();
456         Collection JavaDoc timerList = timerservice.getTimers();
457         long ret = -1;
458         for (Iterator JavaDoc i = timerList.iterator(); i.hasNext(); ) {
459             Timer JavaDoc t = (Timer JavaDoc) i.next();
460             Integer JavaDoc id = (Integer JavaDoc) 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 JavaDoc timerservice = entityContext.getTimerService();
471         Collection JavaDoc timerList = timerservice.getTimers();
472         return timerList.size();
473     }
474
475     /**
476      * This support method calls a required method
477      */

478     public boolean supports_call_required() throws RemoteException JavaDoc {
479         logger.log(BasicLevel.DEBUG, "");
480         Simple myentity = (Simple) entityContext.getEJBLocalObject();
481         return myentity.opwith_required();
482     }
483
484     // -----------------------------------------------------------
485
// TimedObject implementation
486
// -----------------------------------------------------------
487

488     /**
489      * A timer is expired.
490      */

491     public void ejbTimeout(Timer JavaDoc timer) {
492         logger.log(BasicLevel.DEBUG, "");
493         TimerService JavaDoc timerservice = entityContext.getTimerService();
494         Collection JavaDoc timerList = timerservice.getTimers();
495         Integer JavaDoc id = (Integer JavaDoc) timer.getInfo();
496         if (id.intValue() == TOCANCEL) {
497             timer.cancel();
498         }
499         setTimerCount(getTimerCount() + 1);
500         TimerHandle JavaDoc hdl = timer.getHandle();
501         TimerHandle JavaDoc hdl2 = getDeserializedHandle(hdl);
502         if (! timersAreIdentical(hdl, hdl2)) {
503             logger.log(BasicLevel.ERROR, "Bad timer handle");
504             throw new EJBException JavaDoc("Bad timer handle");
505         }
506     }
507 }
508
Popular Tags