Skip to content

Commit 3783d31

Browse files
committed
Quote name attribute if necessary
This commit updates MetadataNamingStrategy to quote an ObjectName attribute value if necessary. For now, only the name attribute is handled as it is usually a bean name, and we have no control over its structure. Closes gh-31708
1 parent 87730f7 commit 3783d31

File tree

2 files changed

+114
-2
lines changed

2 files changed

+114
-2
lines changed

spring-context/src/main/java/org/springframework/jmx/export/naming/MetadataNamingStrategy.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,6 +50,9 @@
5050
*/
5151
public class MetadataNamingStrategy implements ObjectNamingStrategy, InitializingBean {
5252

53+
private static final char[] QUOTABLE_CHARS = new char[] {',', '=', ':', '"'};
54+
55+
5356
/**
5457
* The {@code JmxAttributeSource} implementation to use for reading metadata.
5558
*/
@@ -132,10 +135,23 @@ public ObjectName getObjectName(Object managedBean, @Nullable String beanKey) th
132135
}
133136
Hashtable<String, String> properties = new Hashtable<>();
134137
properties.put("type", ClassUtils.getShortName(managedClass));
135-
properties.put("name", beanKey);
138+
properties.put("name", quoteIfNecessary(beanKey));
136139
return ObjectNameManager.getInstance(domain, properties);
137140
}
138141
}
139142
}
140143

144+
private static String quoteIfNecessary(String value) {
145+
return shouldQuote(value) ? ObjectName.quote(value) : value;
146+
}
147+
148+
private static boolean shouldQuote(String value) {
149+
for (char quotableChar : QUOTABLE_CHARS) {
150+
if (value.indexOf(quotableChar) != -1) {
151+
return true;
152+
}
153+
}
154+
return false;
155+
}
156+
141157
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.jmx.export.naming;
18+
19+
import java.util.function.Consumer;
20+
21+
import javax.management.MalformedObjectNameException;
22+
import javax.management.ObjectName;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource;
27+
import org.springframework.util.ClassUtils;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.assertj.core.api.Assertions.entry;
31+
32+
33+
/**
34+
* Tests for {@link MetadataNamingStrategy}.
35+
*
36+
* @author Stephane Nicoll
37+
*/
38+
class MetadataNamingStrategyTests {
39+
40+
private static final TestBean TEST_BEAN = new TestBean();
41+
42+
private final MetadataNamingStrategy strategy;
43+
44+
MetadataNamingStrategyTests() {
45+
this.strategy = new MetadataNamingStrategy();
46+
this.strategy.setDefaultDomain("com.example");
47+
this.strategy.setAttributeSource(new AnnotationJmxAttributeSource());
48+
}
49+
50+
@Test
51+
void getObjectNameWhenBeanNameIsSimple() throws MalformedObjectNameException {
52+
ObjectName name = this.strategy.getObjectName(TEST_BEAN, "myBean");
53+
assertThat(name.getDomain()).isEqualTo("com.example");
54+
assertThat(name).satisfies(hasDefaultProperties(TEST_BEAN, "myBean"));
55+
}
56+
57+
@Test
58+
void getObjectNameWhenBeanNameIsValidObjectName() throws MalformedObjectNameException {
59+
ObjectName name = this.strategy.getObjectName(TEST_BEAN, "com.another:name=myBean");
60+
assertThat(name.getDomain()).isEqualTo("com.another");
61+
assertThat(name.getKeyPropertyList()).containsOnly(entry("name", "myBean"));
62+
}
63+
64+
@Test
65+
void getObjectNameWhenBeanNamContainsComma() throws MalformedObjectNameException {
66+
ObjectName name = this.strategy.getObjectName(TEST_BEAN, "myBean,");
67+
assertThat(name).satisfies(hasDefaultProperties(TEST_BEAN, "\"myBean,\""));
68+
}
69+
70+
@Test
71+
void getObjectNameWhenBeanNamContainsEquals() throws MalformedObjectNameException {
72+
ObjectName name = this.strategy.getObjectName(TEST_BEAN, "my=Bean");
73+
assertThat(name).satisfies(hasDefaultProperties(TEST_BEAN, "\"my=Bean\""));
74+
}
75+
76+
@Test
77+
void getObjectNameWhenBeanNamContainsColon() throws MalformedObjectNameException {
78+
ObjectName name = this.strategy.getObjectName(TEST_BEAN, "my:Bean");
79+
assertThat(name).satisfies(hasDefaultProperties(TEST_BEAN, "\"my:Bean\""));
80+
}
81+
82+
@Test
83+
void getObjectNameWhenBeanNamContainsQuote() throws MalformedObjectNameException {
84+
ObjectName name = this.strategy.getObjectName(TEST_BEAN, "\"myBean\"");
85+
assertThat(name).satisfies(hasDefaultProperties(TEST_BEAN, "\"\\\"myBean\\\"\""));
86+
}
87+
88+
private Consumer<ObjectName> hasDefaultProperties(Object instance, String expectedName) {
89+
return objectName -> assertThat(objectName.getKeyPropertyList()).containsOnly(
90+
entry("type", ClassUtils.getShortName(instance.getClass())),
91+
entry("name", expectedName));
92+
}
93+
94+
static class TestBean {}
95+
96+
}

0 commit comments

Comments
 (0)