1 8 9 package mx4j.tools.stats; 10 11 import javax.management.MBeanAttributeInfo ; 12 import javax.management.MBeanInfo ; 13 import javax.management.ObjectName ; 14 15 18 public abstract class ObserverStatisticsRecorder extends AbstractStatisticsRecorder implements ObserverStatisticsRecorderMBean 19 { 20 protected ObjectName observedName = null; 21 22 protected String observedAttribute = null; 23 24 public void setObservedObject(ObjectName object) 25 { 26 this.observedName = object; 27 } 28 29 public ObjectName getObservedObject() 30 { 31 return observedName; 32 } 33 34 public String getObservedAttribute() 35 { 36 return observedAttribute; 37 } 38 39 public void setObservedAttribute(String attribute) 40 { 41 this.observedAttribute = attribute; 42 } 43 44 protected void doStart() throws Exception 45 { 46 if (observedName == null || observedAttribute == null) 47 { 48 getLogger().warn(new StringBuffer (this.toString()).append(" cannot start with objectName ").append(observedName).append(" and attribute ").append(observedAttribute).toString()); 49 stop(); 50 return; 51 } 52 if (!server.isRegistered(observedName)) 53 { 54 getLogger().warn(new StringBuffer (this.toString()).append(" cannot start since objectName is not registered").toString()); 55 stop(); 56 return; 57 } 58 59 MBeanInfo info = server.getMBeanInfo(observedName); 60 MBeanAttributeInfo [] attributes = info.getAttributes(); 61 MBeanAttributeInfo theAttribute = null; 62 boolean found = false; 63 for (int i = 0; i < attributes.length; i++) 64 { 65 if (attributes[i].getName().equals(observedAttribute)) 66 { 67 theAttribute = attributes[i]; 68 found = true; 69 break; 70 } 71 } 72 if (!found) 73 { 74 getLogger().warn(new StringBuffer (this.toString()).append(" cannot start with objectName ").append(observedName).append(" since attribute ").append(observedAttribute).append(" does not belong to the MBean interface").toString()); 75 stop(); 76 return; 77 } 78 if (!theAttribute.isReadable()) 79 { 80 getLogger().warn(new StringBuffer (this.toString()).append(" cannot start with objectName ").append(observedName).append(" since attribute ").append(observedAttribute).append(" is not readable").toString()); 81 stop(); 82 return; 83 } 84 Object value = server.getAttribute(observedName, observedAttribute); 85 if (!(value instanceof Number )) 86 { 87 getLogger().warn(new StringBuffer (this.toString()).append(" cannot start with objectName ").append(observedName).append(" since attribute ").append(observedAttribute).append(" is not a number").toString()); 88 stop(); 89 return; 90 } 91 startObserving(); 92 } 93 94 protected abstract void startObserving() throws Exception ; 95 96 protected abstract void stopObserving() throws Exception ; 97 98 protected void doStop() throws Exception 99 { 100 stopObserving(); 101 } 102 103 } 104 | Popular Tags |