[jboss-svn-commits] JBL Code SVN: r14613 - in labs/shotoku/trunk/shotoku-cache: cache-admin/src/java/org/jboss/shotoku/cache/admin and 4 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Aug 26 17:16:15 EDT 2007


Author: adamw
Date: 2007-08-26 17:16:14 -0400 (Sun, 26 Aug 2007)
New Revision: 14613

Modified:
   labs/shotoku/trunk/shotoku-cache/TODO
   labs/shotoku/trunk/shotoku-cache/cache-admin/src/java/org/jboss/shotoku/cache/admin/CacheItemBean.java
   labs/shotoku/trunk/shotoku-cache/cache-admin/src/java/org/jboss/shotoku/cache/admin/MonitorBean.java
   labs/shotoku/trunk/shotoku-cache/cache-admin/src/web/pages/admin.jsp
   labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/service/monitor/CacheAlertFactory.java
   labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/service/monitor/DummyRenewableCacheMonitorService.java
   labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/service/monitor/RenewableCacheMonitorServiceMBean.java
   labs/shotoku/trunk/shotoku-cache/cache-service/src/etc/META-INF/jboss-service.xml
   labs/shotoku/trunk/shotoku-cache/cache-service/src/java/org/jboss/shotoku/cache/service/monitor/RenewableCacheMonitorService.java
Log:
Email notifications

Modified: labs/shotoku/trunk/shotoku-cache/TODO
===================================================================
--- labs/shotoku/trunk/shotoku-cache/TODO	2007-08-26 21:15:56 UTC (rev 14612)
+++ labs/shotoku/trunk/shotoku-cache/TODO	2007-08-26 21:16:14 UTC (rev 14613)
@@ -2,9 +2,9 @@
 * investigate multiple ProjectConfigurationWatcher-s
 * refreshing of keys after a key reset
 * refreshing cachitem_info after alerts reset
-* e-mail notifications of alerts
 * check form-sending for cache item configuration
 * migrate AdministratedServices
 * service unavailability when stopped; null get() when CI unregistered
 * split RenewableCacheService into independent parts
 * generalize UpdateThreadData
+* cleanup with Tools

Modified: labs/shotoku/trunk/shotoku-cache/cache-admin/src/java/org/jboss/shotoku/cache/admin/CacheItemBean.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-admin/src/java/org/jboss/shotoku/cache/admin/CacheItemBean.java	2007-08-26 21:15:56 UTC (rev 14612)
+++ labs/shotoku/trunk/shotoku-cache/cache-admin/src/java/org/jboss/shotoku/cache/admin/CacheItemBean.java	2007-08-26 21:16:14 UTC (rev 14613)
@@ -6,6 +6,7 @@
 import java.util.Map;
 
 import org.jboss.cache.CacheException;
+import org.jboss.cache.Node;
 import org.jboss.shotoku.cache.CacheItemOperations;
 import org.jboss.shotoku.cache.service.RenewableCacheServiceMBean;
 import org.jboss.shotoku.cache.service.monitor.CacheAlert;
@@ -79,7 +80,12 @@
 	
 	public int getFqnKeysCount() {
 		try {
-			return service.getTreeCache().get(cacheItem.getFqn()).numAttributes();
+			Node n = service.getTreeCache().get(cacheItem.getFqn());
+			if (n == null) {
+				return 0;
+			}
+			
+			return n.numAttributes();
 		} catch (CacheException e) {
 			throw new RuntimeException(e);
 		}

Modified: labs/shotoku/trunk/shotoku-cache/cache-admin/src/java/org/jboss/shotoku/cache/admin/MonitorBean.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-admin/src/java/org/jboss/shotoku/cache/admin/MonitorBean.java	2007-08-26 21:15:56 UTC (rev 14612)
+++ labs/shotoku/trunk/shotoku-cache/cache-admin/src/java/org/jboss/shotoku/cache/admin/MonitorBean.java	2007-08-26 21:16:14 UTC (rev 14613)
@@ -77,6 +77,11 @@
     private int newMaximumNumberOfAlerts;
     private int newUpdateAlertIntervalMultiplier;
     
+    private String newAlertEmail;
+	private String newSmtpPassword;
+	private String newSmtpUser;
+	private String newSmtpServer;
+    
     public long getInterval() {
     	return getMonitor().getInterval();
     }
@@ -100,11 +105,48 @@
     public void setUpdateAlertIntervalMultiplier(int updateAlertIntervalMultiplier) {
     	newUpdateAlertIntervalMultiplier = updateAlertIntervalMultiplier;
     }
+
+	public String getAlertEmail() {
+		return getMonitor().getAlertEmail();
+	}
+
+	public void setAlertEmail(String alertEmail) {
+		this.newAlertEmail = alertEmail;
+	}
+
+	public String getSmtpPassword() {
+		return getMonitor().getSmtpPassword();
+	}
+
+	public void setSmtpPassword(String smtpPassword) {
+		this.newSmtpPassword = smtpPassword;
+	}
+
+	public String getSmtpServer() {
+		return getMonitor().getSmtpServer();
+	}
+
+	public void setSmtpServer(String smtpServer) {
+		this.newSmtpServer = smtpServer;
+	}
+
+	public String getSmtpUser() {
+		return getMonitor().getSmtpUser();
+	}
+
+	public void setSmtpUser(String smtpUser) {
+		this.newSmtpUser = smtpUser;
+	}
     
     public String updateServiceConfig() {
     	getMonitor().setInterval(newInterval);
     	getMonitor().setMaximumNumberOfAlerts(newMaximumNumberOfAlerts);
     	getMonitor().setUpdateAlertIntervalMultiplier(newUpdateAlertIntervalMultiplier);
+    	
+    	getMonitor().setSmtpServer(newSmtpServer);
+    	getMonitor().setSmtpUser(newSmtpUser);
+    	getMonitor().setSmtpPassword(newSmtpPassword);
+    	getMonitor().setAlertEmail(newAlertEmail);
     
     	CacheFacesTools.addTimedFacesMessage("Monitor configuration changed successfully.");
     	

Modified: labs/shotoku/trunk/shotoku-cache/cache-admin/src/web/pages/admin.jsp
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-admin/src/web/pages/admin.jsp	2007-08-26 21:15:56 UTC (rev 14612)
+++ labs/shotoku/trunk/shotoku-cache/cache-admin/src/web/pages/admin.jsp	2007-08-26 21:16:14 UTC (rev 14613)
@@ -105,6 +105,18 @@
 								<f:validateLongRange minimum="2" />
 							</h:inputText>
 							
+							<h:outputText value="Update alert email: "/>
+							<h:inputText value="#{monitor.alertEmail}" id="MonitorUpdateAlertEmail" />
+							
+							<h:outputText value="SMTP server: "/>
+							<h:inputText value="#{monitor.smtpServer}" id="MonitorSmtpServer" />
+							
+							<h:outputText value="SMTP user: "/>
+							<h:inputText value="#{monitor.smtpUser}" id="MonitorSmtpUser" />
+							
+							<h:outputText value="SMTP password: "/>
+							<h:inputSecret value="#{monitor.smtpPassword}" id="MonitorSmtpPassword" />
+							
 							<h:outputText value="" />
 							<a4j:commandButton value="Submit" action="#{monitor.updateServiceConfig}" 
 								reRender="monitorServiceMessages" />

Modified: labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/service/monitor/CacheAlertFactory.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/service/monitor/CacheAlertFactory.java	2007-08-26 21:15:56 UTC (rev 14612)
+++ labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/service/monitor/CacheAlertFactory.java	2007-08-26 21:16:14 UTC (rev 14613)
@@ -1,5 +1,8 @@
 package org.jboss.shotoku.cache.service.monitor;
 
+import java.io.ByteArrayOutputStream;
+import java.io.PrintWriter;
+
 public class CacheAlertFactory {
 	public static CacheAlert createAlertKeyNotUpdated(Object key) {
 		return new CacheAlertImpl(null, "Key hasn't been updated for a long time.", key);
@@ -10,6 +13,10 @@
 	}
 	
 	public static CacheAlert createAlertKeyException(Object key, Throwable t) {
-		return new CacheAlertImpl(t.toString(), "An exception occured during key update.", key);
+		ByteArrayOutputStream baos = new ByteArrayOutputStream();
+		PrintWriter target = new PrintWriter(baos);
+		t.printStackTrace(target);
+		target.flush();
+		return new CacheAlertImpl(baos.toString(), "An exception occured during key update.", key);
 	}
 }

Modified: labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/service/monitor/DummyRenewableCacheMonitorService.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/service/monitor/DummyRenewableCacheMonitorService.java	2007-08-26 21:15:56 UTC (rev 14612)
+++ labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/service/monitor/DummyRenewableCacheMonitorService.java	2007-08-26 21:16:14 UTC (rev 14613)
@@ -63,4 +63,36 @@
 	public void update() {
 		
 	}
+
+	public String getAlertEmail() {
+		return null;
+	}
+
+	public String getSmtpPassword() {
+		return null;
+	}
+
+	public String getSmtpServer() {
+		return null;
+	}
+
+	public String getSmtpUser() {
+		return null;
+	}
+
+	public void setAlertEmail(String alertEmail) {
+		
+	}
+
+	public void setSmtpPassword(String smtpPassword) {
+		
+	}
+
+	public void setSmtpServer(String smtpServer) {
+		
+	}
+
+	public void setSmtpUser(String smtpUser) {
+		
+	}
 }

Modified: labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/service/monitor/RenewableCacheMonitorServiceMBean.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/service/monitor/RenewableCacheMonitorServiceMBean.java	2007-08-26 21:15:56 UTC (rev 14612)
+++ labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/service/monitor/RenewableCacheMonitorServiceMBean.java	2007-08-26 21:16:14 UTC (rev 14613)
@@ -72,4 +72,56 @@
      * @return A set of {@link CacheItemOperations}, for which there are any alerts.
      */
     public Set<CacheItemOperations<?,?>> getCacheItemsWithAlerts();
+    
+    /**
+     * 
+     * @return Smtp server to use, when sending alert e-mails.
+     */
+    public String getSmtpServer();
+    
+    /**
+     * 
+     * @param smtpServer Smtp server to use, when sending alert e-mails.
+     */
+    public void setSmtpServer(String smtpServer);
+    
+    /**
+     * 
+     * @return Smtp user to use, when sending alert e-mails. If the server does not require
+     * authentication, an empty string or null.
+     */
+    public String getSmtpUser();
+    
+    /**
+     * 
+     * @param smtpUser Smtp user to use, when sending alert e-mails. If the server does not require
+     * authentication, an empty string or null.
+     */
+    public void setSmtpUser(String smtpUser);
+    
+    /**
+     * 
+     * @return Smtp password to use, when sending alert e-mails.
+     */
+    public String getSmtpPassword();
+    
+    /**
+     * 
+     * @param smtpPassword Smtp password to use, when sending alert e-mails.
+     */
+    public void setSmtpPassword(String smtpPassword);
+    
+    /**
+     * 
+     * @return E-mail address, to whicha alerts will be sent. If this is an empty string or null, no
+     * attemps of sending alerts will be sent.
+     */
+    public String getAlertEmail();
+    
+    /**
+     * 
+     * @param alertEmail E-mail address, to whicha alerts will be sent. If this is an empty string or null, no
+     * attemps of sending alerts will be sent.
+     */
+    public void setAlertEmail(String alertEmail);
 }

Modified: labs/shotoku/trunk/shotoku-cache/cache-service/src/etc/META-INF/jboss-service.xml
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-service/src/etc/META-INF/jboss-service.xml	2007-08-26 21:15:56 UTC (rev 14612)
+++ labs/shotoku/trunk/shotoku-cache/cache-service/src/etc/META-INF/jboss-service.xml	2007-08-26 21:16:14 UTC (rev 14613)
@@ -18,6 +18,10 @@
 		<attribute name="Interval">30000</attribute>
 		<attribute name="MaximumNumberOfAlerts">20</attribute>
 		<attribute name="UpdateAlertIntervalMultiplier">2</attribute>
+		<attribute name="SmtpServer"></attribute>
+		<attribute name="SmtpUser"></attribute>
+		<attribute name="SmtpPassword"></attribute>
+		<attribute name="AlertEmail"></attribute>
 		<depends optional-attribute-name="RenewableCacheService"
 			proxy-type="attribute">
 			shotoku:service=RenewableCache

Modified: labs/shotoku/trunk/shotoku-cache/cache-service/src/java/org/jboss/shotoku/cache/service/monitor/RenewableCacheMonitorService.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-service/src/java/org/jboss/shotoku/cache/service/monitor/RenewableCacheMonitorService.java	2007-08-26 21:15:56 UTC (rev 14612)
+++ labs/shotoku/trunk/shotoku-cache/cache-service/src/java/org/jboss/shotoku/cache/service/monitor/RenewableCacheMonitorService.java	2007-08-26 21:16:14 UTC (rev 14613)
@@ -3,15 +3,25 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Set;
 
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.Transport;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+
 import org.apache.log4j.Logger;
 import org.jboss.shotoku.cache.CacheItemOperations;
 import org.jboss.shotoku.cache.service.RenewableCacheServiceMBean;
+import org.jboss.shotoku.tools.CacheTools;
 
 /**
  * 
@@ -24,6 +34,17 @@
 	private RenewableCacheServiceMBean renewableCacheService;
 	private int maximumNumberOfAlerts;
 	private int updateAlertIntervalMultiplier;
+	
+	private String alertEmail;
+	private String smtpPassword;
+	private String smtpUser;
+	private String smtpServer;
+	
+	/**
+	 * A flag that supresses exceptions after the first one, when sending e-mails, until
+	 * configuration is changed.
+	 */
+	private volatile boolean exceptionInEmail;
 
 	public long getInterval() {
 		return interval;
@@ -58,6 +79,42 @@
 		this.updateAlertIntervalMultiplier = updateAlertIntervalMultiplier;
 	}
 	
+	public String getAlertEmail() {
+		return alertEmail;
+	}
+
+	public void setAlertEmail(String alertEmail) {
+		this.alertEmail = alertEmail;
+		exceptionInEmail = false;
+	}
+
+	public String getSmtpPassword() {
+		return smtpPassword;
+	}
+
+	public void setSmtpPassword(String smtpPassword) {
+		this.smtpPassword = smtpPassword;
+		exceptionInEmail = false;
+	}
+
+	public String getSmtpServer() {
+		return smtpServer;
+	}
+
+	public void setSmtpServer(String smtpServer) {
+		this.smtpServer = smtpServer;
+		exceptionInEmail = false;
+	}
+
+	public String getSmtpUser() {
+		return smtpUser;
+	}
+
+	public void setSmtpUser(String smtpUser) {
+		this.smtpUser = smtpUser;
+		exceptionInEmail = false;
+	}
+	
 	//
 
 	private Thread monitorThread;
@@ -158,7 +215,12 @@
 		}
 		
 		if (alertsForCacheItem.add(alert)) {
+			// The alert that we were supposed to add is a real alert.
 			currentNumberOfAlerts++;
+			
+			if (!CacheTools.isEmpty(getAlertEmail())) {
+				sendAlertEmail(rcid, alert);
+			}
 		}
 		
 		cacheItemsWithAlerts.add(rcid);
@@ -205,4 +267,43 @@
 			rcid.getKeysExceptions().clear();
 		}
 	}
+
+	private void sendAlertEmail(CacheItemOperations<?,?> rcid, CacheAlert alert) {
+		boolean smtpAuth = !CacheTools.isEmpty(getSmtpUser());
+		
+		Properties props = new Properties();
+	    props.put("mail.smtp.host", getSmtpServer());
+	    if (smtpAuth) {
+	    	props.put("mail.smtp.auth", "true");
+	    } else {
+	    	props.put("mail.smtp.auth", "false");
+	    }
+	    
+	    try {
+	    	Session session = Session.getDefaultInstance(props, null);
+	    	Transport transport = session.getTransport("smtp");
+	    	transport.connect(getSmtpServer(), getSmtpUser(), getSmtpPassword());
+	    	
+	    	Message msg = new MimeMessage(session);
+	        msg.setFrom(InternetAddress.parse("Shotoku Cache Admin <do-not-reply at jboss.org>", false)[0]);
+	        msg.setHeader("X-Mailer", "Shotoku Cache Admin Mailer");
+	        msg.setSentDate(new Date());
+	        msg.setRecipients(Message.RecipientType.TO,
+	            InternetAddress.parse(getAlertEmail(), false));
+
+	        msg.setSubject("Shotoku Cache Admin alert!");
+	        msg.setText("Alert in cache:\n" +
+	        		"Cache item name: " + rcid.getName() + "\n" +
+	        		"Key: " + alert.getKey() + "\n" +
+	        		"Date: " + alert.getTimeFormatted() + "\n" +
+	        		"Description: " + alert.getDescription() + "\n" +
+	        		"Cause:\n" + alert.getCause());
+	        transport.sendMessage(msg, msg.getAllRecipients());
+	    } catch (MessagingException e) {
+	    	if (!exceptionInEmail) {
+	    		log.error(e);
+	    		exceptionInEmail = true;
+	    	}
+	    }
+	}
 }




More information about the jboss-svn-commits mailing list