1 /* 2 * Copyright (c) 2016, 2019, 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 configuration provider, for example {@code "Oracle"}. 94 * 95 * @return the provider, or {@code null} if not available 96 * 97 * @see Configuration#getProvider() 98 */ 99 public String getProvider() { 100 return provider; 101 } 102 103 /** 104 * Returns the textual representation of the configuration associated with 105 * this {@code ConfigurationInfo}, typically the contents of the 106 * configuration file that was used to create the configuration. 107 * 108 * @return contents, or {@code null} if not available 109 * 110 * @see Configuration#getContents() 111 */ 112 public String getContents() { 113 return contents; 114 } 115 116 /** 117 * Returns the settings for the configuration associated with this 118 * {@code ConfigurationInfo}. 119 * 120 * @return map with settings, not {@code null} 121 * 122 * @see Configuration#getSettings() 123 */ 124 public Map<String, String> getSettings() { 125 return settings; 126 } 127 128 /** 129 * Returns the human-readable name, for example {@code "Continuous"}. for 130 * the configuration associated with this {@code ConfigurationInfo} 131 * 132 * @return the label, or {@code null} if not available 133 * 134 * @see Configuration#getLabel() 135 */ 136 public String getLabel() { 137 return label; 138 } 139 140 /** 141 * Returns the name of the configuration associated with this 142 * {@code ConfigurationInfo}, for example {@code "default"}. 143 * 144 * @return the name, or {@code null} if not available 145 * 146 * @see Configuration#getLabel() 147 */ 148 public String getName() { 149 return name; 150 } 151 152 /** 153 * Returns a sentence or two, describing the configuration associated with 154 * this {@code ConfigurationInfo}, for example {@code "Low 155 * overhead configuration safe for continuous use in production 156 * environments"}. 157 * 158 * @return the description, or {@code null} if not available 159 */ 160 public String getDescription() { 161 return description; 162 } 163 164 /** 165 * Returns a {@code ConfigurationInfo} object represented by the specified 166 * {@code CompositeData}. 167 * <p> 168 * The following table shows the required attributes that the specified {@code CompositeData} must contain. 169 * <blockquote> 170 * <table class="striped"> 171 * <caption>Required names and types for CompositeData</caption> 172 * <thead> 173 * <tr> 174 * <th scope="col" style="text-align:left">Name</th> 175 * <th scope="col" style="text-align:left">Type</th> 176 * </tr> 177 * </thead> 178 * <tbody> 179 * <tr> 180 * <th scope="row">name</th> 181 * <td>{@code String}</td> 182 * </tr> 183 * <tr> 184 * <th scope="row">label</th> 185 * <td>{@code String}</td> 186 * </tr> 187 * <tr> 188 * <th scope="row">description</th> 189 * <td>{@code String}</td> 190 * </tr> 191 * <tr> 192 * <th scope="row">provider</th> 193 * <td>{@code String}</td> 194 * </tr> 195 * <tr> 196 * <th scope="row">contents</th> 197 * <td>{@code String}</td> 198 * </tr> 199 * 200 * <tr> 201 * <th scope="row">settings</th> 202 * <td>{@code javax.management.openmbean.TabularData} with a 203 * {@code TabularType} with the keys {@code "key"} and {@code "value"}, both 204 * of the {@code String} type</td> 205 * </tr> 206 * </tbody> 207 * </table> 208 * </blockquote> 209 * 210 * @param cd {@code CompositeData} representing a {@code ConfigurationInfo} 211 * 212 * @throws IllegalArgumentException if {@code cd} does not represent a 213 * {@code ConfigurationInfo} with the required attributes 214 * 215 * @return a {@code ConfigurationInfo} object represented by {@code cd} if 216 * {@code cd} is not {@code null}, {@code null} otherwise 217 */ 218 public static ConfigurationInfo from(CompositeData cd) { 219 if (cd == null) { 220 return null; 221 } 222 return new ConfigurationInfo(cd); 223 } 224 225 /** 226 * Returns a description of the configuration that is associated with this 227 * {@code ConfigurationInfo} instance.s 228 * 229 * @return the description of the configuration, not {@code null} 230 */ 231 @Override 232 public String toString() { 233 Stringifier s = new Stringifier(); 234 s.add("name", name); 235 s.add("label", label); 236 s.add("description", description); 237 s.add("provider", provider); 238 return s.toString(); 239 } 240 }