src/jdk.management.jfr/share/classes/jdk/management/jfr/ConfigurationInfo.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.Collection;
       
    29 import java.util.Collections;
       
    30 import java.util.HashMap;
       
    31 import java.util.Map;
       
    32 
       
    33 import javax.management.openmbean.CompositeData;
       
    34 import javax.management.openmbean.TabularData;
       
    35 
       
    36 import jdk.jfr.Configuration;
       
    37 
       
    38 
       
    39 /**
       
    40  * Management representation of a {@code Configuration}.
       
    41  *
       
    42  * @see Configuration
       
    43  *
       
    44  * @since 9
       
    45  */
       
    46 public final class ConfigurationInfo {
       
    47     private final Map<String, String> settings;
       
    48     private final String name;
       
    49     private final String label;
       
    50     private final String description;
       
    51     private final String provider;
       
    52     private final String contents;
       
    53 
       
    54     ConfigurationInfo(Configuration config) {
       
    55         this.settings = config.getSettings();
       
    56         this.name = config.getName();
       
    57         this.label = config.getLabel();
       
    58         this.description = config.getDescription();
       
    59         this.provider = config.getProvider();
       
    60         this.contents = config.getContents();
       
    61     }
       
    62 
       
    63     private ConfigurationInfo(CompositeData cd) {
       
    64         this.settings = createMap(cd.get("settings"));
       
    65         this.name = (String) cd.get("name");
       
    66         this.label = (String) cd.get("label");
       
    67         this.description = (String) cd.get("description");
       
    68         this.provider = (String) cd.get("provider");
       
    69         this.contents = (String) cd.get("contents");
       
    70     }
       
    71 
       
    72     private static Map<String, String> createMap(Object o) {
       
    73         if (o instanceof TabularData) {
       
    74             TabularData td = (TabularData) o;
       
    75             Collection<?> values = td.values();
       
    76             Map<String, String> map = new HashMap<>(values.size());
       
    77             for (Object value : td.values()) {
       
    78                 if (value instanceof CompositeData) {
       
    79                     CompositeData cdRow = (CompositeData) value;
       
    80                     Object k = cdRow.get("key");
       
    81                     Object v = cdRow.get("value");
       
    82                     if (k instanceof String && v instanceof String) {
       
    83                         map.put((String) k, (String) v);
       
    84                     }
       
    85                 }
       
    86             }
       
    87             return Collections.unmodifiableMap(map);
       
    88         }
       
    89         return Collections.emptyMap();
       
    90     }
       
    91 
       
    92     /**
       
    93      * Returns the provider of the configuration associated with this
       
    94      * {@code ConfigurationInfo} (for example, {@code "OpenJDK"}).
       
    95      *
       
    96      * @return the provider, or {@code null} if doesn't exist
       
    97      *
       
    98      * @see Configuration#getProvider()
       
    99      */
       
   100     public String getProvider() {
       
   101         return provider;
       
   102     }
       
   103 
       
   104     /**
       
   105      * Returns the textual representation of the configuration associated with
       
   106      * this {@code ConfigurationInfo}, typically the contents of the
       
   107      * configuration file that was used to create the configuration.
       
   108      *
       
   109      * @return contents, or {@code null} if doesn't exist
       
   110      *
       
   111      * @see Configuration#getContents()
       
   112      */
       
   113     public String getContents() {
       
   114         return contents;
       
   115     }
       
   116 
       
   117     /**
       
   118      * Returns the settings for the configuration associated with this
       
   119      * {@code ConfigurationInfo}.
       
   120      *
       
   121      * @return a {@code Map} with settings, not {@code null}
       
   122      *
       
   123      * @see Configuration#getSettings()
       
   124      */
       
   125     public Map<String, String> getSettings() {
       
   126         return settings;
       
   127     }
       
   128 
       
   129     /**
       
   130      * Returns the human-readable name (for example, {@code "Continuous"} or {@code "Profiling"}) for
       
   131      * the configuration associated with this {@code ConfigurationInfo}
       
   132      *
       
   133      * @return the label, or {@code null} if doesn't exist
       
   134      *
       
   135      * @see Configuration#getLabel()
       
   136      */
       
   137     public String getLabel() {
       
   138         return label;
       
   139     }
       
   140 
       
   141     /**
       
   142      * Returns the name of the configuration associated with this
       
   143      * {@code ConfigurationInfo} (for example, {@code "default"}).
       
   144      *
       
   145      * @return the name, or {@code null} if doesn't exist
       
   146      *
       
   147      * @see Configuration#getLabel()
       
   148      */
       
   149     public String getName() {
       
   150         return name;
       
   151     }
       
   152 
       
   153     /**
       
   154      * Returns a short sentence that describes the configuration associated with
       
   155      * this {@code ConfigurationInfo} (for example, {@code "Low
       
   156      * overhead configuration safe for continuous use in production
       
   157      * environments"}.
       
   158      *
       
   159      * @return the description, or {@code null} if doesn't exist
       
   160      */
       
   161     public String getDescription() {
       
   162         return description;
       
   163     }
       
   164 
       
   165     /**
       
   166      * Returns a {@code ConfigurationInfo} object represented by the specified
       
   167      * {@code CompositeData}.
       
   168      * <p>
       
   169      * The following table shows the required attributes that the specified {@code CompositeData} must contain.
       
   170      * <blockquote>
       
   171      * <table class="striped">
       
   172      * <caption>Required names and types for CompositeData</caption>
       
   173      * <thead>
       
   174      * <tr>
       
   175      * <th scope="col" style="text-align:left">Name</th>
       
   176      * <th scope="col" style="text-align:left">Type</th>
       
   177      * </tr>
       
   178      * </thead>
       
   179      * <tbody>
       
   180      * <tr>
       
   181      * <th scope="row">name</th>
       
   182      * <td>{@code String}</td>
       
   183      * </tr>
       
   184      * <tr>
       
   185      * <th scope="row">label</th>
       
   186      * <td>{@code String}</td>
       
   187      * </tr>
       
   188      * <tr>
       
   189      * <th scope="row">description</th>
       
   190      * <td>{@code String}</td>
       
   191      * </tr>
       
   192      * <tr>
       
   193      * <th scope="row">provider</th>
       
   194      * <td>{@code String}</td>
       
   195      * </tr>
       
   196      * <tr>
       
   197      * <th scope="row">contents</th>
       
   198      * <td>{@code String}</td>
       
   199      * </tr>
       
   200      *
       
   201      * <tr>
       
   202      * <th scope="row">settings</th>
       
   203      * <td>{@code javax.management.openmbean.TabularData} with a
       
   204      * {@code TabularType} with the keys {@code "key"} and {@code "value"}, both
       
   205      * of the {@code String} type</td>
       
   206      * </tr>
       
   207      * </tbody>
       
   208      * </table>
       
   209      * </blockquote>
       
   210      *
       
   211      * @param cd {@code CompositeData} representing a {@code ConfigurationInfo}
       
   212      *
       
   213      * @throws IllegalArgumentException if {@code cd} does not represent a
       
   214      *         {@code ConfigurationInfo} with the required attributes
       
   215      *
       
   216      * @return a {@code ConfigurationInfo} object represented by {@code cd} if
       
   217      *         {@code cd} is not {@code null}, {@code null} otherwise
       
   218      */
       
   219     public static ConfigurationInfo from(CompositeData cd) {
       
   220         if (cd == null) {
       
   221             return null;
       
   222         }
       
   223         return new ConfigurationInfo(cd);
       
   224     }
       
   225 
       
   226     /**
       
   227      * Returns a description of the configuration that is associated with this
       
   228      * {@code ConfigurationInfo}.
       
   229      *
       
   230      * @return the description of the configuration, not {@code null}
       
   231      */
       
   232     @Override
       
   233     public String toString() {
       
   234         Stringifier s = new Stringifier();
       
   235         s.add("name", name);
       
   236         s.add("label", label);
       
   237         s.add("description", description);
       
   238         s.add("provider", provider);
       
   239         return s.toString();
       
   240     }
       
   241 }