KickJava   Java API By Example, From Geeks To Geeks.

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


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: SimpleSL.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.EJBException JavaDoc;
33 import javax.ejb.SessionBean JavaDoc;
34 import javax.ejb.SessionContext JavaDoc;
35 import javax.ejb.TimedObject JavaDoc;
36 import javax.ejb.Timer JavaDoc;
37 import javax.ejb.TimerHandle JavaDoc;
38 import javax.ejb.TimerService JavaDoc;
39 import javax.naming.InitialContext JavaDoc;
40 import javax.naming.NamingException JavaDoc;
41
42 import org.objectweb.util.monolog.api.BasicLevel;
43
44 public class SimpleSL extends SimpleCommon implements SessionBean JavaDoc, TimedObject JavaDoc {
45
46     protected SessionContext JavaDoc sessionContext;
47     static protected int timercount = 0;
48
49     public void setSessionContext(SessionContext JavaDoc sessionContext) {
50         initLogger();
51         logger.log(BasicLevel.DEBUG, "");
52         this.sessionContext = sessionContext;
53     }
54
55     public void ejbActivate() {
56         logger.log(BasicLevel.DEBUG, "");
57     }
58
59     public void ejbPassivate() {
60         logger.log(BasicLevel.DEBUG, "");
61     }
62
63     public void ejbRemove() {
64         logger.log(BasicLevel.DEBUG, "");
65     }
66
67     public void ejbCreate() {
68         logger.log(BasicLevel.DEBUG, "");
69         try {
70             InitialContext JavaDoc ictx = new InitialContext JavaDoc();
71         } catch(NamingException JavaDoc e) {
72             throw new EJBException JavaDoc(e);
73         }
74     }
75
76     public int setTimer(int dur, int period) {
77         logger.log(BasicLevel.DEBUG, "");
78         TimerService JavaDoc timerservice = sessionContext.getTimerService();
79         int ret = dur * 10 + period;
80         if (period > 0) {
81             timerservice.createTimer(dur * 1000, period * 1000, new Integer JavaDoc(ret));
82         } else {
83             timerservice.createTimer(dur * 1000, new Integer JavaDoc(ret));
84         }
85         return ret;
86     }
87
88     public int setTimerGetHandle(int dur, int period) {
89         logger.log(BasicLevel.DEBUG, "");
90         TimerService JavaDoc timerservice = sessionContext.getTimerService();
91         int ret = dur * 10 + period;
92         Timer JavaDoc t = null;
93         if (period > 0) {
94             t = timerservice.createTimer(dur * 1000, period * 1000, new Integer JavaDoc(ret));
95         } else {
96             t = timerservice.createTimer(dur * 1000, new Integer JavaDoc(ret));
97         }
98         TimerHandle JavaDoc hdl = t.getHandle();
99         TimerHandle JavaDoc hdl2 = getDeserializedHandle(hdl);
100         if (! timersAreIdentical(hdl, hdl2)) {
101             logger.log(BasicLevel.ERROR, "Bad timer handle");
102             throw new EJBException JavaDoc("Bad timer handle");
103         }
104         return ret;
105     }
106
107     public TimerHandle JavaDoc getTimerHandle(int ident) {
108         logger.log(BasicLevel.DEBUG, "");
109         TimerHandle JavaDoc hdl = null;
110         TimerService JavaDoc timerservice = sessionContext.getTimerService();
111         Collection JavaDoc timerList = timerservice.getTimers();
112         for (Iterator JavaDoc i = timerList.iterator(); i.hasNext(); ) {
113             Timer JavaDoc t = (Timer JavaDoc) i.next();
114             Integer JavaDoc id = (Integer JavaDoc) t.getInfo();
115             if (id.intValue() == ident) {
116                 hdl = t.getHandle();
117                 TimerHandle JavaDoc hdl2 = getDeserializedHandle(hdl);
118                 if (! timersAreIdentical(hdl, hdl2)) {
119                     logger.log(BasicLevel.ERROR, "Bad timer handle");
120                     throw new EJBException JavaDoc("Bad timer handle");
121                 }
122                 break;
123             }
124         }
125         return hdl;
126     }
127
128     public void cancelTimer(int ident) {
129         logger.log(BasicLevel.DEBUG, "");
130         TimerService JavaDoc timerservice = sessionContext.getTimerService();
131         Collection JavaDoc timerList = timerservice.getTimers();
132         for (Iterator JavaDoc i = timerList.iterator(); i.hasNext(); ) {
133             Timer JavaDoc t = (Timer JavaDoc) i.next();
134             Integer JavaDoc id = (Integer JavaDoc) t.getInfo();
135             if (id.intValue() == ident) {
136                 t.cancel();
137             }
138         }
139     }
140
141     public long getTimeRemaining(int ident) {
142         logger.log(BasicLevel.DEBUG, "");
143         TimerService JavaDoc timerservice = sessionContext.getTimerService();
144         Collection JavaDoc timerList = timerservice.getTimers();
145         long ret = -1;
146         for (Iterator JavaDoc i = timerList.iterator(); i.hasNext(); ) {
147             Timer JavaDoc t = (Timer JavaDoc) i.next();
148             Integer JavaDoc id = (Integer JavaDoc) t.getInfo();
149             if (id.intValue() == ident) {
150                 ret = t.getTimeRemaining();
151             }
152         }
153         return ret;
154     }
155
156     public int getTimerNumber() {
157         logger.log(BasicLevel.DEBUG, "");
158         TimerService JavaDoc timerservice = sessionContext.getTimerService();
159         Collection JavaDoc timerList = timerservice.getTimers();
160         return timerList.size();
161     }
162
163     public int getTimerCount() {
164         logger.log(BasicLevel.DEBUG, "");
165         return timercount;
166     }
167
168     /**
169      * This support method calls a required method
170      */

171     public boolean supports_call_required() throws RemoteException JavaDoc {
172         logger.log(BasicLevel.DEBUG, "");
173         Simple mysession = (Simple) sessionContext.getEJBObject();
174         return mysession.opwith_required();
175     }
176
177     // -----------------------------------------------------------
178
// TimedObject implementation
179
// -----------------------------------------------------------
180

181     /**
182      * A timer is expired.
183      */

184     public void ejbTimeout(Timer JavaDoc timer) {
185         logger.log(BasicLevel.DEBUG, "");
186         timercount++;
187         TimerService JavaDoc timerservice = sessionContext.getTimerService();
188         Collection JavaDoc timerList = timerservice.getTimers();
189         TimerHandle JavaDoc hdl = timer.getHandle();
190         TimerHandle JavaDoc hdl2 = getDeserializedHandle(hdl);
191         if (! timersAreIdentical(hdl, hdl2)) {
192             logger.log(BasicLevel.ERROR, "Bad timer handle");
193             throw new EJBException JavaDoc("Bad timer handle");
194         }
195     }
196 }
197
Popular Tags