1 /*
   2  * Copyright (c) 2005, 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.  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 com.sun.management;
  27 
  28 import com.sun.management.internal.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 {@code VMOption} contains the value of a VM option
  46  * and the origin of that value at the time this {@code VMOption}
  47  * object was constructed.  The value of the VM option
  48  * may be changed after the {@code VMOption} 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 using the attach framework.
  99          * @since 1.9
 100          */
 101         ATTACH_ON_DEMAND,
 102         /**
 103          * The VM option was set via some other mechanism.
 104          */
 105         OTHER
 106     }
 107 
 108     /**
 109      * Constructs a {@code VMOption}.
 110      *
 111      * @param name Name of a VM option.
 112      * @param value Value of a VM option.
 113      * @param writeable {@code true} if a VM option can be set dynamically,
 114      *                  or {@code false} otherwise.
 115      * @param origin where the value of a VM option came from.
 116      *
 117      * @throws NullPointerException if the name or value is {@code null}
 118      */
 119     public VMOption(String name, String value, boolean writeable, Origin origin) {
 120         this.name = name;
 121         this.value = value;
 122         this.writeable = writeable;
 123         this.origin = origin;
 124     }
 125 
 126     /**
 127      * Constructs a {@code VMOption} object from a
 128      * {@link CompositeData CompositeData}.
 129      */
 130     private VMOption(CompositeData cd) {
 131         // validate the input composite data
 132         VMOptionCompositeData.validateCompositeData(cd);
 133 
 134         this.name = VMOptionCompositeData.getName(cd);
 135         this.value = VMOptionCompositeData.getValue(cd);
 136         this.writeable = VMOptionCompositeData.isWriteable(cd);
 137         this.origin = VMOptionCompositeData.getOrigin(cd);
 138     }
 139 
 140     /**
 141      * Returns the name of this VM option.
 142      *
 143      * @return the name of this VM option.
 144      */
 145     public String getName() {
 146         return name;
 147     }
 148 
 149     /**
 150      * Returns the value of this VM option at the time when
 151      * this {@code VMOption} was created. The value could have been changed.
 152      *
 153      * @return the value of the VM option at the time when
 154      *         this {@code VMOption} was created.
 155      */
 156     public String getValue() {
 157         return value;
 158     }
 159 
 160     /**
 161      * Returns the origin of the value of this VM option. That is,
 162      * where the value of this VM option came from.
 163      *
 164      * @return where the value of this VM option came from.
 165      */
 166     public Origin getOrigin() {
 167         return origin;
 168     }
 169 
 170     /**
 171      * Tests if this VM option is writeable.  If this VM option is writeable,
 172      * it can be set by the {@link HotSpotDiagnosticMXBean#setVMOption
 173      * HotSpotDiagnosticMXBean.setVMOption} method.
 174      *
 175      * @return {@code true} if this VM option is writeable; {@code false}
 176      * otherwise.
 177      */
 178     public boolean isWriteable() {
 179         return writeable;
 180     }
 181 
 182     public String toString() {
 183         return "VM option: " + getName() +
 184                " value: " + value + " " +
 185                " origin: " + origin + " " +
 186                (writeable ? "(read-write)" : "(read-only)");
 187     }
 188 
 189     /**
 190      * Returns a {@code VMOption} object represented by the
 191      * given {@code CompositeData}. The given {@code CompositeData}
 192      * must contain the following attributes:
 193      *
 194      * <blockquote>
 195      * <table border>
 196      * <tr>
 197      *   <th align=left>Attribute Name</th>
 198      *   <th align=left>Type</th>
 199      * </tr>
 200      * <tr>
 201      *   <td>name</td>
 202      *   <td>{@code java.lang.String}</td>
 203      * </tr>
 204      * <tr>
 205      *   <td>value</td>
 206      *   <td>{@code java.lang.String}</td>
 207      * </tr>
 208      * <tr>
 209      *   <td>origin</td>
 210      *   <td>{@code java.lang.String}</td>
 211      * </tr>
 212      * <tr>
 213      *   <td>writeable</td>
 214      *   <td>{@code java.lang.Boolean}</td>
 215      * </tr>
 216      * </table>
 217      * </blockquote>
 218      *
 219      * @param cd {@code CompositeData} representing a {@code VMOption}
 220      *
 221      * @throws IllegalArgumentException if {@code cd} does not
 222      *   represent a {@code VMOption} with the attributes described
 223      *   above.
 224      *
 225      * @return a {@code VMOption} object represented by {@code cd}
 226      *         if {@code cd} is not {@code null};
 227      *         {@code null} otherwise.
 228      */
 229     public static VMOption from(CompositeData cd) {
 230         if (cd == null) {
 231             return null;
 232         }
 233 
 234         if (cd instanceof VMOptionCompositeData) {
 235             return ((VMOptionCompositeData) cd).getVMOption();
 236         } else {
 237             return new VMOption(cd);
 238         }
 239 
 240     }
 241 
 242     // for sun.management.MappedMXBeanType
 243     static CompositeData toCompositeData(VMOption option) {
 244         return VMOptionCompositeData.toCompositeData(option);
 245     }
 246 }