Skip to content

Commit 551f03c

Browse files
garyrussellartembilan
authored andcommitted
JMX: Quote object name values if not identifiers
https://www.oracle.com/technetwork/java/javase/tech/best-practices-jsp-136021.html The set of characters in a value is also limited. If special characters may occur, it is recommended that the value be quoted, using ObjectName.quote. If the value for a given key is sometimes quoted, then it should always be quoted. By default, if a value is a string (rather than a number, say), then it should be quoted unless you are sure that it will never contain special characters. Practically, some special characters are allowed, but we will standardize on allowed characters in java identifiers.
1 parent 7ef3891 commit 551f03c

File tree

6 files changed

+44
-12
lines changed

6 files changed

+44
-12
lines changed

spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.concurrent.atomic.AtomicLong;
2929
import java.util.concurrent.atomic.AtomicReference;
3030

31+
import javax.lang.model.SourceVersion;
3132
import javax.management.DynamicMBean;
3233
import javax.management.JMException;
3334
import javax.management.ObjectName;
@@ -745,28 +746,43 @@ private Object extractTarget(Object bean) {
745746
}
746747

747748
private String getChannelBeanKey(String channel) {
748-
String name = "" + channel;
749-
if (name.startsWith("org.springframework.integration")) {
750-
name = name + ",source=anonymous";
749+
String extra = "";
750+
if (channel.startsWith("org.springframework.integration")) {
751+
extra = ",source=anonymous";
751752
}
752-
return String.format(this.domain + ":type=MessageChannel,name=%s" + getStaticNames(), name);
753+
return String.format(this.domain + ":type=MessageChannel,name=%s%s" + getStaticNames(),
754+
quoteIfNecessary(channel), extra);
753755
}
754756

755757
private String getHandlerBeanKey(MessageHandlerMetrics handler) {
756758
// This ordering of keys seems to work with default settings of JConsole
757759
return String.format(this.domain + ":type=MessageHandler,name=%s,bean=%s" + getStaticNames(),
758-
handler.getManagedName(), handler.getManagedType());
760+
quoteIfNecessary(handler.getManagedName()), quoteIfNecessary(handler.getManagedType()));
759761
}
760762

761763
private String getSourceBeanKey(MessageSourceMetrics source) {
762764
// This ordering of keys seems to work with default settings of JConsole
763765
return String.format(this.domain + ":type=MessageSource,name=%s,bean=%s" + getStaticNames(),
764-
source.getManagedName(), source.getManagedType());
766+
quoteIfNecessary(source.getManagedName()), quoteIfNecessary(source.getManagedType()));
765767
}
766768

767769
private String getEndpointBeanKey(AbstractEndpoint endpoint, String name, String source) {
768770
// This ordering of keys seems to work with default settings of JConsole
769-
return String.format(this.domain + ":type=ManagedEndpoint,name=%s,bean=%s" + getStaticNames(), name, source);
771+
return String.format(this.domain + ":type=ManagedEndpoint,name=%s,bean=%s" + getStaticNames(),
772+
quoteIfNecessary(name), source);
773+
}
774+
775+
/*
776+
* https://www.oracle.com/technetwork/java/javase/tech/best-practices-jsp-136021.html
777+
*
778+
* The set of characters in a value is also limited. If special characters may
779+
* occur, it is recommended that the value be quoted, using ObjectName.quote. If
780+
* the value for a given key is sometimes quoted, then it should always be quoted.
781+
* By default, if a value is a string (rather than a number, say), then it should
782+
* be quoted unless you are sure that it will never contain special characters.
783+
*/
784+
private String quoteIfNecessary(String name) {
785+
return SourceVersion.isName(name) ? name : ObjectName.quote(name);
770786
}
771787

772788
private String getStaticNames() {

spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/NotificationPublishingChannelAdapterParserTests-context.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
<si:channel id="channel"/>
2525

26+
<si:channel id="org.springframework.integration.test.anon"/>
27+
2628
<jmx:notification-publishing-channel-adapter
2729
id="adapter" channel="channel"
2830
object-name="test.publisher:name=publisher"

spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/NotificationPublishingChannelAdapterParserTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -139,8 +139,12 @@ public void publishStringMessageWithinChain() throws Exception {
139139
assertEquals("test.type", notification.getType());
140140
assertNull(notification.getUserData());
141141
Set<ObjectName> names = server
142-
.queryNames(new ObjectName("*:type=MessageHandler," + "name=chainWithJmxNotificationPublishing$child."
143-
+ "jmx-notification-publishing-channel-adapter-within-chain,*"), null);
142+
.queryNames(new ObjectName("*:type=MessageHandler," + "name=\"chainWithJmxNotificationPublishing$child."
143+
+ "jmx-notification-publishing-channel-adapter-within-chain\",*"), null);
144+
assertEquals(1, names.size());
145+
names = server
146+
.queryNames(new ObjectName("*:type=MessageChannel,"
147+
+ "name=org.springframework.integration.test.anon,source=anonymous,*"), null);
144148
assertEquals(1, names.size());
145149
}
146150

spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MessagingGatewaySupportRegistrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2018 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.
@@ -60,7 +60,7 @@ public void testHandlerMBeanRegistration() throws Exception {
6060
names = this.server.queryNames(new ObjectName("org.springframework.integration:*,type=MessageSource,name=foo"),
6161
null);
6262
assertEquals(1, names.size());
63-
names = this.server.queryNames(new ObjectName("org.springframework.integration:*,name=foo#2"), null);
63+
names = this.server.queryNames(new ObjectName("org.springframework.integration:*,name=\"foo#2\""), null);
6464
assertEquals(1, names.size());
6565
}
6666

src/reference/asciidoc/jmx.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ The exporter propagates the `default-domain` to that object to let it generate a
332332
If your custom naming strategy is a `MetadataNamingStrategy` (or a subclass of it), the exporter does not propagate the `default-domain`.
333333
You must configure it on your strategy bean.
334334

335+
Starting with version 5.1; any bean names (represented by the `name` key in the object name) will be quoted if they contain any characters that are not allowed in a Java identifier (or period `.`).
336+
335337
[[jmx-42-improvements]]
336338
===== JMX Improvements
337339

src/reference/asciidoc/whats-new.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,11 @@ See <<jms>> for more information.
182182
The `statusCodeExpression` (and `Function`) is now supplied with the `RequestEntity<?>` as a root object for evaluation context, so request headers, method, URI and body are available for target status code calculation.
183183

184184
See <<http>> and <<webflux>> for more information.
185+
186+
[[x51.-jmx]]
187+
=== JMX Changes
188+
189+
Object name key values are now quoted if they contain any characters other than those allowed in a Java identifier (or period `.`).
190+
e.g. `org.springframework.integration:type=MessageChannel,name="input:foo.myGroup.errors"`.
191+
This has the side effect that previously "allowed" names, with such characters, will now be quoted.
192+
e.g. `org.springframework.integration:type=MessageChannel,name="input#foo.myGroup.errors"`.

0 commit comments

Comments
 (0)