001/*
002 * Copyright 2002-2014 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;
021import javax.management.MBeanServer;
022
023import org.springframework.beans.factory.FactoryBean;
024import org.springframework.beans.factory.InitializingBean;
025import org.springframework.jmx.MBeanServerNotFoundException;
026
027/**
028 * {@link FactoryBean} that obtains a WebSphere {@link javax.management.MBeanServer}
029 * reference through WebSphere's proprietary {@code AdminServiceFactory} API,
030 * available on WebSphere 5.1 and higher.
031 *
032 * <p>Exposes the {@code MBeanServer} for bean references.
033 * This FactoryBean is a direct alternative to {@link MBeanServerFactoryBean},
034 * which uses standard JMX 1.2 API to access the platform's MBeanServer.
035 *
036 * <p>See the javadocs for WebSphere's
037 * <a href="https://bit.ly/UzccDt">{@code AdminServiceFactory}</a>
038 * and <a href="https://bit.ly/TRlX2r">{@code MBeanFactory}</a>.
039 *
040 * @author Juergen Hoeller
041 * @author Rob Harrop
042 * @since 2.0.3
043 * @see javax.management.MBeanServer
044 * @see MBeanServerFactoryBean
045 */
046public class WebSphereMBeanServerFactoryBean implements FactoryBean<MBeanServer>, InitializingBean {
047
048        private static final String ADMIN_SERVICE_FACTORY_CLASS = "com.ibm.websphere.management.AdminServiceFactory";
049
050        private static final String GET_MBEAN_FACTORY_METHOD = "getMBeanFactory";
051
052        private static final String GET_MBEAN_SERVER_METHOD = "getMBeanServer";
053
054
055        private MBeanServer mbeanServer;
056
057
058        @Override
059        public void afterPropertiesSet() throws MBeanServerNotFoundException {
060                try {
061                        /*
062                         * this.mbeanServer = AdminServiceFactory.getMBeanFactory().getMBeanServer();
063                         */
064                        Class<?> adminServiceClass = getClass().getClassLoader().loadClass(ADMIN_SERVICE_FACTORY_CLASS);
065                        Method getMBeanFactoryMethod = adminServiceClass.getMethod(GET_MBEAN_FACTORY_METHOD);
066                        Object mbeanFactory = getMBeanFactoryMethod.invoke(null);
067                        Method getMBeanServerMethod = mbeanFactory.getClass().getMethod(GET_MBEAN_SERVER_METHOD);
068                        this.mbeanServer = (MBeanServer) getMBeanServerMethod.invoke(mbeanFactory);
069                }
070                catch (ClassNotFoundException ex) {
071                        throw new MBeanServerNotFoundException("Could not find WebSphere's AdminServiceFactory class", ex);
072                }
073                catch (InvocationTargetException ex) {
074                        throw new MBeanServerNotFoundException(
075                                        "WebSphere's AdminServiceFactory.getMBeanFactory/getMBeanServer method failed", ex.getTargetException());
076                }
077                catch (Exception ex) {
078                        throw new MBeanServerNotFoundException(
079                                        "Could not access WebSphere's AdminServiceFactory.getMBeanFactory/getMBeanServer method", ex);
080                }
081        }
082
083
084        @Override
085        public MBeanServer getObject() {
086                return this.mbeanServer;
087        }
088
089        @Override
090        public Class<? extends MBeanServer> getObjectType() {
091                return (this.mbeanServer != null ? this.mbeanServer.getClass() : MBeanServer.class);
092        }
093
094        @Override
095        public boolean isSingleton() {
096                return true;
097        }
098
099}