J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.management; |
| 27 | |
| 28 | import sun.management.VMOptionCompositeData; |
| 29 | import javax.management.openmbean.CompositeData; |
| 30 | |
| 31 | /** |
| 32 | * Information about a VM option including its value and |
| 33 | * where the value came from which is referred as its |
| 34 | * {@link VMOption.Origin <i>origin</i>}. |
| 35 | * <p> |
| 36 | * Each VM option has a default value. A VM option can |
| 37 | * be set at VM creation time typically as a command line |
| 38 | * argument to the launcher or an argument passed to the |
| 39 | * VM created using the JNI invocation interface. |
| 40 | * In addition, a VM option may be set via an environment |
| 41 | * variable or a configuration file. A VM option can also |
| 42 | * be set dynamically via a management interface after |
| 43 | * the VM was started. |
| 44 | * |
| 45 | * A <tt>VMOption</tt> contains the value of a VM option |
| 46 | * and the origin of that value at the time this <tt>VMOption</tt> |
| 47 | * object was constructed. The value of the VM option |
| 48 | * may be changed after the <tt>VMOption</tt> object was constructed, |
| 49 | * |
| 50 | * @see <a href="{@docRoot}/../../../../technotes/guides/vm/index.html"> |
| 51 | * Java Virtual Machine</a> |
| 52 | * @author Mandy Chung |
| 53 | * @since 1.6 |
| 54 | */ |
| 55 | public class VMOption { |
| 56 | private String name; |
| 57 | private String value; |
| 58 | private boolean writeable; |
| 59 | private Origin origin; |
| 60 | |
| 61 | /** |
| 62 | * Origin of the value of a VM option. It tells where the |
| 63 | * value of a VM option came from. |
| 64 | * |
| 65 | * @since 1.6 |
| 66 | */ |
| 67 | public enum Origin { |
| 68 | /** |
| 69 | * The VM option has not been set and its value |
| 70 | * is the default value. |
| 71 | */ |
| 72 | DEFAULT, |
| 73 | /** |
| 74 | * The VM option was set at VM creation time typically |
| 75 | * as a command line argument to the launcher or |
| 76 | * an argument passed to the VM created using the |
| 77 | * JNI invocation interface. |
| 78 | */ |
| 79 | VM_CREATION, |
| 80 | /** |
| 81 | * The VM option was set via an environment variable. |
| 82 | */ |
| 83 | ENVIRON_VAR, |
| 84 | /** |
| 85 | * The VM option was set via a configuration file. |
| 86 | */ |
| 87 | CONFIG_FILE, |
| 88 | /** |
| 89 | * The VM option was set via the management interface after the VM |
| 90 | * was started. |
| 91 | */ |
| 92 | MANAGEMENT, |
| 93 | /** |
| 94 | * The VM option was set via the VM ergonomic support. |
| 95 | */ |
| 96 | ERGONOMIC, |
| 97 | /** |
| 98 | * The VM option was set via some other mechanism. |
| 99 | */ |
| 100 | OTHER |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Constructs a <tt>VMOption</tt>. |
| 105 | * |
| 106 | * @param name Name of a VM option. |
| 107 | * @param value Value of a VM option. |
| 108 | * @param writeable <tt>true</tt> if a VM option can be set dynamically, |
| 109 | * or <tt>false</tt> otherwise. |
| 110 | * @param origin where the value of a VM option came from. |
| 111 | * |
| 112 | * @throws NullPointerException if the name or value is <tt>null</tt> |
| 113 | */ |
| 114 | public VMOption(String name, String value, boolean writeable, Origin origin) { |
| 115 | this.name = name; |
| 116 | this.value = value; |
| 117 | this.writeable = writeable; |
| 118 | this.origin = origin; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Constructs a <tt>VMOption</tt> object from a |
| 123 | * {@link CompositeData CompositeData}. |
| 124 | */ |
| 125 | private VMOption(CompositeData cd) { |
| 126 | // validate the input composite data |
| 127 | VMOptionCompositeData.validateCompositeData(cd); |
| 128 | |
| 129 | this.name = VMOptionCompositeData.getName(cd); |
| 130 | this.value = VMOptionCompositeData.getValue(cd); |
| 131 | this.writeable = VMOptionCompositeData.isWriteable(cd); |
| 132 | this.origin = VMOptionCompositeData.getOrigin(cd); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns the name of this VM option. |
| 137 | * |
| 138 | * @return the name of this VM option. |
| 139 | */ |
| 140 | public String getName() { |
| 141 | return name; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns the value of this VM option at the time when |
| 146 | * this <tt>VMOption</tt> was created. The value could have been changed. |
| 147 | * |
| 148 | * @return the value of the VM option at the time when |
| 149 | * this <tt>VMOption</tt> was created. |
| 150 | */ |
| 151 | public String getValue() { |
| 152 | return value; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns the origin of the value of this VM option. That is, |
| 157 | * where the value of this VM option came from. |
| 158 | * |
| 159 | * @return where the value of this VM option came from. |
| 160 | */ |
| 161 | public Origin getOrigin() { |
| 162 | return origin; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Tests if this VM option is writeable. If this VM option is writeable, |
| 167 | * it can be set by the {@link HotSpotDiagnosticMXBean#setVMOption |
| 168 | * HotSpotDiagnosticMXBean.setVMOption} method. |
| 169 | * |
| 170 | * @return <tt>true</tt> if this VM option is writeable; <tt>false</tt> |
| 171 | * otherwise. |
| 172 | */ |
| 173 | public boolean isWriteable() { |
| 174 | return writeable; |
| 175 | } |
| 176 | |
| 177 | public String toString() { |
| 178 | return "VM option: " + getName() + |
| 179 | " value: " + value + " " + |
| 180 | " origin: " + origin + " " + |
| 181 | (writeable ? "(read-only)" : "(read-write)"); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Returns a <tt>VMOption</tt> object represented by the |
| 186 | * given <tt>CompositeData</tt>. The given <tt>CompositeData</tt> |
| 187 | * must contain the following attributes: |
| 188 | * <p> |
| 189 | * <blockquote> |
| 190 | * <table border> |
| 191 | * <tr> |
| 192 | * <th align=left>Attribute Name</th> |
| 193 | * <th align=left>Type</th> |
| 194 | * </tr> |
| 195 | * <tr> |
| 196 | * <td>name</td> |
| 197 | * <td><tt>java.lang.String</tt></td> |
| 198 | * </tr> |
| 199 | * <tr> |
| 200 | * <td>value</td> |
| 201 | * <td><tt>java.lang.String</tt></td> |
| 202 | * </tr> |
| 203 | * <tr> |
| 204 | * <td>origin</td> |
| 205 | * <td><tt>java.lang.String</tt></td> |
| 206 | * </tr> |
| 207 | * <tr> |
| 208 | * <td>writeable</td> |
| 209 | * <td><tt>java.lang.Boolean</tt></td> |
| 210 | * </tr> |
| 211 | * </table> |
| 212 | * </blockquote> |
| 213 | * |
| 214 | * @param cd <tt>CompositeData</tt> representing a <tt>VMOption</tt> |
| 215 | * |
| 216 | * @throws IllegalArgumentException if <tt>cd</tt> does not |
| 217 | * represent a <tt>VMOption</tt> with the attributes described |
| 218 | * above. |
| 219 | * |
| 220 | * @return a <tt>VMOption</tt> object represented by <tt>cd</tt> |
| 221 | * if <tt>cd</tt> is not <tt>null</tt>; |
| 222 | * <tt>null</tt> otherwise. |
| 223 | */ |
| 224 | public static VMOption from(CompositeData cd) { |
| 225 | if (cd == null) { |
| 226 | return null; |
| 227 | } |
| 228 | |
| 229 | if (cd instanceof VMOptionCompositeData) { |
| 230 | return ((VMOptionCompositeData) cd).getVMOption(); |
| 231 | } else { |
| 232 | return new VMOption(cd); |
| 233 | } |
| 234 | |
| 235 | } |
| 236 | |
| 237 | |
| 238 | } |