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.
1818
1919import javax .management .MBeanServer ;
2020
21+ import org .springframework .beans .BeansException ;
2122import 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 ;
2524import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
2625import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
2726import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
2827import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
2928import org .springframework .boot .autoconfigure .condition .SearchStrategy ;
29+ import org .springframework .boot .bind .RelaxedPropertyResolver ;
30+ import org .springframework .context .EnvironmentAware ;
3031import org .springframework .context .annotation .Bean ;
3132import org .springframework .context .annotation .Configuration ;
3233import org .springframework .context .annotation .EnableMBeanExport ;
33- import org .springframework .context .annotation .MBeanExportConfiguration ;
34+ import org .springframework .context .annotation .MBeanExportConfiguration . SpecificPlatform ;
3435import org .springframework .core .env .Environment ;
35- import org .springframework .core .type .StandardAnnotationMetadata ;
3636import org .springframework .jmx .export .MBeanExporter ;
3737import org .springframework .jmx .export .annotation .AnnotationJmxAttributeSource ;
3838import org .springframework .jmx .export .annotation .AnnotationMBeanExporter ;
3939import org .springframework .jmx .export .naming .ObjectNamingStrategy ;
4040import 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
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}
0 commit comments