/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// jmx imports
//
/**
*
* Provides the implementation of the timer MBean.
* The timer MBean sends out an alarm at a specified time
* that wakes up all the listeners registered to receive timer notifications.
* <P>
*
* This class manages a list of dated timer notifications.
* When a timer notification is emitted by the timer and becomes obsolete,
* it is automatically removed from the list of timer notifications.
* <BR>Additional timer notifications can be added into regularly repeating notifications.
* <P>
*
* Note:
* <OL>
* <LI>When sending timer notifications, the timer updates the notification sequence number
* irrespective of the notification type.
* <LI>The timer service relies on the system date of the host where the <CODE>Timer</CODE> class is loaded.
* Listeners may receive untimely notifications
* if their host has a different system date.
* To avoid such problems, synchronize the system date of all host machines where timing is needed.
* <LI>The default behavior for periodic notifications is <i>fixed-delay execution</i>, as
* specified in {@link java.util.Timer}. In order to use <i>fixed-rate execution</i>, use the
* overloaded {@link #addNotification(String, String, Object, Date, long, long, boolean)} method.
* <LI>Notification listeners are potentially all executed in the same
* thread. Therefore, they should execute rapidly to avoid holding up
* other listeners or perturbing the regularity of fixed-delay
* executions. See {@link NotificationBroadcasterSupport}.
* </OL>
*
* @since 1.5
*/
implements TimerMBean, MBeanRegistration {
/*
* ------------------------------------------
* PUBLIC VARIABLES
* ------------------------------------------
*/
/**
* Number of milliseconds in one second.
* Useful constant for the <CODE>addNotification</CODE> method.
*/
/**
* Number of milliseconds in one minute.
* Useful constant for the <CODE>addNotification</CODE> method.
*/
/**
* Number of milliseconds in one hour.
* Useful constant for the <CODE>addNotification</CODE> method.
*/
/**
* Number of milliseconds in one day.
* Useful constant for the <CODE>addNotification</CODE> method.
*/
/**
* Number of milliseconds in one week.
* Useful constant for the <CODE>addNotification</CODE> method.
*/
/*
* ------------------------------------------
* PRIVATE VARIABLES
* ------------------------------------------
*/
/**
* Table containing all the timer notifications of this timer,
* with the associated date, period and number of occurrences.
*/
/**
* This attribute is used to specify if the timer has to send past notifications after start.
* <BR>The default value is set to <CODE>false</CODE>.
*/
private boolean sendPastNotifications = false;
/**
* Timer state.
* The default value is set to <CODE>false</CODE>.
*/
private transient boolean isActive = false;
/**
* Timer sequence number.
* The default value is set to 0.
*/
// Flags needed to keep the indexes of the objects in the array.
//
/**
* The notification counter ID.
* Used to keep the max key value inserted into the timer table.
*/
/*
* ------------------------------------------
* CONSTRUCTORS
* ------------------------------------------
*/
/**
* Default constructor.
*/
public Timer() {
}
/*
* ------------------------------------------
* PUBLIC METHODS
* ------------------------------------------
*/
/**
* Allows the timer MBean to perform any operations it needs before being registered
* in the MBean server.
* <P>
* Not used in this context.
*
* @param server The MBean server in which the timer MBean will be registered.
* @param name The object name of the timer MBean.
*
* @return The name of the timer MBean registered.
*
* @exception java.lang.Exception
*/
return name;
}
/**
* Allows the timer MBean to perform any operations needed after having been
* registered in the MBean server or after the registration has failed.
* <P>
* Not used in this context.
*/
}
/**
* Allows the timer MBean to perform any operations it needs before being unregistered
* by the MBean server.
* <P>
* Stops the timer.
*
* @exception java.lang.Exception
*/
"preDeregister", "stop the timer");
// Stop the timer.
//
stop();
}
/**
* Allows the timer MBean to perform any operations needed after having been
* unregistered by the MBean server.
* <P>
* Not used in this context.
*/
public void postDeregister() {
}
/*
* This overrides the method in NotificationBroadcasterSupport.
* Return the MBeanNotificationInfo[] array for this MBean.
* The returned array has one element to indicate that the MBean
* can emit TimerNotification. The array of type strings
* associated with this entry is a snapshot of the current types
* that were given to addNotification.
*/
}
return new MBeanNotificationInfo[] {
TimerNotification.class.getName(),
"Notification sent by Timer MBean")
};
}
/**
* Starts the timer.
* <P>
* If there is one or more timer notifications before the time in the list of notifications, the notification
* is sent according to the <CODE>sendPastNotifications</CODE> flag and then, updated
* according to its period and remaining number of occurrences.
* If the timer notification date remains earlier than the current date, this notification is just removed
* from the list of notifications.
*/
public synchronized void start() {
"start", "starting the timer");
// Start the TimerAlarmClock.
//
if (isActive == false) {
// Send or not past notifications depending on the flag.
// Update the date and the number of occurrences of past notifications
// to make them later than the current date.
//
// Update and start all the TimerAlarmClocks.
// Here, all the notifications in the timer table are later than the current date.
//
// Retrieve the date notification and the TimerAlarmClock.
//
// Update all the TimerAlarmClock timeouts and start them.
//
if (fixedRate)
{
}
else
{
}
}
// Set the state to ON.
//
isActive = true;
"start", "timer started");
} else {
"start", "the timer is already activated");
}
}
/**
* Stops the timer.
*/
public synchronized void stop() {
"stop", "stopping the timer");
// Stop the TimerAlarmClock.
//
if (isActive == true) {
// Stop all the TimerAlarmClock.
//
if (alarmClock != null) {
// alarmClock.interrupt();
// try {
// // Wait until the thread die.
// //
// alarmClock.join();
// } catch (InterruptedException ex) {
// // Ignore...
// }
// // Remove the reference on the TimerAlarmClock.
// //
alarmClock.cancel();
}
}
// Set the state to OFF.
//
isActive = false;
"stop", "timer stopped");
} else {
"stop", "the timer is already deactivated");
}
}
/**
* Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
* and <CODE>userData</CODE> and inserts it into the list of notifications with a given date,
* period and number of occurrences.
* <P>
* If the timer notification to be inserted has a date that is before the current date,
* the method behaves as if the specified date were the current date. <BR>
* For once-off notifications, the notification is delivered immediately. <BR>
* For periodic notifications, the first notification is delivered immediately and the
* subsequent ones are spaced as specified by the period parameter.
* <P>
* Note that once the timer notification has been added into the list of notifications,
* its associated date, period and number of occurrences cannot be updated.
* <P>
* In the case of a periodic notification, the value of parameter <i>fixedRate</i> is used to
* specify the execution scheme, as specified in {@link java.util.Timer}.
*
* @param type The timer notification type.
* @param message The timer notification detailed message.
* @param userData The timer notification user data object.
* @param date The date when the notification occurs.
* @param period The period of the timer notification (in milliseconds).
* @param nbOccurences The total number the timer notification will be emitted.
* @param fixedRate If <code>true</code> and if the notification is periodic, the notification
* is scheduled with a <i>fixed-rate</i> execution scheme. If
* <code>false</code> and if the notification is periodic, the notification
* is scheduled with a <i>fixed-delay</i> execution scheme. Ignored if the
* notification is not periodic.
*
* @return The identifier of the new created timer notification.
*
* @exception java.lang.IllegalArgumentException The date is {@code null} or
* the period or the number of occurrences is negative.
*
* @see #addNotification(String, String, Object, Date, long, long)
*/
// NPCTE fix for bugId 4464388, esc 0, MR, to be added after modification of jmx spec
// public synchronized Integer addNotification(String type, String message, Serializable userData,
// Date date, long period, long nbOccurences)
// end of NPCTE fix for bugId 4464388
}
// Check that all the timer notification attributes are valid.
//
// Invalid timer period value exception:
// Check that the period and the nbOccurences are POSITIVE VALUES.
//
}
// Update the date if it is before the current date.
//
"addNotification",
"update timer notification to add with:" +
"\n\tNotification date = " + date);
}
}
// Create and add the timer notification into the timer table.
//
// The sequenceNumber and the timeStamp attributes are updated
// when the notification is emitted by the timer.
//
if (fixedRate)
{
}
else
{
}
// Fix bug 00417.B
// The date registered into the timer is a clone from the date parameter.
//
.append("adding timer notification:\n\t")
.append("Notification source = ")
.append("\n\tNotification type = ")
.append("\n\tNotification ID = ")
.append("\n\tNotification date = ")
.append(d)
.append("\n\tNotification period = ")
.append("\n\tNotification nb of occurrences = ")
.append("\n\tNotification executes at fixed rate = ")
}
// Update and start the TimerAlarmClock.
//
if (isActive == true) {
if (fixedRate)
{
}
else
{
}
}
"addNotification", "timer notification added");
return notifID;
}
/**
* Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
* and <CODE>userData</CODE> and inserts it into the list of notifications with a given date,
* period and number of occurrences.
* <P>
* If the timer notification to be inserted has a date that is before the current date,
* the method behaves as if the specified date were the current date. <BR>
* For once-off notifications, the notification is delivered immediately. <BR>
* For periodic notifications, the first notification is delivered immediately and the
* subsequent ones are spaced as specified by the period parameter.
* <P>
* Note that once the timer notification has been added into the list of notifications,
* its associated date, period and number of occurrences cannot be updated.
* <P>
* In the case of a periodic notification, uses a <i>fixed-delay</i> execution scheme, as specified in
* {@link java.util.Timer}. In order to use a <i>fixed-rate</i> execution scheme, use
* {@link #addNotification(String, String, Object, Date, long, long, boolean)} instead.
*
* @param type The timer notification type.
* @param message The timer notification detailed message.
* @param userData The timer notification user data object.
* @param date The date when the notification occurs.
* @param period The period of the timer notification (in milliseconds).
* @param nbOccurences The total number the timer notification will be emitted.
*
* @return The identifier of the new created timer notification.
*
* @exception java.lang.IllegalArgumentException The date is {@code null} or
* the period or the number of occurrences is negative.
*
* @see #addNotification(String, String, Object, Date, long, long, boolean)
*/
// NPCTE fix for bugId 4464388, esc 0, MR , to be added after modification of jmx spec
// public synchronized Integer addNotification(String type, String message, Serializable userData,
// Date date, long period)
// end of NPCTE fix for bugId 4464388 */
}
/**
* Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
* and <CODE>userData</CODE> and inserts it into the list of notifications with a given date
* and period and a null number of occurrences.
* <P>
* The timer notification will repeat continuously using the timer period using a <i>fixed-delay</i>
* execution scheme, as specified in {@link java.util.Timer}. In order to use a <i>fixed-rate</i>
* execution scheme, use {@link #addNotification(String, String, Object, Date, long, long,
* boolean)} instead.
* <P>
* If the timer notification to be inserted has a date that is before the current date,
* the method behaves as if the specified date were the current date. The
* first notification is delivered immediately and the subsequent ones are
* spaced as specified by the period parameter.
*
* @param type The timer notification type.
* @param message The timer notification detailed message.
* @param userData The timer notification user data object.
* @param date The date when the notification occurs.
* @param period The period of the timer notification (in milliseconds).
*
* @return The identifier of the new created timer notification.
*
* @exception java.lang.IllegalArgumentException The date is {@code null} or
* the period is negative.
*/
// NPCTE fix for bugId 4464388, esc 0, MR , to be added after modification of jmx spec
// public synchronized Integer addNotification(String type, String message, Serializable userData,
// Date date, long period)
// end of NPCTE fix for bugId 4464388 */
}
/**
* Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
* and <CODE>userData</CODE> and inserts it into the list of notifications with a given date
* and a null period and number of occurrences.
* <P>
* The timer notification will be handled once at the specified date.
* <P>
* If the timer notification to be inserted has a date that is before the current date,
* the method behaves as if the specified date were the current date and the
* notification is delivered immediately.
*
* @param type The timer notification type.
* @param message The timer notification detailed message.
* @param userData The timer notification user data object.
* @param date The date when the notification occurs.
*
* @return The identifier of the new created timer notification.
*
* @exception java.lang.IllegalArgumentException The date is {@code null}.
*/
// NPCTE fix for bugId 4464388, esc 0, MR, to be added after modification of jmx spec
// public synchronized Integer addNotification(String type, String message, Serializable userData, Date date)
// throws java.lang.IllegalArgumentException {
// end of NPCTE fix for bugId 4464388
public synchronized Integer addNotification(String type, String message, Object userData, Date date)
}
/**
* Removes the timer notification corresponding to the specified identifier from the list of notifications.
*
* @param id The timer notification identifier.
*
* @exception InstanceNotFoundException The specified identifier does not correspond to any timer notification
* in the list of notifications of this timer MBean.
*/
// Check that the notification to remove is effectively in the timer table.
//
throw new InstanceNotFoundException("Timer notification to remove not in the list of notifications");
}
// Stop the TimerAlarmClock.
//
if (alarmClock != null) {
// alarmClock.interrupt();
// try {
// // Wait until the thread die.
// //
// alarmClock.join();
// } catch (InterruptedException e) {
// // Ignore...
// }
// // Remove the reference on the TimerAlarmClock.
// //
alarmClock.cancel();
}
// Remove the timer notification from the timer table.
//
.append("removing timer notification:")
.append("\n\tNotification source = ")
.append("\n\tNotification type = ")
.append("\n\tNotification ID = ")
.append("\n\tNotification date = ")
.append("\n\tNotification period = ")
.append("\n\tNotification nb of occurrences = ")
.append("\n\tNotification executes at fixed rate = ")
}
"removeNotification", "timer notification removed");
}
/**
* Removes all the timer notifications corresponding to the specified type from the list of notifications.
*
* @param type The timer notification type.
*
* @exception InstanceNotFoundException The specified type does not correspond to any timer notification
* in the list of notifications of this timer MBean.
*/
if (v.isEmpty())
throw new InstanceNotFoundException("Timer notifications to remove not in the list of notifications");
for (Integer i : v)
}
/**
* Removes all the timer notifications from the list of notifications
* and resets the counter used to update the timer notification identifiers.
*/
public synchronized void removeAllNotifications() {
// Stop the TimerAlarmClock.
//
// if (alarmClock != null) {
// alarmClock.interrupt();
// try {
// // Wait until the thread die.
// //
// alarmClock.join();
// } catch (InterruptedException ex) {
// // Ignore...
// }
// Remove the reference on the TimerAlarmClock.
//
// }
alarmClock.cancel();
}
// Remove all the timer notifications from the timer table.
"removeAllNotifications", "removing all timer notifications");
timerTable.clear();
"removeAllNotifications", "all timer notifications removed");
// Reset the counterID.
//
counterID = 0;
"removeAllNotifications", "timer notification counter ID reset");
}
// GETTERS AND SETTERS
//--------------------
/**
* Gets the number of timer notifications registered into the list of notifications.
*
* @return The number of timer notifications.
*/
public int getNbNotifications() {
return timerTable.size();
}
/**
* Gets all timer notification identifiers registered into the list of notifications.
*
* @return A vector of <CODE>Integer</CODE> objects containing all the timer notification identifiers.
* <BR>The vector is empty if there is no timer notification registered for this timer MBean.
*/
}
/**
* Gets all the identifiers of timer notifications corresponding to the specified type.
*
* @param type The timer notification type.
*
* @return A vector of <CODE>Integer</CODE> objects containing all the identifiers of
* timer notifications with the specified <CODE>type</CODE>.
* <BR>The vector is empty if there is no timer notifications registered for this timer MBean
* with the specified <CODE>type</CODE>.
*/
String s;
}
return v;
}
// 5089997: return is Vector<Integer> not Vector<TimerNotification>
/**
* Gets the timer notification type corresponding to the specified identifier.
*
* @param id The timer notification identifier.
*
* @return The timer notification type or null if the identifier is not mapped to any
* timer notification registered for this timer MBean.
*/
}
return null;
}
/**
* Gets the timer notification detailed message corresponding to the specified identifier.
*
* @param id The timer notification identifier.
*
* @return The timer notification detailed message or null if the identifier is not mapped to any
* timer notification registered for this timer MBean.
*/
}
return null;
}
/**
* Gets the timer notification user data object corresponding to the specified identifier.
*
* @param id The timer notification identifier.
*
* @return The timer notification user data object or null if the identifier is not mapped to any
* timer notification registered for this timer MBean.
*/
// NPCTE fix for bugId 4464388, esc 0, MR, 03 sept 2001, to be added after modification of jmx spec
//public Serializable getNotificationUserData(Integer id) {
// end of NPCTE fix for bugId 4464388
}
return null;
}
/**
* Gets a copy of the date associated to a timer notification.
*
* @param id The timer notification identifier.
*
* @return A copy of the date or null if the identifier is not mapped to any
* timer notification registered for this timer MBean.
*/
}
return null;
}
/**
* Gets a copy of the period (in milliseconds) associated to a timer notification.
*
* @param id The timer notification identifier.
*
* @return A copy of the period or null if the identifier is not mapped to any
* timer notification registered for this timer MBean.
*/
}
return null;
}
/**
* Gets a copy of the remaining number of occurrences associated to a timer notification.
*
* @param id The timer notification identifier.
*
* @return A copy of the remaining number of occurrences or null if the identifier is not mapped to any
* timer notification registered for this timer MBean.
*/
}
return null;
}
/**
* Gets a copy of the flag indicating whether a periodic notification is
* executed at <i>fixed-delay</i> or at <i>fixed-rate</i>.
*
* @param id The timer notification identifier.
*
* @return A copy of the flag indicating whether a periodic notification is
* executed at <i>fixed-delay</i> or at <i>fixed-rate</i>.
*/
}
return null;
}
/**
* Gets the flag indicating whether or not the timer sends past notifications.
*
*
* @see #setSendPastNotifications
*/
public boolean getSendPastNotifications() {
return sendPastNotifications;
}
/**
* Sets the flag indicating whether the timer sends past notifications or not.
*
*
* @see #getSendPastNotifications
*/
}
/**
* Tests whether the timer MBean is active.
* A timer MBean is marked active when the {@link #start start} method is called.
* It becomes inactive when the {@link #stop stop} method is called.
*
* @return <CODE>true</CODE> if the timer MBean is active, <CODE>false</CODE> otherwise.
*/
public boolean isActive() {
return isActive;
}
/**
* Tests whether the list of timer notifications is empty.
*
* @return <CODE>true</CODE> if the list of timer notifications is empty, <CODE>false</CODE> otherwise.
*/
public boolean isEmpty() {
return (timerTable.isEmpty());
}
/*
* ------------------------------------------
* PRIVATE METHODS
* ------------------------------------------
*/
/**
* Sends or not past notifications depending on the specified flag.
*
* @param currentDate The current date.
* @param currentFlag The flag indicating if past notifications must be sent or not.
*/
// Retrieve the timer notification and the date notification.
//
// Update the timer notification while:
// - the timer notification date is earlier than the current date
// - the timer notification has not been removed from the timer table.
//
if (currentFlag == true) {
.append("sending past timer notification:")
.append("\n\tNotification source = ")
.append("\n\tNotification type = ")
.append("\n\tNotification ID = ")
.append("\n\tNotification date = ")
.append("\n\tNotification period = ")
.append("\n\tNotification nb of occurrences = ")
.append("\n\tNotification executes at fixed rate = ")
}
"sendPastNotifications", "past timer notification sent");
}
// Update the date and the number of occurrences of the timer notification.
//
}
}
}
/**
* If the timer notification is not periodic, it is removed from the list of notifications.
* <P>
* If the timer period of the timer notification has a non null periodicity,
* the date of the timer notification is updated by adding the periodicity.
* The associated TimerAlarmClock is updated by setting its timeout to the period value.
* <P>
* If the timer period has a defined number of occurrences, the timer
* notification is updated if the number of occurrences has not yet been reached.
* Otherwise it is removed from the list of notifications.
*
* @param notifID The timer notification identifier to update.
*/
// Retrieve the timer notification and the TimerAlarmClock.
//
// Update the date and the number of occurrences of the timer notification
// and the TimerAlarmClock time out.
// NOTES :
// nbOccurences = 0 notifies an infinite periodicity.
// nbOccurences = 1 notifies a finite periodicity that has reached its end.
// nbOccurences > 1 notifies a finite periodicity that has not yet reached its end.
//
if (isActive == true) {
if (fixedRate.booleanValue())
{
}
else
{
}
}
.append("update timer notification with:")
.append("\n\tNotification source = ")
.append("\n\tNotification type = ")
.append("\n\tNotification ID = ")
.append("\n\tNotification date = ")
.append("\n\tNotification period = ")
.append("\n\tNotification nb of occurrences = ")
.append("\n\tNotification executes at fixed rate = ")
}
}
else {
if (alarmClock != null) {
// alarmClock.interrupt();
// try {
// // Wait until the thread die.
// //
// alarmClock.join();
// } catch (InterruptedException e) {
// // Ignore...
// }
alarmClock.cancel();
}
}
}
else {
if (alarmClock != null) {
// alarmClock.interrupt();
// try {
// // Wait until the thread die.
// //
// alarmClock.join();
// } catch (InterruptedException e) {
// // Ignore...
// }
alarmClock.cancel();
}
}
}
/*
* ------------------------------------------
* PACKAGE METHODS
* ------------------------------------------
*/
/**
* This method is called by the timer each time
* the TimerAlarmClock has exceeded its timeout.
*
* @param notification The TimerAlarmClock notification.
*/
@SuppressWarnings("deprecation")
// Retrieve the timer notification associated to the alarm-clock.
//
break;
}
}
// Notify the timer.
//
// Update the notification and the TimerAlarmClock timeout.
//
}
/**
* This method is used by the timer MBean to update and send a timer
* notification to all the listeners registered for this kind of notification.
*
* @param timeStamp The notification emission date.
* @param notification The timer notification to send.
*/
.append("sending timer notification:")
.append("\n\tNotification source = ")
.append("\n\tNotification type = ")
.append("\n\tNotification ID = ")
.append("\n\tNotification date = ")
}
long curSeqNumber;
synchronized(this) {
}
synchronized (notification) {
}
"sendNotification", "timer notification sent");
}
}
/**
* TimerAlarmClock inner class:
* This class provides a simple implementation of an alarm clock MBean.
* The aim of this MBean is to set up an alarm which wakes up the timer every timeout (fixed-delay)
* or at the specified date (fixed-rate).
*/
/*
* ------------------------------------------
* CONSTRUCTORS
* ------------------------------------------
*/
}
}
/*
* ------------------------------------------
* PUBLIC METHODS
* ------------------------------------------
*/
/**
* This method is called by the timer when it is started.
*/
public void run() {
try {
//this.sleep(timeout);
} catch (Exception e) {
"Got unexpected exception when sending a notification", e);
}
}
}