4235N/A/*
4235N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4235N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4235N/A *
4235N/A * This code is free software; you can redistribute it and/or modify it
4235N/A * under the terms of the GNU General Public License version 2 only, as
4235N/A * published by the Free Software Foundation. Oracle designates this
4235N/A * particular file as subject to the "Classpath" exception as provided
4235N/A * by Oracle in the LICENSE file that accompanied this code.
4235N/A *
4235N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4235N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4235N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4235N/A * version 2 for more details (a copy is included in the LICENSE file that
4235N/A * accompanied this code).
4235N/A *
4235N/A * You should have received a copy of the GNU General Public License version
4235N/A * 2 along with this work; if not, write to the Free Software Foundation,
4235N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4235N/A *
4235N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4235N/A * or visit www.oracle.com if you need additional information or have any
4235N/A * questions.
4235N/A */
4235N/A
4235N/Apackage com.sun.management;
4235N/A
4235N/Aimport javax.management.Notification;
4235N/Aimport javax.management.openmbean.CompositeData;
4235N/Aimport javax.management.openmbean.CompositeDataView;
4235N/Aimport javax.management.openmbean.CompositeType;
4235N/Aimport java.util.Collection;
4235N/Aimport java.util.Collections;
4235N/Aimport sun.management.GarbageCollectionNotifInfoCompositeData;
4235N/A
4235N/A/**
4235N/A * The information about a garbage collection
4235N/A *
4235N/A * <p>
4235N/A * A garbage collection notification is emitted by {@link GarbageCollectorMXBean}
4235N/A * when the Java virtual machine completes a garbage collection action
4235N/A * The notification emitted will contain the garbage collection notification
4235N/A * information about the status of the memory:
4235N/A * <u1>
4235N/A * <li>The name of the garbage collector used perform the collection.</li>
4235N/A * <li>The action performed by the garbage collector.</li>
4235N/A * <li>The cause of the garbage collection action.</li>
4235N/A * <li>A {@link GcInfo} object containing some statistics about the GC cycle
4235N/A (start time, end time) and the memory usage before and after
4235N/A the GC cycle.</li>
4235N/A * </u1>
4235N/A *
4235N/A * <p>
4235N/A * A {@link CompositeData CompositeData} representing
4235N/A * the {@code GarbageCollectionNotificationInfo} object
4235N/A * is stored in the
4235N/A * {@linkplain javax.management.Notification#setUserData userdata}
4235N/A * of a {@linkplain javax.management.Notification notification}.
4235N/A * The {@link #from from} method is provided to convert from
4235N/A * a {@code CompositeData} to a {@code GarbageCollectionNotificationInfo}
4235N/A * object. For example:
4235N/A *
4235N/A * <blockquote><pre>
4235N/A * Notification notif;
4235N/A *
4235N/A * // receive the notification emitted by a GarbageCollectorMXBean and set to notif
4235N/A * ...
4235N/A *
4235N/A * String notifType = notif.getType();
4235N/A * if (notifType.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
4235N/A * // retrieve the garbage collection notification information
4235N/A * CompositeData cd = (CompositeData) notif.getUserData();
4235N/A * GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from(cd);
4235N/A * ....
4235N/A * }
4235N/A * </pre></blockquote>
4235N/A *
4235N/A * <p>
4235N/A * The type of the notification emitted by a {@code GarbageCollectorMXBean} is:
4235N/A * <ul>
4235N/A * <li>A {@linkplain #GARBAGE_COLLECTION_NOTIFICATION garbage collection notification}.
4235N/A * <br>Used by every notification emitted by the garbage collector, the details about
4235N/A * the notification are provided in the {@linkplain #getGcAction action} String
4235N/A * <p></li>
4235N/A * </ul>
4235N/A **/
4235N/A
4235N/Apublic class GarbageCollectionNotificationInfo implements CompositeDataView {
4235N/A
4235N/A private final String gcName;
4235N/A private final String gcAction;
4235N/A private final String gcCause;
4235N/A private final GcInfo gcInfo;
4235N/A private final CompositeData cdata;
4235N/A
4235N/A /**
4235N/A * Notification type denoting that
4235N/A * the Java virtual machine has completed a garbage collection cycle.
4235N/A * This notification is emitted by a {@link GarbageCollectorMXBean}.
4235N/A * The value of this notification type is
4235N/A * {@code com.sun.management.gc.notification}.
4235N/A */
4235N/A public static final String GARBAGE_COLLECTION_NOTIFICATION =
4235N/A "com.sun.management.gc.notification";
4235N/A
4235N/A /**
4235N/A * Constructs a {@code GarbageCollectionNotificationInfo} object.
4235N/A *
4235N/A * @param gcName The name of the garbage collector used to perform the collection
4235N/A * @param gcAction The name of the action performed by the garbage collector
4235N/A * @param gcCause The cause the garbage collection action
4235N/A * @param gcInfo a GcInfo object providing statistics about the GC cycle
4235N/A */
4235N/A public GarbageCollectionNotificationInfo(String gcName,
4235N/A String gcAction,
4235N/A String gcCause,
4235N/A GcInfo gcInfo) {
4235N/A if (gcName == null) {
4235N/A throw new NullPointerException("Null gcName");
4235N/A }
4235N/A if (gcAction == null) {
4235N/A throw new NullPointerException("Null gcAction");
4235N/A }
4235N/A if (gcCause == null) {
4235N/A throw new NullPointerException("Null gcCause");
4235N/A }
4235N/A this.gcName = gcName;
4235N/A this.gcAction = gcAction;
4235N/A this.gcCause = gcCause;
4235N/A this.gcInfo = gcInfo;
4235N/A this.cdata = new GarbageCollectionNotifInfoCompositeData(this);
4235N/A }
4235N/A
4235N/A GarbageCollectionNotificationInfo(CompositeData cd) {
4235N/A GarbageCollectionNotifInfoCompositeData.validateCompositeData(cd);
4235N/A
4235N/A this.gcName = GarbageCollectionNotifInfoCompositeData.getGcName(cd);
4235N/A this.gcAction = GarbageCollectionNotifInfoCompositeData.getGcAction(cd);
4235N/A this.gcCause = GarbageCollectionNotifInfoCompositeData.getGcCause(cd);
4235N/A this.gcInfo = GarbageCollectionNotifInfoCompositeData.getGcInfo(cd);
4235N/A this.cdata = cd;
4235N/A }
4235N/A
4235N/A /**
4235N/A * Returns the name of the garbage collector used to perform the collection
4235N/A *
4235N/A * @return the name of the garbage collector used to perform the collection
4235N/A */
4235N/A public String getGcName() {
4235N/A return gcName;
4235N/A }
4235N/A
4235N/A /**
4235N/A * Returns the action of the performed by the garbage collector
4235N/A *
4235N/A * @return the the action of the performed by the garbage collector
4235N/A */
4235N/A public String getGcAction() {
4235N/A return gcAction;
4235N/A }
4235N/A
4235N/A /**
4235N/A * Returns the cause the garbage collection
4235N/A *
4235N/A * @return the the cause the garbage collection
4235N/A */
4235N/A public String getGcCause() {
4235N/A return gcCause;
4235N/A }
4235N/A
4235N/A /**
4235N/A * Returns the GC information related to the last garbage collection
4235N/A *
4235N/A * @return the GC information related to the
4235N/A * last garbage collection
4235N/A */
4235N/A public GcInfo getGcInfo() {
4235N/A return gcInfo;
4235N/A }
4235N/A
4235N/A /**
4235N/A * Returns a {@code GarbageCollectionNotificationInfo} object represented by the
4235N/A * given {@code CompositeData}.
4235N/A * The given {@code CompositeData} must contain
4235N/A * the following attributes:
4235N/A * <blockquote>
4235N/A * <table border>
4235N/A * <tr>
4235N/A * <th align=left>Attribute Name</th>
4235N/A * <th align=left>Type</th>
4235N/A * </tr>
4235N/A * <tr>
4235N/A * <td>gcName</td>
4235N/A * <td>{@code java.lang.String}</td>
4235N/A * </tr>
4235N/A * <tr>
4235N/A * <td>gcAction</td>
4235N/A * <td>{@code java.lang.String}</td>
4235N/A * </tr>
4235N/A * <tr>
4235N/A * <td>gcCause</td>
4235N/A * <td>{@code java.lang.String}</td>
4235N/A * </tr>
4235N/A * <tr>
4235N/A * <td>gcInfo</td>
4235N/A * <td>{@code javax.management.openmbean.CompositeData}</td>
4235N/A * </tr>
4235N/A * </table>
4235N/A * </blockquote>
4235N/A *
4235N/A * @param cd {@code CompositeData} representing a
4235N/A * {@code GarbageCollectionNotificationInfo}
4235N/A *
4235N/A * @throws IllegalArgumentException if {@code cd} does not
4235N/A * represent a {@code GarbaageCollectionNotificationInfo} object.
4235N/A *
4235N/A * @return a {@code GarbageCollectionNotificationInfo} object represented
4235N/A * by {@code cd} if {@code cd} is not {@code null};
4235N/A * {@code null} otherwise.
4235N/A */
4235N/A public static GarbageCollectionNotificationInfo from(CompositeData cd) {
4235N/A if (cd == null) {
4235N/A return null;
4235N/A }
4235N/A
4235N/A if (cd instanceof GarbageCollectionNotifInfoCompositeData) {
4235N/A return ((GarbageCollectionNotifInfoCompositeData) cd).getGarbageCollectionNotifInfo();
4235N/A } else {
4235N/A return new GarbageCollectionNotificationInfo(cd);
4235N/A }
4235N/A }
4235N/A
4235N/A public CompositeData toCompositeData(CompositeType ct) {
4235N/A return cdata;
4235N/A }
4235N/A
4235N/A}