1 21 22 package uk.org.primrose.pool.jmx; 23 24 import javax.management.Notification ; 25 import javax.management.AttributeChangeNotification ; 26 import javax.management.NotificationListener ; 27 import java.io.PrintWriter ; 28 import javax.naming.InitialContext ; 29 30 public class PoolControllerListener implements NotificationListener { 31 PoolController pc = null; 32 static int numberOfEmailsSent = 0; 33 static long lastEmailSent = System.currentTimeMillis(); 34 boolean crisisAlert = false; 35 36 public void handleNotification(Notification notification, Object handback) { 37 ControllerLogger.log("[PoolControllerListener] Received notification : "); 38 if (pc == null) { 39 try { 40 pc = (PoolController)MBeanUtil.lookup("pool"); 41 } catch (Exception e) { 42 ControllerLogger.log("[PoolControllerListener] SEVERE : Cannot find PoolController !!! StackTrace in stderr log ... \n"); 43 e.printStackTrace(System.err); 44 } 45 } 46 47 long timeStamp = notification.getTimeStamp(); 48 String message = notification.getMessage(); 49 String type = notification.getType(); 50 Object userData = notification.getUserData(); 51 52 ControllerLogger.log("\ttimeStamp(" +new java.util.Date (timeStamp) +")"); 53 ControllerLogger.log("\tmessage(" +message +")"); 54 ControllerLogger.log("\ttype(" +type +")"); 55 ControllerLogger.log("\tuserData(" +userData +")"); 56 57 58 String attributeName = ""; 59 String attributeType = ""; 60 if (notification.getClass().getName().equals("javax.management.AttributeChangeNotification")) { 61 AttributeChangeNotification aNotification = (AttributeChangeNotification )notification; 62 63 attributeName = aNotification.getAttributeName(); 64 attributeType = aNotification.getAttributeType(); 65 66 ControllerLogger.log("\tattributeName(" +attributeName +")"); 67 ControllerLogger.log("\tattributeType(" +attributeType +")"); 68 } 69 70 71 String mxServer = pc.getEmailSMTPServer(); 72 if (mxServer == null || mxServer.length() == 0 || pc.getEmail() == null || pc.getEmail().length == 0) { 74 return; 75 } 76 77 if (attributeType.indexOf("monitor.threshold.reset") != -1) { 78 numberOfEmailsSent = 0; 79 crisisAlert = false; 80 return; 81 } 82 83 if (attributeType.indexOf("monitor.threshold.decreasing") != -1) { 84 return; 85 } 86 87 88 if (attributeType.indexOf("monitor.threshold") != -1) { 89 int maxNumberOfEmailsToSend = pc.getEmailMaxWarningNumber(); 90 long emailWarningInterval = pc.getEmailNotificationPeriod(); 91 if ((numberOfEmailsSent < maxNumberOfEmailsToSend) && ((System.currentTimeMillis() -lastEmailSent) > emailWarningInterval)) { 92 lastEmailSent = System.currentTimeMillis(); 93 numberOfEmailsSent++; 94 } else { 95 if (numberOfEmailsSent == maxNumberOfEmailsToSend && crisisAlert == false && pc.getEmailCrisisAddress().length() != 0) { 100 crisisAlert = true; 101 } else { 102 return; 103 } 104 } 105 } 106 107 108 String host = ""; 109 try { 110 host = java.net.InetAddress.getLocalHost().getHostName(); 111 } catch (Exception e) {} 112 String fromAddress = "PoolController@" +host; 113 String subject = type +" - operation(" +attributeName +"(" +attributeType +"))"; 114 String text = message; 115 try { 116 117 String [] recipients = null; 118 if (crisisAlert) { 119 recipients = new String [] { pc.getEmailCrisisAddress() }; 120 text = subject +"\n" +text; 121 subject = "POOL CRISIS ALERT ! "; 122 123 } else { 124 recipients = pc.getEmail(); 125 } 126 boolean send = pc.getEmailNotifications(); 127 if (send) { 128 for(int i=0;i<recipients.length;i++) { 129 doSend(mxServer,fromAddress, recipients[i], subject, text); 130 } 131 } 132 } catch (Exception e) { 133 e.printStackTrace(); 134 } 135 if (attributeType.indexOf("monitor.differing") != -1) { 136 refreshPoolConfigFile(); 137 } 138 139 140 } 141 142 143 private void refreshPoolConfigFile() { 144 try { 149 if (pc == null) pc = (PoolController)MBeanUtil.lookup("pool"); 150 pc.refreshPoolConfigFile(); 151 } catch (Exception e) { 152 e.printStackTrace(); 153 } 154 155 156 } 157 158 private void doSend(String mxServer, String fromAddress, String toAddress, String subject, String text) { 159 SendMail sendMail = new SendMail(mxServer, toAddress, fromAddress, subject, text); 160 sendMail.send(); 161 } 162 163 } 164 | Popular Tags |