src/jdk.management.jfr/share/classes/jdk/management/jfr/SettingDescriptorInfo.java
changeset 50113 caf115bb98ad
equal deleted inserted replaced
50112:7a2a740815b7 50113:caf115bb98ad
       
     1 /*
       
     2  * Copyright (c) 2016, 2018, 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 jdk.management.jfr;
       
    27 
       
    28 import java.util.concurrent.Callable;
       
    29 
       
    30 import javax.management.openmbean.CompositeData;
       
    31 
       
    32 import jdk.jfr.EventType;
       
    33 import jdk.jfr.SettingDescriptor;
       
    34 import jdk.management.jfr.internal.FlightRecorderMXBeanProvider;
       
    35 
       
    36 /**
       
    37  * Management class that describes a setting, for example name, description and
       
    38  * default value.
       
    39  *
       
    40  * @see EventType#getSettingDescriptors()
       
    41  *
       
    42  * @since 9
       
    43  */
       
    44 public final class SettingDescriptorInfo {
       
    45 
       
    46     // Purpose of this static initializer is to allow
       
    47     // FlightRecorderMXBeanProvider
       
    48     // to be in an internal package and not visible, but at the same time allow
       
    49     // it to instantiate FlightRecorderMXBeanImpl.
       
    50     //
       
    51     // The reason the mechanism is in this class is because it is light weight
       
    52     // and can easily be triggered from FlightRecorderMXBeanProvider.
       
    53     static {
       
    54         FlightRecorderMXBeanProvider.setFlightRecorderMXBeanFactory(new Callable<FlightRecorderMXBean>() {
       
    55             @Override
       
    56             public FlightRecorderMXBean call() throws Exception {
       
    57                 return new FlightRecorderMXBeanImpl();
       
    58             }
       
    59         });
       
    60     }
       
    61 
       
    62     private final String name;
       
    63     private final String label;
       
    64     private final String description;
       
    65     private final String typeName;
       
    66     private final String contentType;
       
    67     private final String defaultValue;
       
    68 
       
    69     // package private
       
    70     SettingDescriptorInfo(SettingDescriptor settingDescriptor) {
       
    71         this.name = settingDescriptor.getName();
       
    72         this.label = settingDescriptor.getLabel();
       
    73         this.description = settingDescriptor.getDescription();
       
    74         this.typeName = settingDescriptor.getTypeName();
       
    75         this.contentType = settingDescriptor.getContentType();
       
    76         this.defaultValue = settingDescriptor.getDefaultValue();
       
    77     }
       
    78 
       
    79     private SettingDescriptorInfo(CompositeData cd) {
       
    80         this.name = (String) cd.get("name");
       
    81         this.label = (String) cd.get("label");
       
    82         this.description = (String) cd.get("description");
       
    83         this.typeName = (String) cd.get("typeName");
       
    84         this.defaultValue = (String) cd.get("defaultValue");
       
    85         this.contentType = (String) cd.get("contentType");
       
    86     }
       
    87 
       
    88     /**
       
    89     * Returns the human-readable name of the setting associated with this
       
    90      * {@code SettingDescriptorInfo} (for example, {@code "Threshold"}).
       
    91      *
       
    92      * @return the label for this setting, not {@code null}
       
    93      */
       
    94     public String getLabel() {
       
    95         return label;
       
    96     }
       
    97 
       
    98     /**
       
    99      * Returns the name of the setting associated with this
       
   100      * {@code SettingDescriptorInfo} (for example, {@code "threshold"}).
       
   101      *
       
   102      * @return the name of this setting, not {@code null}
       
   103      */
       
   104     public String getName() {
       
   105         return name;
       
   106     }
       
   107 
       
   108     /**
       
   109      * Returns the description of the setting associated this
       
   110      * {@code SettingDescriptorInfo} (for example,
       
   111      * {@code "The duration an event must exceed to be be recorded"}).
       
   112      *
       
   113      * @return the description of this setting, not null
       
   114      */
       
   115     public String getDescription() {
       
   116         return description;
       
   117     }
       
   118 
       
   119     /**
       
   120      * Returns the type name of the setting associated this
       
   121      * {@code SettingDescriptorInfo} (for example,
       
   122      * {@code "jdk.settings.Threshold"}).
       
   123      * <p>
       
   124      * The type can be used to identify what type of setting this is.
       
   125      *
       
   126      * @return the name of this settings type, not {@code null}
       
   127      */
       
   128     public String getTypeName() {
       
   129         return typeName;
       
   130     }
       
   131 
       
   132     /**
       
   133      * Returns the content type of the setting associated this
       
   134      * {@code SettingDescriptorInfo} (for example, {@code "jdk.jfr.Timespan"}).
       
   135      * <p>
       
   136      * The content type can be used to determine how the setting should be
       
   137      * rendered in a graphical user interface.
       
   138      *
       
   139      * @return the name of this settings type, not {@code null}
       
   140      */
       
   141     public String getContentType() {
       
   142         return contentType;
       
   143     }
       
   144 
       
   145     /**
       
   146      * Returns the default value of the setting associated this
       
   147      * {@code SettingDescriptorInfo} (for example, {@code "20 ms"}).
       
   148      *
       
   149      * @return default value for this setting, not {@code null}
       
   150      *
       
   151      * @see SettingDescriptor#getDefaultValue()
       
   152      */
       
   153     public String getDefaultValue() {
       
   154         return defaultValue;
       
   155     }
       
   156 
       
   157     /**
       
   158      * Returns an {@code SettingDescriptorInfo} represented by the specified
       
   159      * {@code CompositeData}
       
   160      * <p>
       
   161      * The supplied {@code CompositeData} must have the following item names and
       
   162      * item types to be valid. <blockquote>
       
   163      * <table class="striped">
       
   164      * <caption>The name and type the specified CompositeData must contain</caption>
       
   165      * <thead>
       
   166      * <tr>
       
   167      * <th scope="col" style="text-align:left">Name</th>
       
   168      * <th scope="col" style="text-align:left">Type</th>
       
   169      * </tr>
       
   170      * </thead>
       
   171      * <tbody>
       
   172      * <tr>
       
   173      * <th scope="row">name</th>
       
   174      * <td>{@code String}</td>
       
   175      * </tr>
       
   176      * <tr>
       
   177      * <th scope="row">label</th>
       
   178      * <td>{@code String}</td>
       
   179      * </tr>
       
   180      * <tr>
       
   181      * <th scope="row">description</th>
       
   182      * <td>{@code String}</td>
       
   183      * </tr>
       
   184      * <tr>
       
   185      * <th scope="row">typeName</th>
       
   186      * <td>{@code String}</td>
       
   187      * </tr>
       
   188      * <tr>
       
   189      * <th scope="row">contentType</th>
       
   190      * <td>{@code String}</td>
       
   191      * </tr>
       
   192      * <tr>
       
   193      * <th scope="row">defaultValue</th>
       
   194      * <td>{@code String}</td>
       
   195      * </tr>
       
   196      * </tbody>
       
   197      * </table>
       
   198      * </blockquote>
       
   199      *
       
   200      * @param cd {@code CompositeData} representing the {@code SettingDescriptorInfo} to
       
   201      *        return
       
   202      *
       
   203      * @throws IllegalArgumentException if {@code cd} does not represent a valid
       
   204      *         {@code EventTypeInfo}
       
   205      *
       
   206      * @return a {@code SettingDescriptorInfo}, or {@code null} if {@code cd} is
       
   207      *         {@code null}
       
   208      */
       
   209     public static SettingDescriptorInfo from(CompositeData cd) {
       
   210         if (cd == null) {
       
   211             return null;
       
   212         }
       
   213         return new SettingDescriptorInfo(cd);
       
   214     }
       
   215 
       
   216     /**
       
   217      * Returns a {@code String} description of this {@code SettingDescriptorInfo}.
       
   218      *
       
   219      * @return a string describing this setting, not {@code null}
       
   220      */
       
   221     @Override
       
   222     public String toString() {
       
   223         Stringifier s = new Stringifier();
       
   224         s.add("name", name);
       
   225         s.add("label", label);
       
   226         s.add("description", description);
       
   227         s.add("typeName", typeName);
       
   228         s.add("contentType", contentType);
       
   229         s.add("defaultValue", defaultValue);
       
   230         return s.toString();
       
   231     }
       
   232 }