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-04-29 18:30:59 -04:00
|
|
|
|
import org.apache.log4j.FileAppender;
|
|
|
|
|
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;
|
2009-04-29 18:30:59 -04:00
|
|
|
|
import java.io.File;
|
2008-11-26 19:56:28 -05:00
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
public class DavGateway {
|
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-04-16 17:52:17 -04:00
|
|
|
|
private DavGateway() {
|
2007-05-09 19:37:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2009-04-17 06:32:11 -04:00
|
|
|
|
private static final ArrayList<AbstractServer> serverList = new ArrayList<AbstractServer>();
|
2006-12-12 18:57:24 -05:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start the gateway, listen on sp<EFBFBD>cified 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
|
|
|
|
|
2009-04-29 18:30:59 -04:00
|
|
|
|
updateLogFilePath();
|
|
|
|
|
|
2006-12-14 10:14:18 -05:00
|
|
|
|
Settings.load();
|
|
|
|
|
DavGatewayTray.init();
|
|
|
|
|
|
|
|
|
|
start();
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-29 18:30:59 -04:00
|
|
|
|
public static void updateLogFilePath() {
|
|
|
|
|
// update log file path on Mac OS X
|
|
|
|
|
if (System.getProperty("os.name").toLowerCase().startsWith("mac os x")) {
|
|
|
|
|
String logFileDir = System.getProperty("user.home") + "/Library/Logs/DavMail";
|
|
|
|
|
Logger rootLogger = Logger.getRootLogger();
|
|
|
|
|
try {
|
|
|
|
|
File file = new File(logFileDir);
|
|
|
|
|
if (file.mkdirs()) {
|
|
|
|
|
((FileAppender) rootLogger.getAppender("FileAppender")).setFile(logFileDir + "/davmail.log", true, false, 8192);
|
|
|
|
|
} else {
|
|
|
|
|
DavGatewayTray.error(new BundleMessage("LOG_UNABLE_TO_CREATE_LOG_FILE_DIR"));
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
DavGatewayTray.error(new BundleMessage("LOG_UNABLE_TO_SET_LOG_FILE_PATH"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
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-04-23 10:54:06 -04:00
|
|
|
|
serverList.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) {
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-25 09:13:48 -04:00
|
|
|
|
BundleMessage.BundleMessageList messages = new BundleMessage.BundleMessageList();
|
|
|
|
|
BundleMessage.BundleMessageList errorMessages = new BundleMessage.BundleMessageList();
|
2009-04-17 06:32:11 -04:00
|
|
|
|
for (AbstractServer server : serverList) {
|
|
|
|
|
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-05-21 06:33:00 -04:00
|
|
|
|
String currentVersion = getCurrentVersion();
|
|
|
|
|
DavGatewayTray.info(new BundleMessage("LOG_DAVMAIL_GATEWAY_LISTENING",
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check for new version
|
|
|
|
|
String releasedVersion = getReleasedVersion();
|
|
|
|
|
if (currentVersion != null && releasedVersion != null && currentVersion.compareTo(releasedVersion) < 0) {
|
|
|
|
|
DavGatewayTray.info(new BundleMessage("LOG_NEW_VERSION_AVAILABLE", releasedVersion));
|
2007-08-02 09:33:45 -04:00
|
|
|
|
}
|
2006-12-14 10:14:18 -05:00
|
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
|
}
|
|
|
|
|
|
2009-04-17 06:32:11 -04:00
|
|
|
|
public static void stop() {
|
2009-04-29 18:28:50 -04:00
|
|
|
|
for (AbstractServer server : serverList) {
|
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
|
|
|
|
}
|
|
|
|
|
|
2008-11-03 11:07:13 -05:00
|
|
|
|
public static String getCurrentVersion() {
|
|
|
|
|
Package davmailPackage = DavGateway.class.getPackage();
|
|
|
|
|
return davmailPackage.getImplementationVersion();
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-04 05:38:31 -05:00
|
|
|
|
public static String getReleasedVersion() {
|
2008-11-03 11:07:13 -05:00
|
|
|
|
String version = null;
|
|
|
|
|
BufferedReader versionReader = null;
|
2008-11-30 13:05:36 -05:00
|
|
|
|
HttpClient httpClient = DavGatewayHttpClientFacade.getInstance();
|
2009-04-17 06:32:11 -04:00
|
|
|
|
GetMethod getMethod = new GetMethod(HTTP_DAVMAIL_SOURCEFORGE_NET_VERSION_TXT);
|
2008-11-03 11:07:13 -05:00
|
|
|
|
try {
|
2009-04-06 09:02:36 -04:00
|
|
|
|
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(1000);
|
2008-11-03 11:07:13 -05:00
|
|
|
|
int status = httpClient.executeMethod(getMethod);
|
|
|
|
|
if (status == HttpStatus.SC_OK) {
|
|
|
|
|
versionReader = new BufferedReader(new InputStreamReader(getMethod.getResponseBodyAsStream()));
|
|
|
|
|
version = versionReader.readLine();
|
|
|
|
|
}
|
2008-12-05 09:53:38 -05:00
|
|
|
|
} catch (IOException e) {
|
2009-04-17 06:32:11 -04:00
|
|
|
|
DavGatewayTray.debug(new BundleMessage("LOG_UNABLE_TO_GET_RELEASED_VERSION"));
|
2008-11-03 11:07:13 -05:00
|
|
|
|
} finally {
|
|
|
|
|
if (versionReader != null) {
|
|
|
|
|
try {
|
|
|
|
|
versionReader.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-11-03 20:47:10 -05:00
|
|
|
|
getMethod.releaseConnection();
|
2008-11-03 11:07:13 -05:00
|
|
|
|
}
|
|
|
|
|
return version;
|
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
|
}
|