001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: SimpleSL.java 8456 2006-06-13 08:28:03Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.transacted;
027:
028: import java.rmi.RemoteException;
029: import java.util.Collection;
030: import java.util.Iterator;
031:
032: import javax.ejb.EJBException;
033: import javax.ejb.SessionBean;
034: import javax.ejb.SessionContext;
035: import javax.ejb.TimedObject;
036: import javax.ejb.Timer;
037: import javax.ejb.TimerHandle;
038: import javax.ejb.TimerService;
039: import javax.naming.InitialContext;
040: import javax.naming.NamingException;
041:
042: import org.objectweb.util.monolog.api.BasicLevel;
043:
044: public class SimpleSL extends SimpleCommon implements SessionBean,
045: TimedObject {
046:
047: protected SessionContext sessionContext;
048: static protected int timercount = 0;
049:
050: public void setSessionContext(SessionContext sessionContext) {
051: initLogger();
052: logger.log(BasicLevel.DEBUG, "");
053: this .sessionContext = sessionContext;
054: }
055:
056: public void ejbActivate() {
057: logger.log(BasicLevel.DEBUG, "");
058: }
059:
060: public void ejbPassivate() {
061: logger.log(BasicLevel.DEBUG, "");
062: }
063:
064: public void ejbRemove() {
065: logger.log(BasicLevel.DEBUG, "");
066: }
067:
068: public void ejbCreate() {
069: logger.log(BasicLevel.DEBUG, "");
070: try {
071: InitialContext ictx = new InitialContext();
072: } catch (NamingException e) {
073: throw new EJBException(e);
074: }
075: }
076:
077: public int setTimer(int dur, int period) {
078: logger.log(BasicLevel.DEBUG, "");
079: TimerService timerservice = sessionContext.getTimerService();
080: int ret = dur * 10 + period;
081: if (period > 0) {
082: timerservice.createTimer(dur * 1000, period * 1000,
083: new Integer(ret));
084: } else {
085: timerservice.createTimer(dur * 1000, new Integer(ret));
086: }
087: return ret;
088: }
089:
090: public int setTimer(java.util.Date date, int period) {
091: logger.log(BasicLevel.DEBUG, "");
092: TimerService timerservice = sessionContext.getTimerService();
093: int ret = 200 + period;
094: if (period > 0) {
095: timerservice.createTimer(date, period * 1000, new Integer(
096: ret));
097: } else {
098: timerservice.createTimer(date, new Integer(ret));
099: }
100: return ret;
101: }
102:
103: public int setTimerGetHandle(int dur, int period) {
104: logger.log(BasicLevel.DEBUG, "");
105: TimerService timerservice = sessionContext.getTimerService();
106: int ret = dur * 10 + period;
107: Timer t = null;
108: if (period > 0) {
109: t = timerservice.createTimer(dur * 1000, period * 1000,
110: new Integer(ret));
111: } else {
112: t = timerservice.createTimer(dur * 1000, new Integer(ret));
113: }
114: TimerHandle hdl = t.getHandle();
115: TimerHandle hdl2 = getDeserializedHandle(hdl);
116: if (!timersAreIdentical(hdl, hdl2)) {
117: logger.log(BasicLevel.ERROR, "Bad timer handle");
118: throw new EJBException("Bad timer handle");
119: }
120: return ret;
121: }
122:
123: public TimerHandle getTimerHandle(int ident) {
124: logger.log(BasicLevel.DEBUG, "");
125: TimerHandle hdl = null;
126: TimerService timerservice = sessionContext.getTimerService();
127: Collection timerList = timerservice.getTimers();
128: for (Iterator i = timerList.iterator(); i.hasNext();) {
129: Timer t = (Timer) i.next();
130: Integer id = (Integer) t.getInfo();
131: if (id.intValue() == ident) {
132: hdl = t.getHandle();
133: TimerHandle hdl2 = getDeserializedHandle(hdl);
134: if (!timersAreIdentical(hdl, hdl2)) {
135: logger.log(BasicLevel.ERROR, "Bad timer handle");
136: throw new EJBException("Bad timer handle");
137: }
138: break;
139: }
140: }
141: return hdl;
142: }
143:
144: public void cancelTimer(int ident) {
145: logger.log(BasicLevel.DEBUG, "");
146: TimerService timerservice = sessionContext.getTimerService();
147: Collection timerList = timerservice.getTimers();
148: for (Iterator i = timerList.iterator(); i.hasNext();) {
149: Timer t = (Timer) i.next();
150: Integer id = (Integer) t.getInfo();
151: if (id.intValue() == ident) {
152: t.cancel();
153: }
154: }
155: }
156:
157: public void cancelTimers() {
158: logger.log(BasicLevel.DEBUG, "");
159: TimerService timerservice = sessionContext.getTimerService();
160: Collection timerList = timerservice.getTimers();
161: for (Iterator i = timerList.iterator(); i.hasNext();) {
162: Timer t = (Timer) i.next();
163: t.cancel();
164: }
165: }
166:
167: public long getTimeRemaining(int ident) {
168: logger.log(BasicLevel.DEBUG, "");
169: TimerService timerservice = sessionContext.getTimerService();
170: Collection timerList = timerservice.getTimers();
171: long ret = -1;
172: for (Iterator i = timerList.iterator(); i.hasNext();) {
173: Timer t = (Timer) i.next();
174: Integer id = (Integer) t.getInfo();
175: if (id.intValue() == ident) {
176: ret = t.getTimeRemaining();
177: }
178: }
179: return ret;
180: }
181:
182: public int getTimerNumber() {
183: logger.log(BasicLevel.DEBUG, "");
184: TimerService timerservice = sessionContext.getTimerService();
185: Collection timerList = timerservice.getTimers();
186: return timerList.size();
187: }
188:
189: public int getTimerCount() {
190: logger.log(BasicLevel.DEBUG, "");
191: return timercount;
192: }
193:
194: /**
195: * This support method calls a required method
196: */
197: public boolean supports_call_required() throws RemoteException {
198: logger.log(BasicLevel.DEBUG, "");
199: Simple mysession = (Simple) sessionContext.getEJBObject();
200: return mysession.opwith_required();
201: }
202:
203: public void startInfoTimer(int dur, String info)
204: throws RemoteException {
205: logger.log(BasicLevel.ERROR, "not implemented");
206: }
207:
208: // -----------------------------------------------------------
209: // TimedObject implementation
210: // -----------------------------------------------------------
211:
212: /**
213: * A timer is expired.
214: */
215: public void ejbTimeout(Timer timer) {
216: logger.log(BasicLevel.DEBUG, "");
217: timercount++;
218: TimerService timerservice = sessionContext.getTimerService();
219: Collection timerList = timerservice.getTimers();
220: TimerHandle hdl = timer.getHandle();
221: TimerHandle hdl2 = getDeserializedHandle(hdl);
222: if (!timersAreIdentical(hdl, hdl2)) {
223: logger.log(BasicLevel.ERROR, "Bad timer handle");
224: throw new EJBException("Bad timer handle");
225: }
226: }
227: }
|