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