1292 lines · 48.7 KB · Java
RawDownload
    private synchronized void sendPastNotifications(Date currentDate, boolean currentFlag) {         TimerNotification notif;        Integer notifID;        Date date;         ArrayList<Object[]> values =            new ArrayList<Object[]>(timerTable.values());         for (Object[] obj : values) {             // Retrieve the timer notification and the date notification.            //            notif = (TimerNotification)obj[TIMER_NOTIF_INDEX];            notifID = notif.getNotificationID();            date = (Date)obj[TIMER_DATE_INDEX];             // Update the timer notification while:            //  - the timer notification date is earlier than the current date            //  - the timer notification has not been removed from the timer table.            //            while ( (currentDate.after(date)) && (timerTable.containsKey(notifID)) ) {                 if (currentFlag == true) {                    if (TIMER_LOGGER.isLoggable(Level.FINER)) {                        StringBuilder strb = new StringBuilder()                        .append("sending past timer notification:")                        .append("\n\tNotification source = ")                        .append(notif.getSource())                        .append("\n\tNotification type = ")                        .append(notif.getType())                        .append("\n\tNotification ID = ")                        .append(notif.getNotificationID())                        .append("\n\tNotification date = ")                        .append(date)                        .append("\n\tNotification period = ")                        .append(obj[TIMER_PERIOD_INDEX])                        .append("\n\tNotification nb of occurrences = ")                        .append(obj[TIMER_NB_OCCUR_INDEX])                        .append("\n\tNotification executes at fixed rate = ")                        .append(obj[FIXED_RATE_INDEX]);                        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),                                "sendPastNotifications", strb.toString());                    }                    sendNotification(date, notif);                     TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),                            "sendPastNotifications", "past timer notification sent");                }                 // Update the date and the number of occurrences of the timer notification.                //                updateTimerTable(notif.getNotificationID());            }        }    }     /**     * If the timer notification is not periodic, it is removed from the list of notifications.     * <P>     * If the timer period of the timer notification has a non null periodicity,     * the date of the timer notification is updated by adding the periodicity.     * The associated TimerAlarmClock is updated by setting its timeout to the period value.     * <P>     * If the timer period has a defined number of occurrences, the timer     * notification is updated if the number of occurrences has not yet been reached.     * Otherwise it is removed from the list of notifications.     *     * @param notifID The timer notification identifier to update.     */    private synchronized void updateTimerTable(Integer notifID) {         // Retrieve the timer notification and the TimerAlarmClock.        //        Object[] obj = timerTable.get(notifID);        Date date = (Date)obj[TIMER_DATE_INDEX];        Long period = (Long)obj[TIMER_PERIOD_INDEX];        Long nbOccurences = (Long)obj[TIMER_NB_OCCUR_INDEX];        Boolean fixedRate = (Boolean)obj[FIXED_RATE_INDEX];        TimerAlarmClock alarmClock = (TimerAlarmClock)obj[ALARM_CLOCK_INDEX];         if (period.longValue() != 0) {             // Update the date and the number of occurrences of the timer notification            // and the TimerAlarmClock time out.            // NOTES :            //   nbOccurences = 0 notifies an infinite periodicity.            //   nbOccurences = 1 notifies a finite periodicity that has reached its end.            //   nbOccurences > 1 notifies a finite periodicity that has not yet reached its end.            //            if ((nbOccurences.longValue() == 0) || (nbOccurences.longValue() > 1)) {                 date.setTime(date.getTime() + period.longValue());                obj[TIMER_NB_OCCUR_INDEX] = Long.valueOf(java.lang.Math.max(0L, (nbOccurences.longValue() - 1)));                nbOccurences = (Long)obj[TIMER_NB_OCCUR_INDEX];                 if (isActive == true) {                  if (fixedRate.booleanValue())                  {                    alarmClock = new TimerAlarmClock(this, date);                    obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;                    timer.schedule(alarmClock, alarmClock.next);                  }                  else                  {                    alarmClock = new TimerAlarmClock(this, period.longValue());                    obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;                    timer.schedule(alarmClock, alarmClock.timeout);                  }                }                if (TIMER_LOGGER.isLoggable(Level.FINER)) {                    TimerNotification notif = (TimerNotification)obj[TIMER_NOTIF_INDEX];                    StringBuilder strb = new StringBuilder()                    .append("update timer notification with:")                    .append("\n\tNotification source = ")                    .append(notif.getSource())                    .append("\n\tNotification type = ")                    .append(notif.getType())                    .append("\n\tNotification ID = ")                    .append(notifID)                    .append("\n\tNotification date = ")                    .append(date)                    .append("\n\tNotification period = ")                    .append(period)                    .append("\n\tNotification nb of occurrences = ")                    .append(nbOccurences)                    .append("\n\tNotification executes at fixed rate = ")                    .append(fixedRate);                    TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),                            "updateTimerTable", strb.toString());                }            }            else {                if (alarmClock != null) {//                     alarmClock.interrupt();//                     try {//                         // Wait until the thread die.//                         ////                         alarmClock.join();//                     } catch (InterruptedException e) {//                         // Ignore...//                     }                    alarmClock.cancel();                }                timerTable.remove(notifID);            }        }        else {            if (alarmClock != null) {//                 alarmClock.interrupt();//                 try {//                     // Wait until the thread die.//                     ////                     alarmClock.join();//                 } catch (InterruptedException e) {//                     // Ignore...//                 }                    alarmClock.cancel();            }            timerTable.remove(notifID);        }    }     /*     * ------------------------------------------     *  PACKAGE METHODS     * ------------------------------------------     */     /**     * This method is called by the timer each time     * the TimerAlarmClock has exceeded its timeout.     *     * @param notification The TimerAlarmClock notification.     */    @SuppressWarnings("deprecation")    void notifyAlarmClock(TimerAlarmClockNotification notification) {         TimerNotification timerNotification = null;        Date timerDate = null;         // Retrieve the timer notification associated to the alarm-clock.        //        TimerAlarmClock alarmClock = (TimerAlarmClock)notification.getSource();         for (Object[] obj : timerTable.values()) {            if (obj[ALARM_CLOCK_INDEX] == alarmClock) {                timerNotification = (TimerNotification)obj[TIMER_NOTIF_INDEX];                timerDate = (Date)obj[TIMER_DATE_INDEX];                break;            }        }         // Notify the timer.        //        sendNotification(timerDate, timerNotification);         // Update the notification and the TimerAlarmClock timeout.        //        updateTimerTable(timerNotification.getNotificationID());    }     /**     * This method is used by the timer MBean to update and send a timer     * notification to all the listeners registered for this kind of notification.     *     * @param timeStamp The notification emission date.     * @param notification The timer notification to send.     */    void sendNotification(Date timeStamp, TimerNotification notification) {         if (TIMER_LOGGER.isLoggable(Level.FINER)) {            StringBuilder strb = new StringBuilder()            .append("sending timer notification:")            .append("\n\tNotification source = ")            .append(notification.getSource())            .append("\n\tNotification type = ")            .append(notification.getType())            .append("\n\tNotification ID = ")            .append(notification.getNotificationID())            .append("\n\tNotification date = ")            .append(timeStamp);            TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),                    "sendNotification", strb.toString());        }        long curSeqNumber;        synchronized(this) {            sequenceNumber = sequenceNumber + 1;            curSeqNumber = sequenceNumber;        }        synchronized (notification) {            notification.setTimeStamp(timeStamp.getTime());            notification.setSequenceNumber(curSeqNumber);            this.sendNotification((TimerNotification)notification.cloneTimerNotification());        }         TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),                "sendNotification", "timer notification sent");    }} /** * TimerAlarmClock inner class: * This class provides a simple implementation of an alarm clock MBean. * The aim of this MBean is to set up an alarm which wakes up the timer every timeout (fixed-delay) * or at the specified date (fixed-rate). */ class TimerAlarmClock extends java.util.TimerTask {     Timer listener = null;    long timeout = 10000;    Date next = null;     /*     * ------------------------------------------     *  CONSTRUCTORS     * ------------------------------------------     */     public TimerAlarmClock(Timer listener, long timeout) {        this.listener = listener;        this.timeout = Math.max(0L, timeout);    }     public TimerAlarmClock(Timer listener, Date next) {        this.listener = listener;        this.next = next;    }     /*     * ------------------------------------------     *  PUBLIC METHODS     * ------------------------------------------     */     /**     * This method is called by the timer when it is started.     */    public void run() {         try {            //this.sleep(timeout);            TimerAlarmClockNotification notif = new TimerAlarmClockNotification(this);            listener.notifyAlarmClock(notif);        } catch (Exception e) {            TIMER_LOGGER.logp(Level.FINEST, Timer.class.getName(), "run",                    "Got unexpected exception when sending a notification", e);        }    }}

Lines 1001–1292 of 1292. Previous lines