001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.txtimer.ejb;
023:
024: import java.io.Serializable;
025: import java.rmi.RemoteException;
026: import java.security.Principal;
027: import java.util.ArrayList;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Properties;
031:
032: import javax.ejb.CreateException;
033: import javax.ejb.EJBException;
034: import javax.ejb.SessionBean;
035: import javax.ejb.SessionContext;
036: import javax.ejb.TimedObject;
037: import javax.ejb.Timer;
038: import javax.ejb.TimerService;
039:
040: import org.apache.log4j.Logger;
041:
042: /**
043: * Session Bean Timer Test
044: **/
045: public class TimerSessionBean implements SessionBean, TimedObject {
046: private static Logger log = Logger
047: .getLogger(TimerSessionBean.class);
048:
049: private SessionContext context;
050:
051: // count calls to ejbTimeout
052: private int callCount;
053:
054: // count calls to ejbTimeout
055: private static int globalCallCount;
056:
057: // seen from ejbTimeout
058: private Principal ejbTimeoutCaller;
059:
060: /**
061: * @ejb.interface-method view-type="both"
062: **/
063: public void createTimer(long duration, long periode,
064: Serializable info) {
065: TimerService timerService = context.getTimerService();
066: if (periode > 0)
067: timerService.createTimer(duration, periode, info);
068: else
069: timerService.createTimer(duration, info);
070: }
071:
072: /**
073: * @ejb.interface-method view-type="both"
074: **/
075: public void cancelFirstTimer() {
076: TimerService timerService = context.getTimerService();
077: if (timerService.getTimers().isEmpty())
078: throw new EJBException("There are no timers");
079:
080: Timer timer = (Timer) timerService.getTimers().iterator()
081: .next();
082: timer.cancel();
083: }
084:
085: /**
086: * This is not allowed on the remote interface.
087: * @ejb.interface-method view-type="both"
088: **/
089: public Object createTimerReturnHandle(long duration) {
090: TimerService timerService = context.getTimerService();
091: Timer timer = timerService.createTimer(duration, null);
092: return timer.getHandle();
093: }
094:
095: /**
096: * This is not allowed on the remote interface.
097: * @ejb.interface-method view-type="both"
098: **/
099: public String passTimerHandle(Object handle) {
100: return handle.toString();
101: }
102:
103: /**
104: * @ejb.interface-method view-type="both"
105: **/
106: public void resetCallCount() {
107: callCount = 0;
108: globalCallCount = 0;
109: }
110:
111: /**
112: * @ejb.interface-method view-type="both"
113: **/
114: public int getCallCount() {
115: log.info("getCallCount [count=" + callCount + "]");
116: return callCount;
117: }
118:
119: /**
120: * @ejb.interface-method view-type="both"
121: **/
122: public int getGlobalCallCount() {
123: log.info("getGlobalCallCount [count=" + globalCallCount + "]");
124: return globalCallCount;
125: }
126:
127: /**
128: * @ejb.interface-method view-type="both"
129: **/
130: public List getTimers() {
131: TimerService timerService = context.getTimerService();
132:
133: ArrayList handles = new ArrayList();
134: Iterator it = timerService.getTimers().iterator();
135: while (it.hasNext()) {
136: Timer timer = (Timer) it.next();
137: handles.add(timer.getHandle().toString());
138: }
139: return handles;
140: }
141:
142: /**
143: * @ejb.interface-method view-type="both"
144: **/
145: public Principal getEjbTimeoutCaller() {
146: return ejbTimeoutCaller;
147: }
148:
149: public void ejbTimeout(Timer timer) {
150: callCount++;
151: globalCallCount++;
152:
153: log.info("ejbTimeout [count=" + callCount + "] timer=" + timer);
154:
155: ejbTimeoutCaller = context.getCallerPrincipal();
156: log.info("ejbTimeout [callerPrincipal=" + ejbTimeoutCaller
157: + "]");
158:
159: Serializable info = timer.getInfo();
160: log.info("ejbTimeout [info=" + info + "]");
161:
162: if (info != null) {
163: if (info instanceof Properties) {
164: Properties props = (Properties) timer.getInfo();
165: if ("true".equals(props.getProperty("cancel")))
166: timer.cancel();
167: }
168: }
169: }
170:
171: // -------------------------------------------------------------------------
172: // Framework Callbacks
173: // -------------------------------------------------------------------------
174:
175: public void setSessionContext(SessionContext ctx)
176: throws EJBException, RemoteException {
177: this .context = ctx;
178: }
179:
180: /**
181: * @ejb.create-method view-type="both"
182: **/
183: public void ejbCreate() throws CreateException {
184: }
185:
186: public void ejbRemove() throws EJBException, RemoteException {
187: }
188:
189: public void ejbActivate() throws EJBException, RemoteException {
190: }
191:
192: public void ejbPassivate() throws EJBException, RemoteException {
193: }
194: }
|