1 /* 2 * Copyright (c) 2005, 2008, 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 public native void dumpHeap(String outputFile, boolean live) throws IOException; 44 45 public List<VMOption> getDiagnosticOptions() { 46 List<Flag> allFlags = Flag.getAllFlags(); 47 List<VMOption> result = new ArrayList<VMOption>(); 48 for (Flag flag : allFlags) { 49 if (flag.isWriteable() && flag.isExternal()) { 50 result.add(flag.getVMOption()); 51 } 52 } 53 return result; 54 } 55 56 public VMOption getVMOption(String name) { 57 if (name == null) { 58 throw new NullPointerException("name cannot be null"); 59 } 60 61 Flag f = Flag.getFlag(name); 62 if (f == null) { 63 throw new IllegalArgumentException("VM option \"" + 64 name + "\" does not exist"); 65 } 66 return f.getVMOption(); 67 } 68 69 public void setVMOption(String name, String value) { 70 if (name == null) { 71 throw new NullPointerException("name cannot be null"); 72 } 73 if (value == null) { 74 throw new NullPointerException("value cannot be null"); 75 } 76 77 Util.checkControlAccess(); 78 Flag flag = Flag.getFlag(name); 79 if (flag == null) { 80 throw new IllegalArgumentException("VM option \"" + 81 name + "\" does not exist"); 82 } 83 if (!flag.isWriteable()){ 84 throw new IllegalArgumentException("VM Option \"" + 85 name + "\" is not writeable"); 86 } 87 88 // Check the type of the value 89 Object v = flag.getValue(); 90 if (v instanceof Long) { 91 try { 92 long l = Long.parseLong(value); 93 Flag.setLongValue(name, l); 94 } catch (NumberFormatException e) { 95 IllegalArgumentException iae = 96 new IllegalArgumentException("Invalid value:" + 97 " VM Option \"" + name + "\"" + 98 " expects numeric value"); 99 iae.initCause(e); 100 throw iae; 101 } 102 } else if (v instanceof Boolean) { 103 if (!value.equalsIgnoreCase("true") && 104 !value.equalsIgnoreCase("false")) { 105 throw new IllegalArgumentException("Invalid value:" + 106 " VM Option \"" + name + "\"" + 107 " expects \"true\" or \"false\"."); 108 } 109 Flag.setBooleanValue(name, Boolean.parseBoolean(value)); 110 } else if (v instanceof String) { 111 Flag.setStringValue(name, value); 112 } else { 113 throw new IllegalArgumentException("VM Option \"" + 114 name + "\" is of an unsupported type: " + 115 v.getClass().getName()); 116 } 117 } 118 119 public ObjectName getObjectName() { 120 return Util.newObjectName("com.sun.management:type=HotSpotDiagnostic"); 121 } 122 }