DynamicVMOption.java revision 2619:52fd3ed2536f
1/*
2 * Copyright (c) 2014, 2016, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package jdk.test.lib.management;
25
26import com.sun.management.HotSpotDiagnosticMXBean;
27import java.lang.management.ManagementFactory;
28
29/**
30 * A utility class to work with VM options which could be altered during
31 * execution.
32 *
33 * This class is a wrapper around {@code com.sun.management.VMOption}.
34 * It provides more convenient interface to read/write the values.
35 *
36 */
37public class DynamicVMOption {
38
39    private final HotSpotDiagnosticMXBean mxBean;
40
41    /**
42     * VM option name, like "MinHeapFreeRatio".
43     */
44    public final String name;
45
46    /**
47     * Creates an instance of DynamicVMOption.
48     *
49     * @param name the VM option name
50     */
51    public DynamicVMOption(String name) {
52        this.name = name;
53        mxBean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
54    }
55
56    /**
57     * Sets a new value for the option.
58     * Trying to set not applicable value will cause IllegalArgumentException.
59     * Behavior with null is undefined, most likely NPE will be thrown.
60     *
61     * @param newValue the value to be set
62     * @see #getValue()
63     * @throws IllegalArgumentException if newValue is not applicable to the option
64     */
65    public final void setValue(String newValue) {
66        mxBean.setVMOption(name, newValue);
67    }
68
69    /**
70     * Returns the value of option.
71     *
72     * @return the current option value
73     * @see #setValue(java.lang.String)
74     */
75    public final String getValue() {
76        return mxBean.getVMOption(name).getValue();
77    }
78
79    /**
80     * Returns true, if option is writable, false otherwise.
81     *
82     * @return true, if option is writable, false otherwise
83     */
84    public final boolean isWriteable() {
85        return mxBean.getVMOption(name).isWriteable();
86    }
87
88    /**
89     * Checks if the given value is applicable for the option.
90     *
91     * This method tries to set the option to the new value. If no exception
92     * has been thrown the value is treated as valid.
93     *
94     * Calling this method will not change the option value. After an attempt
95     * to set a new value, the option will be restored to its previous value.
96     *
97     * @param value the value to verify
98     * @return true if option could be set to the given value
99     */
100    public boolean isValidValue(String value) {
101        boolean isValid = true;
102        String oldValue = getValue();
103        try {
104            setValue(value);
105        } catch (NullPointerException e) {
106            if (value == null) {
107                isValid = false;
108            }
109        } catch (IllegalArgumentException e) {
110            isValid = false;
111        } finally {
112            setValue(oldValue);
113        }
114        return isValid;
115    }
116
117    /**
118     * Returns the value of the given VM option as String.
119     *
120     * This is a simple shortcut for {@code new DynamicVMOption(name).getValue()}
121     *
122     * @param name the name of VM option
123     * @return value as a string
124     * @see #getValue()
125     */
126    public static String getString(String name) {
127        return new DynamicVMOption(name).getValue();
128    }
129
130    /**
131     * Returns the value of the given option as int.
132     *
133     * @param name the name of VM option
134     * @return value parsed as integer
135     * @see #getString(java.lang.String)
136     *
137     */
138    public static int getInt(String name) {
139        return Integer.parseInt(getString(name));
140    }
141
142    /**
143     * Sets the VM option to a new value.
144     *
145     * This is a simple shortcut for {@code new DynamicVMOption(name).setValue(value)}
146     *
147     * @param name the name of VM option
148     * @param value the value to be set
149     * @see #setValue(java.lang.String)
150     */
151    public static void setString(String name, String value) {
152        new DynamicVMOption(name).setValue(value);
153    }
154
155    /**
156     * Sets the VM option value to a new integer value.
157     *
158     * @param name the name of VM option
159     * @param value the integer value to be set
160     * @see #setString(java.lang.String, java.lang.String)
161     */
162    public static void setInt(String name, int value) {
163        new DynamicVMOption(name).setValue(Integer.toString(value));
164    }
165
166}
167