1 22 package org.jboss.mx.util; 23 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import javax.management.InstanceNotFoundException ; 27 import javax.management.MBeanAttributeInfo ; 28 import javax.management.MBeanInfo ; 29 import javax.management.MBeanServer ; 30 import javax.management.ObjectName ; 31 import javax.management.monitor.Monitor ; 32 import javax.management.monitor.MonitorNotification ; 33 34 import org.jboss.logging.Logger; 35 36 40 public class MonitorRunnable 41 extends SchedulableRunnable 42 { 43 private static final Logger log = Logger.getLogger(MonitorRunnable.class); 44 45 48 static RunnableScheduler scheduler; 49 50 53 static 54 { 55 scheduler = new RunnableScheduler(); 56 scheduler.start(); 57 } 58 59 61 private Monitor monitor; 63 private ObjectName monitorName; 64 private MonitorCallback callback; 65 private Map observedObjects; 66 private MBeanServer server; 67 68 70 75 public MonitorRunnable(Monitor monitor, ObjectName monitorName, 76 MonitorCallback callback, Map observedObjects, MBeanServer server) 77 { 78 this.monitor = monitor; 79 this.monitorName = monitorName; 80 this.callback = callback; 81 this.observedObjects = observedObjects; 82 this.server = server; 83 setScheduler(scheduler); 84 } 85 86 88 97 void runMonitor(ObservedObject object) 98 { 99 try 101 { 102 MBeanInfo mbeanInfo = null; 103 try 104 { 105 mbeanInfo = server.getMBeanInfo(object.getObjectName()); 106 } 107 catch (InstanceNotFoundException e) 108 { 109 sendObjectErrorNotification(object, "The observed object is not registered."); 110 return; 111 } 112 113 MBeanAttributeInfo [] mbeanAttributeInfo = mbeanInfo.getAttributes(); 115 MBeanAttributeInfo attributeInfo = null; 116 for (int i = 0; i < mbeanAttributeInfo.length; i++) 117 { 118 if (mbeanAttributeInfo[i].getName().equals(monitor.getObservedAttribute())) 119 { 120 attributeInfo = mbeanAttributeInfo[i]; 121 break; 122 } 123 } 124 125 if (attributeInfo == null) 127 { 128 sendAttributeErrorNotification(object, 129 "The observed attribute does not exist"); 130 return; 131 } 132 133 if (!attributeInfo.isReadable()) 135 { 136 sendAttributeErrorNotification(object, "Attribute not readable."); 137 return; 138 } 139 140 Object value = null; 142 try 143 { 144 value = server.getAttribute(object.getObjectName(), monitor.getObservedAttribute()); 145 } 146 catch (InstanceNotFoundException e) 147 { 148 sendObjectErrorNotification(object, "The observed object is not registered."); 149 return; 150 } 151 152 if (value == null) 154 { 155 sendAttributeTypeErrorNotification(object, "Attribute is null"); 156 return; 157 } 158 159 callback.monitorCallback(object, attributeInfo, value); 161 } 162 catch (Throwable e) 164 { 165 log.debug("Error in monitor ", e); 166 sendRuntimeErrorNotification(object, "General error: " + e.toString()); 167 } 168 } 169 170 173 public void doRun() 174 { 175 runMonitor(); 177 178 setNextRun(System.currentTimeMillis() + monitor.getGranularityPeriod()); 180 } 181 182 185 void runMonitor() 186 { 187 boolean isActive = monitor.isActive(); 189 for (Iterator i = observedObjects.values().iterator(); i.hasNext() && isActive;) 190 runMonitor((ObservedObject) i.next()); 191 } 192 193 204 void sendNotification(ObservedObject object, String type, long timestamp, String message, 205 String attribute, Object gauge, Object trigger) 206 { 207 MonitorNotification n = callback.createNotification(type, 208 monitorName, timestamp, message, gauge, attribute, 209 object.getObjectName(), trigger); 210 monitor.sendNotification(n); 211 } 212 213 219 void sendRuntimeErrorNotification(ObservedObject object, String message) 220 { 221 if (object.notAlreadyNotified(ObservedObject.RUNTIME_ERROR_NOTIFIED)) 222 sendNotification(object, MonitorNotification.RUNTIME_ERROR, 0, 223 message, monitor.getObservedAttribute(), null, null); 224 } 225 226 232 void sendObjectErrorNotification(ObservedObject object, String message) 233 { 234 if (object.notAlreadyNotified(ObservedObject.OBSERVED_OBJECT_ERROR_NOTIFIED)) 235 sendNotification(object, MonitorNotification.OBSERVED_OBJECT_ERROR, 0, 236 message, monitor.getObservedAttribute(), null, null); 237 } 238 239 245 void sendAttributeErrorNotification(ObservedObject object, String message) 246 { 247 if (object.notAlreadyNotified(ObservedObject.OBSERVED_ATTRIBUTE_ERROR_NOTIFIED)) 248 sendNotification(object, MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, 0, 249 message, monitor.getObservedAttribute(), null, null); 250 } 251 252 258 void sendAttributeTypeErrorNotification(ObservedObject object, String message) 259 { 260 if (object.notAlreadyNotified(ObservedObject.OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED)) 261 sendNotification(object, MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, 0, 262 message, monitor.getObservedAttribute(), null, null); 263 } 264 265 } 266 | Popular Tags |