KickJava   Java API By Example, From Geeks To Geeks.

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


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

58 public class WebLogicMBeanServerFactoryBean implements FactoryBean, InitializingBean {
59
60     private static final String JavaDoc WEBLOGIC_JMX_HELPER_CLASS = "weblogic.management.Helper";
61
62     private static final String JavaDoc GET_MBEAN_HOME_METHOD = "getMBeanHome";
63
64     private static final String JavaDoc GET_MBEAN_SERVER_METHOD = "getMBeanServer";
65
66
67     private String JavaDoc username = "weblogic";
68
69     private String JavaDoc password = "weblogic";
70
71     private String JavaDoc serverUrl = "t3://localhost:7001";
72
73     private String JavaDoc serverName = "server";
74
75     private MBeanServer JavaDoc mbeanServer;
76
77
78     /**
79      * Set the username to use for retrieving the WebLogic MBeanServer.
80      * Default is "weblogic".
81      */

82     public void setUsername(String JavaDoc username) {
83         this.username = username;
84     }
85
86     /**
87      * Set the password to use for retrieving the WebLogic MBeanServer.
88      * Default is "weblogic".
89      */

90     public void setPassword(String JavaDoc password) {
91         this.password = password;
92     }
93
94     /**
95      * Set the server URL to use for retrieving the WebLogic MBeanServer.
96      * Default is "t3://localhost:7001".
97      */

98     public void setServerUrl(String JavaDoc serverUrl) {
99         this.serverUrl = serverUrl;
100     }
101
102     /**
103      * Set the server name to use for retrieving the WebLogic MBeanServer.
104      * Default is "server".
105      */

106     public void setServerName(String JavaDoc serverName) {
107         this.serverName = serverName;
108     }
109
110
111     public void afterPropertiesSet() throws MBeanServerNotFoundException {
112         try {
113             /*
114              * MBeanHome mbeanHome = Helper.getMBeanHome(this.username, this.password, this.serverUrl, this.serverName);
115              */

116             Class JavaDoc helperClass = getClass().getClassLoader().loadClass(WEBLOGIC_JMX_HELPER_CLASS);
117             Class JavaDoc[] argTypes = new Class JavaDoc[] {String JavaDoc.class, String JavaDoc.class, String JavaDoc.class, String JavaDoc.class};
118             Object JavaDoc[] args = new Object JavaDoc[] {this.username, this.password, this.serverUrl, this.serverName};
119             Object JavaDoc mbeanHome = helperClass.getMethod(GET_MBEAN_HOME_METHOD, argTypes).invoke(null, args);
120
121             /*
122              * this.mbeanServer = mbeanHome.getMBeanServer();
123              */

124             this.mbeanServer = (MBeanServer JavaDoc)
125                     mbeanHome.getClass().getMethod(GET_MBEAN_SERVER_METHOD, null).invoke(mbeanHome, null);
126         }
127         catch (ClassNotFoundException JavaDoc ex) {
128             throw new MBeanServerNotFoundException("Could not find WebLogic's JMX Helper class", ex);
129         }
130         catch (InvocationTargetException JavaDoc ex) {
131             throw new MBeanServerNotFoundException(
132                     "WebLogic's JMX Helper.getMBeanHome/getMBeanServer method failed", ex.getTargetException());
133         }
134         catch (Exception JavaDoc ex) {
135             throw new MBeanServerNotFoundException(
136                     "Could not access WebLogic's JMX Helper.getMBeanHome/getMBeanServer method", ex);
137         }
138     }
139
140
141     public Object JavaDoc getObject() {
142         return this.mbeanServer;
143     }
144
145     public Class JavaDoc getObjectType() {
146         return (this.mbeanServer != null ? this.mbeanServer.getClass() : MBeanServer JavaDoc.class);
147     }
148
149     public boolean isSingleton() {
150         return true;
151     }
152
153 }
154
Popular Tags