KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2006 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
21 import javax.management.MBeanServer JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23
24 import org.springframework.beans.factory.FactoryBean;
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.jmx.MBeanServerNotFoundException;
27 import org.springframework.jndi.JndiLocatorSupport;
28 import org.springframework.util.ClassUtils;
29
30 /**
31  * FactoryBean that obtains a specified WebLogic {@link javax.management.MBeanServer}
32  * reference through a WebLogic <code>MBeanHome</code> obtained via a JNDI lookup.
33  * By default, the server's local <code>MBeanHome</code> will be obtained.
34  *
35  * <p>Exposes the <code>MBeanServer</code> for bean references.
36  * This FactoryBean is a direct alternative to {@link MBeanServerFactoryBean},
37  * which uses standard JMX 1.2 API to access the platform's MBeanServer.
38  *
39  * <p>Note: There is also a more general {@link WebLogicMBeanServerFactoryBean}
40  * for accessing any specified WebLogic <code>MBeanServer</code>,
41  * potentially a remote one.
42  *
43  * <p><b>NOTE: This class is only intended for use with WebLogic 8.1.</b>
44  * On WebLogic 9.x, simply obtain the MBeanServer directly from the JNDI location
45  * "java:comp/env/jmx/runtime", for example through the following configuration:
46  *
47  * <pre>
48  * &lt;bean class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
49  * &lt;property name="jndiName" value="java:comp/env/jmx/runtime"/&gt;
50  * &lt;/bean&gt;</pre>
51  *
52  * @author Rob Harrop
53  * @author Juergen Hoeller
54  * @since 1.2.6
55  * @see weblogic.management.MBeanHome#LOCAL_JNDI_NAME
56  * @see weblogic.management.MBeanHome#getMBeanServer()
57  * @see javax.management.MBeanServer
58  * @see MBeanServerFactoryBean
59  * @see WebLogicMBeanServerFactoryBean
60  */

61 public class WebLogicJndiMBeanServerFactoryBean extends JndiLocatorSupport
62         implements FactoryBean, InitializingBean {
63
64     private static final String JavaDoc WEBLOGIC_MBEAN_HOME_CLASS = "weblogic.management.MBeanHome";
65
66     private static final String JavaDoc LOCAL_JNDI_NAME_FIELD = "LOCAL_JNDI_NAME";
67
68     private static final String JavaDoc GET_MBEAN_SERVER_METHOD = "getMBeanServer";
69
70
71     private String JavaDoc mbeanHomeName;
72
73     private MBeanServer JavaDoc mbeanServer;
74
75
76     /**
77      * Specify the JNDI name of the WebLogic MBeanHome object to use
78      * for creating the JMX MBeanServer reference.
79      * <p>Default is <code>MBeanHome.LOCAL_JNDI_NAME</code>
80      * @see weblogic.management.MBeanHome#LOCAL_JNDI_NAME
81      */

82     public void setMbeanHomeName(String JavaDoc mbeanHomeName) {
83         this.mbeanHomeName = mbeanHomeName;
84     }
85
86
87     public void afterPropertiesSet() throws MBeanServerNotFoundException {
88         try {
89             String JavaDoc jndiName = this.mbeanHomeName;
90             if (jndiName == null) {
91                 /*
92                  * jndiName = MBeanHome.LOCAL_JNDI_NAME;
93                  */

94                 Class JavaDoc mbeanHomeClass = ClassUtils.forName(WEBLOGIC_MBEAN_HOME_CLASS);
95                 jndiName = (String JavaDoc) mbeanHomeClass.getField(LOCAL_JNDI_NAME_FIELD).get(null);
96             }
97             Object JavaDoc mbeanHome = lookup(jndiName);
98
99             /*
100              * this.mbeanServer = mbeanHome.getMBeanServer();
101              */

102             this.mbeanServer = (MBeanServer JavaDoc)
103                     mbeanHome.getClass().getMethod(GET_MBEAN_SERVER_METHOD, null).invoke(mbeanHome, null);
104         }
105         catch (NamingException JavaDoc ex) {
106             throw new MBeanServerNotFoundException("Could not find WebLogic's MBeanHome object in JNDI", ex);
107         }
108         catch (ClassNotFoundException JavaDoc ex) {
109             throw new MBeanServerNotFoundException("Could not find WebLogic's MBeanHome class", ex);
110         }
111         catch (InvocationTargetException JavaDoc ex) {
112             throw new MBeanServerNotFoundException(
113                     "WebLogic's MBeanHome.getMBeanServer method failed", ex.getTargetException());
114         }
115         catch (Exception JavaDoc ex) {
116             throw new MBeanServerNotFoundException(
117                     "Could not access WebLogic's MBeanHome/getMBeanServer method", ex);
118         }
119     }
120
121
122     public Object JavaDoc getObject() {
123         return this.mbeanServer;
124     }
125
126     public Class JavaDoc getObjectType() {
127         return (this.mbeanServer != null ? this.mbeanServer.getClass() : MBeanServer JavaDoc.class);
128     }
129
130     public boolean isSingleton() {
131         return true;
132     }
133
134 }
135
Popular Tags