From 1ab20897af4d3628de1b9b40446096ada6d198fd Mon Sep 17 00:00:00 2001 From: mguessan Date: Tue, 23 Jun 2009 12:37:33 +0000 Subject: [PATCH] Fix server mode: now all listener threads are daemon, avoid main thread exit and add a shutdown hook git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@594 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- src/java/davmail/DavGateway.java | 29 +++++++++++++++++++++++--- src/java/davmailmessages.properties | 2 ++ src/java/davmailmessages_fr.properties | 4 +++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/java/davmail/DavGateway.java b/src/java/davmail/DavGateway.java index 4fa49602..09090464 100644 --- a/src/java/davmail/DavGateway.java +++ b/src/java/davmail/DavGateway.java @@ -13,11 +13,8 @@ import davmail.ui.tray.DavGatewayTray; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.log4j.FileAppender; -import org.apache.log4j.Logger; import java.io.BufferedReader; -import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; @@ -28,6 +25,8 @@ import java.util.ArrayList; public class DavGateway { private static final String HTTP_DAVMAIL_SOURCEFORGE_NET_VERSION_TXT = "http://davmail.sourceforge.net/version.txt"; + private static boolean stopped; + private DavGateway() { } @@ -48,6 +47,30 @@ public class DavGateway { DavGatewayTray.init(); start(); + + // server mode: all threads are daemon threads, do not let main stop + if (Settings.getBooleanProperty("davmail.server")) { + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + DavGatewayTray.debug(new BundleMessage("LOG_GATEWAY_INTERRUPTED")); + DavGateway.stop(); + DavGatewayTray.debug(new BundleMessage("LOG_GATEWAY_STOP")); + stopped = true; + } + }); + + try { + while (!stopped) { + Thread.sleep(1000); + } + } catch (InterruptedException e) { + DavGatewayTray.debug(new BundleMessage("LOG_GATEWAY_INTERRUPTED")); + stop(); + DavGatewayTray.debug(new BundleMessage("LOG_GATEWAY_STOP")); + } + + } } public static void start() { diff --git a/src/java/davmailmessages.properties b/src/java/davmailmessages.properties index 4940f835..b5eb180a 100644 --- a/src/java/davmailmessages.properties +++ b/src/java/davmailmessages.properties @@ -105,6 +105,8 @@ LOG_READ_CLIENT_LOGIN=< LOGIN ******** LOG_READ_CLIENT_PASS=< PASS ******** LOG_READ_CLIENT_PASSWORD=< ******** LOG_REPORT_EVENT=Report event {0}/{1} +LOG_GATEWAY_INTERRUPTED=Stopping DavMail gateway +LOG_GATEWAY_STOP=DavMail gateway stopped LOG_SEARCHING_CALENDAR_EVENTS=Searching calendar events at {0} ... LOG_SEARCHING_CALENDAR_MESSAGES=Searching calendar messages... LOG_SEARCH_QUERY=Search: {0} diff --git a/src/java/davmailmessages_fr.properties b/src/java/davmailmessages_fr.properties index e8129bf2..148ec68e 100644 --- a/src/java/davmailmessages_fr.properties +++ b/src/java/davmailmessages_fr.properties @@ -221,4 +221,6 @@ UI_PKCS11_LIBRARY=Librairie PKCS11 UI_PKCS11_CONFIG_HELP=Configuration PKCS11 complémentaire optionnelle (slot, nssArgs, ...) UI_PKCS11_CONFIG=Configuration PKCS11 UI_CLIENT_CERTIFICATE=Certificat client -UI_LOG_FILE_PATH=Chemin du fichier de traces : \ No newline at end of file +UI_LOG_FILE_PATH=Chemin du fichier de traces : +LOG_GATEWAY_INTERRUPTED=Arrêt de la passerelle DavMail en cours +LOG_GATEWAY_STOP=Passerelle DavMail arrêtée \ No newline at end of file