1 15 package org.apache.hivemind.management.impl; 16 17 import java.lang.reflect.InvocationTargetException ; 18 import java.lang.reflect.Method ; 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import javax.management.DynamicMBean ; 24 import javax.management.InstanceAlreadyExistsException ; 25 import javax.management.InstanceNotFoundException ; 26 import javax.management.JMException ; 27 import javax.management.MBeanRegistrationException ; 28 import javax.management.MBeanServer ; 29 import javax.management.NotCompliantMBeanException ; 30 import javax.management.ObjectInstance ; 31 import javax.management.ObjectName ; 32 import javax.management.StandardMBean ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.hivemind.ErrorHandler; 36 import org.apache.hivemind.events.RegistryShutdownListener; 37 import org.apache.hivemind.internal.ServicePoint; 38 import org.apache.hivemind.management.MBeanRegistry; 39 import org.apache.hivemind.management.ManagementMessages; 40 import org.apache.hivemind.management.ObjectNameBuilder; 41 42 52 public class MBeanRegistryImpl implements MBeanRegistry, RegistryShutdownListener 53 { 54 private ErrorHandler _errorHandler; 55 56 private Log _log; 57 58 private MBeanServer _beanServer; 59 60 private ObjectNameBuilder _objectNameBuilder; 61 62 private List _beans; 63 64 private List _objectInstances = new ArrayList (); 66 67 76 public MBeanRegistryImpl(ErrorHandler errorHandler, Log log, MBeanServer beanServer, 77 ObjectNameBuilder objectNameBuilder, List beans) 78 { 79 _errorHandler = errorHandler; 80 _log = log; 81 _beanServer = beanServer; 82 _objectNameBuilder = objectNameBuilder; 83 _beans = beans; 84 if (_beans != null) 85 processContributions(_beans); 86 } 87 88 94 private void processContributions(List beans) 95 { 96 Iterator iter = beans.iterator(); 97 while (iter.hasNext()) 98 { 99 MBeanRegistrationContribution mbeanReg = (MBeanRegistrationContribution) iter.next(); 100 registerServiceAsMBean(mbeanReg.getObjectName(), mbeanReg.getServicePoint(), mbeanReg 101 .getStartMethod()); 102 } 103 } 104 105 118 private void registerServiceAsMBean(ObjectName objectName, ServicePoint servicePoint, 119 String startMethodName) 120 { 121 if (objectName == null) 124 objectName = _objectNameBuilder.createServiceObjectName(servicePoint); 125 126 Object mbean; 128 try 129 { 130 Class managementInterface = servicePoint.getDeclaredInterface(); 131 mbean = servicePoint.getService(managementInterface); 133 registerMBean(mbean, managementInterface, objectName); 134 } 135 catch (JMException e) 136 { 137 _errorHandler.error( 138 _log, 139 ManagementMessages.errorRegisteringMBean(objectName, e), 140 null, 141 e); 142 return; 143 } 144 try 146 { 147 if (startMethodName != null) 148 invokeStartMethod(mbean, startMethodName); 149 } 150 catch (InvocationTargetException e) 151 { 152 _errorHandler.error(_log, ManagementMessages.errorStartMethodFailed( 153 startMethodName, 154 objectName, 155 e.getTargetException()), null, e); 156 return; 157 } 158 catch (Exception e) 159 { 160 _errorHandler.error(_log, ManagementMessages.errorStartMethodFailed( 161 startMethodName, 162 objectName, 163 e), null, e); 164 return; 165 } 166 } 167 168 174 public ObjectInstance registerMBean(Object obj, Class managementInterface, ObjectName objectName) 175 throws InstanceAlreadyExistsException , MBeanRegistrationException , 176 NotCompliantMBeanException 177 { 178 ObjectInstance instance = null; 179 try 180 { 181 if (_log.isDebugEnabled()) 182 { 183 _log.debug("Trying to register MBean " + objectName); 184 } 185 instance = _beanServer.registerMBean(obj, objectName); 186 } 187 catch (NotCompliantMBeanException e) 188 { 189 if (_log.isDebugEnabled()) 190 { 191 _log.debug("MBean " + objectName + " is not compliant. Registering" 192 + " using StandardMBean"); 193 } 194 if (DynamicMBean .class.isAssignableFrom(obj.getClass()) || managementInterface == null) 195 throw e; 196 StandardMBean standardMBean = new StandardMBean (obj, managementInterface); 203 instance = _beanServer.registerMBean(standardMBean, objectName); 204 } 205 _objectInstances.add(instance); 206 return instance; 207 } 208 209 212 public void unregisterMBean(ObjectName objectName) throws InstanceNotFoundException , 213 MBeanRegistrationException 214 { 215 ObjectInstance instance = _beanServer.getObjectInstance(objectName); 216 _objectInstances.remove(instance); 217 _beanServer.unregisterMBean(objectName); 218 } 219 220 223 private void invokeStartMethod(Object mbean, String methodName) throws IllegalAccessException , 224 InvocationTargetException , NoSuchMethodException 225 { 226 Class serviceClass = mbean.getClass(); 227 Method m = serviceClass.getMethod(methodName, (Class []) null); 228 m.invoke(mbean, (Object []) null); 229 } 230 231 234 public void registryDidShutdown() 235 { 236 for (int i = _objectInstances.size() - 1; i >= 0; i--) 239 { 240 ObjectInstance objectInstance = (ObjectInstance ) _objectInstances.get(i); 241 try 242 { 243 _beanServer.unregisterMBean(objectInstance.getObjectName()); 244 } 245 catch (JMException e) 246 { 247 _log.warn(ManagementMessages.errorUnregisteringMBean( 249 objectInstance.getObjectName(), 250 e)); 251 } 252 } 253 } 254 255 } | Popular Tags |