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:
021: import javax.management.MBeanServer;
022:
023: import org.springframework.beans.factory.FactoryBean;
024: import org.springframework.beans.factory.InitializingBean;
025: import org.springframework.jmx.MBeanServerNotFoundException;
026:
027: /**
028: * FactoryBean that obtains a specified WebLogic {@link javax.management.MBeanServer}
029: * reference through WebLogic's proprietary <code>Helper</code> /
030: * <code>MBeanHome</code> API, which is available on WebLogic 6.1 and higher.
031: *
032: * <p>Exposes the <code>MBeanServer</code> 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>Note: There is also a {@link WebLogicJndiMBeanServerFactoryBean} for
037: * accessing the WebLogic <code>MBeanServer</code> instance through a WebLogic
038: * <code>MBeanHome</code> obtained via a JNDI lookup, typical a local one.
039: *
040: * <p><b>NOTE: This class is only intended for use with WebLogic up to 8.1.</b>
041: * On WebLogic 9.x, simply obtain the MBeanServer directly from the JNDI location
042: * "java:comp/env/jmx/runtime", for example through the following configuration:
043: *
044: * <pre>
045: * <bean class="org.springframework.jndi.JndiObjectFactoryBean">
046: * <property name="jndiName" value="java:comp/env/jmx/runtime"/>
047: * </bean></pre>
048: *
049: * @author Rob Harrop
050: * @author Juergen Hoeller
051: * @since 1.2
052: * @see weblogic.management.Helper#getMBeanHome(String, String, String, String)
053: * @see weblogic.management.MBeanHome#getMBeanServer()
054: * @see javax.management.MBeanServer
055: * @see MBeanServerFactoryBean
056: * @see WebLogicJndiMBeanServerFactoryBean
057: */
058: public class WebLogicMBeanServerFactoryBean implements FactoryBean,
059: InitializingBean {
060:
061: private static final String WEBLOGIC_JMX_HELPER_CLASS = "weblogic.management.Helper";
062:
063: private static final String GET_MBEAN_HOME_METHOD = "getMBeanHome";
064:
065: private static final String GET_MBEAN_SERVER_METHOD = "getMBeanServer";
066:
067: private String username = "weblogic";
068:
069: private String password = "weblogic";
070:
071: private String serverUrl = "t3://localhost:7001";
072:
073: private String serverName = "server";
074:
075: private MBeanServer mbeanServer;
076:
077: /**
078: * Set the username to use for retrieving the WebLogic MBeanServer.
079: * Default is "weblogic".
080: */
081: public void setUsername(String username) {
082: this .username = username;
083: }
084:
085: /**
086: * Set the password to use for retrieving the WebLogic MBeanServer.
087: * Default is "weblogic".
088: */
089: public void setPassword(String password) {
090: this .password = password;
091: }
092:
093: /**
094: * Set the server URL to use for retrieving the WebLogic MBeanServer.
095: * Default is "t3://localhost:7001".
096: */
097: public void setServerUrl(String serverUrl) {
098: this .serverUrl = serverUrl;
099: }
100:
101: /**
102: * Set the server name to use for retrieving the WebLogic MBeanServer.
103: * Default is "server".
104: */
105: public void setServerName(String serverName) {
106: this .serverName = serverName;
107: }
108:
109: public void afterPropertiesSet()
110: throws MBeanServerNotFoundException {
111: try {
112: /*
113: * MBeanHome mbeanHome = Helper.getMBeanHome(this.username, this.password, this.serverUrl, this.serverName);
114: */
115: Class helperClass = getClass().getClassLoader().loadClass(
116: WEBLOGIC_JMX_HELPER_CLASS);
117: Class[] argTypes = new Class[] { String.class,
118: String.class, String.class, String.class };
119: Object[] args = new Object[] { this .username,
120: this .password, this .serverUrl, this .serverName };
121: Object mbeanHome = helperClass.getMethod(
122: GET_MBEAN_HOME_METHOD, argTypes).invoke(null, args);
123:
124: /*
125: * this.mbeanServer = mbeanHome.getMBeanServer();
126: */
127: this .mbeanServer = (MBeanServer) mbeanHome.getClass()
128: .getMethod(GET_MBEAN_SERVER_METHOD, null).invoke(
129: mbeanHome, null);
130: } catch (ClassNotFoundException ex) {
131: throw new MBeanServerNotFoundException(
132: "Could not find WebLogic's JMX Helper class", ex);
133: } catch (InvocationTargetException ex) {
134: throw new MBeanServerNotFoundException(
135: "WebLogic's JMX Helper.getMBeanHome/getMBeanServer method failed",
136: ex.getTargetException());
137: } catch (Exception ex) {
138: throw new MBeanServerNotFoundException(
139: "Could not access WebLogic's JMX Helper.getMBeanHome/getMBeanServer method",
140: ex);
141: }
142: }
143:
144: public Object getObject() {
145: return this .mbeanServer;
146: }
147:
148: public Class getObjectType() {
149: return (this .mbeanServer != null ? this .mbeanServer.getClass()
150: : MBeanServer.class);
151: }
152:
153: public boolean isSingleton() {
154: return true;
155: }
156:
157: }
|