I18N: davmail package

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@531 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-04-17 10:32:11 +00:00
parent b099a9aaa0
commit 041a8fb164
11 changed files with 145 additions and 103 deletions

View File

@ -12,8 +12,9 @@ import java.net.Socket;
*/ */
public abstract class AbstractServer extends Thread { public abstract class AbstractServer extends Thread {
private final int port; private final int port;
private final ServerSocket serverSocket; private ServerSocket serverSocket;
public abstract String getProtocolName();
/** /**
* Server socket TCP port * Server socket TCP port
* *
@ -30,16 +31,21 @@ public abstract class AbstractServer extends Thread {
* @param name thread name * @param name thread name
* @param port tcp socket chosen port * @param port tcp socket chosen port
* @param defaultPort tcp socket default port * @param defaultPort tcp socket default port
* @throws IOException unable to create server socket
*/ */
public AbstractServer(String name, int port, int defaultPort) throws IOException { public AbstractServer(String name, int port, int defaultPort) {
super(name); super(name);
if (port == 0) { if (port == 0) {
this.port = defaultPort; this.port = defaultPort;
} else { } else {
this.port = port; this.port = port;
} }
}
/**
* Bind server socket on defined port.
* @throws IOException unable to create server socket
*/
public void bind() throws IOException {
String bindAddress = Settings.getProperty("davmail.bindAddress"); String bindAddress = Settings.getProperty("davmail.bindAddress");
//noinspection SocketOpenedButNotSafelyClosed //noinspection SocketOpenedButNotSafelyClosed
if (bindAddress == null || bindAddress.length() == 0) { if (bindAddress == null || bindAddress.length() == 0) {

View File

@ -21,4 +21,8 @@ public class BundleMessage {
return MessageFormat.format(MESSAGE_BUNDLE.getString(key), arguments); return MessageFormat.format(MESSAGE_BUNDLE.getString(key), arguments);
} }
public static String format(String key, Object ... arguments) {
return MessageFormat.format(MESSAGE_BUNDLE.getString(key), arguments);
}
} }

View File

@ -17,19 +17,18 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.BindException; import java.net.BindException;
import java.util.ArrayList;
/** /**
* DavGateway main class * DavGateway main class
*/ */
public class DavGateway { public class DavGateway {
private static final String HTTP_DAVMAIL_SOURCEFORGE_NET_VERSION_TXT = "http://davmail.sourceforge.net/version.txt";
private DavGateway() { private DavGateway() {
} }
private static SmtpServer smtpServer; private static final ArrayList<AbstractServer> serverList = new ArrayList<AbstractServer>();
private static PopServer popServer;
private static ImapServer imapServer;
private static CaldavServer caldavServer;
private static LdapServer ldapServer;
/** /**
* Start the gateway, listen on spécified smtp and pop3 ports * Start the gateway, listen on spécified smtp and pop3 ports
@ -51,75 +50,69 @@ public class DavGateway {
public static void start() { public static void start() {
// register custom SSL Socket factory // register custom SSL Socket factory
DavGatewaySSLProtocolSocketFactory.register(); DavGatewaySSLProtocolSocketFactory.register();
try {
// prepare HTTP connection pool
DavGatewayHttpClientFacade.start();
String message = "DavMail gateway listening on"; // prepare HTTP connection pool
int smtpPort = Settings.getIntProperty("davmail.smtpPort"); DavGatewayHttpClientFacade.start();
if (smtpPort != 0) {
smtpServer = new SmtpServer(smtpPort);
smtpServer.start();
message += " SMTP port " + smtpServer.getPort();
}
int popPort = Settings.getIntProperty("davmail.popPort");
if (popPort != 0) {
popServer = new PopServer(Settings.getIntProperty("davmail.popPort"));
popServer.start();
message += " POP port " + popServer.getPort();
}
int imapPort = Settings.getIntProperty("davmail.imapPort");
if (imapPort != 0) {
imapServer = new ImapServer(Settings.getIntProperty("davmail.imapPort"));
imapServer.start();
message += " IMAP port " + imapServer.getPort();
}
int caldavPort = Settings.getIntProperty("davmail.caldavPort");
if (caldavPort != 0) {
caldavServer = new CaldavServer(Settings.getIntProperty("davmail.caldavPort"));
caldavServer.start();
message += " Caldav port " + caldavServer.getPort();
}
int ldapPort = Settings.getIntProperty("davmail.ldapPort");
if (ldapPort != 0) {
ldapServer = new LdapServer(Settings.getIntProperty("davmail.ldapPort"));
ldapServer.start();
message += " LDAP port " + ldapServer.getPort();
}
DavGatewayTray.info(message); int smtpPort = Settings.getIntProperty("davmail.smtpPort");
if (smtpPort != 0) {
serverList.add(new SmtpServer(smtpPort));
}
int popPort = Settings.getIntProperty("davmail.popPort");
if (popPort != 0) {
serverList.add(new PopServer(popPort));
}
int imapPort = Settings.getIntProperty("davmail.imapPort");
if (imapPort != 0) {
serverList.add(new ImapServer(imapPort));
}
int caldavPort = Settings.getIntProperty("davmail.caldavPort");
if (caldavPort != 0) {
serverList.add(new CaldavServer(caldavPort));
}
int ldapPort = Settings.getIntProperty("davmail.ldapPort");
if (ldapPort != 0) {
serverList.add(new LdapServer(ldapPort));
}
// check for new version StringBuilder message = new StringBuilder();
String releasedVersion = getReleasedVersion(); StringBuilder errorMessage = new StringBuilder();
String currentVersion = getCurrentVersion(); message.append(BundleMessage.format("LOG_DAVMAIL_GATEWAY_LISTENING"));
if (currentVersion != null && releasedVersion != null && currentVersion.compareTo(releasedVersion) < 0) { for (AbstractServer server : serverList) {
DavGatewayTray.info("A new version (" + releasedVersion + ") of DavMail Gateway is available !"); try {
server.bind();
server.start();
message.append(' ').append(BundleMessage.format("LOG_PROTOCOL_PORT", server.getProtocolName(), server.getPort()));
} catch (BindException e) {
errorMessage.append(' ').append(BundleMessage.format("LOG_PROTOCOL_PORT", server.getProtocolName(), server.getPort()));
} catch (IOException e) {
errorMessage.append(' ').append(BundleMessage.format("LOG_PROTOCOL_PORT", server.getProtocolName(), server.getPort()));
} }
} catch (BindException e) { }
DavGatewayTray.error("Unable to create server socket: the specified port is not allowed or in use by another process");
} catch (IOException e) { DavGatewayTray.info(message.toString());
DavGatewayTray.error("Exception creating server socket", e); if (errorMessage.length() > 0) {
DavGatewayTray.error(new BundleMessage("LOG_SOCKET_BIND_FAILED", errorMessage.toString()));
}
// check for new version
String releasedVersion = getReleasedVersion();
String currentVersion = getCurrentVersion();
if (currentVersion != null && releasedVersion != null && currentVersion.compareTo(releasedVersion) < 0) {
DavGatewayTray.info(new BundleMessage("LOG_NEW_VERSION_AVAILABLE", releasedVersion));
} }
} }
protected static void stopServer(AbstractServer server) { public static void stop() {
if (server != null) { for (AbstractServer server:serverList) {
server.close(); server.close();
try { try {
server.join(); server.join();
} catch (InterruptedException e) { } catch (InterruptedException e) {
DavGatewayTray.warn("Exception waiting for listener to die", e); DavGatewayTray.warn(new BundleMessage("LOG_EXCEPTION_WAITING_SERVER_THREAD_DIE"), e);
} }
} }
}
public static void stop() {
stopServer(smtpServer);
stopServer(popServer);
stopServer(imapServer);
stopServer(caldavServer);
stopServer(ldapServer);
// close pooled connections // close pooled connections
DavGatewayHttpClientFacade.stop(); DavGatewayHttpClientFacade.stop();
// clear session cache // clear session cache
@ -135,7 +128,7 @@ public class DavGateway {
String version = null; String version = null;
BufferedReader versionReader = null; BufferedReader versionReader = null;
HttpClient httpClient = DavGatewayHttpClientFacade.getInstance(); HttpClient httpClient = DavGatewayHttpClientFacade.getInstance();
GetMethod getMethod = new GetMethod("http://davmail.sourceforge.net/version.txt"); GetMethod getMethod = new GetMethod(HTTP_DAVMAIL_SOURCEFORGE_NET_VERSION_TXT);
try { try {
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(1000); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(1000);
int status = httpClient.executeMethod(getMethod); int status = httpClient.executeMethod(getMethod);
@ -144,7 +137,7 @@ public class DavGateway {
version = versionReader.readLine(); version = versionReader.readLine();
} }
} catch (IOException e) { } catch (IOException e) {
DavGatewayTray.debug("Exception getting released version"); DavGatewayTray.debug(new BundleMessage("LOG_UNABLE_TO_GET_RELEASED_VERSION"));
} finally { } finally {
if (versionReader != null) { if (versionReader != null) {
try { try {

View File

@ -7,6 +7,7 @@ import java.io.*;
import org.apache.log4j.Level; import org.apache.log4j.Level;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
/** /**
* Settings facade * Settings facade
@ -55,31 +56,31 @@ public class Settings {
SETTINGS.put("davmail.keepDelay", "30"); SETTINGS.put("davmail.keepDelay", "30");
SETTINGS.put("davmail.sentKeepDelay", "90"); SETTINGS.put("davmail.sentKeepDelay", "90");
SETTINGS.put("davmail.caldavPastDelay", "90"); SETTINGS.put("davmail.caldavPastDelay", "90");
SETTINGS.put("davmail.allowRemote", "false"); SETTINGS.put("davmail.allowRemote", Boolean.FALSE.toString());
SETTINGS.put("davmail.bindAddress", ""); SETTINGS.put("davmail.bindAddress", "");
SETTINGS.put("davmail.enableProxy", "false"); SETTINGS.put("davmail.enableProxy", Boolean.FALSE.toString());
SETTINGS.put("davmail.proxyHost", ""); SETTINGS.put("davmail.proxyHost", "");
SETTINGS.put("davmail.proxyPort", ""); SETTINGS.put("davmail.proxyPort", "");
SETTINGS.put("davmail.proxyUser", ""); SETTINGS.put("davmail.proxyUser", "");
SETTINGS.put("davmail.proxyPassword", ""); SETTINGS.put("davmail.proxyPassword", "");
SETTINGS.put("davmail.server", "false"); SETTINGS.put("davmail.server", Boolean.FALSE.toString());
SETTINGS.put("davmail.server.certificate.hash", ""); SETTINGS.put("davmail.server.certificate.hash", "");
// logging // logging
SETTINGS.put("log4j.rootLogger", "WARN"); SETTINGS.put("log4j.rootLogger", Priority.WARN.toString());
SETTINGS.put("log4j.logger.davmail", "DEBUG"); SETTINGS.put("log4j.logger.davmail", Priority.DEBUG.toString());
SETTINGS.put("log4j.logger.httpclient.wire", "WARN"); SETTINGS.put("log4j.logger.httpclient.wire", Priority.WARN.toString());
SETTINGS.put("log4j.logger.org.apache.commons.httpclient", "WARN"); SETTINGS.put("log4j.logger.org.apache.commons.httpclient", Priority.WARN.toString());
save(); save();
} }
} catch (IOException e) { } catch (IOException e) {
DavGatewayTray.error("Unable to load settings: ", e); DavGatewayTray.error(new BundleMessage("LOG_UNABLE_TO_LOAD_SETTINGS"), e);
} finally { } finally {
if (fileInputStream != null) { if (fileInputStream != null) {
try { try {
fileInputStream.close(); fileInputStream.close();
} catch (IOException e) { } catch (IOException e) {
DavGatewayTray.debug("Error closing configuration file: ", e); DavGatewayTray.debug(new BundleMessage("LOG_ERROR_CLOGING_CONFIG_FILE"), e);
} }
} }
} }
@ -97,13 +98,13 @@ public class Settings {
fileOutputStream = new FileOutputStream(configFilePath); fileOutputStream = new FileOutputStream(configFilePath);
SETTINGS.store(fileOutputStream, "DavMail settings"); SETTINGS.store(fileOutputStream, "DavMail settings");
} catch (IOException e) { } catch (IOException e) {
DavGatewayTray.error("Unable to store settings: ", e); DavGatewayTray.error(new BundleMessage("LOG_UNABLE_TO_STORE_SETTINGS"), e);
} finally { } finally {
if (fileOutputStream != null) { if (fileOutputStream != null) {
try { try {
fileOutputStream.close(); fileOutputStream.close();
} catch (IOException e) { } catch (IOException e) {
DavGatewayTray.debug("Error closing configuration file: ", e); DavGatewayTray.debug(new BundleMessage("LOG_ERROR_CLOSING_CONFIG_FILE"), e);
} }
} }
} }
@ -129,14 +130,14 @@ public class Settings {
value = Integer.parseInt(propertyValue); value = Integer.parseInt(propertyValue);
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
DavGatewayTray.error("Invalid setting value in " + property, e); DavGatewayTray.error(new BundleMessage("LOG_INVALID_SETTING_VALUE", property), e);
} }
return value; return value;
} }
public static synchronized boolean getBooleanProperty(String property) { public static synchronized boolean getBooleanProperty(String property) {
String propertyValue = SETTINGS.getProperty(property); String propertyValue = SETTINGS.getProperty(property);
return "true".equals(propertyValue); return Boolean.parseBoolean(propertyValue);
} }
protected static String getLoggingPrefix(String category) { protected static String getLoggingPrefix(String category) {

View File

@ -17,10 +17,14 @@ public class CaldavServer extends AbstractServer {
* Start the thread. * Start the thread.
* *
* @param port pop listen port, 80 if not defined (0) * @param port pop listen port, 80 if not defined (0)
* @throws IOException on error
*/ */
public CaldavServer(int port) throws IOException { public CaldavServer(int port) {
super("CaldavServer", port, CaldavServer.DEFAULT_PORT); super(CaldavServer.class.getName(), port, CaldavServer.DEFAULT_PORT);
}
@Override
public String getProtocolName() {
return "CALDAV";
} }
@Override @Override

View File

@ -1,11 +1,10 @@
package davmail.imap; package davmail.imap;
import java.net.Socket;
import java.io.IOException;
import davmail.AbstractServer;
import davmail.AbstractConnection; import davmail.AbstractConnection;
import davmail.AbstractServer;
import java.net.Socket;
/** /**
* Pop3 server * Pop3 server
@ -18,13 +17,18 @@ public class ImapServer extends AbstractServer {
* Start the thread. * Start the thread.
* *
* @param port imap listen port, 143 if not defined (0) * @param port imap listen port, 143 if not defined (0)
* @throws IOException on error
*/ */
public ImapServer(int port) throws IOException { public ImapServer(int port) {
super("ImapServer", port, ImapServer.DEFAULT_PORT); super(ImapServer.class.getName(), port, ImapServer.DEFAULT_PORT);
} }
@Override public AbstractConnection createConnectionHandler(Socket clientSocket) { @Override
public String getProtocolName() {
return "IMAP";
}
@Override
public AbstractConnection createConnectionHandler(Socket clientSocket) {
return new ImapConnection(clientSocket); return new ImapConnection(clientSocket);
} }

View File

@ -17,10 +17,14 @@ public class LdapServer extends AbstractServer {
* Start the thread. * Start the thread.
* *
* @param port pop listen port, 389 if not defined (0) * @param port pop listen port, 389 if not defined (0)
* @throws IOException on error
*/ */
public LdapServer(int port) throws IOException { public LdapServer(int port) {
super("LdapServer", port, LdapServer.DEFAULT_PORT); super(LdapServer.class.getName(), port, LdapServer.DEFAULT_PORT);
}
@Override
public String getProtocolName() {
return "LDAP";
} }
@Override @Override

View File

@ -1,11 +1,10 @@
package davmail.pop; package davmail.pop;
import davmail.AbstractServer;
import davmail.AbstractConnection; import davmail.AbstractConnection;
import davmail.AbstractServer;
import java.net.Socket; import java.net.Socket;
import java.io.IOException;
/** /**
* Pop3 server * Pop3 server
@ -17,10 +16,14 @@ public class PopServer extends AbstractServer {
* Create a ServerSocket to listen for connections. * Create a ServerSocket to listen for connections.
* Start the thread. * Start the thread.
* @param port pop listen port, 110 if not defined (0) * @param port pop listen port, 110 if not defined (0)
* @throws IOException on error
*/ */
public PopServer(int port) throws IOException { public PopServer(int port) {
super("PopServer", port, PopServer.DEFAULT_PORT); super(PopServer.class.getName(), port, PopServer.DEFAULT_PORT);
}
@Override
public String getProtocolName() {
return "POP";
} }
@Override @Override

View File

@ -1,10 +1,9 @@
package davmail.smtp; package davmail.smtp;
import davmail.AbstractServer;
import davmail.AbstractConnection; import davmail.AbstractConnection;
import davmail.AbstractServer;
import java.net.Socket; import java.net.Socket;
import java.io.IOException;
public class SmtpServer extends AbstractServer { public class SmtpServer extends AbstractServer {
public static final int DEFAULT_PORT = 25; public static final int DEFAULT_PORT = 25;
@ -13,10 +12,14 @@ public class SmtpServer extends AbstractServer {
* Create a ServerSocket to listen for connections. * Create a ServerSocket to listen for connections.
* Start the thread. * Start the thread.
* @param port smtp port * @param port smtp port
* @throws IOException on error
*/ */
public SmtpServer(int port) throws IOException { public SmtpServer(int port) {
super("SmtpServer", port, SmtpServer.DEFAULT_PORT); super(SmtpServer.class.getName(), port, SmtpServer.DEFAULT_PORT);
}
@Override
public String getProtocolName() {
return "SMTP";
} }
@Override @Override

View File

@ -110,6 +110,10 @@ public class DavGatewayTray {
displayMessage(message, Priority.ERROR); displayMessage(message, Priority.ERROR);
} }
public static void error(BundleMessage message) {
displayMessage(message, Priority.ERROR);
}
public static void error(Exception e) { public static void error(Exception e) {
displayMessage((String) null, e, Priority.ERROR); displayMessage((String) null, e, Priority.ERROR);
} }
@ -118,6 +122,10 @@ public class DavGatewayTray {
displayMessage(message, e, Priority.DEBUG); displayMessage(message, e, Priority.DEBUG);
} }
public static void debug(BundleMessage message, Exception e) {
displayMessage(message, e, Priority.DEBUG);
}
public static void info(String message, Exception e) { public static void info(String message, Exception e) {
displayMessage(message, e, Priority.INFO); displayMessage(message, e, Priority.INFO);
} }

View File

@ -8,4 +8,16 @@ LOG_EXCEPTION_CLOSING_CLIENT_SOCKET=Exception closing client socket
LOG_EXCEPTION_CLOSING_SERVER_SOCKET=Exception closing server socket LOG_EXCEPTION_CLOSING_SERVER_SOCKET=Exception closing server socket
LOG_EXCEPTION_GETTING_SOCKET_STREAMS=Exception while getting socket streams LOG_EXCEPTION_GETTING_SOCKET_STREAMS=Exception while getting socket streams
LOG_EXCEPTION_CLOSING_CLIENT_INPUT_STREAM=Exception closing client input stream LOG_EXCEPTION_CLOSING_CLIENT_INPUT_STREAM=Exception closing client input stream
LOG_EXCEPTION_CLOSING_CLIENT_OUTPUT_STREAM=Exception closing client output stream LOG_EXCEPTION_CLOSING_CLIENT_OUTPUT_STREAM=Exception closing client output stream
LOG_UNABLE_TO_LOAD_SETTINGS=Unable to load settings:
LOG_ERROR_CLOGING_CONFIG_FILE=Error closing configuration file
LOG_UNABLE_TO_STORE_SETTINGS=Unable to store settings:
LOG_ERROR_CLOSING_CONFIG_FILE=Error closing configuration file:
LOG_INVALID_SETTING_VALUE=Invalid setting value in {0}
LOG_PROTOCOL_PORT={0} port {1}
LOG_DAVMAIL_GATEWAY_LISTENING=DavMail gateway listening on
LOG_SOCKET_BIND_FAILED=Unable to bind server socket to {0}: port not allowed or in use by another process
LOG_EXCEPTION_CREATING_SERVER_SOCKET=Exception creating server socket
LOG_NEW_VERSION_AVAILABLE=A new version ({0}) of DavMail Gateway is available !
LOG_EXCEPTION_WAITING_SERVER_THREAD_DIE=Exception waiting for server thread to die
LOG_UNABLE_TO_GET_RELEASED_VERSION=Unable to get released version