|
|
@ -1,5 +1,5 @@
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Copyright 2012-2014 the original author or authors.
|
|
|
|
* Copyright 2012-2015 the original author or authors.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
@ -18,30 +18,28 @@ package org.springframework.boot.autoconfigure.jmx;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.management.MBeanServer;
|
|
|
|
import javax.management.MBeanServer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
import org.springframework.beans.factory.BeanFactory;
|
|
|
|
import org.springframework.beans.factory.BeanFactory;
|
|
|
|
import org.springframework.beans.factory.FactoryBean;
|
|
|
|
import org.springframework.beans.factory.BeanFactoryAware;
|
|
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
|
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
|
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
|
|
|
|
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
|
|
|
|
|
|
|
|
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
|
|
|
|
|
|
|
import org.springframework.context.EnvironmentAware;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.EnableMBeanExport;
|
|
|
|
import org.springframework.context.annotation.EnableMBeanExport;
|
|
|
|
import org.springframework.context.annotation.MBeanExportConfiguration;
|
|
|
|
import org.springframework.context.annotation.MBeanExportConfiguration.SpecificPlatform;
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
import org.springframework.core.type.StandardAnnotationMetadata;
|
|
|
|
|
|
|
|
import org.springframework.jmx.export.MBeanExporter;
|
|
|
|
import org.springframework.jmx.export.MBeanExporter;
|
|
|
|
import org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource;
|
|
|
|
import org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource;
|
|
|
|
import org.springframework.jmx.export.annotation.AnnotationMBeanExporter;
|
|
|
|
import org.springframework.jmx.export.annotation.AnnotationMBeanExporter;
|
|
|
|
import org.springframework.jmx.export.naming.ObjectNamingStrategy;
|
|
|
|
import org.springframework.jmx.export.naming.ObjectNamingStrategy;
|
|
|
|
import org.springframework.jmx.support.MBeanServerFactoryBean;
|
|
|
|
import org.springframework.jmx.support.MBeanServerFactoryBean;
|
|
|
|
import org.springframework.jmx.support.WebSphereMBeanServerFactoryBean;
|
|
|
|
import org.springframework.jmx.support.RegistrationPolicy;
|
|
|
|
import org.springframework.jndi.JndiObjectFactoryBean;
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
|
|
import org.springframework.util.ClassUtils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* {@link EnableAutoConfiguration Auto-configuration} to enable/disable Spring's
|
|
|
|
* {@link EnableAutoConfiguration Auto-configuration} to enable/disable Spring's
|
|
|
@ -54,35 +52,45 @@ import org.springframework.util.ClassUtils;
|
|
|
|
@Configuration
|
|
|
|
@Configuration
|
|
|
|
@ConditionalOnClass({ MBeanExporter.class })
|
|
|
|
@ConditionalOnClass({ MBeanExporter.class })
|
|
|
|
@ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true", matchIfMissing = true)
|
|
|
|
@ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true", matchIfMissing = true)
|
|
|
|
public class JmxAutoConfiguration {
|
|
|
|
public class JmxAutoConfiguration implements EnvironmentAware, BeanFactoryAware {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private RelaxedPropertyResolver propertyResolver;
|
|
|
|
private Environment environment;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private BeanFactory beanFactory;
|
|
|
|
private BeanFactory beanFactory;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Override
|
|
|
|
private ObjectNamingStrategy namingStrategy;
|
|
|
|
public void setEnvironment(Environment environment) {
|
|
|
|
|
|
|
|
this.propertyResolver = new RelaxedPropertyResolver(environment, "spring.jmx.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
|
|
|
|
|
|
|
this.beanFactory = beanFactory;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
@ConditionalOnMissingBean(value = MBeanExporter.class, search = SearchStrategy.CURRENT)
|
|
|
|
@ConditionalOnMissingBean(value = MBeanExporter.class, search = SearchStrategy.CURRENT)
|
|
|
|
public AnnotationMBeanExporter mbeanExporter() {
|
|
|
|
public AnnotationMBeanExporter mbeanExporter(ObjectNamingStrategy namingStrategy) {
|
|
|
|
// Re-use the @EnableMBeanExport configuration
|
|
|
|
AnnotationMBeanExporter exporter = new AnnotationMBeanExporter();
|
|
|
|
MBeanExportConfiguration config = new MBeanExportConfiguration();
|
|
|
|
exporter.setRegistrationPolicy(RegistrationPolicy.FAIL_ON_EXISTING);
|
|
|
|
config.setEnvironment(this.environment);
|
|
|
|
exporter.setNamingStrategy(namingStrategy);
|
|
|
|
config.setBeanFactory(this.beanFactory);
|
|
|
|
String server = this.propertyResolver.getProperty("server", "mbeanServer");
|
|
|
|
config.setImportMetadata(new StandardAnnotationMetadata(Empty.class));
|
|
|
|
if (StringUtils.hasLength(server)) {
|
|
|
|
// But add a custom naming strategy
|
|
|
|
exporter.setServer(this.beanFactory.getBean(server, MBeanServer.class));
|
|
|
|
AnnotationMBeanExporter exporter = config.mbeanExporter();
|
|
|
|
}
|
|
|
|
exporter.setNamingStrategy(this.namingStrategy);
|
|
|
|
|
|
|
|
return exporter;
|
|
|
|
return exporter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
@ConditionalOnMissingBean(ObjectNamingStrategy.class)
|
|
|
|
@ConditionalOnMissingBean(value = ObjectNamingStrategy.class, search = SearchStrategy.CURRENT)
|
|
|
|
public ParentAwareNamingStrategy objectNamingStrategy() {
|
|
|
|
public ParentAwareNamingStrategy objectNamingStrategy() {
|
|
|
|
return new ParentAwareNamingStrategy(new AnnotationJmxAttributeSource());
|
|
|
|
ParentAwareNamingStrategy namingStrategy = new ParentAwareNamingStrategy(
|
|
|
|
|
|
|
|
new AnnotationJmxAttributeSource());
|
|
|
|
|
|
|
|
String defaultDomain = this.propertyResolver.getProperty("default-domain");
|
|
|
|
|
|
|
|
if (StringUtils.hasLength(defaultDomain)) {
|
|
|
|
|
|
|
|
namingStrategy.setDefaultDomain(defaultDomain);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return namingStrategy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
@ -99,63 +107,4 @@ public class JmxAutoConfiguration {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@EnableMBeanExport(defaultDomain = "${spring.jmx.default_domain:}", server = "${spring.jmx.server:mbeanServer}")
|
|
|
|
|
|
|
|
private static class Empty {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Copied and adapted from MBeanExportConfiguration
|
|
|
|
|
|
|
|
private static enum SpecificPlatform {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WEBLOGIC("weblogic.management.Helper") {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public FactoryBean<?> getMBeanServerFactory() {
|
|
|
|
|
|
|
|
JndiObjectFactoryBean factory = new JndiObjectFactoryBean();
|
|
|
|
|
|
|
|
factory.setJndiName("java:comp/env/jmx/runtime");
|
|
|
|
|
|
|
|
return factory;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WEBSPHERE("com.ibm.websphere.management.AdminServiceFactory") {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public FactoryBean<MBeanServer> getMBeanServerFactory() {
|
|
|
|
|
|
|
|
return new WebSphereMBeanServerFactoryBean();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final String identifyingClass;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private SpecificPlatform(String identifyingClass) {
|
|
|
|
|
|
|
|
this.identifyingClass = identifyingClass;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public MBeanServer getMBeanServer() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
FactoryBean<?> factory = getMBeanServerFactory();
|
|
|
|
|
|
|
|
if (factory instanceof InitializingBean) {
|
|
|
|
|
|
|
|
((InitializingBean) factory).afterPropertiesSet();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Object server = factory.getObject();
|
|
|
|
|
|
|
|
Assert.isInstanceOf(MBeanServer.class, server);
|
|
|
|
|
|
|
|
return (MBeanServer) server;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
|
|
|
throw new IllegalStateException(ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected abstract FactoryBean<?> getMBeanServerFactory();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static SpecificPlatform get() {
|
|
|
|
|
|
|
|
ClassLoader classLoader = MBeanExportConfiguration.class.getClassLoader();
|
|
|
|
|
|
|
|
for (SpecificPlatform environment : values()) {
|
|
|
|
|
|
|
|
if (ClassUtils.isPresent(environment.identifyingClass, classLoader)) {
|
|
|
|
|
|
|
|
return environment;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|