jdk/src/share/classes/javax/management/timer/Timer.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 527 a33264d48a84
child 1510 e747d3193ef2
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 527
diff changeset
     2
 * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.management.timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import static com.sun.jmx.defaults.JmxProperties.TIMER_LOGGER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.TreeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
// jmx imports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.InstanceNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.MBeanNotificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.MBeanRegistration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.NotificationBroadcasterSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Provides the implementation of the timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The timer MBean sends out an alarm at a specified time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * that wakes up all the listeners registered to receive timer notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * This class manages a list of dated timer notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * A method allows users to add/remove as many notifications as required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * When a timer notification is emitted by the timer and becomes obsolete,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * it is automatically removed from the list of timer notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <BR>Additional timer notifications can be added into regularly repeating notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Note:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <LI>When sending timer notifications, the timer updates the notification sequence number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * irrespective of the notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <LI>The timer service relies on the system date of the host where the <CODE>Timer</CODE> class is loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Listeners may receive untimely notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * if their host has a different system date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * To avoid such problems, synchronize the system date of all host machines where timing is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <LI>The default behavior for periodic notifications is <i>fixed-delay execution</i>, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *     specified in {@link java.util.Timer}. In order to use <i>fixed-rate execution</i>, use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *     overloaded {@link #addNotification(String, String, Object, Date, long, long, boolean)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <LI>Notification listeners are potentially all executed in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * thread.  Therefore, they should execute rapidly to avoid holding up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * other listeners or perturbing the regularity of fixed-delay
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * executions.  See {@link NotificationBroadcasterSupport}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * </OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
public class Timer extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        implements TimerMBean, MBeanRegistration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *  PUBLIC VARIABLES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Number of milliseconds in one second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Useful constant for the <CODE>addNotification</CODE> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static final long ONE_SECOND = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Number of milliseconds in one minute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Useful constant for the <CODE>addNotification</CODE> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static final long ONE_MINUTE = 60*ONE_SECOND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Number of milliseconds in one hour.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Useful constant for the <CODE>addNotification</CODE> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public static final long ONE_HOUR   = 60*ONE_MINUTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Number of milliseconds in one day.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Useful constant for the <CODE>addNotification</CODE> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static final long ONE_DAY    = 24*ONE_HOUR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Number of milliseconds in one week.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Useful constant for the <CODE>addNotification</CODE> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public static final long ONE_WEEK   = 7*ONE_DAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *  PRIVATE VARIABLES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Table containing all the timer notifications of this timer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * with the associated date, period and number of occurrences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private Map<Integer,Object[]> timerTable =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        new Hashtable<Integer,Object[]>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Past notifications sending on/off flag value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * This attribute is used to specify if the timer has to send past notifications after start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <BR>The default value is set to <CODE>false</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private boolean sendPastNotifications = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Timer state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * The default value is set to <CODE>false</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private transient boolean isActive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Timer sequence number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * The default value is set to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private transient long sequenceNumber = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    // Flags needed to keep the indexes of the objects in the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private static final int TIMER_NOTIF_INDEX     = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static final int TIMER_DATE_INDEX      = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static final int TIMER_PERIOD_INDEX    = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static final int TIMER_NB_OCCUR_INDEX  = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static final int ALARM_CLOCK_INDEX     = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private static final int FIXED_RATE_INDEX      = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * The notification counter ID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Used to keep the max key value inserted into the timer table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private int counterID = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private java.util.Timer timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *  CONSTRUCTORS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public Timer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *  PUBLIC METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Allows the timer MBean to perform any operations it needs before being registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * in the MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Not used in this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param server The MBean server in which the timer MBean will be registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param name The object name of the timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @return The name of the timer MBean registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @exception java.lang.Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public ObjectName preRegister(MBeanServer server, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        throws java.lang.Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Allows the timer MBean to perform any operations needed after having been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * registered in the MBean server or after the registration has failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Not used in this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public void postRegister (Boolean registrationDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Allows the timer MBean to perform any operations it needs before being unregistered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * by the MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Stops the timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @exception java.lang.Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public void preDeregister() throws java.lang.Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                "preDeregister", "stop the timer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // Stop the timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Allows the timer MBean to perform any operations needed after having been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * unregistered by the MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Not used in this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public void postDeregister() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * This overrides the method in NotificationBroadcasterSupport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Return the MBeanNotificationInfo[] array for this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * The returned array has one element to indicate that the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * can emit TimerNotification.  The array of type strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * associated with this entry is a snapshot of the current types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * that were given to addNotification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public synchronized MBeanNotificationInfo[] getNotificationInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        Set<String> notifTypes = new TreeSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        for (Iterator it = timerTable.values().iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            Object[] entry = (Object[]) it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            TimerNotification notif = (TimerNotification)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                entry[TIMER_NOTIF_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            notifTypes.add(notif.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        String[] notifTypesArray =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            notifTypes.toArray(new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return new MBeanNotificationInfo[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            new MBeanNotificationInfo(notifTypesArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                      TimerNotification.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                      "Notification sent by Timer MBean")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Starts the timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * If there is one or more timer notifications before the time in the list of notifications, the notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * is sent according to the <CODE>sendPastNotifications</CODE> flag and then, updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * according to its period and remaining number of occurrences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * If the timer notification date remains earlier than the current date, this notification is just removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * from the list of notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public synchronized void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                "start", "starting the timer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        // Start the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (isActive == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            timer = new java.util.Timer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            TimerAlarmClock alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            Date date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            Date currentDate = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // Send or not past notifications depending on the flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            // Update the date and the number of occurrences of past notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // to make them later than the current date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            sendPastNotifications(currentDate, sendPastNotifications);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            // Update and start all the TimerAlarmClocks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            // Here, all the notifications in the timer table are later than the current date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            for (Object[] obj : timerTable.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                // Retrieve the date notification and the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                date = (Date)obj[TIMER_DATE_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                // Update all the TimerAlarmClock timeouts and start them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                boolean fixedRate = ((Boolean)obj[FIXED_RATE_INDEX]).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                if (fixedRate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                  alarmClock = new TimerAlarmClock(this, date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                  obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                  timer.schedule(alarmClock, alarmClock.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                  alarmClock = new TimerAlarmClock(this, (date.getTime() - currentDate.getTime()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                  obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                  timer.schedule(alarmClock, alarmClock.timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            // Set the state to ON.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            isActive = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    "start", "timer started");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    "start", "the timer is already activated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Stops the timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public synchronized void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                "stop", "stopping the timer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // Stop the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (isActive == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            for (Object[] obj : timerTable.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                // Stop all the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                //
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   351
                TimerAlarmClock alarmClock = (TimerAlarmClock)obj[ALARM_CLOCK_INDEX];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                if (alarmClock != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
//                     alarmClock.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
//                     try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
//                         // Wait until the thread die.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
//                         //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
//                         alarmClock.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
//                     } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
//                         // Ignore...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
//                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
//                     // Remove the reference on the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
//                     //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    alarmClock.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            timer.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            // Set the state to OFF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            isActive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    "stop", "timer stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    "stop", "the timer is already deactivated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * and <CODE>userData</CODE> and inserts it into the list of notifications with a given date,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * period and number of occurrences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * If the timer notification to be inserted has a date that is before the current date,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * the method behaves as if the specified date were the current date. <BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * For once-off notifications, the notification is delivered immediately. <BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * For periodic notifications, the first notification is delivered immediately and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * subsequent ones are spaced as specified by the period parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Note that once the timer notification has been added into the list of notifications,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * its associated date, period and number of occurrences cannot be updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * In the case of a periodic notification, the value of parameter <i>fixedRate</i> is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * specify the execution scheme, as specified in {@link java.util.Timer}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @param type The timer notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param message The timer notification detailed message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @param userData The timer notification user data object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @param date The date when the notification occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @param period The period of the timer notification (in milliseconds).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @param nbOccurences The total number the timer notification will be emitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param fixedRate If <code>true</code> and if the notification is periodic, the notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *                  is scheduled with a <i>fixed-rate</i> execution scheme. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *                  <code>false</code> and if the notification is periodic, the notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *                  is scheduled with a <i>fixed-delay</i> execution scheme. Ignored if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *                  notification is not periodic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @return The identifier of the new created timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @exception java.lang.IllegalArgumentException The date is {@code null} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * the period or the number of occurrences is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @see #addNotification(String, String, Object, Date, long, long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
// NPCTE fix for bugId 4464388, esc 0,  MR, to be added after modification of jmx spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
//  public synchronized Integer addNotification(String type, String message, Serializable userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
//                                                Date date, long period, long nbOccurences)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
// end of NPCTE fix for bugId 4464388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public synchronized Integer addNotification(String type, String message, Object userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                                                Date date, long period, long nbOccurences, boolean fixedRate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        throws java.lang.IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (date == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            throw new java.lang.IllegalArgumentException("Timer notification date cannot be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        // Check that all the timer notification attributes are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        // Invalid timer period value exception:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        // Check that the period and the nbOccurences are POSITIVE VALUES.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if ((period < 0) || (nbOccurences < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            throw new java.lang.IllegalArgumentException("Negative values for the periodicity");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        Date currentDate = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        // Update the date if it is before the current date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (currentDate.after(date)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            date.setTime(currentDate.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            if (TIMER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        "addNotification",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        "update timer notification to add with:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        "\n\tNotification date = " + date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        // Create and add the timer notification into the timer table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        //
527
a33264d48a84 6703552: Missing files from changeset for 6701459
emcmanus
parents: 526
diff changeset
   458
        Integer notifID = Integer.valueOf(++counterID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        // The sequenceNumber and the timeStamp attributes are updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        // when the notification is emitted by the timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        TimerNotification notif = new TimerNotification(type, this, 0, 0, message, notifID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        notif.setUserData(userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        Object[] obj = new Object[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        TimerAlarmClock alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (fixedRate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
          alarmClock = new TimerAlarmClock(this, date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
          alarmClock = new TimerAlarmClock(this, (date.getTime() - currentDate.getTime()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        // Fix bug 00417.B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        // The date registered into the timer is a clone from the date parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        Date d = new Date(date.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        obj[TIMER_NOTIF_INDEX] = (Object)notif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        obj[TIMER_DATE_INDEX] = (Object)d;
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   485
        obj[TIMER_PERIOD_INDEX] = (Object) period;
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   486
        obj[TIMER_NB_OCCUR_INDEX] = (Object) nbOccurences;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        obj[FIXED_RATE_INDEX] = Boolean.valueOf(fixedRate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if (TIMER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            StringBuilder strb = new StringBuilder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            .append("adding timer notification:\n\t")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            .append("Notification source = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            .append(notif.getSource())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            .append("\n\tNotification type = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            .append(notif.getType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            .append("\n\tNotification ID = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            .append(notifID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            .append("\n\tNotification date = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            .append(d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            .append("\n\tNotification period = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            .append(period)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            .append("\n\tNotification nb of occurrences = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            .append(nbOccurences)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            .append("\n\tNotification executes at fixed rate = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            .append(fixedRate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    "addNotification", strb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        timerTable.put(notifID, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        // Update and start the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if (isActive == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
          if (fixedRate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
          {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            timer.schedule(alarmClock, alarmClock.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
          else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
          {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            timer.schedule(alarmClock, alarmClock.timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                "addNotification", "timer notification added");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        return notifID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * and <CODE>userData</CODE> and inserts it into the list of notifications with a given date,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * period and number of occurrences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * If the timer notification to be inserted has a date that is before the current date,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * the method behaves as if the specified date were the current date. <BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * For once-off notifications, the notification is delivered immediately. <BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * For periodic notifications, the first notification is delivered immediately and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * subsequent ones are spaced as specified by the period parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Note that once the timer notification has been added into the list of notifications,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * its associated date, period and number of occurrences cannot be updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * In the case of a periodic notification, uses a <i>fixed-delay</i> execution scheme, as specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * {@link java.util.Timer}. In order to use a <i>fixed-rate</i> execution scheme, use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * {@link #addNotification(String, String, Object, Date, long, long, boolean)} instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @param type The timer notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @param message The timer notification detailed message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @param userData The timer notification user data object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @param date The date when the notification occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @param period The period of the timer notification (in milliseconds).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @param nbOccurences The total number the timer notification will be emitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @return The identifier of the new created timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @exception java.lang.IllegalArgumentException The date is {@code null} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * the period or the number of occurrences is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @see #addNotification(String, String, Object, Date, long, long, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
// NPCTE fix for bugId 4464388, esc 0,  MR , to be added after modification of jmx spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
//  public synchronized Integer addNotification(String type, String message, Serializable userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
//                                              Date date, long period)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
// end of NPCTE fix for bugId 4464388 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public synchronized Integer addNotification(String type, String message, Object userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                                                Date date, long period, long nbOccurences)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        throws java.lang.IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
      return addNotification(type, message, userData, date, period, nbOccurences, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * and <CODE>userData</CODE> and inserts it into the list of notifications with a given date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * and period and a null number of occurrences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * The timer notification will repeat continuously using the timer period using a <i>fixed-delay</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * execution scheme, as specified in {@link java.util.Timer}. In order to use a <i>fixed-rate</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * execution scheme, use {@link #addNotification(String, String, Object, Date, long, long,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * boolean)} instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * If the timer notification to be inserted has a date that is before the current date,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * the method behaves as if the specified date were the current date. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * first notification is delivered immediately and the subsequent ones are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * spaced as specified by the period parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @param type The timer notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @param message The timer notification detailed message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @param userData The timer notification user data object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @param date The date when the notification occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @param period The period of the timer notification (in milliseconds).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @return The identifier of the new created timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @exception java.lang.IllegalArgumentException The date is {@code null} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * the period is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
// NPCTE fix for bugId 4464388, esc 0,  MR , to be added after modification of jmx spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
//  public synchronized Integer addNotification(String type, String message, Serializable userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
//                                              Date date, long period)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
// end of NPCTE fix for bugId 4464388 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    public synchronized Integer addNotification(String type, String message, Object userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                                                Date date, long period)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        throws java.lang.IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        return (addNotification(type, message, userData, date, period, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * and <CODE>userData</CODE> and inserts it into the list of notifications with a given date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * and a null period and number of occurrences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * The timer notification will be handled once at the specified date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * If the timer notification to be inserted has a date that is before the current date,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * the method behaves as if the specified date were the current date and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * notification is delivered immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @param type The timer notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @param message The timer notification detailed message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @param userData The timer notification user data object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @param date The date when the notification occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @return The identifier of the new created timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @exception java.lang.IllegalArgumentException The date is {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
// NPCTE fix for bugId 4464388, esc 0,  MR, to be added after modification of jmx spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
//  public synchronized Integer addNotification(String type, String message, Serializable userData, Date date)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
//      throws java.lang.IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
// end of NPCTE fix for bugId 4464388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    public synchronized Integer addNotification(String type, String message, Object userData, Date date)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        throws java.lang.IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        return (addNotification(type, message, userData, date, 0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * Removes the timer notification corresponding to the specified identifier from the list of notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @exception InstanceNotFoundException The specified identifier does not correspond to any timer notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * in the list of notifications of this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    public synchronized void removeNotification(Integer id) throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        // Check that the notification to remove is effectively in the timer table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        if (timerTable.containsKey(id) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            throw new InstanceNotFoundException("Timer notification to remove not in the list of notifications");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        // Stop the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        TimerAlarmClock alarmClock = (TimerAlarmClock)obj[ALARM_CLOCK_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        if (alarmClock != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
//             alarmClock.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
//             try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
//                 // Wait until the thread die.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
//                 //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
//                 alarmClock.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
//             } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
//                 // Ignore...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
//             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
//             // Remove the reference on the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
//             //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            alarmClock.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        // Remove the timer notification from the timer table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        if (TIMER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            StringBuilder strb = new StringBuilder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            .append("removing timer notification:")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            .append("\n\tNotification source = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            .append(((TimerNotification)obj[TIMER_NOTIF_INDEX]).getSource())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            .append("\n\tNotification type = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            .append(((TimerNotification)obj[TIMER_NOTIF_INDEX]).getType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            .append("\n\tNotification ID = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            .append(((TimerNotification)obj[TIMER_NOTIF_INDEX]).getNotificationID())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            .append("\n\tNotification date = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            .append(obj[TIMER_DATE_INDEX])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            .append("\n\tNotification period = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            .append(obj[TIMER_PERIOD_INDEX])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            .append("\n\tNotification nb of occurrences = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            .append(obj[TIMER_NB_OCCUR_INDEX])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            .append("\n\tNotification executes at fixed rate = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            .append(obj[FIXED_RATE_INDEX]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                    "removeNotification", strb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        timerTable.remove(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                "removeNotification", "timer notification removed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * Removes all the timer notifications corresponding to the specified type from the list of notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @param type The timer notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * @exception InstanceNotFoundException The specified type does not correspond to any timer notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * in the list of notifications of this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    public synchronized void removeNotifications(String type) throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        Vector<Integer> v = getNotificationIDs(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        if (v.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            throw new InstanceNotFoundException("Timer notifications to remove not in the list of notifications");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        for (Integer i : v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            removeNotification(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * Removes all the timer notifications from the list of notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * and resets the counter used to update the timer notification identifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    public synchronized void removeAllNotifications() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        TimerAlarmClock alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        for (Object[] obj : timerTable.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            // Stop the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            alarmClock = (TimerAlarmClock)obj[ALARM_CLOCK_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
//             if (alarmClock != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
//                 alarmClock.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
//                 try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
//                     // Wait until the thread die.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
//                     //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
//                     alarmClock.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
//                 } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
//                     // Ignore...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
//                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                  // Remove the reference on the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                  //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
//             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            alarmClock.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        // Remove all the timer notifications from the timer table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                "removeAllNotifications", "removing all timer notifications");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        timerTable.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                "removeAllNotifications", "all timer notifications removed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        // Reset the counterID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        counterID = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                "removeAllNotifications", "timer notification counter ID reset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    // GETTERS AND SETTERS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    //--------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * Gets the number of timer notifications registered into the list of notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * @return The number of timer notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    public int getNbNotifications() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        return timerTable.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * Gets all timer notification identifiers registered into the list of notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @return A vector of <CODE>Integer</CODE> objects containing all the timer notification identifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * <BR>The vector is empty if there is no timer notification registered for this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    public synchronized Vector<Integer> getAllNotificationIDs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        return new Vector<Integer>(timerTable.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * Gets all the identifiers of timer notifications corresponding to the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * @param type The timer notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * @return A vector of <CODE>Integer</CODE> objects containing all the identifiers of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * timer notifications with the specified <CODE>type</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * <BR>The vector is empty if there is no timer notifications registered for this timer MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * with the specified <CODE>type</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    public synchronized Vector<Integer> getNotificationIDs(String type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        String s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        Vector<Integer> v = new Vector<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        for (Map.Entry<Integer,Object[]> entry : timerTable.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            Object[] obj = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            s = ((TimerNotification)obj[TIMER_NOTIF_INDEX]).getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            if ((type == null) ? s == null : type.equals(s))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                v.addElement(entry.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    // 5089997: return is Vector<Integer> not Vector<TimerNotification>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * Gets the timer notification type corresponding to the specified identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @return The timer notification type or null if the identifier is not mapped to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * timer notification registered for this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    public String getNotificationType(Integer id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            return ( ((TimerNotification)obj[TIMER_NOTIF_INDEX]).getType() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * Gets the timer notification detailed message corresponding to the specified identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @return The timer notification detailed message or null if the identifier is not mapped to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * timer notification registered for this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    public String getNotificationMessage(Integer id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            return ( ((TimerNotification)obj[TIMER_NOTIF_INDEX]).getMessage() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * Gets the timer notification user data object corresponding to the specified identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * @return The timer notification user data object or null if the identifier is not mapped to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * timer notification registered for this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    // NPCTE fix for bugId 4464388, esc 0, MR, 03 sept 2001, to be added after modification of jmx spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    //public Serializable getNotificationUserData(Integer id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    // end of NPCTE fix for bugId 4464388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    public Object getNotificationUserData(Integer id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            return ( ((TimerNotification)obj[TIMER_NOTIF_INDEX]).getUserData() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * Gets a copy of the date associated to a timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * @return A copy of the date or null if the identifier is not mapped to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * timer notification registered for this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    public Date getDate(Integer id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            Date date = (Date)obj[TIMER_DATE_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            return (new Date(date.getTime()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * Gets a copy of the period (in milliseconds) associated to a timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @return A copy of the period or null if the identifier is not mapped to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * timer notification registered for this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    public Long getPeriod(Integer id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        if (obj != null) {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   903
            return (Long)obj[TIMER_PERIOD_INDEX];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * Gets a copy of the remaining number of occurrences associated to a timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @return A copy of the remaining number of occurrences or null if the identifier is not mapped to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * timer notification registered for this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    public Long getNbOccurences(Integer id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        if (obj != null) {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   920
            return (Long)obj[TIMER_NB_OCCUR_INDEX];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * Gets a copy of the flag indicating whether a periodic notification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * executed at <i>fixed-delay</i> or at <i>fixed-rate</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * @return A copy of the flag indicating whether a periodic notification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     *         executed at <i>fixed-delay</i> or at <i>fixed-rate</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    public Boolean getFixedRate(Integer id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
      Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
      if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        Boolean fixedRate = (Boolean)obj[FIXED_RATE_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        return (Boolean.valueOf(fixedRate.booleanValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
      return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * Gets the flag indicating whether or not the timer sends past notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * <BR>The default value of the past notifications sending on/off flag is <CODE>false</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * @return The past notifications sending on/off flag value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * @see #setSendPastNotifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    public boolean getSendPastNotifications() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        return sendPastNotifications;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * Sets the flag indicating whether the timer sends past notifications or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * <BR>The default value of the past notifications sending on/off flag is <CODE>false</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * @param value The past notifications sending on/off flag value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * @see #getSendPastNotifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    public void setSendPastNotifications(boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        sendPastNotifications = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * Tests whether the timer MBean is active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * A timer MBean is marked active when the {@link #start start} method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * It becomes inactive when the {@link #stop stop} method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * <BR>The default value of the active on/off flag is <CODE>false</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * @return <CODE>true</CODE> if the timer MBean is active, <CODE>false</CODE> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    public boolean isActive() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        return isActive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * Tests whether the list of timer notifications is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * @return <CODE>true</CODE> if the list of timer notifications is empty, <CODE>false</CODE> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        return (timerTable.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     *  PRIVATE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * Sends or not past notifications depending on the specified flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * @param currentDate The current date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * @param currentFlag The flag indicating if past notifications must be sent or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    private synchronized void sendPastNotifications(Date currentDate, boolean currentFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        TimerNotification notif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        Integer notifID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        Date date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        for (Object[] obj : timerTable.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            // Retrieve the timer notification and the date notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            notif = (TimerNotification)obj[TIMER_NOTIF_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            notifID = notif.getNotificationID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            date = (Date)obj[TIMER_DATE_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            // Update the timer notification while:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            //  - the timer notification date is earlier than the current date
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            //  - the timer notification has not been removed from the timer table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            while ( (currentDate.after(date)) && (timerTable.containsKey(notifID)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                if (currentFlag == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                    if (TIMER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                        StringBuilder strb = new StringBuilder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                        .append("sending past timer notification:")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                        .append("\n\tNotification source = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                        .append(notif.getSource())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                        .append("\n\tNotification type = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                        .append(notif.getType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                        .append("\n\tNotification ID = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                        .append(notif.getNotificationID())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                        .append("\n\tNotification date = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                        .append(date)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                        .append("\n\tNotification period = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                        .append(obj[TIMER_PERIOD_INDEX])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                        .append("\n\tNotification nb of occurrences = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                        .append(obj[TIMER_NB_OCCUR_INDEX])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                        .append("\n\tNotification executes at fixed rate = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                        .append(obj[FIXED_RATE_INDEX]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                                "sendPastNotifications", strb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                    sendNotification(date, notif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                    TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                            "sendPastNotifications", "past timer notification sent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                // Update the date and the number of occurrences of the timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                updateTimerTable(notif.getNotificationID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * If the timer notification is not periodic, it is removed from the list of notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * If the timer period of the timer notification has a non null periodicity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * the date of the timer notification is updated by adding the periodicity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * The associated TimerAlarmClock is updated by setting its timeout to the period value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * If the timer period has a defined number of occurrences, the timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * notification is updated if the number of occurrences has not yet been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * Otherwise it is removed from the list of notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @param notifID The timer notification identifier to update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    private synchronized void updateTimerTable(Integer notifID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        // Retrieve the timer notification and the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        Object[] obj = timerTable.get(notifID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        Date date = (Date)obj[TIMER_DATE_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        Long period = (Long)obj[TIMER_PERIOD_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        Long nbOccurences = (Long)obj[TIMER_NB_OCCUR_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        Boolean fixedRate = (Boolean)obj[FIXED_RATE_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        TimerAlarmClock alarmClock = (TimerAlarmClock)obj[ALARM_CLOCK_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        if (period.longValue() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            // Update the date and the number of occurrences of the timer notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            // and the TimerAlarmClock time out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            // NOTES :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            //   nbOccurences = 0 notifies an infinite periodicity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            //   nbOccurences = 1 notifies a finite periodicity that has reached its end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            //   nbOccurences > 1 notifies a finite periodicity that has not yet reached its end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            if ((nbOccurences.longValue() == 0) || (nbOccurences.longValue() > 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                date.setTime(date.getTime() + period.longValue());
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
  1091
                obj[TIMER_NB_OCCUR_INDEX] = Long.valueOf(java.lang.Math.max(0L, (nbOccurences.longValue() - 1)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                nbOccurences = (Long)obj[TIMER_NB_OCCUR_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                if (isActive == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                  if (fixedRate.booleanValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                    alarmClock = new TimerAlarmClock(this, date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                    obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                    timer.schedule(alarmClock, alarmClock.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                  else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                    alarmClock = new TimerAlarmClock(this, period.longValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                    obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                    timer.schedule(alarmClock, alarmClock.timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                if (TIMER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                    TimerNotification notif = (TimerNotification)obj[TIMER_NOTIF_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                    StringBuilder strb = new StringBuilder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                    .append("update timer notification with:")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                    .append("\n\tNotification source = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                    .append(notif.getSource())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                    .append("\n\tNotification type = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                    .append(notif.getType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                    .append("\n\tNotification ID = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                    .append(notifID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                    .append("\n\tNotification date = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                    .append(date)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                    .append("\n\tNotification period = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                    .append(period)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                    .append("\n\tNotification nb of occurrences = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                    .append(nbOccurences)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                    .append("\n\tNotification executes at fixed rate = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                    .append(fixedRate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                    TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                            "updateTimerTable", strb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                if (alarmClock != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
//                     alarmClock.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
//                     try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
//                         // Wait until the thread die.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
//                         //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
//                         alarmClock.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
//                     } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
//                         // Ignore...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
//                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                    alarmClock.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                timerTable.remove(notifID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            if (alarmClock != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
//                 alarmClock.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
//                 try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
//                     // Wait until the thread die.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
//                     //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
//                     alarmClock.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
//                 } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
//                     // Ignore...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
//                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                   alarmClock.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            timerTable.remove(notifID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     *  PACKAGE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * This method is called by the timer each time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * the TimerAlarmClock has exceeded its timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * @param notification The TimerAlarmClock notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    @SuppressWarnings("deprecation")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    void notifyAlarmClock(TimerAlarmClockNotification notification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        TimerNotification timerNotification = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        Date timerDate = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        // Retrieve the timer notification associated to the alarm-clock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        TimerAlarmClock alarmClock = (TimerAlarmClock)notification.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        for (Object[] obj : timerTable.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            if (obj[ALARM_CLOCK_INDEX] == alarmClock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                timerNotification = (TimerNotification)obj[TIMER_NOTIF_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                timerDate = (Date)obj[TIMER_DATE_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        // Notify the timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        sendNotification(timerDate, timerNotification);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        // Update the notification and the TimerAlarmClock timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        updateTimerTable(timerNotification.getNotificationID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * This method is used by the timer MBean to update and send a timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * notification to all the listeners registered for this kind of notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * @param timeStamp The notification emission date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * @param notification The timer notification to send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    void sendNotification(Date timeStamp, TimerNotification notification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        if (TIMER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            StringBuilder strb = new StringBuilder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            .append("sending timer notification:")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            .append("\n\tNotification source = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            .append(notification.getSource())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            .append("\n\tNotification type = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            .append(notification.getType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            .append("\n\tNotification ID = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            .append(notification.getNotificationID())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            .append("\n\tNotification date = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            .append(timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                    "sendNotification", strb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        long curSeqNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            sequenceNumber = sequenceNumber + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            curSeqNumber = sequenceNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        synchronized (notification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            notification.setTimeStamp(timeStamp.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            notification.setSequenceNumber(curSeqNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            this.sendNotification((TimerNotification)notification.cloneTimerNotification());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                "sendNotification", "timer notification sent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
 * TimerAlarmClock inner class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
 * This class provides a simple implementation of an alarm clock MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
 * The aim of this MBean is to set up an alarm which wakes up the timer every timeout (fixed-delay)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
 * or at the specified date (fixed-rate).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
class TimerAlarmClock extends java.util.TimerTask {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    Timer listener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    long timeout = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    Date next = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     *  CONSTRUCTORS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    public TimerAlarmClock(Timer listener, long timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        this.listener = listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        this.timeout = Math.max(0L, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    public TimerAlarmClock(Timer listener, Date next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        this.listener = listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        this.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     *  PUBLIC METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * This method is called by the timer when it is started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            //this.sleep(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            TimerAlarmClockNotification notif = new TimerAlarmClockNotification(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            listener.notifyAlarmClock(notif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            TIMER_LOGGER.logp(Level.FINEST, Timer.class.getName(), "run",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    "Got unexpected exception when sending a notification", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
}