jdk/src/java.management/share/classes/javax/management/timer/Timer.java
author darcy
Thu, 16 Apr 2015 09:51:29 -0700
changeset 29927 9cc3e111a1d8
parent 25859 3317bb8137f4
child 43235 da1786d695b6
permissions -rw-r--r--
8077923: Add missing doclint in javax.management Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29927
9cc3e111a1d8 8077923: Add missing doclint in javax.management
darcy
parents: 25859
diff changeset
     2
 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1510
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1510
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1510
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1510
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1510
diff changeset
    23
 * questions.
2
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;
9690
5f9ce93ee91c 6659215: javax.management.timer.Timer frequently fails to start
kamg
parents: 5506
diff changeset
    29
import java.util.ArrayList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Date;
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
    31
import java.util.HashMap;
2
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
     */
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
   130
    final private Map<Integer,Object[]> timerTable =
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
   131
        new HashMap<>();
2
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
     */
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
   165
    volatile private int counterID = 0;
2
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
     *
29927
9cc3e111a1d8 8077923: Add missing doclint in javax.management
darcy
parents: 25859
diff changeset
   198
     * @exception java.lang.Exception if something goes wrong
2
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
     *
29927
9cc3e111a1d8 8077923: Add missing doclint in javax.management
darcy
parents: 25859
diff changeset
   220
     * @exception java.lang.Exception if something goes wrong
2
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>();
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   251
        for (Object[] entry : timerTable.values()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            TimerNotification notif = (TimerNotification)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                entry[TIMER_NOTIF_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            notifTypes.add(notif.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        String[] notifTypesArray =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            notifTypes.toArray(new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return new MBeanNotificationInfo[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            new MBeanNotificationInfo(notifTypesArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                      TimerNotification.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                      "Notification sent by Timer MBean")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        };
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
     * Starts the timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * 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
   269
     * is sent according to the <CODE>sendPastNotifications</CODE> flag and then, updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * according to its period and remaining number of occurrences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * If the timer notification date remains earlier than the current date, this notification is just removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * from the list of notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public synchronized void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                "start", "starting the timer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        // Start the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (isActive == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            timer = new java.util.Timer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            TimerAlarmClock alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            Date date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            Date currentDate = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // Send or not past notifications depending on the flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // Update the date and the number of occurrences of past notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            // to make them later than the current date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            sendPastNotifications(currentDate, sendPastNotifications);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            // Update and start all the TimerAlarmClocks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            // Here, all the notifications in the timer table are later than the current date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            for (Object[] obj : timerTable.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                // Retrieve the date notification and the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                date = (Date)obj[TIMER_DATE_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                // Update all the TimerAlarmClock timeouts and start them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                boolean fixedRate = ((Boolean)obj[FIXED_RATE_INDEX]).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                if (fixedRate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                  alarmClock = new TimerAlarmClock(this, date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                  obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                  timer.schedule(alarmClock, alarmClock.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                  alarmClock = new TimerAlarmClock(this, (date.getTime() - currentDate.getTime()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                  obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                  timer.schedule(alarmClock, alarmClock.timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            // Set the state to ON.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            isActive = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    "start", "timer started");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    "start", "the timer is already activated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
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
     * Stops the timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public synchronized void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                "stop", "stopping the timer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // Stop the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (isActive == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            for (Object[] obj : timerTable.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                // Stop all the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                //
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   350
                TimerAlarmClock alarmClock = (TimerAlarmClock)obj[ALARM_CLOCK_INDEX];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                if (alarmClock != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
//                     alarmClock.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
//                     try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
//                         // Wait until the thread die.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
//                         //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
//                         alarmClock.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
//                     } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
//                         // Ignore...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
//                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
//                     // Remove the reference on the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
//                     //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    alarmClock.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            timer.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            // Set the state to OFF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            isActive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    "stop", "timer stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    "stop", "the timer is already deactivated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
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
     * Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * and <CODE>userData</CODE> and inserts it into the list of notifications with a given date,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * period and number of occurrences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * If the timer notification to be inserted has a date that is before the current date,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * the method behaves as if the specified date were the current date. <BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * For once-off notifications, the notification is delivered immediately. <BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * For periodic notifications, the first notification is delivered immediately and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * subsequent ones are spaced as specified by the period parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Note that once the timer notification has been added into the list of notifications,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * its associated date, period and number of occurrences cannot be updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * In the case of a periodic notification, the value of parameter <i>fixedRate</i> is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * specify the execution scheme, as specified in {@link java.util.Timer}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @param type The timer notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @param message The timer notification detailed message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param userData The timer notification user data object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @param date The date when the notification occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @param period The period of the timer notification (in milliseconds).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @param nbOccurences The total number the timer notification will be emitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @param fixedRate If <code>true</code> and if the notification is periodic, the notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *                  is scheduled with a <i>fixed-rate</i> execution scheme. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *                  <code>false</code> and if the notification is periodic, the notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *                  is scheduled with a <i>fixed-delay</i> execution scheme. Ignored if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *                  notification is not periodic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @return The identifier of the new created timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @exception java.lang.IllegalArgumentException The date is {@code null} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * the period or the number of occurrences is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @see #addNotification(String, String, Object, Date, long, long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
// NPCTE fix for bugId 4464388, esc 0,  MR, to be added after modification of jmx spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
//  public synchronized Integer addNotification(String type, String message, Serializable userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
//                                                Date date, long period, long nbOccurences)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
// end of NPCTE fix for bugId 4464388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public synchronized Integer addNotification(String type, String message, Object userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                                                Date date, long period, long nbOccurences, boolean fixedRate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        throws java.lang.IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (date == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            throw new java.lang.IllegalArgumentException("Timer notification date cannot be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        // Check that all the timer notification attributes are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        // Invalid timer period value exception:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        // Check that the period and the nbOccurences are POSITIVE VALUES.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if ((period < 0) || (nbOccurences < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            throw new java.lang.IllegalArgumentException("Negative values for the periodicity");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        Date currentDate = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        // Update the date if it is before the current date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (currentDate.after(date)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            date.setTime(currentDate.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            if (TIMER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        "addNotification",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        "update timer notification to add with:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        "\n\tNotification date = " + date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // Create and add the timer notification into the timer table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        //
527
a33264d48a84 6703552: Missing files from changeset for 6701459
emcmanus
parents: 526
diff changeset
   457
        Integer notifID = Integer.valueOf(++counterID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        // The sequenceNumber and the timeStamp attributes are updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        // when the notification is emitted by the timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        TimerNotification notif = new TimerNotification(type, this, 0, 0, message, notifID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        notif.setUserData(userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        Object[] obj = new Object[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        TimerAlarmClock alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (fixedRate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
          alarmClock = new TimerAlarmClock(this, date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
          alarmClock = new TimerAlarmClock(this, (date.getTime() - currentDate.getTime()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        // Fix bug 00417.B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        // The date registered into the timer is a clone from the date parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        Date d = new Date(date.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        obj[TIMER_NOTIF_INDEX] = (Object)notif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        obj[TIMER_DATE_INDEX] = (Object)d;
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   484
        obj[TIMER_PERIOD_INDEX] = (Object) period;
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   485
        obj[TIMER_NB_OCCUR_INDEX] = (Object) nbOccurences;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        obj[FIXED_RATE_INDEX] = Boolean.valueOf(fixedRate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if (TIMER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            StringBuilder strb = new StringBuilder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            .append("adding timer notification:\n\t")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            .append("Notification source = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            .append(notif.getSource())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            .append("\n\tNotification type = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            .append(notif.getType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            .append("\n\tNotification ID = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            .append(notifID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            .append("\n\tNotification date = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            .append(d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            .append("\n\tNotification period = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            .append(period)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            .append("\n\tNotification nb of occurrences = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            .append(nbOccurences)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            .append("\n\tNotification executes at fixed rate = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            .append(fixedRate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    "addNotification", strb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        timerTable.put(notifID, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        // Update and start the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        if (isActive == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
          if (fixedRate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
          {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            timer.schedule(alarmClock, alarmClock.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
          else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
          {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            timer.schedule(alarmClock, alarmClock.timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                "addNotification", "timer notification added");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return notifID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * and <CODE>userData</CODE> and inserts it into the list of notifications with a given date,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * period and number of occurrences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * If the timer notification to be inserted has a date that is before the current date,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * the method behaves as if the specified date were the current date. <BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * For once-off notifications, the notification is delivered immediately. <BR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * For periodic notifications, the first notification is delivered immediately and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * subsequent ones are spaced as specified by the period parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * Note that once the timer notification has been added into the list of notifications,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * its associated date, period and number of occurrences cannot be updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * 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
   545
     * {@link java.util.Timer}. In order to use a <i>fixed-rate</i> execution scheme, use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * {@link #addNotification(String, String, Object, Date, long, long, boolean)} instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @param type The timer notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @param message The timer notification detailed message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @param userData The timer notification user data object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @param date The date when the notification occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @param period The period of the timer notification (in milliseconds).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @param nbOccurences The total number the timer notification will be emitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @return The identifier of the new created timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @exception java.lang.IllegalArgumentException The date is {@code null} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * the period or the number of occurrences is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @see #addNotification(String, String, Object, Date, long, long, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
// NPCTE fix for bugId 4464388, esc 0,  MR , to be added after modification of jmx spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
//  public synchronized Integer addNotification(String type, String message, Serializable userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
//                                              Date date, long period)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
// end of NPCTE fix for bugId 4464388 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public synchronized Integer addNotification(String type, String message, Object userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                                                Date date, long period, long nbOccurences)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        throws java.lang.IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
      return addNotification(type, message, userData, date, period, nbOccurences, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * and <CODE>userData</CODE> and inserts it into the list of notifications with a given date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * and period and a null number of occurrences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * The timer notification will repeat continuously using the timer period using a <i>fixed-delay</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * 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
   581
     * execution scheme, use {@link #addNotification(String, String, Object, Date, long, long,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * boolean)} instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * If the timer notification to be inserted has a date that is before the current date,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * the method behaves as if the specified date were the current date. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * first notification is delivered immediately and the subsequent ones are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * spaced as specified by the period parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @param type The timer notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @param message The timer notification detailed message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @param userData The timer notification user data object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @param date The date when the notification occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @param period The period of the timer notification (in milliseconds).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @return The identifier of the new created timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @exception java.lang.IllegalArgumentException The date is {@code null} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * the period is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
// NPCTE fix for bugId 4464388, esc 0,  MR , to be added after modification of jmx spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
//  public synchronized Integer addNotification(String type, String message, Serializable userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
//                                              Date date, long period)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
// end of NPCTE fix for bugId 4464388 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    public synchronized Integer addNotification(String type, String message, Object userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                                                Date date, long period)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        throws java.lang.IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        return (addNotification(type, message, userData, date, period, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * and <CODE>userData</CODE> and inserts it into the list of notifications with a given date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * and a null period and number of occurrences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * The timer notification will be handled once at the specified date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * If the timer notification to be inserted has a date that is before the current date,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * the method behaves as if the specified date were the current date and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * notification is delivered immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @param type The timer notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @param message The timer notification detailed message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @param userData The timer notification user data object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @param date The date when the notification occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @return The identifier of the new created timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @exception java.lang.IllegalArgumentException The date is {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
// NPCTE fix for bugId 4464388, esc 0,  MR, to be added after modification of jmx spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
//  public synchronized Integer addNotification(String type, String message, Serializable userData, Date date)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
//      throws java.lang.IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
// end of NPCTE fix for bugId 4464388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    public synchronized Integer addNotification(String type, String message, Object userData, Date date)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        throws java.lang.IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        return (addNotification(type, message, userData, date, 0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * Removes the timer notification corresponding to the specified identifier from the list of notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @exception InstanceNotFoundException The specified identifier does not correspond to any timer notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * in the list of notifications of this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    public synchronized void removeNotification(Integer id) throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        // Check that the notification to remove is effectively in the timer table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        if (timerTable.containsKey(id) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            throw new InstanceNotFoundException("Timer notification to remove not in the list of notifications");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        // Stop the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        TimerAlarmClock alarmClock = (TimerAlarmClock)obj[ALARM_CLOCK_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        if (alarmClock != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
//             alarmClock.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
//             try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
//                 // Wait until the thread die.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
//                 //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
//                 alarmClock.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
//             } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
//                 // Ignore...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
//             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
//             // Remove the reference on the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
//             //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            alarmClock.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        // Remove the timer notification from the timer table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        if (TIMER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            StringBuilder strb = new StringBuilder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            .append("removing timer notification:")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            .append("\n\tNotification source = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            .append(((TimerNotification)obj[TIMER_NOTIF_INDEX]).getSource())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            .append("\n\tNotification type = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            .append(((TimerNotification)obj[TIMER_NOTIF_INDEX]).getType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            .append("\n\tNotification ID = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            .append(((TimerNotification)obj[TIMER_NOTIF_INDEX]).getNotificationID())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            .append("\n\tNotification date = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            .append(obj[TIMER_DATE_INDEX])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            .append("\n\tNotification period = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            .append(obj[TIMER_PERIOD_INDEX])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            .append("\n\tNotification nb of occurrences = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            .append(obj[TIMER_NB_OCCUR_INDEX])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            .append("\n\tNotification executes at fixed rate = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            .append(obj[FIXED_RATE_INDEX]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    "removeNotification", strb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        timerTable.remove(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                "removeNotification", "timer notification removed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * Removes all the timer notifications corresponding to the specified type from the list of notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * @param type The timer notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * @exception InstanceNotFoundException The specified type does not correspond to any timer notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * in the list of notifications of this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    public synchronized void removeNotifications(String type) throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        Vector<Integer> v = getNotificationIDs(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        if (v.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            throw new InstanceNotFoundException("Timer notifications to remove not in the list of notifications");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        for (Integer i : v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            removeNotification(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * Removes all the timer notifications from the list of notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * and resets the counter used to update the timer notification identifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    public synchronized void removeAllNotifications() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        TimerAlarmClock alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        for (Object[] obj : timerTable.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            // Stop the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            alarmClock = (TimerAlarmClock)obj[ALARM_CLOCK_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
//             if (alarmClock != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
//                 alarmClock.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
//                 try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
//                     // Wait until the thread die.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
//                     //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
//                     alarmClock.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
//                 } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
//                     // Ignore...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
//                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                  // Remove the reference on the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                  //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
//             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            alarmClock.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        // Remove all the timer notifications from the timer table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                "removeAllNotifications", "removing all timer notifications");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        timerTable.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                "removeAllNotifications", "all timer notifications removed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        // Reset the counterID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        counterID = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                "removeAllNotifications", "timer notification counter ID reset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    // GETTERS AND SETTERS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    //--------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * Gets the number of timer notifications registered into the list of notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * @return The number of timer notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     */
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
   778
    public synchronized int getNbNotifications() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        return timerTable.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * Gets all timer notification identifiers registered into the list of notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @return A vector of <CODE>Integer</CODE> objects containing all the timer notification identifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * <BR>The vector is empty if there is no timer notification registered for this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    public synchronized Vector<Integer> getAllNotificationIDs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        return new Vector<Integer>(timerTable.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * Gets all the identifiers of timer notifications corresponding to the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @param type The timer notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * @return A vector of <CODE>Integer</CODE> objects containing all the identifiers of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * timer notifications with the specified <CODE>type</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * <BR>The vector is empty if there is no timer notifications registered for this timer MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * with the specified <CODE>type</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    public synchronized Vector<Integer> getNotificationIDs(String type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        String s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        Vector<Integer> v = new Vector<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        for (Map.Entry<Integer,Object[]> entry : timerTable.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            Object[] obj = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            s = ((TimerNotification)obj[TIMER_NOTIF_INDEX]).getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            if ((type == null) ? s == null : type.equals(s))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                v.addElement(entry.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    // 5089997: return is Vector<Integer> not Vector<TimerNotification>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * Gets the timer notification type corresponding to the specified identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @return The timer notification type or null if the identifier is not mapped to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * timer notification registered for this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     */
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
   826
    public synchronized String getNotificationType(Integer id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            return ( ((TimerNotification)obj[TIMER_NOTIF_INDEX]).getType() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * Gets the timer notification detailed message corresponding to the specified identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * @return The timer notification detailed message or null if the identifier is not mapped to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * timer notification registered for this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     */
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
   843
    public synchronized String getNotificationMessage(Integer id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            return ( ((TimerNotification)obj[TIMER_NOTIF_INDEX]).getMessage() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * Gets the timer notification user data object corresponding to the specified identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @return The timer notification user data object or null if the identifier is not mapped to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * timer notification registered for this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    // 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
   861
    //public Serializable getNotificationUserData(Integer id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    // end of NPCTE fix for bugId 4464388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
   864
    public synchronized Object getNotificationUserData(Integer id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            return ( ((TimerNotification)obj[TIMER_NOTIF_INDEX]).getUserData() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * Gets a copy of the date associated to a timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * @return A copy of the date or null if the identifier is not mapped to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * timer notification registered for this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     */
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
   880
    public synchronized Date getDate(Integer id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            Date date = (Date)obj[TIMER_DATE_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            return (new Date(date.getTime()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * Gets a copy of the period (in milliseconds) associated to a timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @return A copy of the period or null if the identifier is not mapped to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * timer notification registered for this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     */
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
   898
    public synchronized Long getPeriod(Integer id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        if (obj != null) {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   902
            return (Long)obj[TIMER_PERIOD_INDEX];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * Gets a copy of the remaining number of occurrences associated to a timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * @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
   913
     * timer notification registered for this timer MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     */
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
   915
    public synchronized Long getNbOccurences(Integer id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        if (obj != null) {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   919
            return (Long)obj[TIMER_NB_OCCUR_INDEX];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * Gets a copy of the flag indicating whether a periodic notification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * executed at <i>fixed-delay</i> or at <i>fixed-rate</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * @param id The timer notification identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * @return A copy of the flag indicating whether a periodic notification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     *         executed at <i>fixed-delay</i> or at <i>fixed-rate</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     */
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
   933
    public synchronized Boolean getFixedRate(Integer id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
      Object[] obj = timerTable.get(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
      if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        Boolean fixedRate = (Boolean)obj[FIXED_RATE_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        return (Boolean.valueOf(fixedRate.booleanValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
      return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * Gets the flag indicating whether or not the timer sends past notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * <BR>The default value of the past notifications sending on/off flag is <CODE>false</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * @return The past notifications sending on/off flag value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @see #setSendPastNotifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    public boolean getSendPastNotifications() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        return sendPastNotifications;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * Sets the flag indicating whether the timer sends past notifications or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * <BR>The default value of the past notifications sending on/off flag is <CODE>false</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * @param value The past notifications sending on/off flag value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * @see #getSendPastNotifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    public void setSendPastNotifications(boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        sendPastNotifications = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * Tests whether the timer MBean is active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * A timer MBean is marked active when the {@link #start start} method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * It becomes inactive when the {@link #stop stop} method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * <BR>The default value of the active on/off flag is <CODE>false</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * @return <CODE>true</CODE> if the timer MBean is active, <CODE>false</CODE> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    public boolean isActive() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        return isActive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * Tests whether the list of timer notifications is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * @return <CODE>true</CODE> if the list of timer notifications is empty, <CODE>false</CODE> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     */
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
   984
    public synchronized boolean isEmpty() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        return (timerTable.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    }
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
     *  PRIVATE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * ------------------------------------------
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
     * Sends or not past notifications depending on the specified flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * @param currentDate The current date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * @param currentFlag The flag indicating if past notifications must be sent or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    private synchronized void sendPastNotifications(Date currentDate, boolean currentFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        TimerNotification notif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        Integer notifID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        Date date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
9690
5f9ce93ee91c 6659215: javax.management.timer.Timer frequently fails to start
kamg
parents: 5506
diff changeset
  1006
        ArrayList<Object[]> values =
5f9ce93ee91c 6659215: javax.management.timer.Timer frequently fails to start
kamg
parents: 5506
diff changeset
  1007
            new ArrayList<Object[]>(timerTable.values());
5f9ce93ee91c 6659215: javax.management.timer.Timer frequently fails to start
kamg
parents: 5506
diff changeset
  1008
5f9ce93ee91c 6659215: javax.management.timer.Timer frequently fails to start
kamg
parents: 5506
diff changeset
  1009
        for (Object[] obj : values) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            // Retrieve the timer notification and the date notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            notif = (TimerNotification)obj[TIMER_NOTIF_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            notifID = notif.getNotificationID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            date = (Date)obj[TIMER_DATE_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            // Update the timer notification while:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            //  - the timer notification date is earlier than the current date
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            //  - the timer notification has not been removed from the timer table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            while ( (currentDate.after(date)) && (timerTable.containsKey(notifID)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                if (currentFlag == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                    if (TIMER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                        StringBuilder strb = new StringBuilder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                        .append("sending past timer notification:")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                        .append("\n\tNotification source = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                        .append(notif.getSource())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                        .append("\n\tNotification type = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                        .append(notif.getType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                        .append("\n\tNotification ID = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                        .append(notif.getNotificationID())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                        .append("\n\tNotification date = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                        .append(date)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                        .append("\n\tNotification period = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                        .append(obj[TIMER_PERIOD_INDEX])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                        .append("\n\tNotification nb of occurrences = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                        .append(obj[TIMER_NB_OCCUR_INDEX])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                        .append("\n\tNotification executes at fixed rate = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                        .append(obj[FIXED_RATE_INDEX]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                                "sendPastNotifications", strb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                    sendNotification(date, notif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                    TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                            "sendPastNotifications", "past timer notification sent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                // Update the date and the number of occurrences of the timer notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                updateTimerTable(notif.getNotificationID());
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * If the timer notification is not periodic, it is removed from the list of notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * If the timer period of the timer notification has a non null periodicity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * the date of the timer notification is updated by adding the periodicity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * The associated TimerAlarmClock is updated by setting its timeout to the period value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * If the timer period has a defined number of occurrences, the timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * notification is updated if the number of occurrences has not yet been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * Otherwise it is removed from the list of notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * @param notifID The timer notification identifier to update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    private synchronized void updateTimerTable(Integer notifID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        // Retrieve the timer notification and the TimerAlarmClock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        Object[] obj = timerTable.get(notifID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        Date date = (Date)obj[TIMER_DATE_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        Long period = (Long)obj[TIMER_PERIOD_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        Long nbOccurences = (Long)obj[TIMER_NB_OCCUR_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        Boolean fixedRate = (Boolean)obj[FIXED_RATE_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        TimerAlarmClock alarmClock = (TimerAlarmClock)obj[ALARM_CLOCK_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        if (period.longValue() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            // Update the date and the number of occurrences of the timer notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            // and the TimerAlarmClock time out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            // NOTES :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            //   nbOccurences = 0 notifies an infinite periodicity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            //   nbOccurences = 1 notifies a finite periodicity that has reached its end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            //   nbOccurences > 1 notifies a finite periodicity that has not yet reached its end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            if ((nbOccurences.longValue() == 0) || (nbOccurences.longValue() > 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                date.setTime(date.getTime() + period.longValue());
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
  1093
                obj[TIMER_NB_OCCUR_INDEX] = Long.valueOf(java.lang.Math.max(0L, (nbOccurences.longValue() - 1)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                nbOccurences = (Long)obj[TIMER_NB_OCCUR_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                if (isActive == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                  if (fixedRate.booleanValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                    alarmClock = new TimerAlarmClock(this, date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                    obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                    timer.schedule(alarmClock, alarmClock.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                  else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                    alarmClock = new TimerAlarmClock(this, period.longValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                    obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                    timer.schedule(alarmClock, alarmClock.timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                if (TIMER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                    TimerNotification notif = (TimerNotification)obj[TIMER_NOTIF_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                    StringBuilder strb = new StringBuilder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                    .append("update timer notification with:")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                    .append("\n\tNotification source = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                    .append(notif.getSource())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                    .append("\n\tNotification type = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                    .append(notif.getType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                    .append("\n\tNotification ID = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                    .append(notifID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                    .append("\n\tNotification date = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                    .append(date)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                    .append("\n\tNotification period = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                    .append(period)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                    .append("\n\tNotification nb of occurrences = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                    .append(nbOccurences)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                    .append("\n\tNotification executes at fixed rate = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                    .append(fixedRate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                    TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                            "updateTimerTable", strb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                if (alarmClock != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
//                     alarmClock.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
//                     try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
//                         // Wait until the thread die.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
//                         //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
//                         alarmClock.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
//                     } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
//                         // Ignore...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
//                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                    alarmClock.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                timerTable.remove(notifID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            if (alarmClock != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
//                 alarmClock.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
//                 try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
//                     // Wait until the thread die.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
//                     //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
//                     alarmClock.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
//                 } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
//                     // Ignore...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
//                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                   alarmClock.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            timerTable.remove(notifID);
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     *  PACKAGE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * This method is called by the timer each time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * the TimerAlarmClock has exceeded its timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * @param notification The TimerAlarmClock notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    @SuppressWarnings("deprecation")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    void notifyAlarmClock(TimerAlarmClockNotification notification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        TimerNotification timerNotification = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        Date timerDate = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        // Retrieve the timer notification associated to the alarm-clock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        TimerAlarmClock alarmClock = (TimerAlarmClock)notification.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
  1186
        synchronized(Timer.this) {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
  1187
            for (Object[] obj : timerTable.values()) {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
  1188
                if (obj[ALARM_CLOCK_INDEX] == alarmClock) {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
  1189
                    timerNotification = (TimerNotification)obj[TIMER_NOTIF_INDEX];
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
  1190
                    timerDate = (Date)obj[TIMER_DATE_INDEX];
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
  1191
                    break;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents: 9690
diff changeset
  1192
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        // Notify the timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        sendNotification(timerDate, timerNotification);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        // Update the notification and the TimerAlarmClock timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        updateTimerTable(timerNotification.getNotificationID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * This method is used by the timer MBean to update and send a timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * notification to all the listeners registered for this kind of notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * @param timeStamp The notification emission date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * @param notification The timer notification to send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    void sendNotification(Date timeStamp, TimerNotification notification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        if (TIMER_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            StringBuilder strb = new StringBuilder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            .append("sending timer notification:")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            .append("\n\tNotification source = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            .append(notification.getSource())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            .append("\n\tNotification type = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            .append(notification.getType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            .append("\n\tNotification ID = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            .append(notification.getNotificationID())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            .append("\n\tNotification date = ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            .append(timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                    "sendNotification", strb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        long curSeqNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            sequenceNumber = sequenceNumber + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            curSeqNumber = sequenceNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        synchronized (notification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            notification.setTimeStamp(timeStamp.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            notification.setSequenceNumber(curSeqNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            this.sendNotification((TimerNotification)notification.cloneTimerNotification());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                "sendNotification", "timer notification sent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
}