KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > org > primrose > pool > jmx > PoolControllerListener


1 /**
2 * Library name : Primrose - A Java Database Connection Pool.
3 * Published by Ben Keeping, http://primrose.org.uk .
4 * Copyright (C) 2004 Ben Keeping, primrose.org.uk
5 * Email: Use "Contact Us Form" on website
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */

21
22 package uk.org.primrose.pool.jmx;
23
24 import javax.management.Notification JavaDoc;
25 import javax.management.AttributeChangeNotification JavaDoc;
26 import javax.management.NotificationListener JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29
30 public class PoolControllerListener implements NotificationListener JavaDoc {
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 JavaDoc notification, Object JavaDoc handback) {
37         ControllerLogger.log("[PoolControllerListener] Received notification : ");
38         if (pc == null) {
39             try {
40                 pc = (PoolController)MBeanUtil.lookup("pool");
41             } catch (Exception JavaDoc 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 JavaDoc message = notification.getMessage();
49         String JavaDoc type = notification.getType();
50         Object JavaDoc userData = notification.getUserData();
51
52         ControllerLogger.log("\ttimeStamp(" +new java.util.Date JavaDoc(timeStamp) +")");
53         ControllerLogger.log("\tmessage(" +message +")");
54         ControllerLogger.log("\ttype(" +type +")");
55         ControllerLogger.log("\tuserData(" +userData +")");
56
57
58         String JavaDoc attributeName = "";
59         String JavaDoc attributeType = "";
60         if (notification.getClass().getName().equals("javax.management.AttributeChangeNotification")) {
61             AttributeChangeNotification JavaDoc aNotification = (AttributeChangeNotification JavaDoc)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 JavaDoc mxServer = pc.getEmailSMTPServer();
72         // don't send any mail if there is no smtp server ...
73
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                 // Send an email to the crisis address IFF
96
// we have reached our maximum emails sent to the normal address
97
// and if we have not sent a crisis alert before (if we have, crisisAlert will be true)
98
// and if there is a crisis address
99
if (numberOfEmailsSent == maxNumberOfEmailsToSend && crisisAlert == false && pc.getEmailCrisisAddress().length() != 0) {
100                     crisisAlert = true;
101                 } else {
102                     return;
103                 }
104             }
105         }
106
107
108         String JavaDoc host = "";
109         try {
110             host = java.net.InetAddress.getLocalHost().getHostName();
111         } catch (Exception JavaDoc e) {}
112         String JavaDoc fromAddress = "PoolController@" +host;
113         String JavaDoc subject = type +" - operation(" +attributeName +"(" +attributeType +"))";
114         String JavaDoc text = message;
115         try {
116
117             String JavaDoc[] recipients = null;
118             if (crisisAlert) {
119                 recipients = new String JavaDoc[] { 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 JavaDoc 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         // Have to disconnect remote loading for now, as the tomcat 4 build and JMX < 1.2 ca'y
145
// handle it
146
//RemoteClient ru = new RemoteClient();
147
//ru.refreshPoolConfigFile("PoolController Domain:name=PoolController", "localhost", null);
148
try {
149             if (pc == null) pc = (PoolController)MBeanUtil.lookup("pool");
150             pc.refreshPoolConfigFile();
151         } catch (Exception JavaDoc e) {
152             e.printStackTrace();
153         }
154
155
156     }
157
158     private void doSend(String JavaDoc mxServer, String JavaDoc fromAddress, String JavaDoc toAddress, String JavaDoc subject, String JavaDoc text) {
159         SendMail sendMail = new SendMail(mxServer, toAddress, fromAddress, subject, text);
160         sendMail.send();
161     }
162
163 }
164
Popular Tags