1 16 17 package org.springframework.jmx.support; 18 19 import java.io.IOException ; 20 import java.net.MalformedURLException ; 21 import java.util.Map ; 22 import java.util.Properties ; 23 24 import javax.management.MBeanServerConnection ; 25 import javax.management.remote.JMXConnector ; 26 import javax.management.remote.JMXConnectorFactory ; 27 import javax.management.remote.JMXServiceURL ; 28 29 import org.springframework.aop.TargetSource; 30 import org.springframework.aop.framework.ProxyFactory; 31 import org.springframework.aop.target.AbstractLazyCreationTargetSource; 32 import org.springframework.beans.factory.BeanClassLoaderAware; 33 import org.springframework.beans.factory.DisposableBean; 34 import org.springframework.beans.factory.FactoryBean; 35 import org.springframework.beans.factory.InitializingBean; 36 import org.springframework.util.ClassUtils; 37 38 49 public class MBeanServerConnectionFactoryBean 50 implements FactoryBean, BeanClassLoaderAware, InitializingBean, DisposableBean { 51 52 private JMXServiceURL serviceUrl; 53 54 private Map environment; 55 56 private boolean connectOnStartup = true; 57 58 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 59 60 private JMXConnector connector; 61 62 private MBeanServerConnection connection; 63 64 private JMXConnectorLazyInitTargetSource connectorTargetSource; 65 66 67 70 public void setServiceUrl(String url) throws MalformedURLException { 71 this.serviceUrl = new JMXServiceURL (url); 72 } 73 74 78 public void setEnvironment(Properties environment) { 79 this.environment = environment; 80 } 81 82 86 public void setEnvironmentMap(Map environment) { 87 this.environment = environment; 88 } 89 90 95 public void setConnectOnStartup(boolean connectOnStartup) { 96 this.connectOnStartup = connectOnStartup; 97 } 98 99 public void setBeanClassLoader(ClassLoader classLoader) { 100 this.beanClassLoader = classLoader; 101 } 102 103 104 108 public void afterPropertiesSet() throws IOException { 109 if (this.serviceUrl == null) { 110 throw new IllegalArgumentException ("serviceUrl is required"); 111 } 112 113 if (this.connectOnStartup) { 114 connect(); 115 } 116 else { 117 createLazyConnection(); 118 } 119 } 120 121 125 private void connect() throws IOException { 126 this.connector = JMXConnectorFactory.connect(this.serviceUrl, this.environment); 127 this.connection = this.connector.getMBeanServerConnection(); 128 } 129 130 133 private void createLazyConnection() { 134 this.connectorTargetSource = new JMXConnectorLazyInitTargetSource(); 135 TargetSource connectionTargetSource = new MBeanServerConnectionLazyInitTargetSource(); 136 137 this.connector = (JMXConnector ) 138 new ProxyFactory(JMXConnector .class, this.connectorTargetSource).getProxy(this.beanClassLoader); 139 this.connection = (MBeanServerConnection ) 140 new ProxyFactory(MBeanServerConnection .class, connectionTargetSource).getProxy(this.beanClassLoader); 141 } 142 143 144 public Object getObject() { 145 return this.connection; 146 } 147 148 public Class getObjectType() { 149 return (this.connection != null ? this.connection.getClass() : MBeanServerConnection .class); 150 } 151 152 public boolean isSingleton() { 153 return true; 154 } 155 156 157 160 public void destroy() throws IOException { 161 if (this.connectorTargetSource == null || this.connectorTargetSource.isInitialized()) { 162 this.connector.close(); 163 } 164 } 165 166 167 173 private class JMXConnectorLazyInitTargetSource extends AbstractLazyCreationTargetSource { 174 175 protected Object createObject() throws Exception { 176 return JMXConnectorFactory.connect(serviceUrl, environment); 177 } 178 179 public Class getTargetClass() { 180 return JMXConnector .class; 181 } 182 } 183 184 185 188 private class MBeanServerConnectionLazyInitTargetSource extends AbstractLazyCreationTargetSource { 189 190 protected Object createObject() throws Exception { 191 return connector.getMBeanServerConnection(); 192 } 193 194 public Class getTargetClass() { 195 return MBeanServerConnection .class; 196 } 197 } 198 199 } 200 | Popular Tags |