1 8 package org.apache.avalon.phoenix.components.manager; 9 10 import java.lang.reflect.Constructor ; 11 import java.util.Iterator ; 12 import java.util.Set ; 13 import javax.management.MBeanServer ; 14 import javax.management.MalformedObjectNameException ; 15 import javax.management.ObjectName ; 16 import javax.management.modelmbean.ModelMBean ; 17 import javax.management.modelmbean.ModelMBeanInfo ; 18 import org.apache.avalon.excalibur.i18n.ResourceManager; 19 import org.apache.avalon.excalibur.i18n.Resources; 20 import org.apache.avalon.phoenix.interfaces.ManagerException; 21 22 29 public abstract class AbstractJMXManager 30 extends AbstractSystemManager 31 { 32 private static final Resources REZ = 33 ResourceManager.getPackageResources( AbstractJMXManager.class ); 34 private MBeanInfoBuilder topicBuilder; 35 private MBeanServer m_mBeanServer; 36 private String m_domain = "Phoenix"; 37 38 public void initialize() 39 throws Exception 40 { 41 super.initialize(); 42 43 final MBeanServer mBeanServer = createMBeanServer(); 44 setMBeanServer( mBeanServer ); 45 46 topicBuilder = new MBeanInfoBuilder(); 47 setupLogger( topicBuilder ); 48 } 49 50 public void start() 51 throws Exception 52 { 53 } 54 55 public void stop() 56 throws Exception 57 { 58 } 59 60 public void dispose() 61 { 62 setMBeanServer( null ); 63 } 64 65 76 protected Object export( final String name, 77 final Object object, 78 final Class [] interfaces ) 79 throws ManagerException 80 { 81 try 82 { 83 final Target target = createTarget( name, object, interfaces ); 84 exportTarget( target ); 85 return target; 86 } 87 catch( final Exception e ) 88 { 89 final String message = REZ.getString( "jmxmanager.error.export.fail", name ); 90 getLogger().error( message, e ); 91 throw new ManagerException( message, e ); 92 } 93 } 94 95 102 protected void unexport( final String name, 103 final Object exportedObject ) 104 throws ManagerException 105 { 106 try 107 { 108 final Target target = (Target) exportedObject; 109 final Set topicNames = target.getTopicNames(); 110 final Iterator i = topicNames.iterator(); 111 112 while( i.hasNext() ) 113 { 114 final ObjectName objectName = 115 createObjectName( name, target.getTopic( ( String ) i.next() ) ); 116 117 getMBeanServer().unregisterMBean( objectName ); 118 } 119 } 120 catch( final Exception e ) 121 { 122 final String message = 123 REZ.getString( "jmxmanager.error.unexport.fail", name ); 124 getLogger().error( message, e ); 125 throw new ManagerException( message, e ); 126 } 127 } 128 129 135 protected void verifyInterface( final Class clazz ) 136 throws ManagerException 137 { 138 } 142 143 protected MBeanServer getMBeanServer() 144 { 145 return m_mBeanServer; 146 } 147 148 protected void setMBeanServer( MBeanServer mBeanServer ) 149 { 150 m_mBeanServer = mBeanServer; 151 } 152 153 protected String getDomain() 154 { 155 return m_domain; 156 } 157 158 protected void setDomain( final String domain ) 159 { 160 m_domain = domain; 161 } 162 163 167 protected abstract MBeanServer createMBeanServer() 168 throws Exception ; 169 170 179 protected Target createTarget( final String name, 180 final Object object, 181 Class [] interfaces ) 182 { 183 final Target target = new Target( name, object ); 184 try 185 { 186 topicBuilder.build( target, object.getClass(), interfaces ); 187 } 188 catch( final Exception e ) 189 { 190 getLogger().debug( e.getMessage(), e ); 191 } 192 193 return target; 194 } 195 196 202 protected void exportTarget( final Target target ) 203 throws Exception 204 { 205 final Set topicNames = target.getTopicNames(); 207 final Iterator i = topicNames.iterator(); 208 while( i.hasNext() ) 209 { 210 final String topicName = (String )i.next(); 211 final ModelMBeanInfo topic = target.getTopic( topicName ); 212 final String targetName = target.getName(); 213 final Object managedResource = target.getManagedResource(); 214 Object targetObject = managedResource; 215 if( topic.getMBeanDescriptor().getFieldValue( "proxyClassName" ) != null ) 216 { 217 targetObject = createManagementProxy( topic, managedResource ); 218 } 219 220 exportTopic( topic, 222 targetObject, 223 targetName ); 224 } 225 } 226 227 235 protected Object exportTopic( final ModelMBeanInfo topic, 236 final Object target, 237 final String targetName ) 238 throws Exception 239 { 240 final Object mBean = createMBean( topic, target ); 241 final ObjectName objectName = createObjectName( targetName, topic ); 242 getMBeanServer().registerMBean( mBean, objectName ); 243 244 264 265 return mBean; 266 } 267 268 276 private Object createMBean( final ModelMBeanInfo topic, 277 final Object target ) 278 throws ManagerException 279 { 280 final String className = topic.getClassName(); 281 Class clazz; 283 try 284 { 285 clazz = Class.forName( className ); 286 } 287 catch( Exception e ) 288 { 289 final String message = 290 REZ.getString( "jmxmanager.error.mbean.load.class", 291 className ); 292 getLogger().error( message, e ); 293 throw new ManagerException( message, e ); 294 } 295 296 ModelMBean mbean = null; 298 try 299 { 300 mbean = (ModelMBean )clazz.newInstance(); 301 mbean.setModelMBeanInfo( topic ); 302 } 303 catch( final Exception e ) 304 { 305 final String message = 306 REZ.getString( "jmxmanager.error.mbean.instantiate", 307 className ); 308 getLogger().error( message, e ); 309 throw new ManagerException( message, e ); 310 } 311 312 try 314 { 315 if( null != target ) 316 { 317 mbean.setManagedResource( target, "ObjectReference" ); 318 } 319 } 320 catch( Exception e ) 321 { 322 final String message = 323 REZ.getString( "jmxmanager.error.mbean.set.resource", 324 className ); 325 getLogger().error( message, e ); 326 throw new ManagerException( message, e ); 327 } 328 329 return mbean; 330 } 331 332 339 private ObjectName createObjectName( final String name, final ModelMBeanInfo topic ) 340 throws MalformedObjectNameException 341 { 342 return new ObjectName ( getDomain() + ":" + name + ",topic=" + topic.getDescription() ); 343 } 344 345 350 private Object createManagementProxy( final ModelMBeanInfo topic, 351 final Object managedObject ) 352 throws Exception 353 { 354 final String proxyClassname = (String )topic.getMBeanDescriptor().getFieldValue( "proxyClassName" ); 355 final ClassLoader classLoader = managedObject.getClass().getClassLoader(); 356 final Class proxyClass = classLoader.loadClass( proxyClassname ); 357 final Class [] argTypes = new Class []{Object .class}; 358 final Object [] argValues = new Object []{managedObject}; 359 final Constructor constructor = proxyClass.getConstructor( argTypes ); 360 return constructor.newInstance( argValues ); 361 } 362 } 363 | Popular Tags |