E-Mail reporting
This commit is contained in:
+22
-3
@@ -1,8 +1,27 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<classpath>
|
<classpath>
|
||||||
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
|
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
|
<attributes>
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.4"/>
|
<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.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"/>
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
|||||||
@@ -15,8 +15,14 @@
|
|||||||
<arguments>
|
<arguments>
|
||||||
</arguments>
|
</arguments>
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
</buildSpec>
|
</buildSpec>
|
||||||
<natures>
|
<natures>
|
||||||
|
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
<nature>org.maven.ide.eclipse.maven2Nature</nature>
|
<nature>org.maven.ide.eclipse.maven2Nature</nature>
|
||||||
</natures>
|
</natures>
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
#Thu Mar 17 11:56:15 CET 2011
|
|
||||||
eclipse.preferences.version=1
|
eclipse.preferences.version=1
|
||||||
|
encoding//src/main/java=UTF-8
|
||||||
|
encoding//src/test/java=UTF-8
|
||||||
encoding/<project>=UTF-8
|
encoding/<project>=UTF-8
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
#Sat Feb 19 22:13:15 CET 2011
|
|
||||||
eclipse.preferences.version=1
|
eclipse.preferences.version=1
|
||||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
activeProfiles=
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
resolveWorkspaceProjects=true
|
||||||
|
version=1
|
||||||
@@ -1,82 +1,85 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>SyncTool</groupId>
|
<groupId>SyncTool</groupId>
|
||||||
<artifactId>synctool</artifactId>
|
<artifactId>synctool</artifactId>
|
||||||
<version>1.2.1</version>
|
<version>1.3</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>SyncTool</name>
|
<name>SyncTool</name>
|
||||||
<description>Directory Synchronization Tool</description>
|
<description>Directory Synchronization Tool</description>
|
||||||
<url>http://www.tilman.de/programme/synctool</url>
|
<url>http://www.tilman.de/programme/synctool</url>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>3.8.1</version>
|
<version>3.8.1</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.martiansoftware</groupId>
|
<groupId>com.martiansoftware</groupId>
|
||||||
<artifactId>jsap</artifactId>
|
<artifactId>jsap</artifactId>
|
||||||
<version>2.1</version>
|
<version>2.1</version>
|
||||||
<type>jar</type>
|
<type>jar</type>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>log4j</groupId>
|
<groupId>log4j</groupId>
|
||||||
<artifactId>log4j</artifactId>
|
<artifactId>log4j</artifactId>
|
||||||
<version>1.2.14</version>
|
<version>1.2.17</version>
|
||||||
<type>jar</type>
|
</dependency>
|
||||||
<scope>compile</scope>
|
<dependency>
|
||||||
</dependency>
|
<groupId>com.h2database</groupId>
|
||||||
<dependency>
|
<artifactId>h2</artifactId>
|
||||||
<groupId>com.h2database</groupId>
|
<version>1.3.150</version>
|
||||||
<artifactId>h2</artifactId>
|
<type>jar</type>
|
||||||
<version>1.3.150</version>
|
<scope>compile</scope>
|
||||||
<type>jar</type>
|
</dependency>
|
||||||
<scope>compile</scope>
|
<dependency>
|
||||||
</dependency>
|
<groupId>commons-codec</groupId>
|
||||||
<dependency>
|
<artifactId>commons-codec</artifactId>
|
||||||
<groupId>commons-codec</groupId>
|
<version>1.4</version>
|
||||||
<artifactId>commons-codec</artifactId>
|
<type>jar</type>
|
||||||
<version>1.4</version>
|
<scope>compile</scope>
|
||||||
<type>jar</type>
|
</dependency>
|
||||||
<scope>compile</scope>
|
<dependency>
|
||||||
</dependency>
|
<groupId>commons-io</groupId>
|
||||||
<dependency>
|
<artifactId>commons-io</artifactId>
|
||||||
<groupId>commons-io</groupId>
|
<version>2.0.1</version>
|
||||||
<artifactId>commons-io</artifactId>
|
<type>jar</type>
|
||||||
<version>2.0.1</version>
|
<scope>compile</scope>
|
||||||
<type>jar</type>
|
</dependency>
|
||||||
<scope>compile</scope>
|
<dependency>
|
||||||
</dependency>
|
<groupId>jivesoftware</groupId>
|
||||||
<dependency>
|
<artifactId>smack</artifactId>
|
||||||
<groupId>jivesoftware</groupId>
|
<version>3.1.0</version>
|
||||||
<artifactId>smack</artifactId>
|
<type>jar</type>
|
||||||
<version>3.1.0</version>
|
<scope>compile</scope>
|
||||||
<type>jar</type>
|
</dependency>
|
||||||
<scope>compile</scope>
|
<dependency>
|
||||||
</dependency>
|
<groupId>jivesoftware</groupId>
|
||||||
<dependency>
|
<artifactId>smackx</artifactId>
|
||||||
<groupId>jivesoftware</groupId>
|
<version>3.1.0</version>
|
||||||
<artifactId>smackx</artifactId>
|
<type>jar</type>
|
||||||
<version>3.1.0</version>
|
<scope>compile</scope>
|
||||||
<type>jar</type>
|
</dependency>
|
||||||
<scope>compile</scope>
|
<dependency>
|
||||||
</dependency>
|
<groupId>org.hsqldb</groupId>
|
||||||
<dependency>
|
<artifactId>hsqldb</artifactId>
|
||||||
<groupId>org.hsqldb</groupId>
|
<version>2.0.0</version>
|
||||||
<artifactId>hsqldb</artifactId>
|
<type>jar</type>
|
||||||
<version>2.0.0</version>
|
<scope>compile</scope>
|
||||||
<type>jar</type>
|
</dependency>
|
||||||
<scope>compile</scope>
|
<dependency>
|
||||||
</dependency>
|
<groupId>javax.mail</groupId>
|
||||||
</dependencies>
|
<artifactId>mail</artifactId>
|
||||||
|
<version>1.4.5</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
</project>
|
</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.
|
* A simple log4j appender that connects the logger to an XMPP recipient.
|
||||||
*
|
*
|
||||||
* @author Tilman Walther
|
* @author Tilman Liero
|
||||||
*/
|
*/
|
||||||
public class JabberAppender extends AppenderSkeleton {
|
public class JabberAppender extends AppenderSkeleton {
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2011 Tilman Walther
|
* Copyright 2011, 2012 Tilman Liero
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.ConsoleAppender;
|
||||||
import org.apache.log4j.FileAppender;
|
import org.apache.log4j.FileAppender;
|
||||||
import org.apache.log4j.Level;
|
import org.apache.log4j.Level;
|
||||||
|
import org.apache.log4j.LogManager;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.apache.log4j.PatternLayout;
|
import org.apache.log4j.PatternLayout;
|
||||||
import org.apache.log4j.RollingFileAppender;
|
import org.apache.log4j.RollingFileAppender;
|
||||||
@@ -50,13 +51,14 @@ import com.martiansoftware.jsap.JSAPResult;
|
|||||||
import com.martiansoftware.jsap.Switch;
|
import com.martiansoftware.jsap.Switch;
|
||||||
import com.martiansoftware.jsap.UnflaggedOption;
|
import com.martiansoftware.jsap.UnflaggedOption;
|
||||||
|
|
||||||
|
import de.tilman.log4j.EmailCollector;
|
||||||
import de.tilman.log4j.JabberAppender;
|
import de.tilman.log4j.JabberAppender;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SyncTool synchronized two directories recursively.
|
* SyncTool synchronized two directories recursively.
|
||||||
*
|
*
|
||||||
* @author Tilman Walther
|
* @author Tilman Liero
|
||||||
*/
|
*/
|
||||||
public class SyncTool {
|
public class SyncTool {
|
||||||
|
|
||||||
@@ -449,10 +451,13 @@ public class SyncTool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d{ISO8601} - %m%n")));
|
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();
|
JSAP jsap = new JSAP();
|
||||||
|
|
||||||
@@ -477,6 +482,21 @@ public class SyncTool {
|
|||||||
logfileOption.setHelp("the path for a logfile to write");
|
logfileOption.setHelp("the path for a logfile to write");
|
||||||
jsap.registerParameter(logfileOption);
|
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(
|
FlaggedOption jabberAddress = new FlaggedOption("jabber address").setStringParser(JSAP.STRING_PARSER).setLongFlag(
|
||||||
"jabber").setShortFlag('j');
|
"jabber").setShortFlag('j');
|
||||||
jabberAddress.setHelp("send logging output as jabber message to the given address");
|
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(
|
FlaggedOption checkFileExists = new FlaggedOption("checkfile").setStringParser(JSAP.STRING_PARSER).setLongFlag(
|
||||||
"check-file-exists");
|
"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);
|
jsap.registerParameter(checkFileExists);
|
||||||
|
|
||||||
Switch debugSwitch = new Switch("debug").setLongFlag("debug");
|
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) {
|
if (config.getString("jabber address") != null) {
|
||||||
try {
|
try {
|
||||||
BasicConfigurator.configure(new JabberAppender(config.getString("jabber address"), config.getString("jabber server"), config.getString("jabber user"), config.getString("jabber password")));
|
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 syncTool = new SyncTool(config);
|
||||||
syncTool.sync(config.getString("source path"), config.getString("destination path"));
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user