001/* 002 * Copyright 2002-2019 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.jmx.support; 018 019import java.lang.reflect.InvocationTargetException; 020import java.lang.reflect.Method; 021 022import javax.management.MBeanServer; 023 024import org.springframework.beans.factory.FactoryBean; 025import org.springframework.beans.factory.InitializingBean; 026import org.springframework.jmx.MBeanServerNotFoundException; 027import org.springframework.lang.Nullable; 028 029/** 030 * {@link FactoryBean} that obtains a WebSphere {@link javax.management.MBeanServer} 031 * reference through WebSphere's proprietary {@code AdminServiceFactory} API, 032 * available on WebSphere 5.1 and higher. 033 * 034 * <p>Exposes the {@code MBeanServer} for bean references. 035 * 036 * <p>This {@code FactoryBean} is a direct alternative to {@link MBeanServerFactoryBean}, 037 * which uses standard JMX 1.2 API to access the platform's {@link MBeanServer}. 038 * 039 * <p>See the javadocs for WebSphere's 040 * <a href="https://www.ibm.com/support/knowledgecenter/SSEQTJ_9.0.0/com.ibm.websphere.javadoc.doc/web/apidocs/com/ibm/websphere/management/AdminServiceFactory.html">{@code AdminServiceFactory}</a> 041 * and 042 * <a href="https://www.ibm.com/support/knowledgecenter/SSEQTJ_9.0.0/com.ibm.websphere.javadoc.doc/web/apidocs/com/ibm/websphere/management/MBeanFactory.html">{@code MBeanFactory}</a>. 043 * 044 * @author Juergen Hoeller 045 * @author Rob Harrop 046 * @since 2.0.3 047 * @see javax.management.MBeanServer 048 * @see MBeanServerFactoryBean 049 */ 050public class WebSphereMBeanServerFactoryBean implements FactoryBean<MBeanServer>, InitializingBean { 051 052 private static final String ADMIN_SERVICE_FACTORY_CLASS = "com.ibm.websphere.management.AdminServiceFactory"; 053 054 private static final String GET_MBEAN_FACTORY_METHOD = "getMBeanFactory"; 055 056 private static final String GET_MBEAN_SERVER_METHOD = "getMBeanServer"; 057 058 059 @Nullable 060 private MBeanServer mbeanServer; 061 062 063 @Override 064 public void afterPropertiesSet() throws MBeanServerNotFoundException { 065 try { 066 /* 067 * this.mbeanServer = AdminServiceFactory.getMBeanFactory().getMBeanServer(); 068 */ 069 Class<?> adminServiceClass = getClass().getClassLoader().loadClass(ADMIN_SERVICE_FACTORY_CLASS); 070 Method getMBeanFactoryMethod = adminServiceClass.getMethod(GET_MBEAN_FACTORY_METHOD); 071 Object mbeanFactory = getMBeanFactoryMethod.invoke(null); 072 Method getMBeanServerMethod = mbeanFactory.getClass().getMethod(GET_MBEAN_SERVER_METHOD); 073 this.mbeanServer = (MBeanServer) getMBeanServerMethod.invoke(mbeanFactory); 074 } 075 catch (ClassNotFoundException ex) { 076 throw new MBeanServerNotFoundException("Could not find WebSphere's AdminServiceFactory class", ex); 077 } 078 catch (InvocationTargetException ex) { 079 throw new MBeanServerNotFoundException( 080 "WebSphere's AdminServiceFactory.getMBeanFactory/getMBeanServer method failed", ex.getTargetException()); 081 } 082 catch (Exception ex) { 083 throw new MBeanServerNotFoundException( 084 "Could not access WebSphere's AdminServiceFactory.getMBeanFactory/getMBeanServer method", ex); 085 } 086 } 087 088 089 @Override 090 @Nullable 091 public MBeanServer getObject() { 092 return this.mbeanServer; 093 } 094 095 @Override 096 public Class<? extends MBeanServer> getObjectType() { 097 return (this.mbeanServer != null ? this.mbeanServer.getClass() : MBeanServer.class); 098 } 099 100 @Override 101 public boolean isSingleton() { 102 return true; 103 } 104 105}