1 package org.webjmx.example; 2 3 4 import java.lang.reflect.Constructor ; 7 import java.util.Iterator ; 8 9 import javax.management.*; 10 11 36 37 public class SimpleDynamic implements DynamicMBean 38 { 39 public SimpleDynamic() 40 { 41 buildDynamicMBeanInfo(); 44 } 45 46 51 52 55 public Object getAttribute(String attribute_name) 56 throws AttributeNotFoundException, MBeanException, ReflectionException 57 { 58 59 if (attribute_name == null) 61 throw new RuntimeOperationsException(new IllegalArgumentException ("Attribute name cannot be null"), 62 "Cannot invoke a getter of " + dClassName + " with null attribute name"); 63 64 if (attribute_name.equals("State")) 66 return getState(); 67 68 if (attribute_name.equals("NbChanges")) 69 return getNbChanges(); 70 71 throw(new AttributeNotFoundException("Cannot find " + attribute_name + " attribute in " + dClassName)); 73 } 74 75 78 public void setAttribute(Attribute attribute) 79 throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException 80 { 81 82 if (attribute == null) { 84 throw new RuntimeOperationsException(new IllegalArgumentException ("Attribute cannot be null"), 85 "Cannot invoke a setter of " + dClassName + " with null attribute"); 86 } 87 String name = attribute.getName(); 88 Object value = attribute.getValue(); 89 90 if (name == null) { 91 throw new RuntimeOperationsException(new IllegalArgumentException ("Attribute name cannot be null"), 92 "Cannot invoke the setter of " + dClassName + " with null attribute name"); 93 } 94 if (name.equals("State")) { 97 if (value == null) { 99 try { 100 setState( null ); 101 } catch (Exception e) { 102 throw(new InvalidAttributeValueException("Cannot set attribute "+ name +" to null")); 103 } 104 } 105 else { 107 try { 108 if ((Class.forName("java.lang.String")).isAssignableFrom(value.getClass())) { 109 setState((String ) value); 110 } 111 else { 112 throw(new InvalidAttributeValueException("Cannot set attribute "+ name +" to a " + 113 value.getClass().getName() + " object, String expected")); 114 } 115 } catch (ClassNotFoundException e) { 116 e.printStackTrace(); 117 } 118 } 119 } 120 else if (name.equals("NbChanges")) { 122 throw(new AttributeNotFoundException("Cannot set attribute "+ name +" because it is read-only")); 123 } 124 else { 126 throw(new AttributeNotFoundException("Attribute " + name + 127 " not found in " + this.getClass().getName())); 128 } 129 } 130 131 134 public AttributeList getAttributes(String [] attributeNames) { 135 136 if (attributeNames == null) { 138 throw new RuntimeOperationsException(new IllegalArgumentException ("attributeNames[] cannot be null"), 139 "Cannot invoke a getter of " + dClassName); 140 } 141 AttributeList resultList = new AttributeList(); 142 143 if (attributeNames.length == 0) 145 return resultList; 146 147 for (int i=0 ; i<attributeNames.length ; i++){ 149 try { 150 Object value = getAttribute((String ) attributeNames[i]); 151 resultList.add(new Attribute(attributeNames[i],value)); 152 } catch (Exception e) { 153 e.printStackTrace(); 154 } 155 } 156 return(resultList); 157 } 158 159 163 public AttributeList setAttributes(AttributeList attributes) { 164 165 if (attributes == null) { 167 throw new RuntimeOperationsException(new IllegalArgumentException ("AttributeList attributes cannot be null"), 168 "Cannot invoke a setter of " + dClassName); 169 } 170 AttributeList resultList = new AttributeList(); 171 172 if (attributes.isEmpty()) 174 return resultList; 175 176 for (Iterator i = attributes.iterator(); i.hasNext();) { 178 Attribute attr = (Attribute) i.next(); 179 try { 180 setAttribute(attr); 181 String name = attr.getName(); 182 Object value = getAttribute(name); 183 resultList.add(new Attribute(name,value)); 184 } catch(Exception e) { 185 e.printStackTrace(); 186 } 187 } 188 return(resultList); 189 } 190 191 194 public Object invoke(String operationName, Object params[], String signature[]) 195 throws MBeanException, 196 ReflectionException { 197 198 if (operationName == null) { 200 throw new RuntimeOperationsException(new IllegalArgumentException ("Operation name cannot be null"), 201 "Cannot invoke a null operation in " + dClassName); 202 } 203 if (operationName.equals("reset")){ 205 reset(); 206 return null; 207 } else { 208 throw new ReflectionException(new NoSuchMethodException (operationName), 210 "Cannot find the operation " + operationName + " in " + dClassName); 211 } 212 } 213 214 218 public MBeanInfo getMBeanInfo() { 219 220 return(dMBeanInfo); 223 } 224 225 226 231 232 235 public String getState() { 236 return state; 237 } 238 239 242 public void setState(String s) { 243 state = s; 244 nbChanges++; 245 } 246 247 250 public Integer getNbChanges() { 251 return new Integer (nbChanges); 252 } 253 254 258 public void reset() { 259 state = "initial state"; 260 nbChanges = 0; 261 nbResets++; 262 } 263 264 269 public Integer getNbResets() { 270 return new Integer (nbResets); 271 } 272 273 278 279 288 private void buildDynamicMBeanInfo() { 289 290 dAttributes[0] = new MBeanAttributeInfo("State", 291 "java.lang.String", 292 "State: state string.", 293 true, 294 true, 295 false); 296 dAttributes[1] = new MBeanAttributeInfo("NbChanges", 297 "java.lang.Integer", 298 "NbChanges: number of times the State string has been changed.", 299 true, 300 false, 301 false); 302 303 Constructor [] constructors = this.getClass().getConstructors(); 304 dConstructors[0] = new MBeanConstructorInfo("SimpleDynamic(): Constructs a SimpleDynamic object", 305 constructors[0]); 306 307 MBeanParameterInfo[] params = null; 308 dOperations[0] = new MBeanOperationInfo("reset", 309 "reset(): reset State and NbChanges attributes to their initial values", 310 params , 311 "void", 312 MBeanOperationInfo.ACTION); 313 314 dMBeanInfo = new MBeanInfo(dClassName, 315 dDescription, 316 dAttributes, 317 dConstructors, 318 dOperations, 319 new MBeanNotificationInfo[0]); 320 } 321 322 327 328 private String state = "initial state"; 329 private int nbChanges = 0; 330 private int nbResets = 0; 331 332 333 private String dClassName = this.getClass().getName(); 334 private String dDescription = "Simple implementation of a dynamic MBean."; 335 336 private MBeanAttributeInfo[] dAttributes = new MBeanAttributeInfo[2]; 337 private MBeanConstructorInfo[] dConstructors = new MBeanConstructorInfo[1]; 338 private MBeanOperationInfo[] dOperations = new MBeanOperationInfo[1]; 339 private MBeanInfo dMBeanInfo = null; 340 341 } 342
| Popular Tags
|