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.EntityBean;
035: import javax.ejb.EntityContext;
036: import javax.ejb.FinderException;
037: import javax.ejb.RemoveException;
038: import javax.ejb.TimedObject;
039: import javax.ejb.Timer;
040: import javax.ejb.TimerService;
041:
042: import org.apache.log4j.Logger;
043:
044: /**
045: * Entity Bean Timer Test
046: **/
047: public class TimerEntityBean implements EntityBean, TimedObject {
048: private static Logger log = Logger.getLogger(TimerEntityBean.class);
049:
050: private EntityContext context;
051:
052: // count calls to ejbTimeout
053: private int callCount;
054:
055: // seen from ejbTimeout
056: private Principal ejbTimeoutCaller;
057:
058: /**
059: * @ejb.interface-method view-type="both"
060: **/
061: public void createTimer(long duration, long periode,
062: Serializable info) {
063: TimerService timerService = context.getTimerService();
064: if (periode > 0)
065: timerService.createTimer(duration, periode, info);
066: else
067: timerService.createTimer(duration, info);
068: }
069:
070: /**
071: * @ejb.interface-method view-type="both"
072: **/
073: public void cancelFirstTimer() {
074: TimerService timerService = context.getTimerService();
075: if (timerService.getTimers().isEmpty())
076: throw new EJBException("There are no timers");
077:
078: Timer timer = (Timer) timerService.getTimers().iterator()
079: .next();
080: timer.cancel();
081: }
082:
083: /**
084: * This is not allowed on the remote interface.
085: * @ejb.interface-method view-type="both"
086: **/
087: public Object createTimerReturnHandle(long duration) {
088: TimerService timerService = context.getTimerService();
089: Timer timer = timerService.createTimer(duration, null);
090: return timer.getHandle();
091: }
092:
093: /**
094: * This is not allowed on the remote interface.
095: * @ejb.interface-method view-type="both"
096: **/
097: public String passTimerHandle(Object handle) {
098: return handle.toString();
099: }
100:
101: /**
102: * @ejb.interface-method view-type="both"
103: **/
104: public void resetCallCount() {
105: callCount = 0;
106: }
107:
108: /**
109: * @ejb.interface-method view-type="both"
110: **/
111: public int getCallCount() {
112: Object pk = context.getPrimaryKey();
113: log.info("getCallCount [pk=" + pk + ",count=" + callCount
114: + ",this=" + this + "]");
115: return callCount;
116: }
117:
118: /**
119: * @ejb.interface-method view-type="both"
120: **/
121: public List getTimers() {
122: TimerService timerService = context.getTimerService();
123:
124: ArrayList handles = new ArrayList();
125: Iterator it = timerService.getTimers().iterator();
126: while (it.hasNext()) {
127: Timer timer = (Timer) it.next();
128: handles.add(timer.getHandle().toString());
129: }
130: return handles;
131: }
132:
133: /**
134: * @ejb.interface-method view-type="both"
135: **/
136: public Principal getEjbTimeoutCaller() {
137: return ejbTimeoutCaller;
138: }
139:
140: public void ejbTimeout(Timer timer) {
141: callCount++;
142: Object pk = context.getPrimaryKey();
143: log.info("ejbTimeout [pk=" + pk + ",count=" + callCount
144: + ",this=" + this + "] timer=" + timer);
145:
146: ejbTimeoutCaller = context.getCallerPrincipal();
147:
148: if (timer.getInfo() != null) {
149: Properties props = (Properties) timer.getInfo();
150: if ("true".equals(props.getProperty("rollback"))) {
151: props.setProperty("rollback", "false");
152: throw new IllegalStateException(
153: "rollback on ejbTimeout");
154: }
155: }
156: }
157:
158: // -------------------------------------------------------------------------
159: // Framework Callbacks
160: // -------------------------------------------------------------------------
161:
162: /**
163: * @ejb.create-method view-type="both"
164: **/
165: public Integer ejbCreate(Integer pk) throws CreateException {
166: log.info("ejbCreate [pk=" + pk + "]");
167: return pk;
168: }
169:
170: public void ejbPostCreate(Integer pk) throws CreateException {
171: }
172:
173: /**
174: * @ejb.finder view-type="both"
175: **/
176: public Integer ejbFindByPrimaryKey(Integer pk)
177: throws FinderException {
178: return pk;
179: }
180:
181: public void ejbActivate() throws EJBException, RemoteException {
182: }
183:
184: public void ejbLoad() throws EJBException, RemoteException {
185: }
186:
187: public void ejbPassivate() throws EJBException, RemoteException {
188: }
189:
190: public void ejbRemove() throws RemoveException, EJBException,
191: RemoteException {
192: }
193:
194: public void ejbStore() throws EJBException, RemoteException {
195: }
196:
197: public void setEntityContext(EntityContext ctx)
198: throws EJBException, RemoteException {
199: this .context = ctx;
200: }
201:
202: public void unsetEntityContext() throws EJBException,
203: RemoteException {
204: }
205: }
|