1 8 9 package test.javax.management.support; 10 11 import java.util.ArrayList ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import javax.management.Attribute ; 15 import javax.management.AttributeList ; 16 import javax.management.AttributeNotFoundException ; 17 import javax.management.DynamicMBean ; 18 import javax.management.InvalidAttributeValueException ; 19 import javax.management.MBeanAttributeInfo ; 20 import javax.management.MBeanConstructorInfo ; 21 import javax.management.MBeanException ; 22 import javax.management.MBeanInfo ; 23 import javax.management.MBeanNotificationInfo ; 24 import javax.management.MBeanOperationInfo ; 25 import javax.management.ReflectionException ; 26 27 30 public class QuerySupport 31 { 32 public interface TestMBean 33 { 34 public Integer getNumber(); 35 36 public String getStr(); 37 38 public Boolean getBoolean(); 39 } 40 41 public static class Test implements TestMBean 42 { 43 private Integer n; 44 private String str; 45 private Boolean b; 46 47 public Test(String str, Integer n, Boolean b) 48 { 49 this.str = str; 50 this.n = n; 51 this.b = b; 52 } 53 54 public Integer getNumber() 55 { 56 return n; 57 } 58 59 public String getStr() 60 { 61 return str; 62 } 63 64 public Boolean getBoolean() 65 { 66 return b; 67 } 68 } 69 70 public static class DynamicTest implements DynamicMBean 71 { 72 private Boolean boolval; 73 private long numval; 74 private String strval; 75 76 public DynamicTest(String s, long n, Boolean b) 77 { 78 this.boolval = b; 79 this.numval = n; 80 this.strval = s; 81 } 82 83 public Boolean getBoolean() 84 { 85 return this.boolval; 86 } 87 88 public long getNumber() 89 { 90 return this.numval; 91 } 92 93 public void setNumber(long value) 94 { 95 this.numval = value; 96 } 97 98 public String getStr() 99 { 100 throw new RuntimeException ("Don't call me!"); 101 } 102 103 public Object getAttribute(String attribute) 104 throws AttributeNotFoundException , MBeanException , ReflectionException 105 { 106 Object result; 107 if (attribute.compareTo("Boolean") == 0) 108 { 109 result = getBoolean(); 110 } 111 else if (attribute.compareTo("Number") == 0) 112 { 113 result = new Long (getNumber()); 114 } 115 else if (attribute.compareTo("Str") == 0) 116 { 117 result = getStr(); 118 } 119 else 120 { 121 throw new AttributeNotFoundException ("Can't find " + attribute); 122 } 123 return result; 124 } 125 126 public AttributeList getAttributes(String [] attributes) 127 { 128 List attrnames = new ArrayList (); 129 MBeanAttributeInfo [] attrs = getMBeanInfo().getAttributes(); 130 for (int i = 0; i < attrs.length; i++) 131 { 132 attrnames.add(attrs[i].getName()); 133 } 134 AttributeList result = new AttributeList (); 135 for (int i = 0; i < attributes.length; i++) 136 { 137 if (attrnames.contains(attributes[i])) 138 { 139 try 140 { 141 Attribute attr = new Attribute (attributes[i], getAttribute(attributes[i])); 142 result.add(attr); 143 } 144 catch (AttributeNotFoundException e) 145 { 146 } 148 catch (MBeanException e) 149 { 150 } 152 catch (ReflectionException e) 153 { 154 } 156 } 157 } 158 return result; 159 } 160 161 public MBeanInfo getMBeanInfo() 162 { 163 MBeanInfo result; 164 MBeanAttributeInfo [] attrs; 165 try 166 { 167 attrs = 168 new MBeanAttributeInfo []{ 169 new MBeanAttributeInfo ("Number", 170 "A number", 171 DynamicTest.class.getMethod("getNumber", 172 new Class [0]), 173 DynamicTest.class.getMethod("setNumber", 174 new Class []{long.class})), 175 new MBeanAttributeInfo ("Str", 176 "A string", 177 DynamicTest.class.getMethod("getStr", new Class [0]), 178 null), 179 new MBeanAttributeInfo ("Boolean", 180 "A Boolean", 181 DynamicTest.class.getMethod("getBoolean", 182 new Class [0]), 183 null) 184 }; 185 } 186 catch (Exception x) 187 { 188 attrs = new MBeanAttributeInfo [0]; 189 } 190 MBeanConstructorInfo [] ctors = new MBeanConstructorInfo [0]; 191 MBeanOperationInfo [] ops = new MBeanOperationInfo [0]; 192 MBeanNotificationInfo [] notifs = new MBeanNotificationInfo [0]; 193 result = 194 new MBeanInfo (DynamicTest.class.getName(), 195 "DynamicTest MBean", 196 attrs, 197 ctors, 198 ops, 199 notifs); 200 return result; 201 } 202 203 public Object invoke(String method, 204 Object [] arguments, 205 String [] params) 206 throws MBeanException , ReflectionException 207 { 208 return null; 209 } 210 211 public void setAttribute(Attribute attribute) 212 throws 213 AttributeNotFoundException , 214 InvalidAttributeValueException , 215 MBeanException , 216 ReflectionException 217 { 218 if (attribute.getName().compareTo("Number") == 0) 219 { 220 setNumber(((Long )attribute.getValue()).longValue()); 221 } 222 else 223 { 224 throw new AttributeNotFoundException ("Can't find " + attribute.getName()); 225 } 226 } 227 228 public AttributeList setAttributes(AttributeList attributes) 229 { 230 AttributeList result = new AttributeList (); 231 Iterator i = attributes.iterator(); 232 while (i.hasNext()) 233 { 234 try 235 { 236 Attribute attr = (Attribute )i.next(); 237 setAttribute(attr); 238 result.add(attr); 239 } 240 catch (Exception x) 241 { 242 } 244 } 245 return result; 246 } 247 248 } 249 } 250 | Popular Tags |