1 /* 2 Copyright (C) 2000 - 2007 Grid Systems, S.A. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License, version 2, as 6 published by the Free Software Foundation. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program; if not, write to the Free Software 15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 */ 17 package com.gridsystems.jmx; 18 19 //import javax.management.Descriptor; 20 import javax.management.MBeanConstructorInfo; 21 import javax.management.MBeanFeatureInfo; 22 //import javax.management.MBeanInfo; 23 import javax.management.MBeanOperationInfo; 24 import javax.management.MBeanParameterInfo; 25 //import javax.management.MBeanServer; 26 import javax.management.NotCompliantMBeanException; 27 //import javax.management.ObjectName; 28 import javax.management.StandardMBean; 29 30 /** 31 * AnnotatedStandardMBean type. 32 * <p> 33 * This class has lots of commented code that only works in Java 6. 34 * 35 * @author Rodrigo Ruiz 36 */ 37 public class AnnotatedStandardMXBean extends StandardMBean { 38 39 /** 40 * <p>Make a DynamicMBean out of the object 41 * <var>implementation</var>, using the specified 42 * <var>mbeanInterface</var> class.</p> 43 * 44 * @param impl The implementation of this MBean. 45 * @param iface The Management Interface exported by this 46 * MBean's implementation. If <code>null</code>, then this 47 * object will use standard JMX design pattern to determine 48 * the management interface associated with the given 49 * implementation. 50 * @param <T> Allows the compiler to check 51 * that {@code implementation} does indeed implement the class 52 * described by {@code mbeanInterface}. The compiler can only 53 * check this if {@code mbeanInterface} is a class literal such 54 * as {@code MyMBean.class}. 55 * 56 * @exception IllegalArgumentException if the given 57 * <var>implementation</var> is null. 58 * @exception NotCompliantMBeanException if the <var>iface</var> 59 * does not follow JMX design patterns for Management Interfaces, or 60 * if the given <var>implementation</var> does not implement the 61 * specified interface. 62 */ 63 public <T> AnnotatedStandardMXBean(T impl, Class<T> iface) 64 throws NotCompliantMBeanException { 65 super(impl, iface); 66 } 67 68 /** 69 * <p>Make a DynamicMBean out of <var>this</var>, using the specified 70 * <var>mbeanInterface</var> class.</p> 71 * 72 * <p>Call {@link #StandardMBean(java.lang.Object, java.lang.Class) 73 * this(this,mbeanInterface)}. 74 * This constructor is reserved to subclasses.</p> 75 * 76 * @param iface The Management Interface exported by this MBean. 77 * @param <T> Allows the compiler to check 78 * that {@code implementation} does indeed implement the class 79 * described by {@code mbeanInterface}. The compiler can only 80 * check this if {@code mbeanInterface} is a class literal such 81 * as {@code MyMBean.class}. 82 * 83 * @exception NotCompliantMBeanException if the <var>mbeanInterface</var> 84 * does not follow JMX design patterns for Management Interfaces, or 85 * if <var>this</var> does not implement the specified interface. 86 */ 87 protected <T> AnnotatedStandardMXBean(Class<T> iface) 88 throws NotCompliantMBeanException { 89 super(iface); 90 } 91 92 /* 93 * {@inheritDoc} 94 * 95 @Override 96 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception { 97 ObjectName result = super.preRegister(server, name); 98 99 if (result == null) { 100 // No default name, lookup through the annotations 101 Class<?> iface = super.getMBeanInterface(); 102 ManagedResource ann = (ManagedResource)iface.getAnnotation(ManagedResource.class); 103 if (ann != null) { 104 String aName = ann.objectName(); 105 if (aName.length() > 0) { 106 result = new ObjectName(aName); 107 } 108 } 109 } 110 return result; 111 }*/ 112 113 /** 114 * Gets the value of an MBean feature info descriptor field. 115 * 116 * @param info The mbean instance 117 * @param fieldName The descriptor field name 118 * @return The field value, or null 119 */ 120 protected Object getField(MBeanFeatureInfo info, String fieldName) { 121 /* 122 if (info == null || fieldName == null) { 123 return null; 124 } else { 125 Descriptor d = info.getDescriptor(); 126 return (d == null) ? null : d.getFieldValue(fieldName); 127 }*/ 128 return null; 129 } 130 131 /* 132 * {@inheritDoc} 133 * 134 @Override protected String getDescription(MBeanInfo info) { 135 Descriptor d = info.getDescriptor(); 136 Object value = d.getFieldValue("Description"); 137 return (value == null) ? super.getDescription(info) : value.toString(); 138 }*/ 139 140 /** 141 * {@inheritDoc} 142 */ 143 @Override protected String getDescription(MBeanConstructorInfo ctor, 144 MBeanParameterInfo param, int sequence) { 145 Object descr = getField(param, "Description"); 146 return (descr == null) ? super.getDescription(ctor, param, sequence) 147 : descr.toString(); 148 } 149 150 /** 151 * {@inheritDoc} 152 */ 153 @Override protected String getDescription(MBeanFeatureInfo info) { 154 Object descr = getField(info, "Description"); 155 return (descr == null) ? super.getDescription(info) : descr.toString(); 156 } 157 158 /** 159 * {@inheritDoc} 160 */ 161 @Override protected String getDescription(MBeanOperationInfo op, 162 MBeanParameterInfo param, int sequence) { 163 Object descr = getField(param, "Description"); 164 return (descr == null) ? super.getDescription(op, param, sequence) 165 : descr.toString(); 166 } 167 168 }