Skip to content

Commit 6333d4c

Browse files
committed
Merge branch '1.1.x'
Conflicts: spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfiguration.java spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jmx/ParentAwareNamingStrategy.java spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfigurationTests.java
2 parents 4616be9 + 7e771bb commit 6333d4c

File tree

3 files changed

+74
-114
lines changed

3 files changed

+74
-114
lines changed
Lines changed: 35 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 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.
@@ -18,30 +18,28 @@
1818

1919
import javax.management.MBeanServer;
2020

21+
import org.springframework.beans.BeansException;
2122
import org.springframework.beans.factory.BeanFactory;
22-
import org.springframework.beans.factory.FactoryBean;
23-
import org.springframework.beans.factory.InitializingBean;
24-
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.beans.factory.BeanFactoryAware;
2524
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2625
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2726
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2827
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2928
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
29+
import org.springframework.boot.bind.RelaxedPropertyResolver;
30+
import org.springframework.context.EnvironmentAware;
3031
import org.springframework.context.annotation.Bean;
3132
import org.springframework.context.annotation.Configuration;
3233
import org.springframework.context.annotation.EnableMBeanExport;
33-
import org.springframework.context.annotation.MBeanExportConfiguration;
34+
import org.springframework.context.annotation.MBeanExportConfiguration.SpecificPlatform;
3435
import org.springframework.core.env.Environment;
35-
import org.springframework.core.type.StandardAnnotationMetadata;
3636
import org.springframework.jmx.export.MBeanExporter;
3737
import org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource;
3838
import org.springframework.jmx.export.annotation.AnnotationMBeanExporter;
3939
import org.springframework.jmx.export.naming.ObjectNamingStrategy;
4040
import org.springframework.jmx.support.MBeanServerFactoryBean;
41-
import org.springframework.jmx.support.WebSphereMBeanServerFactoryBean;
42-
import org.springframework.jndi.JndiObjectFactoryBean;
43-
import org.springframework.util.Assert;
44-
import org.springframework.util.ClassUtils;
41+
import org.springframework.jmx.support.RegistrationPolicy;
42+
import org.springframework.util.StringUtils;
4543

4644
/**
4745
* {@link EnableAutoConfiguration Auto-configuration} to enable/disable Spring's
@@ -54,35 +52,45 @@
5452
@Configuration
5553
@ConditionalOnClass({ MBeanExporter.class })
5654
@ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true", matchIfMissing = true)
57-
public class JmxAutoConfiguration {
55+
public class JmxAutoConfiguration implements EnvironmentAware, BeanFactoryAware {
5856

59-
@Autowired
60-
private Environment environment;
57+
private RelaxedPropertyResolver propertyResolver;
6158

62-
@Autowired
6359
private BeanFactory beanFactory;
6460

65-
@Autowired
66-
private ObjectNamingStrategy namingStrategy;
61+
@Override
62+
public void setEnvironment(Environment environment) {
63+
this.propertyResolver = new RelaxedPropertyResolver(environment, "spring.jmx.");
64+
}
65+
66+
@Override
67+
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
68+
this.beanFactory = beanFactory;
69+
}
6770

6871
@Bean
6972
@ConditionalOnMissingBean(value = MBeanExporter.class, search = SearchStrategy.CURRENT)
70-
public AnnotationMBeanExporter mbeanExporter() {
71-
// Re-use the @EnableMBeanExport configuration
72-
MBeanExportConfiguration config = new MBeanExportConfiguration();
73-
config.setEnvironment(this.environment);
74-
config.setBeanFactory(this.beanFactory);
75-
config.setImportMetadata(new StandardAnnotationMetadata(Empty.class));
76-
// But add a custom naming strategy
77-
AnnotationMBeanExporter exporter = config.mbeanExporter();
78-
exporter.setNamingStrategy(this.namingStrategy);
73+
public AnnotationMBeanExporter mbeanExporter(ObjectNamingStrategy namingStrategy) {
74+
AnnotationMBeanExporter exporter = new AnnotationMBeanExporter();
75+
exporter.setRegistrationPolicy(RegistrationPolicy.FAIL_ON_EXISTING);
76+
exporter.setNamingStrategy(namingStrategy);
77+
String server = this.propertyResolver.getProperty("server", "mbeanServer");
78+
if (StringUtils.hasLength(server)) {
79+
exporter.setServer(this.beanFactory.getBean(server, MBeanServer.class));
80+
}
7981
return exporter;
8082
}
8183

8284
@Bean
83-
@ConditionalOnMissingBean(ObjectNamingStrategy.class)
85+
@ConditionalOnMissingBean(value = ObjectNamingStrategy.class, search = SearchStrategy.CURRENT)
8486
public ParentAwareNamingStrategy objectNamingStrategy() {
85-
return new ParentAwareNamingStrategy(new AnnotationJmxAttributeSource());
87+
ParentAwareNamingStrategy namingStrategy = new ParentAwareNamingStrategy(
88+
new AnnotationJmxAttributeSource());
89+
String defaultDomain = this.propertyResolver.getProperty("default-domain");
90+
if (StringUtils.hasLength(defaultDomain)) {
91+
namingStrategy.setDefaultDomain(defaultDomain);
92+
}
93+
return namingStrategy;
8694
}
8795

8896
@Bean
@@ -99,63 +107,4 @@ public MBeanServer mbeanServer() {
99107

100108
}
101109

102-
@EnableMBeanExport(defaultDomain = "${spring.jmx.default_domain:}", server = "${spring.jmx.server:mbeanServer}")
103-
private static class Empty {
104-
105-
}
106-
107-
// Copied and adapted from MBeanExportConfiguration
108-
private static enum SpecificPlatform {
109-
110-
WEBLOGIC("weblogic.management.Helper") {
111-
@Override
112-
public FactoryBean<?> getMBeanServerFactory() {
113-
JndiObjectFactoryBean factory = new JndiObjectFactoryBean();
114-
factory.setJndiName("java:comp/env/jmx/runtime");
115-
return factory;
116-
}
117-
},
118-
119-
WEBSPHERE("com.ibm.websphere.management.AdminServiceFactory") {
120-
@Override
121-
public FactoryBean<MBeanServer> getMBeanServerFactory() {
122-
return new WebSphereMBeanServerFactoryBean();
123-
}
124-
};
125-
126-
private final String identifyingClass;
127-
128-
private SpecificPlatform(String identifyingClass) {
129-
this.identifyingClass = identifyingClass;
130-
}
131-
132-
public MBeanServer getMBeanServer() {
133-
try {
134-
FactoryBean<?> factory = getMBeanServerFactory();
135-
if (factory instanceof InitializingBean) {
136-
((InitializingBean) factory).afterPropertiesSet();
137-
}
138-
Object server = factory.getObject();
139-
Assert.isInstanceOf(MBeanServer.class, server);
140-
return (MBeanServer) server;
141-
}
142-
catch (Exception ex) {
143-
throw new IllegalStateException(ex);
144-
}
145-
}
146-
147-
protected abstract FactoryBean<?> getMBeanServerFactory();
148-
149-
public static SpecificPlatform get() {
150-
ClassLoader classLoader = MBeanExportConfiguration.class.getClassLoader();
151-
for (SpecificPlatform environment : values()) {
152-
if (ClassUtils.isPresent(environment.identifyingClass, classLoader)) {
153-
return environment;
154-
}
155-
}
156-
return null;
157-
}
158-
159-
}
160-
161110
}
Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2015 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.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure.jmx;
1818

19+
import java.util.Hashtable;
20+
1921
import javax.management.MalformedObjectNameException;
2022
import javax.management.ObjectName;
2123

@@ -24,11 +26,11 @@
2426
import org.springframework.context.ApplicationContextAware;
2527
import org.springframework.jmx.export.metadata.JmxAttributeSource;
2628
import org.springframework.jmx.export.naming.MetadataNamingStrategy;
27-
import org.springframework.jmx.export.naming.ObjectNamingStrategy;
29+
import org.springframework.jmx.support.ObjectNameManager;
2830
import org.springframework.util.ObjectUtils;
2931

3032
/**
31-
* JMX {@link ObjectNamingStrategy} that takes into consideration the parent
33+
* Extension of {@link MetadataNamingStrategy} that supports a parent
3234
* {@link ApplicationContext}.
3335
*
3436
* @author Dave Syer
@@ -39,7 +41,7 @@ public class ParentAwareNamingStrategy extends MetadataNamingStrategy implements
3941

4042
private ApplicationContext applicationContext;
4143

42-
private boolean ensureUniqueRuntimeObjectNames = false;
44+
private boolean ensureUniqueRuntimeObjectNames;
4345

4446
public ParentAwareNamingStrategy(JmxAttributeSource attributeSource) {
4547
super(attributeSource);
@@ -55,16 +57,17 @@ public void setEnsureUniqueRuntimeObjectNames(boolean ensureUniqueRuntimeObjectN
5557
@Override
5658
public ObjectName getObjectName(Object managedBean, String beanKey)
5759
throws MalformedObjectNameException {
58-
StringBuilder builder = new StringBuilder(beanKey);
59-
if (parentContextContainsSameBean(this.applicationContext, beanKey)) {
60-
builder.append(",context="
61-
+ ObjectUtils.getIdentityHexString(this.applicationContext));
62-
}
60+
ObjectName name = super.getObjectName(managedBean, beanKey);
61+
Hashtable<String, String> properties = new Hashtable<String, String>();
62+
properties.putAll(name.getKeyPropertyList());
6363
if (this.ensureUniqueRuntimeObjectNames) {
64-
builder.append(",identity=" + ObjectUtils.getIdentityHexString(managedBean));
64+
properties.put("identity", ObjectUtils.getIdentityHexString(managedBean));
6565
}
66-
ObjectName name = super.getObjectName(managedBean, beanKey);
67-
return name;
66+
else if (parentContextContainsSameBean(this.applicationContext, beanKey)) {
67+
properties.put("context",
68+
ObjectUtils.getIdentityHexString(this.applicationContext));
69+
}
70+
return ObjectNameManager.getInstance(name.getDomain(), properties);
6871
}
6972

7073
@Override
@@ -73,19 +76,18 @@ public void setApplicationContext(ApplicationContext applicationContext)
7376
this.applicationContext = applicationContext;
7477
}
7578

76-
private boolean parentContextContainsSameBean(ApplicationContext applicationContext,
79+
private boolean parentContextContainsSameBean(ApplicationContext context,
7780
String beanKey) {
78-
if (applicationContext.getParent() != null) {
79-
try {
80-
this.applicationContext.getParent().getBean(beanKey);
81-
return true;
82-
}
83-
catch (BeansException ex) {
84-
return parentContextContainsSameBean(applicationContext.getParent(),
85-
beanKey);
86-
}
81+
if (context.getParent() == null) {
82+
return false;
83+
}
84+
try {
85+
this.applicationContext.getParent().getBean(beanKey);
86+
return true;
87+
}
88+
catch (BeansException ex) {
89+
return parentContextContainsSameBean(context.getParent(), beanKey);
8790
}
88-
return false;
8991
}
9092

9193
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfigurationTests.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ public void testEnabledMBeanExport() {
7878
this.context.setEnvironment(env);
7979
this.context.register(JmxAutoConfiguration.class);
8080
this.context.refresh();
81-
8281
assertNotNull(this.context.getBean(MBeanExporter.class));
8382
}
8483

@@ -90,7 +89,6 @@ public void testDisabledMBeanExport() {
9089
this.context.setEnvironment(env);
9190
this.context.register(TestConfiguration.class, JmxAutoConfiguration.class);
9291
this.context.refresh();
93-
9492
this.context.getBean(MBeanExporter.class);
9593
}
9694

@@ -103,17 +101,16 @@ public void testDefaultDomainConfiguredOnMBeanExport() {
103101
this.context.setEnvironment(env);
104102
this.context.register(TestConfiguration.class, JmxAutoConfiguration.class);
105103
this.context.refresh();
106-
107104
MBeanExporter mBeanExporter = this.context.getBean(MBeanExporter.class);
108105
assertNotNull(mBeanExporter);
109106
MetadataNamingStrategy naming = (MetadataNamingStrategy) ReflectionTestUtils
110-
.getField(mBeanExporter, "metadataNamingStrategy");
107+
.getField(mBeanExporter, "namingStrategy");
111108
assertEquals("my-test-domain",
112109
ReflectionTestUtils.getField(naming, "defaultDomain"));
113110
}
114111

115112
@Test
116-
public void testParentContext() {
113+
public void testBasicParentContext() {
117114
this.context = new AnnotationConfigApplicationContext();
118115
this.context.register(JmxAutoConfiguration.class);
119116
this.context.refresh();
@@ -124,6 +121,18 @@ public void testParentContext() {
124121
this.context.refresh();
125122
}
126123

124+
@Test
125+
public void testParentContext() throws Exception {
126+
this.context = new AnnotationConfigApplicationContext();
127+
this.context.register(JmxAutoConfiguration.class, TestConfiguration.class);
128+
this.context.refresh();
129+
AnnotationConfigApplicationContext parent = this.context;
130+
this.context = new AnnotationConfigApplicationContext();
131+
this.context.setParent(parent);
132+
this.context.register(JmxAutoConfiguration.class, TestConfiguration.class);
133+
this.context.refresh();
134+
}
135+
127136
@Test
128137
public void customJmxDomain() {
129138
this.context = new AnnotationConfigApplicationContext();

0 commit comments

Comments
 (0)