jdk/src/java.management/share/classes/sun/management/HotSpotDiagnostic.java
author weijun
Thu, 11 Dec 2014 15:23:02 +0800
changeset 27957 24b4e6082f19
parent 27493 8a2a7d7c52a0
permissions -rw-r--r--
8055723: Replace concat String to append in StringBuilder parameters (dev) Reviewed-by: redestad, ulfzibis, weijun, prappo, igerasim, alanb Contributed-by: Otavio Santana <otaviojava@java.net>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
27493
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
     2
 * Copyright (c) 2005, 2014, 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: 4156
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: 4156
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: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
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 sun.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1226
diff changeset
    29
import java.util.ArrayList;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1226
diff changeset
    30
import java.util.List;
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    31
import javax.management.ObjectName;
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    32
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.management.HotSpotDiagnosticMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.management.VMOption;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Implementation of the diagnostic MBean for Hotspot VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class HotSpotDiagnostic implements HotSpotDiagnosticMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public HotSpotDiagnostic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
27493
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
    43
    @Override
20826
d9ee83c801f6 8016357: Update hotspot diagnostic class
sgabdura
parents: 11849
diff changeset
    44
    public void dumpHeap(String outputFile, boolean live) throws IOException {
d9ee83c801f6 8016357: Update hotspot diagnostic class
sgabdura
parents: 11849
diff changeset
    45
        SecurityManager security = System.getSecurityManager();
d9ee83c801f6 8016357: Update hotspot diagnostic class
sgabdura
parents: 11849
diff changeset
    46
        if (security != null) {
d9ee83c801f6 8016357: Update hotspot diagnostic class
sgabdura
parents: 11849
diff changeset
    47
            security.checkWrite(outputFile);
d9ee83c801f6 8016357: Update hotspot diagnostic class
sgabdura
parents: 11849
diff changeset
    48
            Util.checkControlAccess();
d9ee83c801f6 8016357: Update hotspot diagnostic class
sgabdura
parents: 11849
diff changeset
    49
        }
d9ee83c801f6 8016357: Update hotspot diagnostic class
sgabdura
parents: 11849
diff changeset
    50
d9ee83c801f6 8016357: Update hotspot diagnostic class
sgabdura
parents: 11849
diff changeset
    51
        dumpHeap0(outputFile, live);
d9ee83c801f6 8016357: Update hotspot diagnostic class
sgabdura
parents: 11849
diff changeset
    52
    }
d9ee83c801f6 8016357: Update hotspot diagnostic class
sgabdura
parents: 11849
diff changeset
    53
d9ee83c801f6 8016357: Update hotspot diagnostic class
sgabdura
parents: 11849
diff changeset
    54
    private native void dumpHeap0(String outputFile, boolean live) throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
27493
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
    56
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public List<VMOption> getDiagnosticOptions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        List<Flag> allFlags = Flag.getAllFlags();
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11365
diff changeset
    59
        List<VMOption> result = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        for (Flag flag : allFlags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            if (flag.isWriteable() && flag.isExternal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                result.add(flag.getVMOption());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
27493
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
    68
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public VMOption getVMOption(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new NullPointerException("name cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Flag f = Flag.getFlag(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new IllegalArgumentException("VM option \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                name + "\" does not exist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return f.getVMOption();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
27493
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
    82
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public void setVMOption(String name, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw new NullPointerException("name cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new NullPointerException("value cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    91
        Util.checkControlAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Flag flag = Flag.getFlag(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (flag == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new IllegalArgumentException("VM option \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                name + "\" does not exist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (!flag.isWriteable()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new IllegalArgumentException("VM Option \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                name + "\" is not writeable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // Check the type of the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        Object v = flag.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (v instanceof Long) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                long l = Long.parseLong(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                Flag.setLongValue(name, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            } catch (NumberFormatException e) {
27493
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
   109
                throw new IllegalArgumentException("Invalid value:" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        " VM Option \"" + name + "\"" +
27493
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
   111
                        " expects numeric value", e);
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
   112
            }
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
   113
        } else if (v instanceof Double) {
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
   114
            try {
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
   115
                double d = Double.parseDouble(value);
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
   116
                Flag.setDoubleValue(name, d);
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
   117
            } catch (NumberFormatException e) {
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
   118
                throw new IllegalArgumentException("Invalid value:" +
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
   119
                        " VM Option \"" + name + "\"" +
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
   120
                        " expects numeric value", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } else if (v instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (!value.equalsIgnoreCase("true") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                !value.equalsIgnoreCase("false")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                throw new IllegalArgumentException("Invalid value:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    " VM Option \"" + name + "\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    " expects \"true\" or \"false\".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            Flag.setBooleanValue(name, Boolean.parseBoolean(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } else if (v instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            Flag.setStringValue(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new IllegalArgumentException("VM Option \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                name + "\" is of an unsupported type: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                v.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   138
27493
8a2a7d7c52a0 8061616: HotspotDiagnosticMXBean.getVMOption() throws IllegalArgumentException for flags of type double
jbachorik
parents: 25859
diff changeset
   139
    @Override
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   140
    public ObjectName getObjectName() {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1226
diff changeset
   141
        return Util.newObjectName("com.sun.management:type=HotSpotDiagnostic");
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   142
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}