1. package org.apache.commons.modeler.modules;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import javax.management.DynamicMBean;
  5. import javax.management.MBeanAttributeInfo;
  6. import javax.management.MBeanInfo;
  7. import javax.management.MBeanOperationInfo;
  8. import javax.management.MBeanParameterInfo;
  9. import org.apache.commons.logging.Log;
  10. import org.apache.commons.logging.LogFactory;
  11. import org.apache.commons.modeler.AttributeInfo;
  12. import org.apache.commons.modeler.ManagedBean;
  13. import org.apache.commons.modeler.OperationInfo;
  14. import org.apache.commons.modeler.ParameterInfo;
  15. import org.apache.commons.modeler.Registry;
  16. /** Extract metadata from a dynamic mbean.
  17. * Used to wrap a dynamic mbean in order to implement persistence.
  18. *
  19. * This is really an ugly asspect of the JMX spec - we need to convery
  20. * from normal metainfo to model metainfo. The info is the same, but
  21. * they use a different class. Just like the DOM spec - where all implementations
  22. * get an order of unneeded complexity from the various types.
  23. *
  24. */
  25. public class MbeansDescriptorsDynamicMBeanSource extends ModelerSource
  26. {
  27. private static Log log = LogFactory.getLog(MbeansDescriptorsDynamicMBeanSource.class);
  28. Registry registry;
  29. String location;
  30. String type;
  31. Object source;
  32. List mbeans=new ArrayList();
  33. public void setRegistry(Registry reg) {
  34. this.registry=reg;
  35. }
  36. public void setLocation( String loc ) {
  37. this.location=loc;
  38. }
  39. /** Used if a single component is loaded
  40. *
  41. * @param type
  42. */
  43. public void setType( String type ) {
  44. this.type=type;
  45. }
  46. public void setSource( Object source ) {
  47. this.source=source;
  48. }
  49. public List loadDescriptors( Registry registry, String location,
  50. String type, Object source)
  51. throws Exception
  52. {
  53. setRegistry(registry);
  54. setLocation(location);
  55. setType(type);
  56. setSource(source);
  57. execute();
  58. return mbeans;
  59. }
  60. public void execute() throws Exception {
  61. if( registry==null ) registry=Registry.getRegistry();
  62. try {
  63. ManagedBean managed=createManagedBean(registry, null, source, type);
  64. if( managed==null ) return;
  65. managed.setName( type );
  66. mbeans.add(managed);
  67. } catch( Exception ex ) {
  68. log.error( "Error reading descriptors ", ex);
  69. }
  70. }
  71. // ------------ Implementation for non-declared introspection classes
  72. /**
  73. * XXX Find if the 'className' is the name of the MBean or
  74. * the real class ( I suppose first )
  75. * XXX Read (optional) descriptions from a .properties, generated
  76. * from source
  77. * XXX Deal with constructors
  78. *
  79. */
  80. public ManagedBean createManagedBean(Registry registry, String domain,
  81. Object realObj, String type)
  82. {
  83. if( ! ( realObj instanceof DynamicMBean )) {
  84. return null;
  85. }
  86. DynamicMBean dmb=(DynamicMBean)realObj;
  87. ManagedBean mbean= new ManagedBean();
  88. MBeanInfo mbi=dmb.getMBeanInfo();
  89. try {
  90. MBeanAttributeInfo attInfo[]=mbi.getAttributes();
  91. for( int i=0; i<attInfo.length; i++ ) {
  92. MBeanAttributeInfo mai=attInfo[i];
  93. String name=mai.getName();
  94. AttributeInfo ai=new AttributeInfo();
  95. ai.setName( name );
  96. ai.setType( mai.getType());
  97. ai.setReadable( mai.isReadable());
  98. ai.setWriteable( mai.isWritable());
  99. mbean.addAttribute(ai);
  100. }
  101. MBeanOperationInfo opInfo[]=mbi.getOperations();
  102. for( int i=0; i<opInfo.length; i++ ) {
  103. MBeanOperationInfo moi=opInfo[i];
  104. OperationInfo op=new OperationInfo();
  105. op.setName(moi.getName());
  106. op.setReturnType(moi.getReturnType());
  107. MBeanParameterInfo parms[]=moi.getSignature();
  108. for(int j=0; j<parms.length; j++ ) {
  109. ParameterInfo pi=new ParameterInfo();
  110. pi.setType(parms[i].getType());
  111. pi.setName(parms[i].getName());
  112. op.addParameter(pi);
  113. }
  114. mbean.addOperation(op);
  115. }
  116. if( log.isDebugEnabled())
  117. log.debug("Setting name: " + type );
  118. mbean.setName( type );
  119. return mbean;
  120. } catch( Exception ex ) {
  121. ex.printStackTrace();
  122. return null;
  123. }
  124. }
  125. }