001: /*
002: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.management;
027:
028: import java.util.*;
029: import java.io.IOException;
030: import java.lang.reflect.Method;
031: import com.sun.management.HotSpotDiagnosticMXBean;
032: import com.sun.management.VMOption;
033:
034: /**
035: * Implementation of the diagnostic MBean for Hotspot VM.
036: */
037: public class HotSpotDiagnostic implements HotSpotDiagnosticMXBean {
038: public HotSpotDiagnostic() {
039: }
040:
041: public native void dumpHeap(String outputFile, boolean live)
042: throws IOException;
043:
044: public List<VMOption> getDiagnosticOptions() {
045: List<Flag> allFlags = Flag.getAllFlags();
046: List<VMOption> result = new ArrayList<VMOption>();
047: for (Flag flag : allFlags) {
048: if (flag.isWriteable() && flag.isExternal()) {
049: result.add(flag.getVMOption());
050: }
051: }
052: return result;
053: }
054:
055: public VMOption getVMOption(String name) {
056: if (name == null) {
057: throw new NullPointerException("name cannot be null");
058: }
059:
060: Flag f = Flag.getFlag(name);
061: if (f == null) {
062: throw new IllegalArgumentException("VM option \"" + name
063: + "\" does not exist");
064: }
065: return f.getVMOption();
066: }
067:
068: public void setVMOption(String name, String value) {
069: if (name == null) {
070: throw new NullPointerException("name cannot be null");
071: }
072: if (value == null) {
073: throw new NullPointerException("value cannot be null");
074: }
075:
076: ManagementFactory.checkControlAccess();
077: Flag flag = Flag.getFlag(name);
078: if (flag == null) {
079: throw new IllegalArgumentException("VM option \"" + name
080: + "\" does not exist");
081: }
082: if (!flag.isWriteable()) {
083: throw new IllegalArgumentException("VM Option \"" + name
084: + "\" is not writeable");
085: }
086:
087: // Check the type of the value
088: Object v = flag.getValue();
089: if (v instanceof Long) {
090: try {
091: long l = Long.parseLong(value);
092: Flag.setLongValue(name, l);
093: } catch (NumberFormatException e) {
094: IllegalArgumentException iae = new IllegalArgumentException(
095: "Invalid value:" + " VM Option \"" + name
096: + "\"" + " expects numeric value");
097: iae.initCause(e);
098: throw iae;
099: }
100: } else if (v instanceof Boolean) {
101: if (!value.equalsIgnoreCase("true")
102: && !value.equalsIgnoreCase("false")) {
103: throw new IllegalArgumentException("Invalid value:"
104: + " VM Option \"" + name + "\""
105: + " expects \"true\" or \"false\".");
106: }
107: Flag.setBooleanValue(name, Boolean.parseBoolean(value));
108: } else if (v instanceof String) {
109: Flag.setStringValue(name, value);
110: } else {
111: throw new IllegalArgumentException("VM Option \"" + name
112: + "\" is of an unsupported type: "
113: + v.getClass().getName());
114: }
115: }
116: }
|