jdk/src/java.management/share/classes/sun/management/HotSpotDiagnostic.java
changeset 30365 551470085a1d
parent 30073 989253a902c3
parent 30364 429945d7189c
child 30366 74651100cf08
child 30664 337397328b72
equal deleted inserted replaced
30073:989253a902c3 30365:551470085a1d
     1 /*
       
     2  * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.management;
       
    27 
       
    28 import java.io.IOException;
       
    29 import java.util.ArrayList;
       
    30 import java.util.List;
       
    31 import javax.management.ObjectName;
       
    32 
       
    33 import com.sun.management.HotSpotDiagnosticMXBean;
       
    34 import com.sun.management.VMOption;
       
    35 
       
    36 /**
       
    37  * Implementation of the diagnostic MBean for Hotspot VM.
       
    38  */
       
    39 public class HotSpotDiagnostic implements HotSpotDiagnosticMXBean {
       
    40     public HotSpotDiagnostic() {
       
    41     }
       
    42 
       
    43     @Override
       
    44     public void dumpHeap(String outputFile, boolean live) throws IOException {
       
    45         SecurityManager security = System.getSecurityManager();
       
    46         if (security != null) {
       
    47             security.checkWrite(outputFile);
       
    48             Util.checkControlAccess();
       
    49         }
       
    50 
       
    51         dumpHeap0(outputFile, live);
       
    52     }
       
    53 
       
    54     private native void dumpHeap0(String outputFile, boolean live) throws IOException;
       
    55 
       
    56     @Override
       
    57     public List<VMOption> getDiagnosticOptions() {
       
    58         List<Flag> allFlags = Flag.getAllFlags();
       
    59         List<VMOption> result = new ArrayList<>();
       
    60         for (Flag flag : allFlags) {
       
    61             if (flag.isWriteable() && flag.isExternal()) {
       
    62                 result.add(flag.getVMOption());
       
    63             }
       
    64         }
       
    65         return result;
       
    66     }
       
    67 
       
    68     @Override
       
    69     public VMOption getVMOption(String name) {
       
    70         if (name == null) {
       
    71             throw new NullPointerException("name cannot be null");
       
    72         }
       
    73 
       
    74         Flag f = Flag.getFlag(name);
       
    75         if (f == null) {
       
    76             throw new IllegalArgumentException("VM option \"" +
       
    77                 name + "\" does not exist");
       
    78         }
       
    79         return f.getVMOption();
       
    80     }
       
    81 
       
    82     @Override
       
    83     public void setVMOption(String name, String value) {
       
    84         if (name == null) {
       
    85             throw new NullPointerException("name cannot be null");
       
    86         }
       
    87         if (value == null) {
       
    88             throw new NullPointerException("value cannot be null");
       
    89         }
       
    90 
       
    91         Util.checkControlAccess();
       
    92         Flag flag = Flag.getFlag(name);
       
    93         if (flag == null) {
       
    94             throw new IllegalArgumentException("VM option \"" +
       
    95                 name + "\" does not exist");
       
    96         }
       
    97         if (!flag.isWriteable()){
       
    98             throw new IllegalArgumentException("VM Option \"" +
       
    99                 name + "\" is not writeable");
       
   100         }
       
   101 
       
   102         // Check the type of the value
       
   103         Object v = flag.getValue();
       
   104         if (v instanceof Long) {
       
   105             try {
       
   106                 long l = Long.parseLong(value);
       
   107                 Flag.setLongValue(name, l);
       
   108             } catch (NumberFormatException e) {
       
   109                 throw new IllegalArgumentException("Invalid value:" +
       
   110                         " VM Option \"" + name + "\"" +
       
   111                         " expects numeric value", e);
       
   112             }
       
   113         } else if (v instanceof Double) {
       
   114             try {
       
   115                 double d = Double.parseDouble(value);
       
   116                 Flag.setDoubleValue(name, d);
       
   117             } catch (NumberFormatException e) {
       
   118                 throw new IllegalArgumentException("Invalid value:" +
       
   119                         " VM Option \"" + name + "\"" +
       
   120                         " expects numeric value", e);
       
   121             }
       
   122         } else if (v instanceof Boolean) {
       
   123             if (!value.equalsIgnoreCase("true") &&
       
   124                 !value.equalsIgnoreCase("false")) {
       
   125                 throw new IllegalArgumentException("Invalid value:" +
       
   126                     " VM Option \"" + name + "\"" +
       
   127                     " expects \"true\" or \"false\".");
       
   128             }
       
   129             Flag.setBooleanValue(name, Boolean.parseBoolean(value));
       
   130         } else if (v instanceof String) {
       
   131             Flag.setStringValue(name, value);
       
   132         } else {
       
   133             throw new IllegalArgumentException("VM Option \"" +
       
   134                 name + "\" is of an unsupported type: " +
       
   135                 v.getClass().getName());
       
   136         }
       
   137     }
       
   138 
       
   139     @Override
       
   140     public ObjectName getObjectName() {
       
   141         return Util.newObjectName("com.sun.management:type=HotSpotDiagnostic");
       
   142     }
       
   143 }