View Javadoc
1   /*
2    * Copyright (c) 2005, 2013, 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 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  @jdk.Exported
56  public class VMOption {
57      private String name;
58      private String value;
59      private boolean writeable;
60      private Origin origin;
61  
62      /**
63       * Origin of the value of a VM option.  It tells where the
64       * value of a VM option came from.
65       *
66       * @since 1.6
67       */
68      @jdk.Exported
69      public enum Origin {
70          /**
71           * The VM option has not been set and its value
72           * is the default value.
73           */
74          DEFAULT,
75          /**
76           * The VM option was set at VM creation time typically
77           * as a command line argument to the launcher or
78           * an argument passed to the VM created using the
79           * JNI invocation interface.
80           */
81          VM_CREATION,
82          /**
83           * The VM option was set via an environment variable.
84           */
85          ENVIRON_VAR,
86          /**
87           * The VM option was set via a configuration file.
88           */
89          CONFIG_FILE,
90          /**
91           * The VM option was set via the management interface after the VM
92           * was started.
93           */
94          MANAGEMENT,
95          /**
96           * The VM option was set via the VM ergonomic support.
97           */
98          ERGONOMIC,
99          /**
100          * The VM option was set via some other mechanism.
101          */
102         OTHER
103     }
104 
105     /**
106      * Constructs a <tt>VMOption</tt>.
107      *
108      * @param name Name of a VM option.
109      * @param value Value of a VM option.
110      * @param writeable <tt>true</tt> if a VM option can be set dynamically,
111      *                  or <tt>false</tt> otherwise.
112      * @param origin where the value of a VM option came from.
113      *
114      * @throws NullPointerException if the name or value is <tt>null</tt>
115      */
116     public VMOption(String name, String value, boolean writeable, Origin origin) {
117         this.name = name;
118         this.value = value;
119         this.writeable = writeable;
120         this.origin = origin;
121     }
122 
123     /**
124      * Constructs a <tt>VMOption</tt> object from a
125      * {@link CompositeData CompositeData}.
126      */
127     private VMOption(CompositeData cd) {
128         // validate the input composite data
129         VMOptionCompositeData.validateCompositeData(cd);
130 
131         this.name = VMOptionCompositeData.getName(cd);
132         this.value = VMOptionCompositeData.getValue(cd);
133         this.writeable = VMOptionCompositeData.isWriteable(cd);
134         this.origin = VMOptionCompositeData.getOrigin(cd);
135     }
136 
137     /**
138      * Returns the name of this VM option.
139      *
140      * @return the name of this VM option.
141      */
142     public String getName() {
143         return name;
144     }
145 
146     /**
147      * Returns the value of this VM option at the time when
148      * this <tt>VMOption</tt> was created. The value could have been changed.
149      *
150      * @return the value of the VM option at the time when
151      *         this <tt>VMOption</tt> was created.
152      */
153     public String getValue() {
154         return value;
155     }
156 
157     /**
158      * Returns the origin of the value of this VM option. That is,
159      * where the value of this VM option came from.
160      *
161      * @return where the value of this VM option came from.
162      */
163     public Origin getOrigin() {
164         return origin;
165     }
166 
167     /**
168      * Tests if this VM option is writeable.  If this VM option is writeable,
169      * it can be set by the {@link HotSpotDiagnosticMXBean#setVMOption
170      * HotSpotDiagnosticMXBean.setVMOption} method.
171      *
172      * @return <tt>true</tt> if this VM option is writeable; <tt>false</tt>
173      * otherwise.
174      */
175     public boolean isWriteable() {
176         return writeable;
177     }
178 
179     public String toString() {
180         return "VM option: " + getName() +
181                " value: " + value + " " +
182                " origin: " + origin + " " +
183                (writeable ? "(read-write)" : "(read-only)");
184     }
185 
186     /**
187      * Returns a <tt>VMOption</tt> object represented by the
188      * given <tt>CompositeData</tt>. The given <tt>CompositeData</tt>
189      * must contain the following attributes:
190      * <p>
191      * <blockquote>
192      * <table border>
193      * <tr>
194      *   <th align=left>Attribute Name</th>
195      *   <th align=left>Type</th>
196      * </tr>
197      * <tr>
198      *   <td>name</td>
199      *   <td><tt>java.lang.String</tt></td>
200      * </tr>
201      * <tr>
202      *   <td>value</td>
203      *   <td><tt>java.lang.String</tt></td>
204      * </tr>
205      * <tr>
206      *   <td>origin</td>
207      *   <td><tt>java.lang.String</tt></td>
208      * </tr>
209      * <tr>
210      *   <td>writeable</td>
211      *   <td><tt>java.lang.Boolean</tt></td>
212      * </tr>
213      * </table>
214      * </blockquote>
215      *
216      * @param cd <tt>CompositeData</tt> representing a <tt>VMOption</tt>
217      *
218      * @throws IllegalArgumentException if <tt>cd</tt> does not
219      *   represent a <tt>VMOption</tt> with the attributes described
220      *   above.
221      *
222      * @return a <tt>VMOption</tt> object represented by <tt>cd</tt>
223      *         if <tt>cd</tt> is not <tt>null</tt>;
224      *         <tt>null</tt> otherwise.
225      */
226     public static VMOption from(CompositeData cd) {
227         if (cd == null) {
228             return null;
229         }
230 
231         if (cd instanceof VMOptionCompositeData) {
232             return ((VMOptionCompositeData) cd).getVMOption();
233         } else {
234             return new VMOption(cd);
235         }
236 
237     }
238 
239 
240 }