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: JTimerHandle.java 8307 2006-04-27 13:36:00Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.container;
025:
026: import java.io.Serializable;
027:
028: import javax.ejb.EJBException;
029: import javax.ejb.NoSuchObjectLocalException;
030: import javax.ejb.Timer;
031: import javax.ejb.TimerHandle;
032: import javax.naming.Context;
033: import javax.naming.InitialContext;
034: import javax.naming.NamingException;
035:
036: import org.objectweb.jonas.naming.NamingManager;
037: import org.objectweb.jonas_ejb.container.JContext;
038: import org.objectweb.jonas_lib.naming.ContainerNaming;
039: import org.objectweb.jonas.container.EJBServiceImpl;
040: import org.objectweb.jonas.service.ServiceManager;
041: import org.objectweb.jonas_timer.TraceTimer;
042: import org.objectweb.util.monolog.api.BasicLevel;
043:
044: /**
045: * TimerHandle implementation
046: */
047: public class JTimerHandle implements TimerHandle {
048:
049: private long period;
050: private long starttime;
051: private long duration;
052: private Serializable info;
053: private String beanname;
054: private String container;
055:
056: /**
057: * Encoded PK. If CMP2 is not used, no encoding is done.
058: */
059: private Serializable pk;
060:
061: /**
062: * Constructor
063: */
064: public JTimerHandle(long starttime, long duration, long period,
065: Serializable info, String beanname, String container,
066: Serializable pk) {
067: if (TraceTimer.isDebug()) {
068: TraceTimer.logger.log(BasicLevel.DEBUG,
069: "New JTimerHandle start = " + starttime
070: + ", initial = " + duration + ", period = "
071: + period);
072: }
073: this .starttime = starttime;
074: this .duration = duration;
075: this .period = period;
076: this .info = info;
077: this .beanname = beanname;
078: this .container = container;
079: this .pk = pk;
080: }
081:
082: /**
083: * @return a reference to the timer represented by this handle.
084: * @throws java.lang.IllegalStateException If this method is invoked while the instance
085: * is in a state that does not allow access to this method.
086: * @throws NoSuchObjectLocalException If invoked on a handle whose associated timer
087: * has expired or has been cancelled.
088: * @throws EJBException If this method could not complete due to a system-level failure.
089: */
090: public Timer getTimer() throws IllegalStateException,
091: NoSuchObjectLocalException, EJBException {
092:
093: // Must reject Timer operations according to the EJB spec
094: // See EJB 2.1 page 87 (Stateful session beans)
095: try {
096: ContainerNaming naming = NamingManager.getInstance();
097: Context ctx = naming.getComponentContext();
098: JContext sc = (JContext) ctx.lookup("MY_SF_CONTEXT");
099: if (sc.getState() != 2) {
100: TraceTimer.logger.log(BasicLevel.DEBUG,
101: "This operation is not allowed here");
102: throw new IllegalStateException(
103: "This operation is not allowed here");
104: }
105: } catch (NamingException ne) {
106: // No context registered. Assume it's OK.
107: // It's the case for Entity or MDB.
108: }
109:
110: EJBServiceImpl ejbserv = null;
111: try {
112: ejbserv = (EJBServiceImpl) ServiceManager.getInstance()
113: .getEjbService();
114: } catch (Exception e) {
115: TraceTimer.logger.log(BasicLevel.DEBUG,
116: "Cannot use Timer outside jonas server");
117: throw new IllegalStateException(
118: "Cannot use Timer outside jonas server");
119: }
120: JContainer cont = (JContainer) ejbserv.getContainer(container);
121: if (cont == null) {
122: TraceTimer.logger.log(BasicLevel.ERROR,
123: "Cannot get container =" + container);
124: throw new IllegalStateException("Cannot get container");
125: }
126: JFactory bf = (JFactory) cont.getBeanFactory(beanname);
127: JTimerService timerservice = null;
128: if (bf instanceof JEntityFactory) {
129: // entity bean
130: JEntityFactory ef = (JEntityFactory) bf;
131: Serializable pks = ef.decodePK(pk);
132: if (TraceTimer.isDebug()) {
133: TraceTimer.logger.log(BasicLevel.DEBUG, "encoded PK="
134: + pk);
135: TraceTimer.logger.log(BasicLevel.DEBUG, "decoded PK="
136: + pks);
137: }
138: JEntitySwitch es = ef.getEJB(pks);
139: if (es == null) {
140: TraceTimer.logger.log(BasicLevel.DEBUG,
141: "No entity for this pk");
142: throw new NoSuchObjectLocalException(
143: "No entity for this pk");
144: }
145: timerservice = (JTimerService) es.getEntityTimerService();
146: } else {
147: // stateless session or message driven bean
148: timerservice = (JTimerService) bf.getTimerService();
149: }
150: if (timerservice == null) {
151: TraceTimer.logger.log(BasicLevel.DEBUG,
152: "Cannot retrieve TimerService");
153: throw new IllegalStateException(
154: "Cannot retrieve TimerService");
155: }
156: // Get the Timer from the list.
157: Timer ret = timerservice.getTimerByTime(duration, period, info);
158: if (ret == null) {
159: // The Timer should have been canceled.
160: throw new NoSuchObjectLocalException(
161: "The Timer should have been canceled");
162: }
163: return ret;
164: }
165:
166: /**
167: * Restart a Timer read from disk (persistent timer)
168: * @return
169: * @throws IllegalStateException
170: * @throws NoSuchObjectLocalException
171: * @throws EJBException
172: */
173: public Timer restartTimer() throws IllegalStateException,
174: NoSuchObjectLocalException, EJBException {
175: try {
176: ContainerNaming naming = NamingManager.getInstance();
177: Context ctx = naming.getComponentContext();
178: JContext sc = (JContext) ctx.lookup("MY_SF_CONTEXT");
179: if (sc.getState() != 2) {
180: TraceTimer.logger.log(BasicLevel.DEBUG,
181: "This operation is not allowed here");
182: throw new IllegalStateException(
183: "This operation is not allowed here");
184: }
185: } catch (NamingException ne) {
186: // No context registered. Assume it's OK.
187: // It's the case for Entity or MDB.
188: }
189:
190: EJBServiceImpl ejbserv = null;
191: try {
192: ejbserv = (EJBServiceImpl) ServiceManager.getInstance()
193: .getEjbService();
194: } catch (Exception e) {
195: TraceTimer.logger.log(BasicLevel.DEBUG,
196: "Cannot use Timer outside jonas server");
197: throw new IllegalStateException(
198: "Cannot use Timer outside jonas server");
199: }
200: JContainer cont = (JContainer) ejbserv.getContainer(container);
201: if (cont == null) {
202: TraceTimer.logger.log(BasicLevel.ERROR,
203: "Cannot get container =" + container);
204: throw new IllegalStateException("Cannot get container");
205: }
206: JFactory bf = (JFactory) cont.getBeanFactory(beanname);
207: JTimerService timerservice = null;
208: if (bf instanceof JEntityFactory) {
209: // entity bean
210: JEntityFactory ef = (JEntityFactory) bf;
211: Serializable pks = ef.decodePK(pk);
212: if (TraceEjb.isDebugIc()) {
213: TraceEjb.interp.log(BasicLevel.DEBUG, "encoded PK="
214: + pk);
215: TraceEjb.interp.log(BasicLevel.DEBUG, "decoded PK="
216: + pks);
217: }
218: JEntitySwitch es = ef.getEJB(pks);
219: if (es == null) {
220: TraceTimer.logger.log(BasicLevel.DEBUG,
221: "No entity for this pk");
222: throw new NoSuchObjectLocalException(
223: "No entity for this pk");
224: }
225: timerservice = (JTimerService) es.getEntityTimerService();
226: } else {
227: // stateless session or message driven bean
228: timerservice = (JTimerService) bf.getTimerService();
229: }
230: if (timerservice == null) {
231: TraceTimer.logger.log(BasicLevel.DEBUG,
232: "Cannot retrieve TimerService");
233: throw new IllegalStateException(
234: "Cannot retrieve TimerService");
235: }
236: // Recreate a Timer with recomputed initial duration.
237: duration += starttime - System.currentTimeMillis();
238: if (duration < 100) {
239: duration = 100;
240: }
241: Timer timer = timerservice.createTimer(duration, period, info);
242: if (timer.getTimeRemaining() > 0) {
243: TraceTimer.logger.log(BasicLevel.DEBUG, "timer restarted");
244: } else {
245: TraceTimer.logger.log(BasicLevel.DEBUG, "timer terminated");
246: timer.cancel();
247: }
248: return timer;
249: }
250: }
|