1 14 15 16 package test.javax.management.support; 17 18 19 import javax.management.Attribute ; 20 import javax.management.AttributeList ; 21 import javax.management.AttributeNotFoundException ; 22 import javax.management.DynamicMBean ; 23 import javax.management.InvalidAttributeValueException ; 24 import javax.management.MBeanAttributeInfo ; 25 import javax.management.MBeanConstructorInfo ; 26 import javax.management.MBeanException ; 27 import javax.management.MBeanInfo ; 28 import javax.management.MBeanNotificationInfo ; 29 import javax.management.MBeanOperationInfo ; 30 import javax.management.MBeanParameterInfo ; 31 import javax.management.ReflectionException ; 32 33 34 37 38 class PrivateMBeanDynamic implements DynamicMBean 39 40 { 41 42 private String m_value1 = ""; 43 44 private String m_value2 = ""; 45 46 47 public MBeanInfo getMBeanInfo() 48 49 { 50 51 MBeanAttributeInfo [] attrs = new MBeanAttributeInfo [2]; 52 53 attrs[0] = new MBeanAttributeInfo ("DynamicAttribute1", "java.lang.String", "A first dynamic attribute", true, true, false); 54 55 attrs[1] = new MBeanAttributeInfo ("DynamicAttribute2", "java.lang.String", "A second dynamic attribute", true, true, false); 56 57 58 MBeanConstructorInfo [] ctors = new MBeanConstructorInfo [1]; 59 60 ctors[0] = new MBeanConstructorInfo ("Parameterless Constructor", "A dynamic constructor", new MBeanParameterInfo [0]); 61 62 63 MBeanOperationInfo [] opers = new MBeanOperationInfo [1]; 64 65 MBeanParameterInfo [] params = new MBeanParameterInfo [1]; 66 67 params[0] = new MBeanParameterInfo ("supposedAttributeValue", "java.lang.String", "Checks if the value of the argument is equal to the value of the attribute"); 68 69 opers[0] = new MBeanOperationInfo ("dynamicOperation", "A dynamic operation", params, "boolean", MBeanOperationInfo.INFO); 70 71 72 MBeanNotificationInfo [] notifs = new MBeanNotificationInfo [0]; 73 74 75 return new MBeanInfo (getClass().getName(), "A MBeanDynamic MBean", attrs, ctors, opers, notifs); 76 77 } 78 79 80 private String getDynamicAttribute1() 81 { 82 return m_value1; 83 } 84 85 private void setDynamicAttribute1(String value) 86 { 87 m_value1 = value; 88 } 89 90 private String getDynamicAttribute2() 91 { 92 return m_value2; 93 } 94 95 private void setDynamicAttribute2(String value) 96 { 97 m_value2 = value; 98 } 99 100 private boolean dynamicOperation(String value) 101 { 102 return m_value1.equals(value); 103 } 104 105 106 public Object getAttribute(String attribute) throws AttributeNotFoundException , MBeanException , ReflectionException 107 108 { 109 110 if (attribute.equals("DynamicAttribute1")) 111 { 112 return getDynamicAttribute1(); 113 } 114 115 else if (attribute.equals("DynamicAttribute2")) 116 { 117 return getDynamicAttribute2(); 118 } 119 120 else 121 throw new AttributeNotFoundException (attribute); 122 123 } 124 125 126 public void setAttribute(Attribute attribute) throws AttributeNotFoundException , InvalidAttributeValueException , MBeanException , ReflectionException 127 128 { 129 130 if (attribute.getName().equals("DynamicAttribute1")) 131 132 { 133 134 Object val = attribute.getValue(); 135 136 if (val instanceof String ) 137 { 138 setDynamicAttribute1((String )val); 139 } 140 141 else 142 { 143 throw new InvalidAttributeValueException (val == null ? "null" : val.toString()); 144 } 145 146 } 147 148 else if (attribute.getName().equals("DynamicAttribute2")) 149 150 { 151 152 Object val = attribute.getValue(); 153 154 if (val instanceof String ) 155 { 156 setDynamicAttribute2((String )val); 157 } 158 159 else 160 { 161 throw new InvalidAttributeValueException (val == null ? "null" : val.toString()); 162 } 163 164 } 165 166 else 167 { 168 throw new AttributeNotFoundException (attribute.getName()); 169 } 170 171 } 172 173 174 public AttributeList getAttributes(String [] attributes) 175 176 { 177 178 AttributeList list = new AttributeList (); 179 180 for (int i = 0; i < attributes.length; ++i) 181 182 { 183 184 if (attributes[i].equals("DynamicAttribute1")) 185 186 { 187 188 list.add(new Attribute (attributes[i], getDynamicAttribute1())); 189 190 } 191 192 else if (attributes[i].equals("DynamicAttribute2")) 193 194 { 195 196 list.add(new Attribute (attributes[i], getDynamicAttribute2())); 197 198 } 199 200 } 201 202 return list; 203 204 } 205 206 207 public AttributeList setAttributes(AttributeList attributes) 208 209 { 210 211 AttributeList list = new AttributeList (); 212 213 for (int i = 0; i < attributes.size(); ++i) 214 215 { 216 217 Attribute attr = (Attribute )attributes.get(i); 218 219 if (attr.getName().equals("DynamicAttribute1") || attr.getName().equals("DynamicAttribute2")) 220 221 { 222 223 try 224 225 { 226 227 setAttribute(attr); 228 229 list.add(attr); 230 231 } 232 233 catch (AttributeNotFoundException ignored) 234 { 235 } 236 237 catch (InvalidAttributeValueException ignored) 238 { 239 } 240 241 catch (MBeanException ignored) 242 { 243 } 244 245 catch (ReflectionException ignored) 246 { 247 } 248 249 } 250 251 } 252 253 return list; 254 255 } 256 257 258 public Object invoke(String method, Object [] arguments, String [] params) throws MBeanException , ReflectionException 259 260 { 261 262 if (method.equals("dynamicOperation") && 263 264 params.length == 1 && 265 266 params[0].equals("java.lang.String") && 267 268 arguments.length == 1 && 269 270 arguments[0] instanceof String ) 271 272 { 273 274 boolean match = dynamicOperation((String )arguments[0]); 275 276 return new Boolean (match); 277 278 } 279 280 else 281 282 { 283 284 throw new MBeanException (new IllegalArgumentException ("Invalid method or arguments for invoke")); 285 286 } 287 288 } 289 290 } 291 292 | Popular Tags |