You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sirona.apache.org by rm...@apache.org on 2015/11/02 05:44:51 UTC
svn commit: r1711896 - in /incubator/sirona/trunk: ./ alerter/ alerter/mail/
alerter/mail/src/ alerter/mail/src/main/ alerter/mail/src/main/java/
alerter/mail/src/main/java/org/ alerter/mail/src/main/java/org/apache/
alerter/mail/src/main/java/org/apac...
Author: rmannibucau
Date: Mon Nov 2 04:44:50 2015
New Revision: 1711896
URL: http://svn.apache.org/viewvc?rev=1711896&view=rev
Log:
SIRONA-48 adding mail alerter
Added:
incubator/sirona/trunk/alerter/
incubator/sirona/trunk/alerter/mail/
incubator/sirona/trunk/alerter/mail/pom.xml
incubator/sirona/trunk/alerter/mail/src/
incubator/sirona/trunk/alerter/mail/src/main/
incubator/sirona/trunk/alerter/mail/src/main/java/
incubator/sirona/trunk/alerter/mail/src/main/java/org/
incubator/sirona/trunk/alerter/mail/src/main/java/org/apache/
incubator/sirona/trunk/alerter/mail/src/main/java/org/apache/sirona/
incubator/sirona/trunk/alerter/mail/src/main/java/org/apache/sirona/alerter/
incubator/sirona/trunk/alerter/mail/src/main/java/org/apache/sirona/alerter/mail/
incubator/sirona/trunk/alerter/mail/src/main/java/org/apache/sirona/alerter/mail/MailAlerter.java
incubator/sirona/trunk/alerter/mail/src/main/resources/
incubator/sirona/trunk/alerter/mail/src/test/
incubator/sirona/trunk/alerter/mail/src/test/java/
incubator/sirona/trunk/alerter/mail/src/test/java/org/
incubator/sirona/trunk/alerter/mail/src/test/java/org/apache/
incubator/sirona/trunk/alerter/mail/src/test/java/org/apache/sirona/
incubator/sirona/trunk/alerter/mail/src/test/java/org/apache/sirona/alerter/
incubator/sirona/trunk/alerter/mail/src/test/java/org/apache/sirona/alerter/mail/
incubator/sirona/trunk/alerter/mail/src/test/java/org/apache/sirona/alerter/mail/MailAlerterTest.java
incubator/sirona/trunk/alerter/pom.xml
Modified:
incubator/sirona/trunk/api/src/main/java/org/apache/sirona/alert/AlertListener.java
incubator/sirona/trunk/api/src/main/java/org/apache/sirona/alert/AlerterSupport.java
incubator/sirona/trunk/api/src/main/java/org/apache/sirona/configuration/Configuration.java
incubator/sirona/trunk/pom.xml
Added: incubator/sirona/trunk/alerter/mail/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/alerter/mail/pom.xml?rev=1711896&view=auto
==============================================================================
--- incubator/sirona/trunk/alerter/mail/pom.xml (added)
+++ incubator/sirona/trunk/alerter/mail/pom.xml Mon Nov 2 04:44:50 2015
@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<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">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <artifactId>sirona-alerter</artifactId>
+ <groupId>org.apache.sirona</groupId>
+ <version>0.3-incubating-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>mail</artifactId>
+ <name>Apache Sirona Incubator :: Alerter :: Mail</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.sirona</groupId>
+ <artifactId>sirona-api</artifactId>
+ <version>${project.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.geronimo.javamail</groupId>
+ <artifactId>geronimo-javamail_1.4_mail</artifactId>
+ <version>1.9.0-alpha-2</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ <version>3.4</version>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.icegreen</groupId>
+ <artifactId>greenmail</artifactId>
+ <version>1.4.0</version>
+ <scope>test</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>com.sun.mail</groupId>
+ <artifactId>javax.mail</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ </dependencies>
+</project>
\ No newline at end of file
Added: incubator/sirona/trunk/alerter/mail/src/main/java/org/apache/sirona/alerter/mail/MailAlerter.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/alerter/mail/src/main/java/org/apache/sirona/alerter/mail/MailAlerter.java?rev=1711896&view=auto
==============================================================================
--- incubator/sirona/trunk/alerter/mail/src/main/java/org/apache/sirona/alerter/mail/MailAlerter.java (added)
+++ incubator/sirona/trunk/alerter/mail/src/main/java/org/apache/sirona/alerter/mail/MailAlerter.java Mon Nov 2 04:44:50 2015
@@ -0,0 +1,227 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sirona.alerter.mail;
+
+import org.apache.commons.lang3.text.StrSubstitutor;
+import org.apache.sirona.alert.AlertListener;
+import org.apache.sirona.configuration.Configuration;
+import org.apache.sirona.configuration.ioc.Created;
+
+import javax.mail.Authenticator;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.PasswordAuthentication;
+import javax.mail.Session;
+import javax.mail.Transport;
+import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Properties;
+
+public class MailAlerter implements AlertListener {
+ private String template;
+ private String subjectTemplate;
+
+ private boolean useDefault;
+ private InternetAddress from;
+ private String protocol;
+ private String host;
+ private String user;
+ private String password;
+ private int port;
+ private int timeout;
+ private boolean tls;
+ private boolean auth;
+
+ private volatile boolean init = false;
+ private Session session;
+ private Collection<InternetAddress> recipients;
+
+ @Created
+ public void init() {
+ // validations
+ if (from == null) {
+ throw new IllegalArgumentException("Missing 'from'");
+ }
+ if (host == null) {
+ throw new IllegalArgumentException("Missing 'host'");
+ }
+ if (port == 0) {
+ throw new IllegalArgumentException("Missing 'port'");
+ }
+ if (recipients == null || recipients.size() == 0) {
+ throw new IllegalArgumentException("Missing 'to'");
+ }
+
+ // some defaults
+ if (protocol == null) {
+ protocol = "smtp";
+ }
+ if (template == null) {
+ template = "${marker} throw an alert for:\n\n${resultsCsv}\n";
+ }
+ if (subjectTemplate == null) {
+ subjectTemplate = "${marker} throw an alert at ${date}";
+ }
+
+ // actual init
+ final Properties properties = new Properties();
+ properties.setProperty("mail.protocol.protocol", protocol);
+ properties.setProperty("mail." + protocol + ".host", host);
+ properties.setProperty("mail." + protocol + ".port", Integer.toString(port));
+ if (tls) {
+ properties.setProperty("mail." + protocol + ".starttls.enable", "true");
+ }
+ if (user != null) {
+ properties.setProperty("mail." + protocol + ".user", user);
+ }
+ if (password != null) {
+ properties.setProperty("password", password);
+ }
+ if (auth || password != null) {
+ properties.setProperty("mail." + protocol + ".auth", "true");
+ }
+ if (timeout > 0) {
+ properties.setProperty("mail." + protocol + ".timeout", Integer.toString(timeout));
+ }
+
+ final String password = properties.getProperty("password");
+
+ Authenticator auth = null;
+ if (password != null) {
+ final String protocol = properties.getProperty("mail.protocol.protocol", "smtp");
+
+ String user = properties.getProperty("mail." + protocol + ".user");
+ if (user == null) {
+ user = properties.getProperty("mail.user");
+ }
+
+ if (user != null) {
+ final PasswordAuthentication pa = new PasswordAuthentication(user, password);
+ auth = new Authenticator() {
+ protected PasswordAuthentication getPasswordAuthentication() {
+ return pa;
+ }
+ };
+ }
+ }
+
+ if (useDefault) {
+ if (auth != null) {
+ session = Session.getDefaultInstance(properties, auth);
+ } else {
+ session = Session.getDefaultInstance(properties);
+ }
+ } else if (auth != null) {
+ session = Session.getInstance(properties, auth);
+ } else {
+ session = Session.getInstance(properties);
+ }
+ }
+
+ @Override
+ public void onAlert(final Alert alert) {
+ try {
+ Transport.send(buildMessage(alert.asMap()));
+ } catch (final MessagingException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ private MimeMessage buildMessage(final Map<String, Object> placeholders) throws MessagingException {
+ final MimeMessage message = new MimeMessage(session);
+ message.setFrom(from);
+ message.setSubject(StrSubstitutor.replace(subjectTemplate, placeholders));
+ message.setText(StrSubstitutor.replace(template, placeholders));
+ for (final InternetAddress recipient : recipients) {
+ message.addRecipient(Message.RecipientType.TO, recipient);
+ }
+ return message;
+ }
+
+ public void setTemplate(final String template) {
+ this.template = StrSubstitutor.replace(template, Configuration.properties());
+ }
+
+ public void setFrom(final String from) {
+ try {
+ this.from = new InternetAddress(from);
+ } catch (final AddressException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ public void setPassword(final String password) {
+ this.password = StrSubstitutor.replace(password, Configuration.properties());
+ }
+
+ public void setTo(final String to) {
+ recipients = new LinkedList<InternetAddress>();
+ if (to != null) {
+ try {
+ for (final String recipient : to.split(" *, *")) {
+ recipients.add(new InternetAddress(recipient));
+ }
+ } catch (final AddressException ae) {
+ throw new IllegalArgumentException(ae);
+ }
+ }
+ }
+
+ public void setSubjectTemplate(final String subjectTemplate) {
+ this.subjectTemplate = StrSubstitutor.replace(subjectTemplate, Configuration.properties());
+ }
+
+ public void setUseDefault(final boolean useDefault) {
+ this.useDefault = useDefault;
+ }
+
+ public void setProtocol(final String protocol) {
+ this.protocol = protocol;
+ }
+
+ public void setHost(final String host) {
+ this.host = host;
+ }
+
+ public void setUser(final String user) {
+ this.user = user;
+ }
+
+ public void setPort(final String port) {
+ this.port = Integer.parseInt(StrSubstitutor.replace(port, Configuration.properties()));
+ }
+
+ public void setTimeout(final int timeout) {
+ this.timeout = timeout;
+ }
+
+ public void setTls(final boolean tls) {
+ this.tls = tls;
+ }
+
+ public void setAuth(boolean auth) {
+ this.auth = auth;
+ }
+
+ public void setInit(boolean init) {
+ this.init = init;
+ }
+}
Added: incubator/sirona/trunk/alerter/mail/src/test/java/org/apache/sirona/alerter/mail/MailAlerterTest.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/alerter/mail/src/test/java/org/apache/sirona/alerter/mail/MailAlerterTest.java?rev=1711896&view=auto
==============================================================================
--- incubator/sirona/trunk/alerter/mail/src/test/java/org/apache/sirona/alerter/mail/MailAlerterTest.java (added)
+++ incubator/sirona/trunk/alerter/mail/src/test/java/org/apache/sirona/alerter/mail/MailAlerterTest.java Mon Nov 2 04:44:50 2015
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sirona.alerter.mail;
+
+import com.icegreen.greenmail.junit.GreenMailRule;
+import com.icegreen.greenmail.util.ServerSetup;
+import org.apache.sirona.alert.AlertListener;
+import org.apache.sirona.status.NodeStatus;
+import org.apache.sirona.status.Status;
+import org.apache.sirona.status.ValidationResult;
+import org.junit.Rule;
+import org.junit.Test;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+import java.io.IOException;
+import java.util.Date;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class MailAlerterTest {
+ @Rule
+ public final GreenMailRule greenMail = new GreenMailRule(new ServerSetup(1235, "localhost", "smtp"));
+
+ @Test
+ public void run() throws MessagingException, IOException {
+ final MailAlerter listener = new MailAlerter();
+ listener.setFrom("me@here.asf");
+ listener.setPort(Integer.toString(greenMail.getSmtp().getPort()));
+ listener.setHost("localhost");
+ listener.setTo("target@mock.com");
+ listener.init();
+ listener.onAlert(new AlertListener.Alert("node1", new NodeStatus(
+ new ValidationResult[]{ new ValidationResult("val", Status.OK, "msg")}, new Date())) {});
+
+ greenMail.waitForIncomingEmail(1);
+ final MimeMessage[] messages = greenMail.getReceivedMessages();
+ assertEquals(1, messages.length);
+ assertEquals("me@here.asf", messages[0].getFrom()[0].toString());
+ assertEquals(1, messages[0].getAllRecipients().length);
+ assertEquals("target@mock.com", messages[0].getAllRecipients()[0].toString());
+ assertTrue(messages[0].getContent().toString().contains("node1 throw an alert for:\r\n\r\nval;msg;OK\r\n"));
+ }
+}
Added: incubator/sirona/trunk/alerter/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/alerter/pom.xml?rev=1711896&view=auto
==============================================================================
--- incubator/sirona/trunk/alerter/pom.xml (added)
+++ incubator/sirona/trunk/alerter/pom.xml Mon Nov 2 04:44:50 2015
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<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">
+ <parent>
+ <artifactId>sirona</artifactId>
+ <groupId>org.apache.sirona</groupId>
+ <version>0.3-incubating-SNAPSHOT</version>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>sirona-alerter</artifactId>
+ <name>Apache Sirona Incubator :: Alerter</name>
+ <packaging>pom</packaging>
+
+ <modules>
+ <module>mail</module>
+ </modules>
+</project>
Modified: incubator/sirona/trunk/api/src/main/java/org/apache/sirona/alert/AlertListener.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/api/src/main/java/org/apache/sirona/alert/AlertListener.java?rev=1711896&r1=1711895&r2=1711896&view=diff
==============================================================================
--- incubator/sirona/trunk/api/src/main/java/org/apache/sirona/alert/AlertListener.java (original)
+++ incubator/sirona/trunk/api/src/main/java/org/apache/sirona/alert/AlertListener.java Mon Nov 2 04:44:50 2015
@@ -17,6 +17,10 @@
package org.apache.sirona.alert;
import org.apache.sirona.status.NodeStatus;
+import org.apache.sirona.status.ValidationResult;
+
+import java.util.HashMap;
+import java.util.Map;
public interface AlertListener {
void onAlert(Alert alert);
@@ -37,5 +41,24 @@ public interface AlertListener {
public NodeStatus getStatus() {
return status;
}
+
+ public Map<String, Object> asMap() {
+ final String ln = System.getProperty("line.separator");
+
+ final StringBuilder csv = new StringBuilder();
+ for (final ValidationResult result : status.getResults()) {
+ csv.append(result.getName()).append(";")
+ .append(result.getMessage()).append(";")
+ .append(result.getStatus().name()).append(ln);
+ }
+
+ final Map<String, Object> map = new HashMap<String, Object>();
+ map.put("marker", marker == null ? "-" : marker);
+ map.put("status", status.getStatus().name());
+ map.put("date", status.getDate());
+ map.put("resultsLength", status.getResults().length);
+ map.put("resultsCsv", csv.toString());
+ return map;
+ }
}
}
Modified: incubator/sirona/trunk/api/src/main/java/org/apache/sirona/alert/AlerterSupport.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/api/src/main/java/org/apache/sirona/alert/AlerterSupport.java?rev=1711896&r1=1711895&r2=1711896&view=diff
==============================================================================
--- incubator/sirona/trunk/api/src/main/java/org/apache/sirona/alert/AlerterSupport.java (original)
+++ incubator/sirona/trunk/api/src/main/java/org/apache/sirona/alert/AlerterSupport.java Mon Nov 2 04:44:50 2015
@@ -22,6 +22,8 @@ import org.apache.sirona.status.Status;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.logging.Level;
+import java.util.logging.Logger;
public class AlerterSupport {
protected final Collection<AlertListener> listeners = new CopyOnWriteArraySet<AlertListener>();
@@ -35,7 +37,11 @@ public class AlerterSupport {
if (status.getStatus() != Status.OK) {
final AlertListener.Alert alert = new AlertListener.Alert(entry.getKey(), status);
for (final AlertListener listener : listeners) {
- listener.onAlert(alert);
+ try {
+ listener.onAlert(alert);
+ } catch (final RuntimeException ex) {
+ Logger.getLogger(AlerterSupport.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
+ }
}
}
}
Modified: incubator/sirona/trunk/api/src/main/java/org/apache/sirona/configuration/Configuration.java
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/api/src/main/java/org/apache/sirona/configuration/Configuration.java?rev=1711896&r1=1711895&r2=1711896&view=diff
==============================================================================
--- incubator/sirona/trunk/api/src/main/java/org/apache/sirona/configuration/Configuration.java (original)
+++ incubator/sirona/trunk/api/src/main/java/org/apache/sirona/configuration/Configuration.java Mon Nov 2 04:44:50 2015
@@ -102,6 +102,10 @@ public final class Configuration
return property.split( ";" );
}
+ public static Properties properties() {
+ return PROPERTIES;
+ }
+
private Configuration()
{
// no-op
Modified: incubator/sirona/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sirona/trunk/pom.xml?rev=1711896&r1=1711895&r2=1711896&view=diff
==============================================================================
--- incubator/sirona/trunk/pom.xml (original)
+++ incubator/sirona/trunk/pom.xml Mon Nov 2 04:44:50 2015
@@ -72,6 +72,7 @@
<module>agent</module>
<module>plugins</module>
<module>server</module>
+ <module>alerter</module>
</modules>
<developers>