| 1 7 8 package javax.management.timer; 9 10 11 12 import java.util.Date ; 15 import java.util.Enumeration ; 16 import java.util.Hashtable ; 17 import java.util.Iterator ; 18 import java.util.Set ; 19 import java.util.TreeSet ; 20 import java.util.Vector ; 21 22 import javax.management.MBeanNotificationInfo ; 25 import javax.management.MBeanRegistration ; 26 import javax.management.MBeanServer ; 27 import javax.management.NotificationBroadcasterSupport ; 28 import javax.management.ObjectName ; 29 import javax.management.InstanceNotFoundException ; 30 31 import com.sun.jmx.trace.Trace; 32 33 71 public class Timer extends NotificationBroadcasterSupport  72 implements TimerMBean , MBeanRegistration { 73 74 75 80 81 85 public static final long ONE_SECOND = 1000; 86 87 91 public static final long ONE_MINUTE = 60*ONE_SECOND; 92 93 97 public static final long ONE_HOUR = 60*ONE_MINUTE; 98 99 103 public static final long ONE_DAY = 24*ONE_HOUR; 104 105 109 public static final long ONE_WEEK = 7*ONE_DAY; 110 111 112 115 boolean isTraceOn() { 116 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_TIMER); 117 } 118 119 void trace(String clz, String func, String info) { 120 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_TIMER, clz, func, info); 121 } 122 123 void trace(String func, String info) { 124 trace(dbgTag, func, info); 125 } 126 127 boolean isDebugOn() { 128 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_TIMER); 129 } 130 131 void debug(String clz, String func, String info) { 132 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_TIMER, clz, func, info); 133 } 134 135 void debug(String func, String info) { 136 debug(dbgTag, func, info); 137 } 138 139 140 145 146 private static final String dbgTag = "Timer"; 147 148 152 private Hashtable timerTable = new Hashtable (); 153 154 159 private boolean sendPastNotifications = false; 160 161 165 private transient boolean isActive = false; 166 167 171 private transient long sequenceNumber = 0; 172 173 private static final int TIMER_NOTIF_INDEX = 0; 176 private static final int TIMER_DATE_INDEX = 1; 177 private static final int TIMER_PERIOD_INDEX = 2; 178 private static final int TIMER_NB_OCCUR_INDEX = 3; 179 private static final int ALARM_CLOCK_INDEX = 4; 180 private static final int FIXED_RATE_INDEX = 5; 181 182 186 private int counterID = 0; 187 188 private java.util.Timer timer; 189 190 195 196 199 public Timer() { 200 } 201 202 207 208 221 public ObjectName preRegister(MBeanServer server, ObjectName name) 222 throws java.lang.Exception { 223 return name; 224 } 225 226 232 public void postRegister (Boolean registrationDone) { 233 } 234 235 243 public void preDeregister() throws java.lang.Exception { 244 245 if (isTraceOn()) { 246 trace("preDeregister", "stop the timer"); 247 } 248 249 stop(); 252 } 253 254 260 public void postDeregister() { 261 } 262 263 271 public synchronized MBeanNotificationInfo [] getNotificationInfo() { 272 Set notifTypes = new TreeSet (); 273 for (Iterator it = timerTable.values().iterator(); it.hasNext(); ) { 274 Object [] entry = (Object []) it.next(); 275 TimerNotification notif = (TimerNotification ) 276 entry[TIMER_NOTIF_INDEX]; 277 notifTypes.add(notif.getType()); 278 } 279 String [] notifTypesArray = (String []) 280 notifTypes.toArray(new String [0]); 281 return new MBeanNotificationInfo [] { 282 new MBeanNotificationInfo (notifTypesArray, 283 TimerNotification .class.getName(), 284 "Notification sent by Timer MBean") 285 }; 286 } 287 288 297 public synchronized void start() { 298 299 if (isTraceOn()) { 300 trace("start", "starting the timer"); 301 } 302 303 if (isActive == false) { 306 307 timer = new java.util.Timer (); 308 309 TimerAlarmClock alarmClock; 310 Object [] obj; 311 Date date; 312 313 Date currentDate = new Date (); 314 315 sendPastNotifications(currentDate, sendPastNotifications); 320 321 Enumeration e = timerTable.elements(); 325 while (e.hasMoreElements()) { 326 327 obj = (Object [])e.nextElement(); 328 329 date = (Date )obj[TIMER_DATE_INDEX]; 332 333 boolean fixedRate = ((Boolean )obj[FIXED_RATE_INDEX]).booleanValue(); 336 if (fixedRate) 337 { 338 alarmClock = new TimerAlarmClock (this, date); 339 obj[ALARM_CLOCK_INDEX] = (Object )alarmClock; 340 timer.schedule(alarmClock, alarmClock.next); 341 } 342 else 343 { 344 alarmClock = new TimerAlarmClock (this, (date.getTime() - currentDate.getTime())); 345 obj[ALARM_CLOCK_INDEX] = (Object )alarmClock; 346 timer.schedule(alarmClock, alarmClock.timeout); 347 } 348 } 349 350 isActive = true; 353 354 if (isTraceOn()) { 355 trace("start", "timer started"); 356 } 357 } else { 358 if (isTraceOn()) { 359 trace("start", "the timer is already activated"); 360 } 361 } 362 } 363 364 367 public synchronized void stop() { 368 369 if (isTraceOn()) { 370 trace("stop", "stoping the timer"); 371 } 372 373 if (isActive == true) { 376 377 TimerAlarmClock alarmClock; 378 Object [] obj; 379 380 Enumeration e = timerTable.elements(); 381 while (e.hasMoreElements()) { 382 383 obj = (Object [])e.nextElement(); 384 385 alarmClock = (TimerAlarmClock )obj[ALARM_CLOCK_INDEX]; 388 if (alarmClock != null) { 389 400 alarmClock.cancel(); 401 alarmClock = null; 402 } 403 } 404 405 timer.cancel(); 406 407 isActive = false; 410 411 if (isTraceOn()) { 412 trace("stop", "timer stopped"); 413 } 414 } else { 415 if (isTraceOn()) { 416 trace("stop", "the timer is already deactivated"); 417 } 418 } 419 } 420 421 456 461 public synchronized Integer addNotification(String type, String message, Object userData, 462 Date date, long period, long nbOccurences, boolean fixedRate) 463 throws java.lang.IllegalArgumentException { 464 465 if (date == null) { 466 throw new java.lang.IllegalArgumentException ("Timer notification date cannot be null."); 467 } 468 469 472 if ((period < 0) || (nbOccurences < 0)) { 476 throw new java.lang.IllegalArgumentException ("Negative values for the periodicity"); 477 } 478 479 Date currentDate = new Date (); 480 481 if (currentDate.after(date)) { 484 485 date.setTime(currentDate.getTime()); 486 if (isTraceOn()) { 487 trace("addNotification", "update timer notification to add with:" + 488 "\n\tNotification date = " + date); 489 } 490 } 491 492 Integer notifID = null; 495 notifID = new Integer (++counterID); 496 497 TimerNotification notif = new TimerNotification (type, this, 0, 0, message, notifID); 501 notif.setUserData(userData); 502 503 Object [] obj = new Object [6]; 504 505 TimerAlarmClock alarmClock; 506 if (fixedRate) 507 { 508 alarmClock = new TimerAlarmClock (this, date); 509 } 510 else 511 { 512 alarmClock = new TimerAlarmClock (this, (date.getTime() - currentDate.getTime())); 513 } 514 515 Date d = new Date (date.getTime()); 519 520 obj[TIMER_NOTIF_INDEX] = (Object )notif; 521 obj[TIMER_DATE_INDEX] = (Object )d; 522 obj[TIMER_PERIOD_INDEX] = (Object ) new Long (period); 523 obj[TIMER_NB_OCCUR_INDEX] = (Object ) new Long (nbOccurences); 524 obj[ALARM_CLOCK_INDEX] = (Object )alarmClock; 525 obj[FIXED_RATE_INDEX] = new Boolean (fixedRate); 526 527 if (isTraceOn()) { 528 trace("addNotification", "adding timer notification:" + 529 "\n\tNotification source = " + notif.getSource() + 530 "\n\tNotification type = " + notif.getType() + 531 "\n\tNotification ID = " + notifID + 532 "\n\tNotification date = " + d + 533 "\n\tNotification period = " + period + 534 "\n\tNotification nb of occurrences = " + nbOccurences + 535 "\n\tNotification executes at fixed rate = " + fixedRate); 536 } 537 538 timerTable.put(notifID, obj); 539 540 if (isActive == true) { 543 if (fixedRate) 544 { 545 timer.schedule(alarmClock, alarmClock.next); 546 } 547 else 548 { 549 timer.schedule(alarmClock, alarmClock.timeout); 550 } 551 } 552 553 if (isTraceOn()) { 554 trace("addNotification", "timer notification added"); 555 } 556 return notifID; 557 } 558 559 590 595 public synchronized Integer addNotification(String type, String message, Object userData, 596 Date date, long period, long nbOccurences) 597 throws java.lang.IllegalArgumentException { 598 599 return addNotification(type, message, userData, date, period, nbOccurences, false); 600 } 601 602 628 633 public synchronized Integer addNotification(String type, String message, Object userData, 634 Date date, long period) 635 throws java.lang.IllegalArgumentException { 636 637 return (addNotification(type, message, userData, date, period, 0)); 638 } 639 640 660 665 public synchronized Integer addNotification(String type, String message, Object userData, Date date) 666 throws java.lang.IllegalArgumentException { 667 668 669 return (addNotification(type, message, userData, date, 0, 0)); 670 } 671 672 680 public synchronized void removeNotification(Integer id) throws InstanceNotFoundException { 681 682 if (timerTable.containsKey(id) == false) { 685 throw new InstanceNotFoundException ("Timer notification to remove not in the list of notifications"); 686 } 687 688 Object [] obj = (Object [])timerTable.get(id); 691 TimerAlarmClock alarmClock = (TimerAlarmClock )obj[ALARM_CLOCK_INDEX]; 692 if (alarmClock != null) { 693 alarmClock.cancel(); 704 alarmClock = null; 705 } 706 707 if (isTraceOn()) { 710 trace("removeNotification", "removing timer notification:" + 711 "\n\tNotification source = " + ((TimerNotification )obj[TIMER_NOTIF_INDEX]).getSource() + 712 "\n\tNotification type = " + ((TimerNotification )obj[TIMER_NOTIF_INDEX]).getType() + 713 "\n\tNotification ID = " + ((TimerNotification )obj[TIMER_NOTIF_INDEX]).getNotificationID() + 714 "\n\tNotification date = " + obj[TIMER_DATE_INDEX] + 715 "\n\tNotification period = " + obj[TIMER_PERIOD_INDEX] + 716 "\n\tNotification nb of occurrences = " + obj[TIMER_NB_OCCUR_INDEX] + 717 "\n\tNotification executes at fixed rate = " + obj[FIXED_RATE_INDEX]); 718 } 719 720 timerTable.remove(id); 721 722 if (isTraceOn()) { 723 trace("removeNotification", "timer notification removed"); 724 } 725 } 726 727 735 public synchronized void removeNotifications(String type) throws InstanceNotFoundException { 736 737 TimerNotification notif; 738 Integer id; 739 TimerAlarmClock alarmClock; 740 Object [] obj; 741 742 Vector v = getNotificationIDs(type); 743 744 if (v.isEmpty()) { 747 throw new InstanceNotFoundException ("Timer notifications to remove not in the list of notifications"); 748 } 749 750 Enumeration e = v.elements(); 751 while (e.hasMoreElements()) { 752 notif = (TimerNotification )e.nextElement(); 753 id = notif.getNotificationID(); 754 obj = (Object [])timerTable.get(id); 755 756 timerTable.remove(id); 757 758 alarmClock = (TimerAlarmClock )obj[ALARM_CLOCK_INDEX]; 759 if (alarmClock != null) { 760 alarmClock.cancel(); 761 } 762 } 763 } 764 765 769 public synchronized void removeAllNotifications() { 770 771 Object [] obj; 772 TimerAlarmClock alarmClock; 773 774 Enumeration e = timerTable.elements(); 775 while (e.hasMoreElements()) { 776 777 obj = (Object [])e.nextElement(); 778 779 alarmClock = (TimerAlarmClock )obj[ALARM_CLOCK_INDEX]; 782 alarmClock.cancel(); 795 alarmClock = null; 796 } 797 798 if (isTraceOn()) { 801 trace("removeAllNotifications", "removing all timer notifications"); 802 } 803 804 timerTable.clear(); 805 806 if (isTraceOn()) { 807 trace("removeAllNotifications", "all timer notifications removed"); 808 } 809 810 counterID = 0; 813 814 if (isTraceOn()) { 815 trace("removeAllNotifications", "timer notification counter ID resetted"); 816 } 817 } 818 |