1 25 26 package org.objectweb.jonas_ejb.container; 27 28 import java.io.Serializable ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Date ; 32 import java.util.Iterator ; 33 34 import javax.ejb.EJBException ; 35 import javax.ejb.Timer ; 36 import javax.ejb.TimerService ; 37 import javax.ejb.TransactionRolledbackLocalException ; 38 import javax.transaction.TransactionManager ; 39 40 import org.objectweb.jonas.jtm.TransactionService; 41 import org.objectweb.jonas.service.ServiceException; 42 import org.objectweb.jonas.service.ServiceManager; 43 import org.objectweb.jonas_timer.TraceTimer; 44 import org.objectweb.util.monolog.api.BasicLevel; 45 46 52 public class JTimerService implements TimerService { 53 54 private ArrayList mytimers = new ArrayList (); 55 56 private JFactory bf = null; 57 58 private JEntitySwitch es = null; 59 60 private TransactionManager tm; 61 62 65 public JTimerService(JFactory bf) { 66 this.bf = bf; 67 try { 69 TransactionService ts = (TransactionService) ServiceManager.getInstance().getTransactionService(); 70 tm = ts.getTransactionManager(); 71 } catch (Exception e) { 72 throw new EJBException ("Error when starting the Timer service ", e); 73 } 74 75 } 76 77 80 public JTimerService(JEntitySwitch es) { 81 this.es = es; 82 try { 84 TransactionService ts = (TransactionService) ServiceManager.getInstance().getTransactionService(); 85 tm = ts.getTransactionManager(); 86 } catch (Exception e) { 87 throw new EJBException ("Error when starting the Timer service ", e); 88 } 89 90 } 91 92 95 public TransactionManager getTransactionManager() { 96 return tm; 97 } 98 99 103 public void notify(Timer timer) { 104 if (es != null) { 105 es.notifyTimeout(timer); 107 } else { 108 if (bf instanceof JMdbFactory) { 109 ((JMdbFactory) bf).notifyTimeout(timer); 110 } else if (bf instanceof JMdbEndpointFactory) { 111 ((JMdbEndpointFactory) bf).notifyTimeout(timer); 112 } else if (bf instanceof JStatelessFactory) { 113 ((JStatelessFactory) bf).notifyTimeout(timer); 114 } else { 115 TraceEjb.logger.log(BasicLevel.ERROR, "Cannot notify this type of bean"); 116 } 117 } 118 } 119 120 124 public void remove(Timer timer) { 125 synchronized (this) { 126 int index = mytimers.lastIndexOf(timer); 127 if (index == -1) { 128 TraceTimer.logger.log(BasicLevel.WARN, "try to remove unexisting timer"); 129 } else { 130 mytimers.remove(index); 131 } 132 } 133 } 134 135 138 public void cancelAllTimers() { 139 synchronized (this) { 140 es = null; 141 for (Iterator i = mytimers.iterator(); i.hasNext(); ) { 142 JTimer timer = (JTimer) i.next(); 143 timer.stopTimer(); 144 } 145 mytimers.clear(); 146 } 147 } 148 149 152 public Timer getTimer(long initialDuration, long intervalDuration, Serializable info) { 153 Timer dummy = new JTimer(this, initialDuration, intervalDuration, info); 154 synchronized (this) { 155 for (Iterator i = mytimers.iterator(); i.hasNext(); ) { 156 JTimer timer = (JTimer) i.next(); 157 if (timer.equals(dummy)) { 158 return timer; 159 } 160 } 161 } 162 return null; 163 } 164 165 169 187 public Timer createTimer(Date initialExpiration, long intervalDuration, Serializable info) 188 throws IllegalArgumentException , IllegalStateException , EJBException { 189 if (initialExpiration == null) { 190 throw new IllegalArgumentException ("expiration date is null"); 191 } 192 long initialDuration = initialExpiration.getTime() - System.currentTimeMillis() + 20; 194 return createTimer(initialDuration, intervalDuration, info); 195 } 196 197 211 public Timer createTimer(Date expiration, Serializable info) throws IllegalArgumentException , 212 IllegalStateException , EJBException { 213 return createTimer(expiration, 0, info); 214 } 215 216 234 public Timer createTimer(long initialDuration, long intervalDuration, Serializable info) 235 throws IllegalArgumentException , IllegalStateException , EJBException { 236 237 if (es != null) { 239 es.getPrimaryKey(); } 241 242 if (initialDuration < 0 || intervalDuration < 0) { 244 throw new IllegalArgumentException ("duration is negative"); 245 } 246 247 JTimer timer = new JTimer(this, initialDuration, intervalDuration, info); 248 synchronized (this) { 249 if (mytimers.add(timer)) { 250 timer.startTimer(); 251 } else { 252 TraceTimer.logger.log(BasicLevel.WARN, "create a timer already known"); 253 } 254 } 255 return timer; 256 } 257 258 259 273 public Timer createTimer(long duration, Serializable info) throws IllegalArgumentException , IllegalStateException , 274 EJBException { 275 return createTimer(duration, 0, info); 276 } 277 278 286 public Collection getTimers() throws IllegalStateException , EJBException { 287 if (es != null) { 289 es.getPrimaryKey(); } 291 ArrayList ret = new ArrayList (); 292 for (Iterator i = mytimers.iterator(); i.hasNext(); ) { 293 JTimer t = (JTimer) i.next(); 294 if (! t.isCancelled()) { 295 ret.add(t); 296 } 297 } 298 return ret; 299 } 300 301 304 public String getEjbName() { 305 if (bf != null) { 306 return bf.getEJBName(); 307 } 308 if (es != null) { 309 return es.getBeanFactory().getEJBName(); 310 } 311 return null; 312 } 313 314 317 public Serializable getPK() { 318 if (es != null) { 319 JEntityFactory ef = (JEntityFactory) es.getBeanFactory(); 320 Serializable pks = (Serializable ) es.getPrimaryKey(); 321 Serializable pk = (Serializable ) ef.encodePK(pks); 322 if (TraceEjb.isDebugIc()) { 323 TraceEjb.interp.log(BasicLevel.DEBUG, "pk = " + pks); 324 TraceEjb.interp.log(BasicLevel.DEBUG, "encoded pk = " + pk); 325 } 326 return pk; 327 } else { 328 return null; 329 } 330 } 331 332 335 public String getContainer() { 336 JFactory f = bf; 337 if (es != null) { 338 f = es.getBeanFactory(); 339 } 340 JContainer cont = f.getContainer(); 341 return cont.getExternalFileName(); 342 } 343 } 344 | Popular Tags |