1 /* 2 * Copyright (c) 2014, 2015, 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 package jdk.test.lib; 24 25 import com.sun.management.HotSpotDiagnosticMXBean; 26 import java.lang.management.ManagementFactory; 27 28 /** 29 * A utility class to work with VM options which could be altered during 30 * execution. 31 * 32 * This class is a wrapper around {@code com.sun.management.VMOption}. 33 * It provides more convenient interface to read/write the values. 34 * 35 */ 36 public class DynamicVMOption { 37 38 private final HotSpotDiagnosticMXBean mxBean; 39 40 /** 41 * VM option name, like "MinHeapFreeRatio". 42 */ 43 public final String name; 44 45 /** 46 * Creates an instance of DynamicVMOption. 47 * 48 * @param name the VM option name 49 */ 50 public DynamicVMOption(String name) { 51 this.name = name; 52 mxBean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class); 53 } 54 55 /** 56 * Sets a new value for the option. 57 * Trying to set not applicable value will cause IllegalArgumentException. 58 * Behavior with null is undefined, most likely NPE will be thrown. 59 * 60 * @param newValue the value to be set 61 * @see #getValue() 62 * @throws IllegalArgumentException if newValue is not applicable to the option 63 */ 64 public final void setValue(String newValue) { 65 mxBean.setVMOption(name, newValue); 66 } 67 68 /** 69 * Returns the value of option. 70 * 71 * @return the current option value 72 * @see #setValue(java.lang.String) 73 */ 74 public final String getValue() { 75 return mxBean.getVMOption(name).getValue(); 76 } 77 78 /** 79 * Returns true, if option is writable, false otherwise. 80 * 81 * @return true, if option is writable, false otherwise 82 */ 83 public final boolean isWriteable() { 84 return mxBean.getVMOption(name).isWriteable(); 85 } 86 87 /** 88 * Checks if the given value is applicable for the option. 89 * 90 * This method tries to set the option to the new value. If no exception 91 * has been thrown the value is treated as valid. 92 * 93 * Calling this method will not change the option value. After an attempt 94 * to set a new value, the option will be restored to its previous value. 95 * 96 * @param value the value to verify 97 * @return true if option could be set to the given value 98 */ 99 public boolean isValidValue(String value) { 100 boolean isValid = true; 101 String oldValue = getValue(); 102 try { 103 setValue(value); 104 } catch (NullPointerException e) { 105 if (value == null) { 106 isValid = false; 107 } 108 } catch (IllegalArgumentException e) { 109 isValid = false; 110 } finally { 111 setValue(oldValue); 112 } 113 return isValid; 114 } 115 116 /** 117 * Returns the value of the given VM option as String. 118 * 119 * This is a simple shortcut for {@code new DynamicVMOption(name).getValue()} 120 * 121 * @param name the name of VM option 122 * @return value as a string 123 * @see #getValue() 124 */ 125 public static String getString(String name) { 126 return new DynamicVMOption(name).getValue(); 127 } 128 129 /** 130 * Returns the value of the given option as int. 131 * 132 * @param name the name of VM option 133 * @return value parsed as integer 134 * @see #getString(java.lang.String) 135 * 136 */ 137 public static int getInt(String name) { 138 return Integer.parseInt(getString(name)); 139 } 140 141 /** 142 * Sets the VM option to a new value. 143 * 144 * This is a simple shortcut for {@code new DynamicVMOption(name).setValue(value)} 145 * 146 * @param name the name of VM option 147 * @param value the value to be set 148 * @see #setValue(java.lang.String) 149 */ 150 public static void setString(String name, String value) { 151 new DynamicVMOption(name).setValue(value); 152 } 153 154 /** 155 * Sets the VM option value to a new integer value. 156 * 157 * @param name the name of VM option 158 * @param value the integer value to be set 159 * @see #setString(java.lang.String, java.lang.String) 160 */ 161 public static void setInt(String name, int value) { 162 new DynamicVMOption(name).setValue(Integer.toString(value)); 163 } 164 165 }