KickJava   Java API By Example, From Geeks To Geeks.

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


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.EntityBean JavaDoc;
35 import javax.ejb.EntityContext JavaDoc;
36 import javax.ejb.FinderException JavaDoc;
37 import javax.ejb.RemoveException JavaDoc;
38 import javax.ejb.TimedObject JavaDoc;
39 import javax.ejb.Timer JavaDoc;
40 import javax.ejb.TimerService JavaDoc;
41
42 import org.jboss.logging.Logger;
43
44 /**
45  * Entity Bean Timer Test
46  **/

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

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

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

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

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

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

116    public int getCallCount()
117    {
118       Object JavaDoc pk = context.getPrimaryKey();
119       log.info("getCallCount [pk=" + pk + ",count=" + callCount + ",this=" + this + "]");
120       return callCount;
121    }
122
123    /**
124     * @ejb.interface-method view-type="both"
125     **/

126    public List JavaDoc getTimers()
127    {
128       TimerService JavaDoc timerService = context.getTimerService();
129
130       ArrayList JavaDoc handles = new ArrayList JavaDoc();
131       Iterator JavaDoc it = timerService.getTimers().iterator();
132       while (it.hasNext())
133       {
134          Timer JavaDoc timer = (Timer JavaDoc) it.next();
135          handles.add(timer.getHandle().toString());
136       }
137       return handles;
138    }
139
140    /**
141     * @ejb.interface-method view-type="both"
142     **/

143    public Principal JavaDoc getEjbTimeoutCaller()
144    {
145       return ejbTimeoutCaller;
146    }
147
148    public void ejbTimeout(Timer JavaDoc timer)
149    {
150       callCount++;
151       Object JavaDoc pk = context.getPrimaryKey();
152       log.info("ejbTimeout [pk=" + pk + ",count=" + callCount + ",this=" + this + "] timer=" + timer);
153
154       ejbTimeoutCaller = context.getCallerPrincipal();
155
156       if (timer.getInfo() != null)
157       {
158          Properties JavaDoc props = (Properties JavaDoc)timer.getInfo();
159          if ("true".equals(props.getProperty("rollback")))
160          {
161             props.setProperty("rollback", "false");
162             throw new IllegalStateException JavaDoc("rollback on ejbTimeout");
163          }
164       }
165    }
166
167    // -------------------------------------------------------------------------
168
// Framework Callbacks
169
// -------------------------------------------------------------------------
170

171    /**
172     * @ejb.create-method view-type="both"
173     **/

174    public Integer JavaDoc ejbCreate(Integer JavaDoc pk) throws CreateException JavaDoc
175    {
176       log.info("ejbCreate [pk=" + pk + "]");
177       return pk;
178    }
179
180    public void ejbPostCreate(Integer JavaDoc pk) throws CreateException JavaDoc
181    {
182    }
183
184    /**
185     * @ejb.finder view-type="both"
186     **/

187    public Integer JavaDoc ejbFindByPrimaryKey(Integer JavaDoc pk) throws FinderException JavaDoc
188    {
189       return pk;
190    }
191
192    public void ejbActivate() throws EJBException JavaDoc, RemoteException JavaDoc
193    {
194    }
195
196    public void ejbLoad() throws EJBException JavaDoc, RemoteException JavaDoc
197    {
198    }
199
200    public void ejbPassivate() throws EJBException JavaDoc, RemoteException JavaDoc
201    {
202    }
203
204    public void ejbRemove() throws RemoveException JavaDoc, EJBException JavaDoc, RemoteException JavaDoc
205    {
206    }
207
208    public void ejbStore() throws EJBException JavaDoc, RemoteException JavaDoc
209    {
210    }
211
212    public void setEntityContext(EntityContext JavaDoc ctx) throws EJBException JavaDoc, RemoteException JavaDoc
213    {
214       this.context = ctx;
215    }
216
217    public void unsetEntityContext() throws EJBException JavaDoc, RemoteException JavaDoc
218    {
219    }
220 }
221
Popular Tags