1 16 17 package org.springframework.jmx.support; 18 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.Set ; 22 23 import javax.management.InstanceAlreadyExistsException ; 24 import javax.management.InstanceNotFoundException ; 25 import javax.management.JMException ; 26 import javax.management.MBeanServer ; 27 import javax.management.ObjectInstance ; 28 import javax.management.ObjectName ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 import org.springframework.core.Constants; 34 35 69 public class MBeanRegistrationSupport { 70 71 76 public static final int REGISTRATION_FAIL_ON_EXISTING = 0; 77 78 82 public static final int REGISTRATION_IGNORE_EXISTING = 1; 83 84 88 public static final int REGISTRATION_REPLACE_EXISTING = 2; 89 90 91 94 private static final Constants constants = new Constants(MBeanRegistrationSupport.class); 95 96 99 protected final Log logger = LogFactory.getLog(getClass()); 100 101 104 protected MBeanServer server; 105 106 109 protected Set registeredBeans = new HashSet (); 110 111 115 private int registrationBehavior = REGISTRATION_FAIL_ON_EXISTING; 116 117 118 123 public void setServer(MBeanServer server) { 124 this.server = server; 125 } 126 127 135 public void setRegistrationBehaviorName(String registrationBehavior) { 136 setRegistrationBehavior(constants.asNumber(registrationBehavior).intValue()); 137 } 138 139 148 public void setRegistrationBehavior(int registrationBehavior) { 149 this.registrationBehavior = registrationBehavior; 150 } 151 152 153 159 protected void doRegister(Object mbean, ObjectName objectName) throws JMException { 160 ObjectInstance registeredBean = null; 161 try { 162 registeredBean = this.server.registerMBean(mbean, objectName); 163 } 164 catch (InstanceAlreadyExistsException ex) { 165 if (this.registrationBehavior == REGISTRATION_IGNORE_EXISTING) { 166 if (logger.isDebugEnabled()) { 167 logger.debug("Ignoring existing MBean at [" + objectName + "]"); 168 } 169 } 170 else if (this.registrationBehavior == REGISTRATION_REPLACE_EXISTING) { 171 try { 172 if (logger.isDebugEnabled()) { 173 logger.debug("Replacing existing MBean at [" + objectName + "]"); 174 } 175 this.server.unregisterMBean(objectName); 176 registeredBean = this.server.registerMBean(mbean, objectName); 177 } 178 catch (InstanceNotFoundException ex2) { 179 logger.error("Unable to replace existing MBean at [" + objectName + "]", ex2); 180 throw ex; 181 } 182 } 183 else { 184 throw ex; 185 } 186 } 187 188 ObjectName actualObjectName = (registeredBean != null ? registeredBean.getObjectName() : null); 190 if (actualObjectName == null) { 191 actualObjectName = objectName; 192 } 193 this.registeredBeans.add(actualObjectName); 194 onRegister(actualObjectName); 195 } 196 197 200 protected void unregisterBeans() { 201 for (Iterator it = this.registeredBeans.iterator(); it.hasNext();) { 202 ObjectName objectName = (ObjectName ) it.next(); 203 try { 204 if (this.server.isRegistered(objectName)) { 206 this.server.unregisterMBean(objectName); 207 onUnregister(objectName); 208 } 209 else { 210 if (logger.isWarnEnabled()) { 211 logger.warn("Could not unregister MBean [" + objectName + "] as said MBean " + 212 "is not registered (perhaps already unregistered by an external process)"); 213 } 214 } 215 } 216 catch (JMException ex) { 217 if (logger.isErrorEnabled()) { 218 logger.error("Could not unregister MBean [" + objectName + "]", ex); 219 } 220 } 221 } 222 this.registeredBeans.clear(); 223 } 224 225 226 231 protected void onRegister(ObjectName objectName) { 232 } 233 234 239 protected void onUnregister(ObjectName objectName) { 240 } 241 242 } 243 | Popular Tags |