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