| 1 16 package org.jmanage.core.alert.source; 17 18 import org.jmanage.core.alert.AlertSource; 19 import org.jmanage.core.alert.AlertInfo; 20 import org.jmanage.core.config.AlertSourceConfig; 21 import org.jmanage.core.management.*; 22 import org.jmanage.core.util.Loggers; 23 24 import java.util.Set ; 25 import java.util.List ; 26 import java.util.LinkedList ; 27 import java.util.logging.Logger ; 28 import java.util.logging.Level ; 29 import java.io.IOException ; 30 31 35 public class StringAlertSource extends AlertSource{ 36 37 private static final Logger logger = 38 Loggers.getLogger(StringAlertSource.class); 39 40 private ObjectName monitorObjName = null; 41 private ObjectNotificationListener listener = null; 42 private ObjectNotificationFilterSupport filter = null; 43 44 public StringAlertSource(AlertSourceConfig sourceConfig){ 45 super(sourceConfig); 46 } 47 48 protected void registerInternal(){ 49 50 51 monitorObjName = new ObjectName("jmanage.alerts:name=" + alertName + 52 ",id=" + alertId + ",type=StringMonitor"); 53 54 55 Set mbeans = connection.queryNames(monitorObjName); 56 if(mbeans != null && mbeans.size() > 0){ 57 58 connection.unregisterMBean(monitorObjName); 59 } 60 61 62 connection.createMBean("javax.management.monitor.StringMonitor", 63 monitorObjName, null, null); 64 65 List attributes = new LinkedList (); 66 attributes.add(new ObjectAttribute("ObservedAttribute", 67 sourceConfig.getAttributeName())); 68 attributes.add(new ObjectAttribute("ObservedObject", 70 connection.buildObjectName(sourceConfig.getObjectName()))); 71 attributes.add(new ObjectAttribute("StringToCompare", 72 sourceConfig.getStringAttributeValue())); 73 attributes.add(new ObjectAttribute("NotifyMatch", Boolean.TRUE)); 74 attributes.add(new ObjectAttribute("NotifyDiffer", Boolean.FALSE)); 75 attributes.add(new ObjectAttribute("GranularityPeriod", new Long (5000))); 76 77 connection.setAttributes(monitorObjName, attributes); 78 79 connection.invoke(monitorObjName, "start", new Object [0], new String [0]); 80 listener = new ObjectNotificationListener(){ 81 public void handleNotification(ObjectNotification notification, 82 Object handback) { 83 try { 84 StringAlertSource.this.handler.handle( 85 new AlertInfo(notification)); 86 } catch (Exception e) { 87 logger.log(Level.SEVERE, "Error while handling alert", e); 88 } 89 } 90 }; 91 filter = new ObjectNotificationFilterSupport(); 92 filter.enableType("jmx.monitor.string.matches"); 93 filter.enableType("jmx.monitor.string.differs"); 94 filter.enableType("jmx.monitor.error.attribute"); 95 filter.enableType("jmx.monitor.error.type"); 96 filter.enableType("jmx.monitor.error.mbean"); 97 filter.enableType("jmx.monitor.error.runtime"); 98 connection.addNotificationListener(monitorObjName, 99 listener, filter, null); 100 } 101 102 protected void unregisterInternal() { 103 assert connection != null; 104 assert monitorObjName != null; 105 106 try { 107 108 connection.removeNotificationListener(monitorObjName, listener, 109 filter, null); 110 } catch (Exception e) { 111 logger.log(Level.WARNING, 112 "Error while Removing Notification Listener. error: " + 113 e.getMessage()); 114 } 115 116 try { 117 118 connection.unregisterMBean(monitorObjName); 119 } catch (Exception e) { 120 logger.log(Level.WARNING, 121 "Error while unregistering MBean: " + monitorObjName + 122 ". error=" + e.getMessage()); 123 } 124 125 listener = null; 126 filter = null; 127 } 128 } 129 | Popular Tags |