2009-07-21 04:39:18 -04:00
|
|
|
/*
|
|
|
|
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
|
|
|
|
* Copyright (C) 2009 Mickael Guessant
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2006-12-12 18:57:24 -05:00
|
|
|
package davmail;
|
|
|
|
|
2008-11-26 19:56:28 -05:00
|
|
|
import davmail.caldav.CaldavServer;
|
2009-05-21 06:33:00 -04:00
|
|
|
import davmail.exception.DavMailException;
|
2008-12-17 10:29:16 -05:00
|
|
|
import davmail.exchange.ExchangeSessionFactory;
|
2008-11-03 20:47:10 -05:00
|
|
|
import davmail.http.DavGatewayHttpClientFacade;
|
2008-11-26 19:56:28 -05:00
|
|
|
import davmail.http.DavGatewaySSLProtocolSocketFactory;
|
2009-04-29 18:30:59 -04:00
|
|
|
import davmail.imap.ImapServer;
|
2008-12-03 12:18:51 -05:00
|
|
|
import davmail.ldap.LdapServer;
|
2006-12-12 18:57:24 -05:00
|
|
|
import davmail.pop.PopServer;
|
|
|
|
import davmail.smtp.SmtpServer;
|
2009-04-03 03:38:31 -04:00
|
|
|
import davmail.ui.tray.DavGatewayTray;
|
2008-11-03 11:07:13 -05:00
|
|
|
import org.apache.commons.httpclient.HttpClient;
|
|
|
|
import org.apache.commons.httpclient.HttpStatus;
|
|
|
|
import org.apache.commons.httpclient.methods.GetMethod;
|
2009-11-15 08:01:12 -05:00
|
|
|
import org.apache.log4j.Logger;
|
2007-08-02 09:33:45 -04:00
|
|
|
|
2008-11-26 19:56:28 -05:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
2009-04-17 06:32:11 -04:00
|
|
|
import java.util.ArrayList;
|
2008-11-26 19:56:28 -05:00
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
/**
|
|
|
|
* DavGateway main class
|
|
|
|
*/
|
2009-09-21 17:34:13 -04:00
|
|
|
public final class DavGateway {
|
2009-11-15 08:01:12 -05:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(DavGateway.class);
|
2009-04-17 06:32:11 -04:00
|
|
|
private static final String HTTP_DAVMAIL_SOURCEFORGE_NET_VERSION_TXT = "http://davmail.sourceforge.net/version.txt";
|
|
|
|
|
2009-06-23 08:37:33 -04:00
|
|
|
private static boolean stopped;
|
|
|
|
|
2009-04-16 17:52:17 -04:00
|
|
|
private DavGateway() {
|
2007-05-09 19:37:24 -04:00
|
|
|
}
|
|
|
|
|
2009-09-21 17:34:13 -04:00
|
|
|
private static final ArrayList<AbstractServer> SERVER_LIST = new ArrayList<AbstractServer>();
|
2006-12-12 18:57:24 -05:00
|
|
|
|
|
|
|
/**
|
2009-12-20 05:51:02 -05:00
|
|
|
* Start the gateway, listen on specified smtp and pop3 ports
|
2007-05-09 19:37:24 -04:00
|
|
|
*
|
2007-03-14 07:55:37 -04:00
|
|
|
* @param args command line parameter config file path
|
2006-12-12 18:57:24 -05:00
|
|
|
*/
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
if (args.length >= 1) {
|
2006-12-15 11:44:31 -05:00
|
|
|
Settings.setConfigFilePath(args[0]);
|
2006-12-14 10:14:18 -05:00
|
|
|
}
|
2006-12-15 11:44:31 -05:00
|
|
|
|
2006-12-14 10:14:18 -05:00
|
|
|
Settings.load();
|
|
|
|
DavGatewayTray.init();
|
|
|
|
|
|
|
|
start();
|
2009-06-23 08:37:33 -04:00
|
|
|
|
|
|
|
// server mode: all threads are daemon threads, do not let main stop
|
|
|
|
if (Settings.getBooleanProperty("davmail.server")) {
|
2009-06-29 16:14:38 -04:00
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread("Shutdown") {
|
2009-06-23 08:37:33 -04:00
|
|
|
@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"));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2006-12-14 10:14:18 -05:00
|
|
|
}
|
|
|
|
|
2009-07-23 04:25:28 -04:00
|
|
|
/**
|
|
|
|
* Start DavMail listeners.
|
|
|
|
*/
|
2006-12-14 10:14:18 -05:00
|
|
|
public static void start() {
|
2009-02-02 02:14:25 -05:00
|
|
|
// register custom SSL Socket factory
|
|
|
|
DavGatewaySSLProtocolSocketFactory.register();
|
2009-01-22 19:57:03 -05:00
|
|
|
|
2009-04-17 06:32:11 -04:00
|
|
|
// prepare HTTP connection pool
|
|
|
|
DavGatewayHttpClientFacade.start();
|
2008-12-03 12:18:51 -05:00
|
|
|
|
2009-09-21 17:34:13 -04:00
|
|
|
SERVER_LIST.clear();
|
2009-04-29 18:28:50 -04:00
|
|
|
|
2009-04-17 06:32:11 -04:00
|
|
|
int smtpPort = Settings.getIntProperty("davmail.smtpPort");
|
|
|
|
if (smtpPort != 0) {
|
2009-09-21 17:34:13 -04:00
|
|
|
SERVER_LIST.add(new SmtpServer(smtpPort));
|
2009-04-17 06:32:11 -04:00
|
|
|
}
|
|
|
|
int popPort = Settings.getIntProperty("davmail.popPort");
|
|
|
|
if (popPort != 0) {
|
2009-09-21 17:34:13 -04:00
|
|
|
SERVER_LIST.add(new PopServer(popPort));
|
2009-04-17 06:32:11 -04:00
|
|
|
}
|
|
|
|
int imapPort = Settings.getIntProperty("davmail.imapPort");
|
|
|
|
if (imapPort != 0) {
|
2009-09-21 17:34:13 -04:00
|
|
|
SERVER_LIST.add(new ImapServer(imapPort));
|
2009-04-17 06:32:11 -04:00
|
|
|
}
|
|
|
|
int caldavPort = Settings.getIntProperty("davmail.caldavPort");
|
|
|
|
if (caldavPort != 0) {
|
2009-09-21 17:34:13 -04:00
|
|
|
SERVER_LIST.add(new CaldavServer(caldavPort));
|
2009-04-17 06:32:11 -04:00
|
|
|
}
|
|
|
|
int ldapPort = Settings.getIntProperty("davmail.ldapPort");
|
|
|
|
if (ldapPort != 0) {
|
2009-09-21 17:34:13 -04:00
|
|
|
SERVER_LIST.add(new LdapServer(ldapPort));
|
2009-04-17 06:32:11 -04:00
|
|
|
}
|
|
|
|
|
2009-04-25 09:13:48 -04:00
|
|
|
BundleMessage.BundleMessageList messages = new BundleMessage.BundleMessageList();
|
|
|
|
BundleMessage.BundleMessageList errorMessages = new BundleMessage.BundleMessageList();
|
2009-09-21 17:34:13 -04:00
|
|
|
for (AbstractServer server : SERVER_LIST) {
|
2009-04-17 06:32:11 -04:00
|
|
|
try {
|
|
|
|
server.bind();
|
|
|
|
server.start();
|
2009-04-25 09:13:48 -04:00
|
|
|
messages.add(new BundleMessage("LOG_PROTOCOL_PORT", server.getProtocolName(), server.getPort()));
|
2009-05-12 19:14:36 -04:00
|
|
|
} catch (DavMailException e) {
|
|
|
|
errorMessages.add(e.getBundleMessage());
|
2009-04-17 06:32:11 -04:00
|
|
|
} catch (IOException e) {
|
2009-04-29 18:28:50 -04:00
|
|
|
errorMessages.add(new BundleMessage("LOG_SOCKET_BIND_FAILED", server.getProtocolName(), server.getPort()));
|
2008-11-03 11:07:13 -05:00
|
|
|
}
|
2009-04-17 06:32:11 -04:00
|
|
|
}
|
|
|
|
|
2009-11-15 08:01:12 -05:00
|
|
|
final String currentVersion = getCurrentVersion();
|
2009-05-21 06:33:00 -04:00
|
|
|
DavGatewayTray.info(new BundleMessage("LOG_DAVMAIL_GATEWAY_LISTENING",
|
2009-06-09 15:50:13 -04:00
|
|
|
currentVersion == null ? "" : currentVersion, messages));
|
2009-04-25 09:13:48 -04:00
|
|
|
if (!errorMessages.isEmpty()) {
|
2009-04-29 18:28:50 -04:00
|
|
|
DavGatewayTray.error(new BundleMessage("LOG_MESSAGE", errorMessages));
|
2009-04-17 06:32:11 -04:00
|
|
|
}
|
|
|
|
|
2009-11-15 08:01:12 -05:00
|
|
|
// check for new version in a separate thread
|
|
|
|
new Thread("CheckRelease") {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
String releasedVersion = getReleasedVersion();
|
|
|
|
if (currentVersion != null && releasedVersion != null && currentVersion.compareTo(releasedVersion) < 0) {
|
|
|
|
DavGatewayTray.info(new BundleMessage("LOG_NEW_VERSION_AVAILABLE", releasedVersion));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}.start();
|
2006-12-14 10:14:18 -05:00
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
|
2009-07-23 04:25:28 -04:00
|
|
|
/**
|
|
|
|
* Stop all listeners, shutdown connection pool and clear session cache.
|
|
|
|
*/
|
2009-04-17 06:32:11 -04:00
|
|
|
public static void stop() {
|
2009-09-21 17:34:13 -04:00
|
|
|
for (AbstractServer server : SERVER_LIST) {
|
2008-12-03 12:18:51 -05:00
|
|
|
server.close();
|
2008-10-31 13:12:30 -04:00
|
|
|
try {
|
2008-12-03 12:18:51 -05:00
|
|
|
server.join();
|
2008-12-03 06:38:35 -05:00
|
|
|
} catch (InterruptedException e) {
|
2009-04-17 06:32:11 -04:00
|
|
|
DavGatewayTray.warn(new BundleMessage("LOG_EXCEPTION_WAITING_SERVER_THREAD_DIE"), e);
|
2008-12-03 06:38:35 -05:00
|
|
|
}
|
|
|
|
}
|
2008-12-08 08:49:21 -05:00
|
|
|
// close pooled connections
|
|
|
|
DavGatewayHttpClientFacade.stop();
|
|
|
|
// clear session cache
|
|
|
|
ExchangeSessionFactory.reset();
|
2008-12-03 12:18:51 -05:00
|
|
|
}
|
|
|
|
|
2009-07-23 04:25:28 -04:00
|
|
|
/**
|
|
|
|
* Get current DavMail version.
|
|
|
|
* @return current version
|
|
|
|
*/
|
2008-11-03 11:07:13 -05:00
|
|
|
public static String getCurrentVersion() {
|
|
|
|
Package davmailPackage = DavGateway.class.getPackage();
|
|
|
|
return davmailPackage.getImplementationVersion();
|
|
|
|
}
|
|
|
|
|
2009-07-23 04:25:28 -04:00
|
|
|
/**
|
|
|
|
* Get latest released version from SourceForge.
|
|
|
|
* @return latest version
|
|
|
|
*/
|
2008-11-04 05:38:31 -05:00
|
|
|
public static String getReleasedVersion() {
|
2008-11-03 11:07:13 -05:00
|
|
|
String version = null;
|
2009-06-09 15:50:13 -04:00
|
|
|
if (!Settings.getBooleanProperty("davmail.disableUpdateCheck")) {
|
|
|
|
BufferedReader versionReader = null;
|
|
|
|
GetMethod getMethod = new GetMethod(HTTP_DAVMAIL_SOURCEFORGE_NET_VERSION_TXT);
|
|
|
|
try {
|
2010-03-24 05:59:23 -04:00
|
|
|
HttpClient httpClient = DavGatewayHttpClientFacade.getInstance(HTTP_DAVMAIL_SOURCEFORGE_NET_VERSION_TXT);
|
2009-06-09 15:50:13 -04:00
|
|
|
int status = httpClient.executeMethod(getMethod);
|
|
|
|
if (status == HttpStatus.SC_OK) {
|
|
|
|
versionReader = new BufferedReader(new InputStreamReader(getMethod.getResponseBodyAsStream()));
|
|
|
|
version = versionReader.readLine();
|
2009-11-15 08:01:12 -05:00
|
|
|
LOGGER.debug("DavMail released version: "+version);
|
2009-06-09 15:50:13 -04:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
DavGatewayTray.debug(new BundleMessage("LOG_UNABLE_TO_GET_RELEASED_VERSION"));
|
|
|
|
} finally {
|
|
|
|
if (versionReader != null) {
|
|
|
|
try {
|
|
|
|
versionReader.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
// ignore
|
|
|
|
}
|
2008-11-03 11:07:13 -05:00
|
|
|
}
|
2009-06-09 15:50:13 -04:00
|
|
|
getMethod.releaseConnection();
|
2008-11-03 11:07:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return version;
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|