1 25 26 package org.objectweb.jonas_ejb.container; 27 28 import java.io.Serializable ; 29 import java.util.Date ; 30 31 import javax.ejb.EJBException ; 32 import javax.ejb.NoSuchObjectLocalException ; 33 import javax.ejb.Timer ; 34 import javax.ejb.TimerHandle ; 35 import javax.transaction.RollbackException ; 36 import javax.transaction.Status ; 37 import javax.transaction.Synchronization ; 38 import javax.transaction.SystemException ; 39 import javax.transaction.Transaction ; 40 import javax.transaction.TransactionManager ; 41 42 import org.objectweb.jonas_timer.TimerEvent; 43 import org.objectweb.jonas_timer.TimerEventListener; 44 import org.objectweb.jonas_timer.TimerManager; 45 import org.objectweb.jonas_timer.TraceTimer; 46 import org.objectweb.util.monolog.api.BasicLevel; 47 48 54 public class JTimer implements Timer , TimerEventListener, Synchronization { 55 56 59 private boolean oneshot; 60 61 64 private long period; 65 66 69 private long initial; 70 71 74 private JTimerService timerservice; 75 76 79 private Serializable info; 80 81 84 private TimerManager tim = TimerManager.getInstance(); 85 86 89 private TimerEvent te1 = null; 90 91 94 private TimerEvent te2 = null; 95 96 99 private long starttime; 100 101 104 private long endtime; 105 106 109 private boolean createdInTx = false; 110 111 114 private boolean cancelledInTx = false; 115 116 119 private boolean cancelled = false; 120 121 124 private TimerHandle myHandle = null; 125 126 129 public JTimer(JTimerService timerservice, long initial, long period, Serializable info) { 130 if (TraceTimer.isDebug()) { 131 TraceTimer.logger.log(BasicLevel.DEBUG, "New JTimer initial = " + initial + ", period = " + period); 132 } 133 this.timerservice = timerservice; 134 this.info = info; 135 this.period = period; 136 this.initial = initial; 137 oneshot = (period == 0); 138 139 } 140 141 144 public long getStartTime() { 145 return starttime; 146 } 147 148 151 public long getInitial() { 152 return initial; 153 } 154 155 158 public long getPeriod() { 159 return period; 160 } 161 162 165 public JTimerService getTimerService() { 166 return timerservice; 167 } 168 169 172 public void startTimer() { 173 174 TransactionManager tm = timerservice.getTransactionManager(); 176 try { 177 Transaction tx = tm.getTransaction(); 178 if (tx != null) { 179 tx.registerSynchronization(this); 180 createdInTx = true; 181 } 182 } catch (SystemException e) { 183 TraceTimer.logger.log(BasicLevel.ERROR, "Cannot get Transaction", e); 184 } catch (IllegalStateException e) { 185 TraceTimer.logger.log(BasicLevel.ERROR, "Cannot register synchronization:", e); 186 } catch (RollbackException e) { 187 TraceTimer.logger.log(BasicLevel.ERROR, "transaction already rolled back", e); 188 } 189 190 starttime = System.currentTimeMillis(); 192 endtime = starttime + initial; 193 194 te1 = tim.addTimerMs(this, initial, null, false); 196 } 197 198 201 public void stopTimer() { 202 if (TraceTimer.isDebug()) { 203 TraceTimer.logger.log(BasicLevel.DEBUG, "Stop JTimer"); 204 } 205 cancelled = true; 206 if (te1 != null) { 207 te1.unset(); 208 te1 = null; 209 } 210 if (te2 != null) { 211 te2.unset(); 212 te2 = null; 213 } 214 } 215 216 219 public boolean isCancelled() { 220 return (cancelled || cancelledInTx); 221 } 222 223 227 233 public boolean equals(Object obj) { 234 if (obj instanceof JTimer) { 235 JTimer timer2 = (JTimer) obj; 236 if (timer2.getInitial() != getInitial()) { 237 if (TraceTimer.isDebug()) { 238 TraceTimer.logger.log(BasicLevel.DEBUG, "different initial duration"); 239 } 240 return false; 241 } 242 if (timer2.getPeriod() != getPeriod()) { 243 if (TraceTimer.isDebug()) { 244 TraceTimer.logger.log(BasicLevel.DEBUG, "different period"); 245 } 246 return false; 247 } 248 if (timer2.getTimerService() != getTimerService()) { 249 if (TraceTimer.isDebug()) { 250 TraceTimer.logger.log(BasicLevel.DEBUG, "different timerservice"); 251 } 252 return false; 253 } 254 if (TraceTimer.isDebug()) { 255 TraceTimer.logger.log(BasicLevel.DEBUG, "timers are equal"); 256 } 257 return true; 258 } else { 259 if (TraceTimer.isDebug()) { 260 TraceTimer.logger.log(BasicLevel.DEBUG, "not a Timer"); 261 } 262 return false; 263 } 264 } 265 266 270 public int hashCode() { 271 return (int) (getPeriod() / 1000); 272 } 273 274 278 281 public void timeoutExpired(Object arg) { 282 if (TraceTimer.isDebug()) { 283 TraceTimer.logger.log(BasicLevel.DEBUG, "JTimer expires"); 284 } 285 timerservice.notify(this); 287 if (cancelled) { 288 if (TraceTimer.isDebug()) { 289 TraceTimer.logger.log(BasicLevel.DEBUG, "JTimer cancelled during timeout"); 290 } 291 return; 292 } 293 if (te2 == null) { 294 te1 = null; 295 if (oneshot) { 296 timerservice.remove(this); 297 cancelled = true; 298 } else { 299 if (TraceTimer.isDebug()) { 301 TraceTimer.logger.log(BasicLevel.DEBUG, "Start periodic jonas timer"); 302 } 303 endtime = System.currentTimeMillis() + period; 304 te2 = tim.addTimerMs(this, period, null, true); 305 } 306 } else { 307 endtime = System.currentTimeMillis() + period; 309 } 310 } 311 312 316 326 public void cancel() throws IllegalStateException , NoSuchObjectLocalException , EJBException { 327 if (cancelled || cancelledInTx) { 328 TraceTimer.logger.log(BasicLevel.DEBUG, "Timer cancelled"); 329 throw new NoSuchObjectLocalException ("Timer already cancelled"); 330 } 331 if (TraceTimer.isDebug()) { 332 TraceTimer.logger.log(BasicLevel.DEBUG, ""); 333 } 334 335 TransactionManager tm = timerservice.getTransactionManager(); 337 try { 338 Transaction tx = tm.getTransaction(); 339 if (tx != null) { 340 tx.registerSynchronization(this); 342 cancelledInTx = true; 343 } else { 344 doCancel(); 345 } 346 } catch (SystemException e) { 347 TraceTimer.logger.log(BasicLevel.ERROR, "Cannot get Transaction", e); 348 } catch (IllegalStateException e) { 349 TraceTimer.logger.log(BasicLevel.ERROR, "Cannot register synchronization:", e); 350 } catch (RollbackException e) { 351 TraceTimer.logger.log(BasicLevel.ERROR, "transaction already rolled back", e); 352 } 353 } 354 355 private void doCancel() { 356 stopTimer(); 357 timerservice.remove(this); 358 } 359 360 372 public long getTimeRemaining() throws IllegalStateException , NoSuchObjectLocalException , EJBException { 373 if (cancelled || cancelledInTx) { 374 TraceTimer.logger.log(BasicLevel.DEBUG, "Timer cancelled"); 375 throw new NoSuchObjectLocalException ("Timer cancelled or expired"); 376 } 377 if (TraceTimer.isDebug()) { 378 TraceTimer.logger.log(BasicLevel.DEBUG, ""); 379 } 380 return endtime - System.currentTimeMillis(); 381 } 382 383 395 public Date getNextTimeout() throws IllegalStateException , NoSuchObjectLocalException , EJBException { 396 if (cancelled || cancelledInTx) { 397 TraceTimer.logger.log(BasicLevel.DEBUG, "Timer cancelled"); 398 throw new NoSuchObjectLocalException ("Timer cancelled or expired"); 399 } 400 if (TraceTimer.isDebug()) { 401 TraceTimer.logger.log(BasicLevel.DEBUG, ""); 402 } 403 return new Date (endtime); 404 } 405 406 417 public Serializable getInfo() throws IllegalStateException , NoSuchObjectLocalException , EJBException { 418 if (cancelled || cancelledInTx) { 419 TraceTimer.logger.log(BasicLevel.DEBUG, "Timer cancelled"); 420 throw new NoSuchObjectLocalException ("Timer cancelled or expired"); 421 } 422 if (TraceTimer.isDebug()) { 423 TraceTimer.logger.log(BasicLevel.DEBUG, ""); 424 } 425 return info; 426 } 427 428 439 public TimerHandle getHandle() throws IllegalStateException , NoSuchObjectLocalException , EJBException { 440 if (cancelled || cancelledInTx) { 441 TraceTimer.logger.log(BasicLevel.DEBUG, "Timer cancelled"); 442 throw new NoSuchObjectLocalException ("Timer cancelled or expired"); 443 } 444 if (TraceTimer.isDebug()) { 445 TraceTimer.logger.log(BasicLevel.DEBUG, ""); 446 } 447 if (myHandle == null) { 448 myHandle = new JTimerHandle(initial, period, info, timerservice.getEjbName(), 449 timerservice.getContainer(), timerservice.getPK()); 450 } 451 return myHandle; 452 } 453 454 458 464 public void afterCompletion(int status) { 465 if (TraceEjb.isDebugTx()) { 466 TraceEjb.tx.log(BasicLevel.DEBUG, ""); 467 } 468 469 if (createdInTx) { 471 if (status != Status.STATUS_COMMITTED && !cancelled) { 472 doCancel(); 473 } 474 createdInTx = false; 475 } 476 477 if (cancelledInTx) { 479 if (status == Status.STATUS_COMMITTED && !cancelled) { 480 doCancel(); 481 } 482 cancelledInTx = false; 483 } 484 } 485 486 489 public void beforeCompletion() { 490 } 492 493 } 494 | Popular Tags |