50113
|
1 |
/*
|
|
2 |
* Copyright (c) 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.jfr.api.settings;
|
|
27 |
import java.util.HashSet;
|
|
28 |
import java.util.Set;
|
|
29 |
import java.util.StringJoiner;
|
|
30 |
|
|
31 |
import jdk.jfr.Description;
|
|
32 |
import jdk.jfr.Label;
|
|
33 |
import jdk.jfr.MetadataDefinition;
|
|
34 |
import jdk.jfr.SettingControl;
|
|
35 |
|
|
36 |
@Label("String List")
|
|
37 |
@Description("Accepts set of strings, such as \"text\" or \"text1\", \"text2\", or nothing to reject all strings")
|
|
38 |
@MetadataDefinition
|
|
39 |
public final class StringListSetting extends SettingControl {
|
|
40 |
|
|
41 |
private Set<String> acceptedStrings = new HashSet<>();
|
|
42 |
|
|
43 |
@Override
|
|
44 |
public void setValue(String s) {
|
|
45 |
acceptedStrings = parseSetting(s);
|
|
46 |
}
|
|
47 |
|
|
48 |
private Set<String> parseSetting(String s) {
|
|
49 |
Set<String> stringSet = new HashSet<>();
|
|
50 |
StringBuilder sb = new StringBuilder();
|
|
51 |
boolean inString = false;
|
|
52 |
for (int index = 0; index < s.length(); index++) {
|
|
53 |
char c = s.charAt(index);
|
|
54 |
if (c != '"') {
|
|
55 |
if (inString) {
|
|
56 |
// escape double quotes
|
|
57 |
if (c == '\\' && index + 1 < s.length()) {
|
|
58 |
if (s.charAt(index + 1) == '"') {
|
|
59 |
index++;
|
|
60 |
c = '"';
|
|
61 |
}
|
|
62 |
}
|
|
63 |
sb.append(c);
|
|
64 |
}
|
|
65 |
} else {
|
|
66 |
if (inString) {
|
|
67 |
stringSet.add(sb.toString());
|
|
68 |
sb.setLength(0);
|
|
69 |
}
|
|
70 |
inString = !inString;
|
|
71 |
}
|
|
72 |
}
|
|
73 |
return stringSet;
|
|
74 |
}
|
|
75 |
|
|
76 |
@Override
|
|
77 |
public String getValue() {
|
|
78 |
StringJoiner sj = new StringJoiner(", ", "\"", "\"");
|
|
79 |
for (String s : acceptedStrings) {
|
|
80 |
sj.add(s);
|
|
81 |
}
|
|
82 |
return sj.toString();
|
|
83 |
}
|
|
84 |
|
|
85 |
@Override
|
|
86 |
public String combine(Set<String> values) {
|
|
87 |
Set<String> nameSet = new HashSet<>();
|
|
88 |
for (String s : values) {
|
|
89 |
nameSet.addAll(parseSetting(s));
|
|
90 |
}
|
|
91 |
if (nameSet.isEmpty()) {
|
|
92 |
return "";
|
|
93 |
}
|
|
94 |
StringJoiner sj = new StringJoiner(", ");
|
|
95 |
for (String s : nameSet) {
|
|
96 |
s = s.replace("\"", "\\\""); // escape quotes
|
|
97 |
sj.add("\"" + s + "\"");
|
|
98 |
}
|
|
99 |
return sj.toString();
|
|
100 |
}
|
|
101 |
|
|
102 |
public boolean accept(String string) {
|
|
103 |
return acceptedStrings.contains(string);
|
|
104 |
}
|
|
105 |
}
|