KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > support > WebSphereMBeanServerFactoryBean


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jmx.support;
18
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21
22 import javax.management.MBeanServer JavaDoc;
23
24 import org.springframework.beans.factory.FactoryBean;
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.jmx.MBeanServerNotFoundException;
27
28 /**
29  * FactoryBean that obtains a WebSphere {@link javax.management.MBeanServer}
30  * reference through WebSphere's proprietary <code>AdminServiceFactory</code> API,
31  * available on WebSphere 5.1 and higher.
32  *
33  * <p>Exposes the <code>MBeanServer</code> for bean references.
34  * This FactoryBean is a direct alternative to {@link MBeanServerFactoryBean},
35  * which uses standard JMX 1.2 API to access the platform's MBeanServer.
36  *
37  * @author Juergen Hoeller
38  * @author Rob Harrop
39  * @since 2.0.3
40  * @see com.ibm.websphere.management.AdminServiceFactory#getMBeanFactory()
41  * @see com.ibm.websphere.management.MBeanFactory#getMBeanServer()
42  * @see javax.management.MBeanServer
43  * @see MBeanServerFactoryBean
44  */

45 public class WebSphereMBeanServerFactoryBean implements FactoryBean, InitializingBean {
46
47     private static final String JavaDoc ADMIN_SERVICE_FACTORY_CLASS = "com.ibm.websphere.management.AdminServiceFactory";
48
49     private static final String JavaDoc GET_MBEAN_FACTORY_METHOD = "getMBeanFactory";
50
51     private static final String JavaDoc GET_MBEAN_SERVER_METHOD = "getMBeanServer";
52
53
54     private MBeanServer JavaDoc mbeanServer;
55
56
57     public void afterPropertiesSet() throws MBeanServerNotFoundException {
58         try {
59             /*
60              * this.mbeanServer = AdminServiceFactory.getMBeanFactory().getMBeanServer();
61              */

62             Class JavaDoc adminServiceClass = getClass().getClassLoader().loadClass(ADMIN_SERVICE_FACTORY_CLASS);
63             Method JavaDoc getMBeanFactoryMethod = adminServiceClass.getMethod(GET_MBEAN_FACTORY_METHOD, new Class JavaDoc[0]);
64             Object JavaDoc mbeanFactory = getMBeanFactoryMethod.invoke(null, new Object JavaDoc[0]);
65             Method JavaDoc getMBeanServerMethod = mbeanFactory.getClass().getMethod(GET_MBEAN_SERVER_METHOD, new Class JavaDoc[0]);
66             this.mbeanServer = (MBeanServer JavaDoc) getMBeanServerMethod.invoke(mbeanFactory, new Object JavaDoc[0]);
67         }
68         catch (ClassNotFoundException JavaDoc ex) {
69             throw new MBeanServerNotFoundException("Could not find WebSphere's AdminServiceFactory class", ex);
70         }
71         catch (InvocationTargetException JavaDoc ex) {
72             throw new MBeanServerNotFoundException(
73                     "WebSphere's AdminServiceFactory.getMBeanFactory/getMBeanServer method failed", ex.getTargetException());
74         }
75         catch (Exception JavaDoc ex) {
76             throw new MBeanServerNotFoundException(
77                     "Could not access WebSphere's AdminServiceFactory.getMBeanFactory/getMBeanServer method", ex);
78         }
79     }
80
81
82     public Object JavaDoc getObject() {
83         return this.mbeanServer;
84     }
85
86     public Class JavaDoc getObjectType() {
87         return (this.mbeanServer != null ? this.mbeanServer.getClass() : MBeanServer JavaDoc.class);
88     }
89
90     public boolean isSingleton() {
91         return true;
92     }
93
94 }
95
Popular Tags