Skip to content

Commit a979925

Browse files
committed
Merge branch '2.5.x' into 2.6.x
Closes gh-29970
2 parents 4f8a18f + 7aca75c commit a979925

File tree

2 files changed

+118
-11
lines changed

2 files changed

+118
-11
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jmx/ParentAwareNamingStrategy.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -26,6 +26,7 @@
2626
import org.springframework.context.ApplicationContextAware;
2727
import org.springframework.jmx.export.metadata.JmxAttributeSource;
2828
import org.springframework.jmx.export.naming.MetadataNamingStrategy;
29+
import org.springframework.jmx.support.JmxUtils;
2930
import org.springframework.jmx.support.ObjectNameManager;
3031
import org.springframework.util.ObjectUtils;
3132

@@ -55,22 +56,21 @@ public void setEnsureUniqueRuntimeObjectNames(boolean ensureUniqueRuntimeObjectN
5556
this.ensureUniqueRuntimeObjectNames = ensureUniqueRuntimeObjectNames;
5657
}
5758

59+
@Override
60+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
61+
this.applicationContext = applicationContext;
62+
}
63+
5864
@Override
5965
public ObjectName getObjectName(Object managedBean, String beanKey) throws MalformedObjectNameException {
6066
ObjectName name = super.getObjectName(managedBean, beanKey);
61-
Hashtable<String, String> properties = new Hashtable<>(name.getKeyPropertyList());
6267
if (this.ensureUniqueRuntimeObjectNames) {
63-
properties.put("identity", ObjectUtils.getIdentityHexString(managedBean));
68+
return JmxUtils.appendIdentityToObjectName(name, managedBean);
6469
}
65-
else if (parentContextContainsSameBean(this.applicationContext, beanKey)) {
66-
properties.put("context", ObjectUtils.getIdentityHexString(this.applicationContext));
70+
if (parentContextContainsSameBean(this.applicationContext, beanKey)) {
71+
return appendToObjectName(name, "context", ObjectUtils.getIdentityHexString(this.applicationContext));
6772
}
68-
return ObjectNameManager.getInstance(name.getDomain(), properties);
69-
}
70-
71-
@Override
72-
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
73-
this.applicationContext = applicationContext;
73+
return name;
7474
}
7575

7676
private boolean parentContextContainsSameBean(ApplicationContext context, String beanKey) {
@@ -86,4 +86,11 @@ private boolean parentContextContainsSameBean(ApplicationContext context, String
8686
}
8787
}
8888

89+
private ObjectName appendToObjectName(ObjectName name, String key, String value)
90+
throws MalformedObjectNameException {
91+
Hashtable<String, String> keyProperties = name.getKeyPropertyList();
92+
keyProperties.put(key, value);
93+
return ObjectNameManager.getInstance(name.getDomain(), keyProperties);
94+
}
95+
8996
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2012-2022 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.boot.autoconfigure.jmx;
18+
19+
import javax.management.MalformedObjectNameException;
20+
import javax.management.ObjectName;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
25+
import org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource;
26+
import org.springframework.jmx.export.annotation.ManagedResource;
27+
import org.springframework.util.ObjectUtils;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Tests for {@link ParentAwareNamingStrategy}.
33+
*
34+
* @author Andy Wilkinson
35+
*/
36+
class ParentAwareNamingStrategyTests {
37+
38+
private ApplicationContextRunner contextRunner = new ApplicationContextRunner();
39+
40+
@Test
41+
void objectNameMatchesManagedResourceByDefault() throws MalformedObjectNameException {
42+
this.contextRunner.withBean("testManagedResource", TestManagedResource.class).run((context) -> {
43+
ParentAwareNamingStrategy strategy = new ParentAwareNamingStrategy(new AnnotationJmxAttributeSource());
44+
strategy.setApplicationContext(context);
45+
assertThat(strategy.getObjectName(context.getBean("testManagedResource"), "testManagedResource")
46+
.getKeyPropertyListString()).isEqualTo("type=something,name1=def,name2=ghi");
47+
});
48+
}
49+
50+
@Test
51+
void uniqueObjectNameAddsIdentityProperty() throws MalformedObjectNameException {
52+
this.contextRunner.withBean("testManagedResource", TestManagedResource.class).run((context) -> {
53+
ParentAwareNamingStrategy strategy = new ParentAwareNamingStrategy(new AnnotationJmxAttributeSource());
54+
strategy.setApplicationContext(context);
55+
strategy.setEnsureUniqueRuntimeObjectNames(true);
56+
Object resource = context.getBean("testManagedResource");
57+
ObjectName objectName = strategy.getObjectName(resource, "testManagedResource");
58+
assertThat(objectName.getDomain()).isEqualTo("ABC");
59+
assertThat(objectName.getCanonicalKeyPropertyListString()).isEqualTo(
60+
"identity=" + ObjectUtils.getIdentityHexString(resource) + ",name1=def,name2=ghi,type=something");
61+
});
62+
}
63+
64+
@Test
65+
void sameBeanInParentContextAddsContextProperty() throws MalformedObjectNameException {
66+
this.contextRunner.withBean("testManagedResource", TestManagedResource.class).run((parent) -> this.contextRunner
67+
.withBean("testManagedResource", TestManagedResource.class).withParent(parent).run((context) -> {
68+
ParentAwareNamingStrategy strategy = new ParentAwareNamingStrategy(
69+
new AnnotationJmxAttributeSource());
70+
strategy.setApplicationContext(context);
71+
Object resource = context.getBean("testManagedResource");
72+
ObjectName objectName = strategy.getObjectName(resource, "testManagedResource");
73+
assertThat(objectName.getDomain()).isEqualTo("ABC");
74+
assertThat(objectName.getCanonicalKeyPropertyListString()).isEqualTo("context="
75+
+ ObjectUtils.getIdentityHexString(context) + ",name1=def,name2=ghi,type=something");
76+
}));
77+
}
78+
79+
@Test
80+
void uniqueObjectNameAndSameBeanInParentContextOnlyAddsIdentityProperty() throws MalformedObjectNameException {
81+
this.contextRunner.withBean("testManagedResource", TestManagedResource.class).run((parent) -> this.contextRunner
82+
.withBean("testManagedResource", TestManagedResource.class).withParent(parent).run((context) -> {
83+
ParentAwareNamingStrategy strategy = new ParentAwareNamingStrategy(
84+
new AnnotationJmxAttributeSource());
85+
strategy.setApplicationContext(context);
86+
strategy.setEnsureUniqueRuntimeObjectNames(true);
87+
Object resource = context.getBean("testManagedResource");
88+
ObjectName objectName = strategy.getObjectName(resource, "testManagedResource");
89+
assertThat(objectName.getDomain()).isEqualTo("ABC");
90+
assertThat(objectName.getCanonicalKeyPropertyListString()).isEqualTo("identity="
91+
+ ObjectUtils.getIdentityHexString(resource) + ",name1=def,name2=ghi,type=something");
92+
}));
93+
}
94+
95+
@ManagedResource(objectName = "ABC:type=something,name1=def,name2=ghi")
96+
public static class TestManagedResource {
97+
98+
}
99+
100+
}

0 commit comments

Comments
 (0)