1 16 17 package org.springframework.jmx.support; 18 19 import javax.management.MBeanServer ; 20 import javax.management.MBeanServerFactory ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 import org.springframework.beans.factory.DisposableBean; 26 import org.springframework.beans.factory.FactoryBean; 27 import org.springframework.beans.factory.InitializingBean; 28 import org.springframework.jmx.MBeanServerNotFoundException; 29 30 54 public class MBeanServerFactoryBean implements FactoryBean, InitializingBean, DisposableBean { 55 56 protected final Log logger = LogFactory.getLog(getClass()); 57 58 private boolean locateExistingServerIfPossible = false; 59 60 private String agentId; 61 62 private String defaultDomain; 63 64 private boolean registerWithFactory = true; 65 66 private MBeanServer server; 67 68 private boolean newlyRegistered = false; 69 70 71 76 public void setLocateExistingServerIfPossible(boolean locateExistingServerIfPossible) { 77 this.locateExistingServerIfPossible = locateExistingServerIfPossible; 78 } 79 80 89 public void setAgentId(String agentId) { 90 this.agentId = agentId; 91 } 92 93 101 public void setDefaultDomain(String defaultDomain) { 102 this.defaultDomain = defaultDomain; 103 } 104 105 112 public void setRegisterWithFactory(boolean registerWithFactory) { 113 this.registerWithFactory = registerWithFactory; 114 } 115 116 117 120 public void afterPropertiesSet() throws MBeanServerNotFoundException { 121 if (this.locateExistingServerIfPossible || this.agentId != null) { 123 try { 124 this.server = locateMBeanServer(this.agentId); 125 } 126 catch (MBeanServerNotFoundException ex) { 127 if (this.agentId != null) { 130 throw ex; 131 } 132 logger.info("No existing MBeanServer found - creating new one"); 133 } 134 } 135 136 if (this.server == null) { 138 this.server = createMBeanServer(this.defaultDomain, this.registerWithFactory); 139 this.newlyRegistered = this.registerWithFactory; 140 } 141 } 142 143 158 protected MBeanServer locateMBeanServer(String agentId) throws MBeanServerNotFoundException { 159 return JmxUtils.locateMBeanServer(agentId); 160 } 161 162 171 protected MBeanServer createMBeanServer(String defaultDomain, boolean registerWithFactory) { 172 if (registerWithFactory) { 173 return MBeanServerFactory.createMBeanServer(defaultDomain); 174 } 175 else { 176 return MBeanServerFactory.newMBeanServer(defaultDomain); 177 } 178 } 179 180 181 public Object getObject() { 182 return this.server; 183 } 184 185 public Class getObjectType() { 186 return (this.server != null ? this.server.getClass() : MBeanServer .class); 187 } 188 189 public boolean isSingleton() { 190 return true; 191 } 192 193 194 197 public void destroy() { 198 if (this.newlyRegistered) { 199 MBeanServerFactory.releaseMBeanServer(this.server); 200 } 201 } 202 203 } 204 | Popular Tags |