KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > txtimer > ejb > TimerSessionBean


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.test.txtimer.ejb;
23
24 import java.io.Serializable JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26 import java.security.Principal JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import javax.ejb.CreateException JavaDoc;
33 import javax.ejb.EJBException JavaDoc;
34 import javax.ejb.SessionBean JavaDoc;
35 import javax.ejb.SessionContext JavaDoc;
36 import javax.ejb.TimedObject JavaDoc;
37 import javax.ejb.Timer JavaDoc;
38 import javax.ejb.TimerService JavaDoc;
39
40 import org.jboss.logging.Logger;
41
42 /**
43  * Session Bean Timer Test
44  **/

45 public class TimerSessionBean
46         implements SessionBean JavaDoc, TimedObject JavaDoc
47 {
48    private static Logger log = Logger.getLogger(TimerSessionBean.class);
49
50    private SessionContext JavaDoc context;
51
52    // count calls to ejbTimeout
53
private int callCount;
54
55    // count calls to ejbTimeout
56
private static int globalCallCount;
57
58    // seen from ejbTimeout
59
private Principal JavaDoc ejbTimeoutCaller;
60
61
62    /**
63     * @ejb.interface-method view-type="both"
64     **/

65    public void createTimer(long duration, long periode, Serializable JavaDoc info)
66    {
67       TimerService JavaDoc timerService = context.getTimerService();
68       if (periode > 0)
69          timerService.createTimer(duration, periode, info);
70       else
71          timerService.createTimer(duration, info);
72    }
73
74    /**
75     * @ejb.interface-method view-type="both"
76     **/

77    public void cancelFirstTimer()
78    {
79       TimerService JavaDoc timerService = context.getTimerService();
80       if (timerService.getTimers().isEmpty())
81          throw new EJBException JavaDoc("There are no timers");
82
83       Timer JavaDoc timer = (Timer JavaDoc)timerService.getTimers().iterator().next();
84       timer.cancel();
85    }
86
87    /**
88     * This is not allowed on the remote interface.
89     * @ejb.interface-method view-type="both"
90     **/

91    public Object JavaDoc createTimerReturnHandle(long duration)
92    {
93       TimerService JavaDoc timerService = context.getTimerService();
94       Timer JavaDoc timer = timerService.createTimer(duration, null);
95       return timer.getHandle();
96    }
97
98    /**
99     * This is not allowed on the remote interface.
100     * @ejb.interface-method view-type="both"
101     **/

102    public String JavaDoc passTimerHandle(Object JavaDoc handle)
103    {
104       return handle.toString();
105    }
106
107    /**
108     * @ejb.interface-method view-type="both"
109     **/

110    public void resetCallCount()
111    {
112       callCount = 0;
113       globalCallCount = 0;
114    }
115
116    /**
117     * @ejb.interface-method view-type="both"
118     **/

119    public int getCallCount()
120    {
121       log.info("getCallCount [count=" + callCount + "]");
122       return callCount;
123    }
124
125    /**
126     * @ejb.interface-method view-type="both"
127     **/

128    public int getGlobalCallCount()
129    {
130       log.info("getGlobalCallCount [count=" + globalCallCount + "]");
131       return globalCallCount;
132    }
133
134    /**
135     * @ejb.interface-method view-type="both"
136     **/

137    public List JavaDoc getTimers()
138    {
139       TimerService JavaDoc timerService = context.getTimerService();
140
141       ArrayList JavaDoc handles = new ArrayList JavaDoc();
142       Iterator JavaDoc it = timerService.getTimers().iterator();
143       while (it.hasNext())
144       {
145          Timer JavaDoc timer = (Timer JavaDoc) it.next();
146          handles.add(timer.getHandle().toString());
147       }
148       return handles;
149    }
150
151    /**
152     * @ejb.interface-method view-type="both"
153     **/

154    public Principal JavaDoc getEjbTimeoutCaller()
155    {
156       return ejbTimeoutCaller;
157    }
158
159    public void ejbTimeout(Timer JavaDoc timer)
160    {
161       callCount++;
162       globalCallCount++;
163
164       log.info("ejbTimeout [count=" + callCount + "] timer=" + timer);
165
166       ejbTimeoutCaller = context.getCallerPrincipal();
167       log.info("ejbTimeout [callerPrincipal=" + ejbTimeoutCaller + "]");
168
169       Serializable JavaDoc info = timer.getInfo();
170       log.info("ejbTimeout [info=" + info + "]");
171       
172       if (info != null)
173       {
174          if (info instanceof Properties JavaDoc)
175          {
176             Properties JavaDoc props = (Properties JavaDoc)timer.getInfo();
177             if ("true".equals(props.getProperty("cancel")))
178                timer.cancel();
179          }
180       }
181    }
182
183    // -------------------------------------------------------------------------
184
// Framework Callbacks
185
// -------------------------------------------------------------------------
186

187    public void setSessionContext(SessionContext JavaDoc ctx) throws EJBException JavaDoc, RemoteException JavaDoc
188    {
189       this.context = ctx;
190    }
191
192    /**
193     * @ejb.create-method view-type="both"
194     **/

195    public void ejbCreate() throws CreateException JavaDoc
196    {
197    }
198
199    public void ejbRemove() throws EJBException JavaDoc, RemoteException JavaDoc
200    {
201    }
202
203    public void ejbActivate() throws EJBException JavaDoc, RemoteException JavaDoc
204    {
205    }
206
207    public void ejbPassivate() throws EJBException JavaDoc, RemoteException JavaDoc
208    {
209    }
210 }
211
Popular Tags