1 16 17 package org.springframework.jmx.support; 18 19 import java.lang.reflect.InvocationTargetException ; 20 21 import javax.management.MBeanServer ; 22 import javax.naming.NamingException ; 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 61 public class WebLogicJndiMBeanServerFactoryBean extends JndiLocatorSupport 62 implements FactoryBean, InitializingBean { 63 64 private static final String WEBLOGIC_MBEAN_HOME_CLASS = "weblogic.management.MBeanHome"; 65 66 private static final String LOCAL_JNDI_NAME_FIELD = "LOCAL_JNDI_NAME"; 67 68 private static final String GET_MBEAN_SERVER_METHOD = "getMBeanServer"; 69 70 71 private String mbeanHomeName; 72 73 private MBeanServer mbeanServer; 74 75 76 82 public void setMbeanHomeName(String mbeanHomeName) { 83 this.mbeanHomeName = mbeanHomeName; 84 } 85 86 87 public void afterPropertiesSet() throws MBeanServerNotFoundException { 88 try { 89 String jndiName = this.mbeanHomeName; 90 if (jndiName == null) { 91 94 Class mbeanHomeClass = ClassUtils.forName(WEBLOGIC_MBEAN_HOME_CLASS); 95 jndiName = (String ) mbeanHomeClass.getField(LOCAL_JNDI_NAME_FIELD).get(null); 96 } 97 Object mbeanHome = lookup(jndiName); 98 99 102 this.mbeanServer = (MBeanServer ) 103 mbeanHome.getClass().getMethod(GET_MBEAN_SERVER_METHOD, null).invoke(mbeanHome, null); 104 } 105 catch (NamingException ex) { 106 throw new MBeanServerNotFoundException("Could not find WebLogic's MBeanHome object in JNDI", ex); 107 } 108 catch (ClassNotFoundException ex) { 109 throw new MBeanServerNotFoundException("Could not find WebLogic's MBeanHome class", ex); 110 } 111 catch (InvocationTargetException ex) { 112 throw new MBeanServerNotFoundException( 113 "WebLogic's MBeanHome.getMBeanServer method failed", ex.getTargetException()); 114 } 115 catch (Exception ex) { 116 throw new MBeanServerNotFoundException( 117 "Could not access WebLogic's MBeanHome/getMBeanServer method", ex); 118 } 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() : MBeanServer .class); 128 } 129 130 public boolean isSingleton() { 131 return true; 132 } 133 134 } 135 | Popular Tags |