Revamped configuration to be read from file

This commit is contained in:
Simon Pamies committed 2012-11-26 19:14:36 +01:00
1 parent b91b0fbe72
commit 82ed5425f2
4 files changed
+86 -26

No files matched your search

@@ -2,9 +2,11 @@ package com.sap.sailing.monitoring;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.Properties;
import java.util.logging.Logger;
/**
@@ -18,9 +20,9 @@ public abstract class AbstractPortMonitor extends Thread {
Logger log = Logger.getLogger(AbstractPortMonitor.class.getName());
private final int TIMEOUT = 1000;
private final int GRACEFUL = 5000;
private final int PAUSE = 1000;
private int TIMEOUT = 1000;
private int GRACEFUL = 5000;
private int PAUSE = 1000;
protected InetSocketAddress[] endpoints = null;
protected int interval;
@@ -30,6 +32,8 @@ public abstract class AbstractPortMonitor extends Thread {
private long lastmillis;
private InetSocketAddress currentendpoint;
protected Properties properties;
public AbstractPortMonitor(InetSocketAddress[] endpoints, int interval) {
this.endpoints = endpoints;
@@ -43,6 +47,31 @@ public abstract class AbstractPortMonitor extends Thread {
log.info("Initialized monitoring!");
}
public AbstractPortMonitor(Properties properties) {
String[] prop_endpoints = properties.getProperty("monitor.endpoints").split(",");
this.endpoints = new InetSocketAddress[prop_endpoints.length];
try {
for (int i=0;i<prop_endpoints.length;i++) {
String[] data = prop_endpoints[i].split(":");
this.endpoints[i] = new InetSocketAddress(InetAddress.getByName(data[0].trim()), Integer.parseInt(data[1].trim()));
}
} catch (Exception ex) {
ex.printStackTrace();
}
this.interval = Integer.parseInt(properties.getProperty("monitor.interval").trim());
this.TIMEOUT = Integer.parseInt(properties.getProperty("monitor.timeout").trim());
this.GRACEFUL = Integer.parseInt(properties.getProperty("monitor.gracetime").trim());
this.PAUSE = Integer.parseInt(properties.getProperty("monitor.pause").trim());
this.started = true;
this.properties = properties;
log.info("Initialized monitoring!");
}
public void startMonitoring() {
this.start();
@@ -1,7 +1,8 @@
package com.sap.sailing.monitoring;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.io.File;
import java.io.FileReader;
import java.util.Properties;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
@@ -28,17 +29,13 @@ public class Activator implements BundleActivator {
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
/* Configuration */
InetSocketAddress[] endpoints = new InetSocketAddress[]{
new InetSocketAddress(InetAddress.getByName("localhost"), 8080)
};
String[] services = new String[] {
"com.sap.sailing.server"
};
/* Load properties */
File propertiesfile = new File(new File(System.getProperty("jetty.home")).getParent() + "/monitoring.properties");
Properties props = new Properties();
props.load(new FileReader(propertiesfile));
/* Starts a new port monitoring app */
monitor = new OSGiRestartingPortMonitor(endpoints, services, 10000);
monitor = new OSGiRestartingPortMonitor(props);
monitor.startMonitoring();
}
@@ -42,6 +42,20 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
endpointservices.put(endpoints[i], services[i]);
}
}
public OSGiRestartingPortMonitor(Properties properties) {
super(properties);
/* store bundles so that we can reference them later */
for (Bundle bundle : Activator.getContext().getBundles()) {
bundles.put(bundle.getSymbolicName(), bundle);
}
String[] prop_services = properties.getProperty("monitor.services").split(",");
for (int i=0; i<endpoints.length;i++) {
endpointservices.put(endpoints[i], prop_services[i].trim());
}
}
@Override
public void handleFailure(InetSocketAddress endpoint) {
@@ -64,21 +78,13 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
log.info("Bundle " + servicename + " restarted");
/* Send out mail */
Properties props = new Properties();
props.setProperty("mail.from", "s.pamies@banality.de");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "mail.banality.de");
props.setProperty("mail.smtp.port", "25");
props.setProperty("mail.smtp.auth", "true");
try {
Session session = Session.getDefaultInstance(props, new SMTPAuthenticator());
Session session = Session.getDefaultInstance(this.properties, new SMTPAuthenticator());
MimeMessage msg = new MimeMessage(session);
msg.setSubject("Bundle " + servicename + " restarted");
msg.setContent("The Bundle " + servicename + " has been restarted - port check on " + endpoint.getPort() + " didn't respond!", "text/plain");
msg.addRecipient(RecipientType.TO, new InternetAddress("spamsch@gmail.com"));
msg.addRecipient(RecipientType.TO, new InternetAddress(this.properties.getProperty("mail.to")));
Transport ts = session.getTransport();
ts.connect();
@@ -92,8 +98,8 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = "pamiesmail@banality.de";
String password = "***";
String username = properties.getProperty("mail.smtp.user");
String password = properties.getProperty("mail.smtp.password");
return new PasswordAuthentication(username, password);
}
}