MBeanExportConfiguration's SpecificPlatform properly calls afterPropertiesSet
Issue: SPR-11877
(cherry picked from commit 0a34f86)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,20 +17,19 @@
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
import javax.naming.NamingException;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.jmx.MBeanServerNotFoundException;
|
||||
import org.springframework.jmx.export.annotation.AnnotationMBeanExporter;
|
||||
import org.springframework.jmx.support.RegistrationPolicy;
|
||||
import org.springframework.jmx.support.WebSphereMBeanServerFactoryBean;
|
||||
import org.springframework.jndi.JndiObjectFactoryBean;
|
||||
import org.springframework.jndi.JndiLocatorDelegate;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -38,8 +37,8 @@ import org.springframework.util.StringUtils;
|
||||
/**
|
||||
* {@code @Configuration} class that registers a {@link AnnotationMBeanExporter} bean.
|
||||
*
|
||||
* <p>This configuration class is automatically imported when using the @{@link
|
||||
* EnableMBeanExport} annotation. See its Javadoc for complete usage details.
|
||||
* <p>This configuration class is automatically imported when using the
|
||||
* {@link EnableMBeanExport} annotation. See its javadoc for complete usage details.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Chris Beams
|
||||
@@ -59,11 +58,11 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
|
||||
public void setImportMetadata(AnnotationMetadata importMetadata) {
|
||||
Map<String, Object> map = importMetadata.getAnnotationAttributes(EnableMBeanExport.class.getName());
|
||||
this.attributes = AnnotationAttributes.fromMap(map);
|
||||
Assert.notNull(this.attributes, "@EnableMBeanExport is not present on " +
|
||||
"importing class " + importMetadata.getClassName());
|
||||
Assert.notNull(this.attributes,
|
||||
"@EnableMBeanExport is not present on importing class " + importMetadata.getClassName());
|
||||
}
|
||||
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@@ -91,7 +90,7 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
|
||||
}
|
||||
else {
|
||||
SpecificPlatform specificPlatform = SpecificPlatform.get();
|
||||
if(specificPlatform != null) {
|
||||
if (specificPlatform != null) {
|
||||
exporter.setServer(specificPlatform.getMBeanServer());
|
||||
}
|
||||
}
|
||||
@@ -103,21 +102,26 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
|
||||
}
|
||||
|
||||
|
||||
private static enum SpecificPlatform {
|
||||
public static enum SpecificPlatform {
|
||||
|
||||
WEBLOGIC("weblogic.management.Helper") {
|
||||
@Override
|
||||
public FactoryBean<?> getMBeanServerFactory() {
|
||||
JndiObjectFactoryBean factory = new JndiObjectFactoryBean();
|
||||
factory.setJndiName("java:comp/env/jmx/runtime");
|
||||
return factory;
|
||||
public MBeanServer getMBeanServer() {
|
||||
try {
|
||||
return new JndiLocatorDelegate().lookup("java:comp/env/jmx/runtime", MBeanServer.class);
|
||||
}
|
||||
catch (NamingException ex) {
|
||||
throw new MBeanServerNotFoundException("Failed to retrieve WebLogic MBeanServer from JNDI", ex);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
WEBSPHERE("com.ibm.websphere.management.AdminServiceFactory") {
|
||||
@Override
|
||||
public FactoryBean<MBeanServer> getMBeanServerFactory() {
|
||||
return new WebSphereMBeanServerFactoryBean();
|
||||
public MBeanServer getMBeanServer() {
|
||||
WebSphereMBeanServerFactoryBean fb = new WebSphereMBeanServerFactoryBean();
|
||||
fb.afterPropertiesSet();
|
||||
return fb.getObject();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -127,27 +131,17 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
|
||||
this.identifyingClass = identifyingClass;
|
||||
}
|
||||
|
||||
public MBeanServer getMBeanServer() {
|
||||
Object server;
|
||||
try {
|
||||
server = getMBeanServerFactory().getObject();
|
||||
Assert.isInstanceOf(MBeanServer.class, server);
|
||||
return (MBeanServer) server;
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract FactoryBean<?> getMBeanServerFactory();
|
||||
public abstract MBeanServer getMBeanServer();
|
||||
|
||||
public static SpecificPlatform get() {
|
||||
ClassLoader classLoader = MBeanExportConfiguration.class.getClassLoader();
|
||||
for (SpecificPlatform environment : values()) {
|
||||
if(ClassUtils.isPresent(environment.identifyingClass, classLoader)) {
|
||||
if (ClassUtils.isPresent(environment.identifyingClass, classLoader)) {
|
||||
return environment;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user