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: import javax.naming.NamingException;
023:
024: import org.springframework.beans.factory.FactoryBean;
025: import org.springframework.beans.factory.InitializingBean;
026: import org.springframework.jmx.MBeanServerNotFoundException;
027: import org.springframework.jndi.JndiLocatorSupport;
028:
029: /**
030: * FactoryBean that obtains a specified WebLogic {@link javax.management.MBeanServer}
031: * reference through a WebLogic <code>MBeanHome</code> obtained via a JNDI lookup.
032: * By default, the server's local <code>MBeanHome</code> will be obtained.
033: *
034: * <p>Exposes the <code>MBeanServer</code> for bean references.
035: * This FactoryBean is a direct alternative to {@link MBeanServerFactoryBean},
036: * which uses standard JMX 1.2 API to access the platform's MBeanServer.
037: *
038: * <p>Note: There is also a more general {@link WebLogicMBeanServerFactoryBean}
039: * for accessing any specified WebLogic <code>MBeanServer</code>,
040: * potentially a remote one.
041: *
042: * <p><b>NOTE: This class is only intended for use with WebLogic 8.1.</b>
043: * On WebLogic 9.x, simply obtain the MBeanServer directly from the JNDI location
044: * "java:comp/env/jmx/runtime", for example through the following configuration:
045: *
046: * <pre>
047: * <bean class="org.springframework.jndi.JndiObjectFactoryBean">
048: * <property name="jndiName" value="java:comp/env/jmx/runtime"/>
049: * </bean></pre>
050: *
051: * @author Rob Harrop
052: * @author Juergen Hoeller
053: * @since 1.2.6
054: * @see weblogic.management.MBeanHome#LOCAL_JNDI_NAME
055: * @see weblogic.management.MBeanHome#getMBeanServer()
056: * @see javax.management.MBeanServer
057: * @see MBeanServerFactoryBean
058: * @see WebLogicMBeanServerFactoryBean
059: */
060: public class WebLogicJndiMBeanServerFactoryBean extends
061: JndiLocatorSupport implements FactoryBean, InitializingBean {
062:
063: private static final String WEBLOGIC_MBEAN_HOME_CLASS = "weblogic.management.MBeanHome";
064:
065: private static final String LOCAL_JNDI_NAME_FIELD = "LOCAL_JNDI_NAME";
066:
067: private static final String GET_MBEAN_SERVER_METHOD = "getMBeanServer";
068:
069: private String mbeanHomeName;
070:
071: private MBeanServer mbeanServer;
072:
073: /**
074: * Specify the JNDI name of the WebLogic MBeanHome object to use
075: * for creating the JMX MBeanServer reference.
076: * <p>Default is <code>MBeanHome.LOCAL_JNDI_NAME</code>
077: * @see weblogic.management.MBeanHome#LOCAL_JNDI_NAME
078: */
079: public void setMbeanHomeName(String mbeanHomeName) {
080: this .mbeanHomeName = mbeanHomeName;
081: }
082:
083: public void afterPropertiesSet()
084: throws MBeanServerNotFoundException {
085: try {
086: String jndiName = this .mbeanHomeName;
087: if (jndiName == null) {
088: /*
089: * jndiName = MBeanHome.LOCAL_JNDI_NAME;
090: */
091: Class mbeanHomeClass = getClass().getClassLoader()
092: .loadClass(WEBLOGIC_MBEAN_HOME_CLASS);
093: jndiName = (String) mbeanHomeClass.getField(
094: LOCAL_JNDI_NAME_FIELD).get(null);
095: }
096: Object mbeanHome = lookup(jndiName);
097:
098: /*
099: * this.mbeanServer = mbeanHome.getMBeanServer();
100: */
101: this .mbeanServer = (MBeanServer) mbeanHome.getClass()
102: .getMethod(GET_MBEAN_SERVER_METHOD, null).invoke(
103: mbeanHome, null);
104: } catch (NamingException ex) {
105: throw new MBeanServerNotFoundException(
106: "Could not find WebLogic's MBeanHome object in JNDI",
107: ex);
108: } catch (ClassNotFoundException ex) {
109: throw new MBeanServerNotFoundException(
110: "Could not find WebLogic's MBeanHome class", ex);
111: } catch (InvocationTargetException ex) {
112: throw new MBeanServerNotFoundException(
113: "WebLogic's MBeanHome.getMBeanServer method failed",
114: ex.getTargetException());
115: } catch (Exception ex) {
116: throw new MBeanServerNotFoundException(
117: "Could not access WebLogic's MBeanHome/getMBeanServer method",
118: ex);
119: }
120: }
121:
122: public Object getObject() {
123: return this .mbeanServer;
124: }
125:
126: public Class getObjectType() {
127: return (this .mbeanServer != null ? this .mbeanServer.getClass()
128: : MBeanServer.class);
129: }
130:
131: public boolean isSingleton() {
132: return true;
133: }
134:
135: }
|