1 8 9 package test.javax.management.support; 10 11 import javax.management.Attribute ; 12 import javax.management.AttributeList ; 13 import javax.management.AttributeNotFoundException ; 14 import javax.management.DynamicMBean ; 15 import javax.management.InvalidAttributeValueException ; 16 import javax.management.MBeanAttributeInfo ; 17 import javax.management.MBeanConstructorInfo ; 18 import javax.management.MBeanException ; 19 import javax.management.MBeanInfo ; 20 import javax.management.MBeanNotificationInfo ; 21 import javax.management.MBeanOperationInfo ; 22 import javax.management.MBeanParameterInfo ; 23 import javax.management.ReflectionException ; 24 25 28 public class MBeanDynamic implements DynamicMBean 29 { 30 private String m_value1 = ""; 31 private String m_value2 = ""; 32 33 public MBeanInfo getMBeanInfo() 34 { 35 MBeanAttributeInfo [] attrs = new MBeanAttributeInfo [2]; 36 attrs[0] = new MBeanAttributeInfo ("DynamicAttribute1", "java.lang.String", "A first dynamic attribute", true, true, false); 37 attrs[1] = new MBeanAttributeInfo ("DynamicAttribute2", "java.lang.String", "A second dynamic attribute", true, true, false); 38 39 MBeanConstructorInfo [] ctors = new MBeanConstructorInfo [1]; 40 ctors[0] = new MBeanConstructorInfo ("ParameterlessConstructor", "A dynamic constructor", new MBeanParameterInfo [0]); 41 42 MBeanOperationInfo [] opers = new MBeanOperationInfo [1]; 43 MBeanParameterInfo [] params = new MBeanParameterInfo [1]; 44 params[0] = new MBeanParameterInfo ("supposedAttributeValue", "java.lang.String", "Checks if the value of the argument is equal to the value of the attribute"); 45 opers[0] = new MBeanOperationInfo ("dynamicOperation", "A dynamic operation", params, "boolean", MBeanOperationInfo.INFO); 46 47 MBeanNotificationInfo [] notifs = new MBeanNotificationInfo [0]; 48 49 return new MBeanInfo (getClass().getName(), "A MBeanDynamic MBean", attrs, ctors, opers, notifs); 50 } 51 52 private String getDynamicAttribute1() 53 { 54 return m_value1; 55 } 56 57 private void setDynamicAttribute1(String value) 58 { 59 m_value1 = value; 60 } 61 62 private String getDynamicAttribute2() 63 { 64 return m_value2; 65 } 66 67 private void setDynamicAttribute2(String value) 68 { 69 m_value2 = value; 70 } 71 72 private boolean dynamicOperation(String value) 73 { 74 return m_value1.equals(value); 75 } 76 77 public Object getAttribute(String attribute) throws AttributeNotFoundException , MBeanException , ReflectionException 78 { 79 if (attribute.equals("DynamicAttribute1")) 80 { 81 return getDynamicAttribute1(); 82 } 83 else if (attribute.equals("DynamicAttribute2")) 84 { 85 return getDynamicAttribute2(); 86 } 87 else 88 throw new AttributeNotFoundException (attribute); 89 } 90 91 public void setAttribute(Attribute attribute) throws AttributeNotFoundException , InvalidAttributeValueException , MBeanException , ReflectionException 92 { 93 if (attribute.getName().equals("DynamicAttribute1")) 94 { 95 Object val = attribute.getValue(); 96 if (val instanceof String ) 97 { 98 setDynamicAttribute1((String )val); 99 } 100 else 101 { 102 throw new InvalidAttributeValueException (val == null ? "null" : val.toString()); 103 } 104 } 105 else if (attribute.getName().equals("DynamicAttribute2")) 106 { 107 Object val = attribute.getValue(); 108 if (val instanceof String ) 109 { 110 setDynamicAttribute2((String )val); 111 } 112 else 113 { 114 throw new InvalidAttributeValueException (val == null ? "null" : val.toString()); 115 } 116 } 117 else 118 { 119 throw new AttributeNotFoundException (attribute.getName()); 120 } 121 } 122 123 public AttributeList getAttributes(String [] attributes) 124 { 125 AttributeList list = new AttributeList (); 126 for (int i = 0; i < attributes.length; ++i) 127 { 128 if (attributes[i].equals("DynamicAttribute1")) 129 { 130 list.add(new Attribute (attributes[i], getDynamicAttribute1())); 131 } 132 else if (attributes[i].equals("DynamicAttribute2")) 133 { 134 list.add(new Attribute (attributes[i], getDynamicAttribute2())); 135 } 136 } 137 return list; 138 } 139 140 public AttributeList setAttributes(AttributeList attributes) 141 { 142 AttributeList list = new AttributeList (); 143 for (int i = 0; i < attributes.size(); ++i) 144 { 145 Attribute attr = (Attribute )attributes.get(i); 146 if (attr.getName().equals("DynamicAttribute1") || attr.getName().equals("DynamicAttribute2")) 147 { 148 try 149 { 150 setAttribute(attr); 151 list.add(attr); 152 } 153 catch (AttributeNotFoundException ignored) 154 { 155 } 156 catch (InvalidAttributeValueException ignored) 157 { 158 } 159 catch (MBeanException ignored) 160 { 161 } 162 catch (ReflectionException ignored) 163 { 164 } 165 } 166 } 167 return list; 168 } 169 170 public Object invoke(String method, Object [] arguments, String [] params) throws MBeanException , ReflectionException 171 { 172 if (method.equals("dynamicOperation") && 173 params.length == 1 && 174 params[0].equals("java.lang.String") && 175 arguments.length == 1 && 176 arguments[0] instanceof String ) 177 { 178 boolean match = dynamicOperation((String )arguments[0]); 179 return new Boolean (match); 180 } 181 else 182 { 183 throw new MBeanException (new IllegalArgumentException ("Invalid method or arguments for invoke")); 184 } 185 } 186 } 187 | Popular Tags |