00001
00026 package org.objectweb.jonas_ejb.container;
00027
00028 import java.io.Serializable;
00029 import java.util.ArrayList;
00030 import java.util.Collection;
00031 import java.util.Date;
00032 import java.util.Iterator;
00033
00034 import javax.ejb.EJBException;
00035 import javax.ejb.Timer;
00036 import javax.ejb.TimerService;
00037 import javax.ejb.TransactionRolledbackLocalException;
00038 import javax.transaction.TransactionManager;
00039
00040 import org.objectweb.jonas.jtm.TransactionService;
00041 import org.objectweb.jonas.service.ServiceException;
00042 import org.objectweb.jonas.service.ServiceManager;
00043 import org.objectweb.jonas_timer.TraceTimer;
00044 import org.objectweb.util.monolog.api.BasicLevel;
00045
00052 public class JTimerService implements TimerService {
00053
00054 private ArrayList mytimers = new ArrayList();
00055
00056 private JFactory bf = null;
00057
00058 private JEntitySwitch es = null;
00059
00060 private TransactionManager tm;
00061
00065 public JTimerService(JFactory bf) {
00066 this.bf = bf;
00067
00068 try {
00069 TransactionService ts = (TransactionService) ServiceManager.getInstance().getTransactionService();
00070 tm = ts.getTransactionManager();
00071 } catch (Exception e) {
00072 throw new EJBException("Error when starting the Timer service ", e);
00073 }
00074
00075 }
00076
00080 public JTimerService(JEntitySwitch es) {
00081 this.es = es;
00082
00083 try {
00084 TransactionService ts = (TransactionService) ServiceManager.getInstance().getTransactionService();
00085 tm = ts.getTransactionManager();
00086 } catch (Exception e) {
00087 throw new EJBException("Error when starting the Timer service ", e);
00088 }
00089
00090 }
00091
00095 public TransactionManager getTransactionManager() {
00096 return tm;
00097 }
00098
00103 public void notify(Timer timer) {
00104 if (es != null) {
00105
00106 es.notifyTimeout(timer);
00107 } else {
00108 if (bf instanceof JMdbFactory) {
00109 ((JMdbFactory) bf).notifyTimeout(timer);
00110 } else if (bf instanceof JMdbEndpointFactory) {
00111 ((JMdbEndpointFactory) bf).notifyTimeout(timer);
00112 } else if (bf instanceof JStatelessFactory) {
00113 ((JStatelessFactory) bf).notifyTimeout(timer);
00114 } else {
00115 TraceEjb.logger.log(BasicLevel.ERROR, "Cannot notify this type of bean");
00116 }
00117 }
00118 }
00119
00124 public void remove(Timer timer) {
00125 synchronized (this) {
00126 int index = mytimers.lastIndexOf(timer);
00127 if (index == -1) {
00128 TraceTimer.logger.log(BasicLevel.WARN, "try to remove unexisting timer");
00129 } else {
00130 mytimers.remove(index);
00131 }
00132 }
00133 }
00134
00138 public void cancelAllTimers() {
00139 synchronized (this) {
00140 es = null;
00141 for (Iterator i = mytimers.iterator(); i.hasNext(); ) {
00142 JTimer timer = (JTimer) i.next();
00143 timer.stopTimer();
00144 }
00145 mytimers.clear();
00146 }
00147 }
00148
00152 public Timer getTimer(long initialDuration, long intervalDuration, Serializable info) {
00153 Timer dummy = new JTimer(this, initialDuration, intervalDuration, info);
00154 synchronized (this) {
00155 for (Iterator i = mytimers.iterator(); i.hasNext(); ) {
00156 JTimer timer = (JTimer) i.next();
00157 if (timer.equals(dummy)) {
00158 return timer;
00159 }
00160 }
00161 }
00162 return null;
00163 }
00164
00165
00166
00167
00168
00187 public Timer createTimer(Date initialExpiration, long intervalDuration, Serializable info)
00188 throws IllegalArgumentException, IllegalStateException, EJBException {
00189 if (initialExpiration == null) {
00190 throw new IllegalArgumentException("expiration date is null");
00191 }
00192
00193 long initialDuration = initialExpiration.getTime() - System.currentTimeMillis() + 20;
00194 return createTimer(initialDuration, intervalDuration, info);
00195 }
00196
00211 public Timer createTimer(Date expiration, Serializable info) throws IllegalArgumentException,
00212 IllegalStateException, EJBException {
00213 return createTimer(expiration, 0, info);
00214 }
00215
00234 public Timer createTimer(long initialDuration, long intervalDuration, Serializable info)
00235 throws IllegalArgumentException, IllegalStateException, EJBException {
00236
00237
00238 if (es != null) {
00239 es.getPrimaryKey();
00240 }
00241
00242
00243 if (initialDuration < 0 || intervalDuration < 0) {
00244 throw new IllegalArgumentException("duration is negative");
00245 }
00246
00247 JTimer timer = new JTimer(this, initialDuration, intervalDuration, info);
00248 synchronized (this) {
00249 if (mytimers.add(timer)) {
00250 timer.startTimer();
00251 } else {
00252 TraceTimer.logger.log(BasicLevel.WARN, "create a timer already known");
00253 }
00254 }
00255 return timer;
00256 }
00257
00258
00273 public Timer createTimer(long duration, Serializable info) throws IllegalArgumentException, IllegalStateException,
00274 EJBException {
00275 return createTimer(duration, 0, info);
00276 }
00277
00286 public Collection getTimers() throws IllegalStateException, EJBException {
00287
00288 if (es != null) {
00289 es.getPrimaryKey();
00290 }
00291 return (ArrayList) mytimers.clone();
00292 }
00293
00297 public String getEjbName() {
00298 if (bf != null) {
00299 return bf.getEJBName();
00300 }
00301 if (es != null) {
00302 return es.getBeanFactory().getEJBName();
00303 }
00304 return null;
00305 }
00306
00310 public Serializable getPK() {
00311 if (es != null) {
00312 JEntityFactory ef = (JEntityFactory) es.getBeanFactory();
00313 Serializable pks = (Serializable) es.getPrimaryKey();
00314 Serializable pk = (Serializable) ef.encodePK(pks);
00315 TraceEjb.interp.log(BasicLevel.DEBUG, "pk = " + pks);
00316 TraceEjb.interp.log(BasicLevel.DEBUG, "encoded pk = " + pk);
00317 return pk;
00318 } else {
00319 return null;
00320 }
00321 }
00322
00326 public String getContainer() {
00327 JFactory f = bf;
00328 if (es != null) {
00329 f = es.getBeanFactory();
00330 }
00331 JContainer cont = f.getContainer();
00332 return cont.getExternalFileName();
00333 }
00334 }