E-Mail reporting

This commit is contained in:
2012-10-07 18:38:57 +02:00
parent 3351daaef4
commit 8b4eac33fc
9 changed files with 244 additions and 86 deletions
+22 -3
View File
@@ -1,8 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.4"/>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
+6
View File
@@ -15,8 +15,14 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
</natures>
+2 -1
View File
@@ -1,3 +1,4 @@
#Thu Mar 17 11:56:15 CET 2011
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
-1
View File
@@ -1,4 +1,3 @@
#Sat Feb 19 22:13:15 CET 2011
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+4
View File
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
+7 -4
View File
@@ -4,7 +4,7 @@
<groupId>SyncTool</groupId>
<artifactId>synctool</artifactId>
<version>1.2.1</version>
<version>1.3</version>
<packaging>jar</packaging>
<name>SyncTool</name>
@@ -32,9 +32,7 @@
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<type>jar</type>
<scope>compile</scope>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
@@ -78,5 +76,10 @@
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.5</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,98 @@
package de.tilman.log4j;
import java.util.LinkedList;
import java.util.Properties;
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.AppenderSkeleton;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;
public class EmailCollector extends AppenderSkeleton {
private final static Logger log = Logger.getLogger(EmailCollector.class);
/**
* Maximum number of lines to collect
*/
private int bufferSize;
private LinkedList<String> lineBuffer;
private String smtpUser;
private String smtpPassword;
private String emailRecipient;
private String emailSubject;
public EmailCollector(int bufferSize, String smtpUser, String smtpPassword, String emailRecipient, String emailSubject) {
this.bufferSize = bufferSize;
lineBuffer = new LinkedList<String>();
this.smtpUser= smtpUser;
this.smtpPassword = smtpPassword;
this.emailRecipient = emailRecipient;
this.emailSubject = emailSubject;
}
/**
* Close the EmailCollector and send collected messages.
*
* @see org.apache.log4j.Appender#close()
*/
@Override
public void close() {
log.info("Sending e-mail report to " + emailRecipient);
String host = "smtp.gmail.com";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", smtpUser);
props.put("mail.smtp.password", smtpPassword);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(smtpUser));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailRecipient));
message.setSubject(emailSubject);
StringBuffer sb = new StringBuffer();
while (lineBuffer.size() > 0) {
sb.append(lineBuffer.poll());
sb.append("\n");
}
message.setText(sb.toString());
Transport transport = session.getTransport("smtp");
transport.connect(host, smtpUser, smtpPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (MessagingException e) {
log.error("Error sending e-mail", e);
}
this.closed = true;
}
@Override
public boolean requiresLayout() {
return false;
}
@Override
protected void append(LoggingEvent event) {
if (lineBuffer.size() >= bufferSize) {
lineBuffer.removeFirst();
}
lineBuffer.add(event.toString());
}
}
@@ -34,7 +34,7 @@ import org.jivesoftware.smack.packet.Message;
/**
* A simple log4j appender that connects the logger to an XMPP recipient.
*
* @author Tilman Walther
* @author Tilman Liero
*/
public class JabberAppender extends AppenderSkeleton {
+32 -4
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Tilman Walther
* Copyright 2011, 2012 Tilman Liero
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@ import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
@@ -50,13 +51,14 @@ import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.Switch;
import com.martiansoftware.jsap.UnflaggedOption;
import de.tilman.log4j.EmailCollector;
import de.tilman.log4j.JabberAppender;
/**
* SyncTool synchronized two directories recursively.
*
* @author Tilman Walther
* @author Tilman Liero
*/
public class SyncTool {
@@ -449,10 +451,13 @@ public class SyncTool {
}
/**
* @param args
*/
public static void main(String[] args) {
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d{ISO8601} - %m%n")));
log.info("Starting SyncTool version 1.2.1");
log.info("Starting SyncTool version 1.3");
JSAP jsap = new JSAP();
@@ -477,6 +482,21 @@ public class SyncTool {
logfileOption.setHelp("the path for a logfile to write");
jsap.registerParameter(logfileOption);
FlaggedOption smtpUser = new FlaggedOption("SMTP user").setStringParser(JSAP.STRING_PARSER).setLongFlag(
"smtpuser").setShortFlag('t');
smtpUser.setHelp("the SMTP user for e-mail reporting");
jsap.registerParameter(smtpUser);
FlaggedOption smtpPassword = new FlaggedOption("SMTP password").setStringParser(JSAP.STRING_PARSER).setLongFlag(
"smtppassword").setShortFlag('w');
smtpPassword.setHelp("the SMTP password used for sending the e-mail report");
jsap.registerParameter(smtpPassword);
FlaggedOption emailAddress = new FlaggedOption("e-mail address").setStringParser(JSAP.STRING_PARSER).setLongFlag(
"email").setShortFlag('e');
emailAddress.setHelp("send logging output as jabber message to the given address");
jsap.registerParameter(emailAddress);
FlaggedOption jabberAddress = new FlaggedOption("jabber address").setStringParser(JSAP.STRING_PARSER).setLongFlag(
"jabber").setShortFlag('j');
jabberAddress.setHelp("send logging output as jabber message to the given address");
@@ -519,7 +539,7 @@ public class SyncTool {
FlaggedOption checkFileExists = new FlaggedOption("checkfile").setStringParser(JSAP.STRING_PARSER).setLongFlag(
"check-file-exists");
checkFileExists.setHelp("perfom synchronization only if the given file exists");
checkFileExists.setHelp("perform synchronization only if the given file exists");
jsap.registerParameter(checkFileExists);
Switch debugSwitch = new Switch("debug").setLongFlag("debug");
@@ -570,6 +590,11 @@ public class SyncTool {
}
}
if (config.getString("SMTP user") != null) {
BasicConfigurator.configure(new EmailCollector(200, config.getString("smtpuser"), config.getString("smtppassword"), config.getString("email"), "SyncTool Report"));
log.info("Prepared e-mail report for " + config.getString("e-mail address"));
}
if (config.getString("jabber address") != null) {
try {
BasicConfigurator.configure(new JabberAppender(config.getString("jabber address"), config.getString("jabber server"), config.getString("jabber user"), config.getString("jabber password")));
@@ -589,5 +614,8 @@ public class SyncTool {
SyncTool syncTool = new SyncTool(config);
syncTool.sync(config.getString("source path"), config.getString("destination path"));
// shutting down the logger will trigger the generation of the e-mail report
LogManager.shutdown();
}
}