diff --git a/src/java/davmail/DavGateway.java b/src/java/davmail/DavGateway.java index 8f21a846..7e871952 100644 --- a/src/java/davmail/DavGateway.java +++ b/src/java/davmail/DavGateway.java @@ -7,6 +7,7 @@ import davmail.ldap.LdapServer; import davmail.pop.PopServer; import davmail.smtp.SmtpServer; import davmail.tray.DavGatewayTray; +import davmail.exchange.ExchangeSessionFactory; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; @@ -33,6 +34,8 @@ public class DavGateway { * @param args command line parameter config file path */ public static void main(String[] args) { + // enable system proxy setup + System.setProperty("java.net.useSystemProxies","true"); if (args.length >= 1) { Settings.setConfigFilePath(args[0]); @@ -45,10 +48,9 @@ public class DavGateway { } public static void start() { - // first stop existing servers - DavGateway.stop(); - try { + // prepare HTTP connection pool + DavGatewayHttpClientFacade.start(); smtpServer = new SmtpServer(Settings.getIntProperty("davmail.smtpPort")); popServer = new PopServer(Settings.getIntProperty("davmail.popPort")); caldavServer = new CaldavServer(Settings.getIntProperty("davmail.caldavPort")); @@ -94,6 +96,10 @@ public class DavGateway { stopServer(popServer); stopServer(caldavServer); stopServer(ldapServer); + // close pooled connections + DavGatewayHttpClientFacade.stop(); + // clear session cache + ExchangeSessionFactory.reset(); } public static String getCurrentVersion() { diff --git a/src/java/davmail/exchange/ExchangeSessionFactory.java b/src/java/davmail/exchange/ExchangeSessionFactory.java index 7011b8b1..69016dfe 100644 --- a/src/java/davmail/exchange/ExchangeSessionFactory.java +++ b/src/java/davmail/exchange/ExchangeSessionFactory.java @@ -69,7 +69,7 @@ public class ExchangeSessionFactory { public void clean() { while (!isEmpty()) { - pop().close(); + pop(); } } } @@ -101,7 +101,6 @@ public class ExchangeSessionFactory { if (session != null && session.isExpired()) { ExchangeSession.LOGGER.debug("Session " + session + " expired"); - session.close(); session = null; } @@ -134,8 +133,7 @@ public class ExchangeSessionFactory { sessionStack = new ExchangeSessionStack(); poolMap.put(poolKey, sessionStack); } - // keep httpClient, but close HTTP connection - session.close(); + // keep httpClient sessionStack.push(session); ExchangeSession.LOGGER.debug("Pooled session: " + session); } @@ -218,4 +216,8 @@ public class ExchangeSessionFactory { } return up; } + + public static void reset() { + poolMap.clear(); + } } diff --git a/src/java/davmail/http/DavGatewayHttpClientFacade.java b/src/java/davmail/http/DavGatewayHttpClientFacade.java index e11b72ea..276fa87a 100644 --- a/src/java/davmail/http/DavGatewayHttpClientFacade.java +++ b/src/java/davmail/http/DavGatewayHttpClientFacade.java @@ -6,15 +6,18 @@ import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.GetMethod; import java.io.IOException; +import java.net.*; +import java.net.URI; +import java.util.List; /** * Create HttpClient instance according to DavGateway Settings */ public class DavGatewayHttpClientFacade { - static final MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager(); + static MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager; static { - multiThreadedHttpConnectionManager.setMaxConnectionsPerHost(10); + DavGatewayHttpClientFacade.start(); // force XML response with Internet Explorer header System.getProperties().setProperty("httpclient.useragent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"); } @@ -46,12 +49,39 @@ public class DavGatewayHttpClientFacade { httpClient.getState().setAuthenticationPreemptive(false); boolean enableProxy = Settings.getBooleanProperty("davmail.enableProxy"); + boolean systemProxy = Settings.getBooleanProperty("davmail.systemProxy"); String proxyHost = null; int proxyPort = 0; String proxyUser = null; String proxyPassword = null; - if (enableProxy) { + if (systemProxy) { + String url = Settings.getProperty("davmail.url"); + try { + List proxyList = ProxySelector.getDefault().select( + new URI(url)); + // get first returned proxy + if (proxyList.size() > 0) { + Proxy proxy = proxyList.get(0); + if (proxy.equals(Proxy.NO_PROXY)) { + DavGatewayTray.debug("System proxy : direct connection"); + } else { + InetSocketAddress addr = (InetSocketAddress) proxy.address(); + proxyHost = addr.getHostName(); + proxyPort = addr.getPort(); + // no way to get credentials from system proxy + proxyUser = Settings.getProperty("davmail.proxyUser"); + proxyPassword = Settings.getProperty("davmail.proxyPassword"); + + DavGatewayTray.debug("System proxy : " + proxyHost + ":" + proxyPort); + } + + } + } catch (URISyntaxException e) { + DavGatewayTray.error(e); + } + + } else if (enableProxy) { proxyHost = Settings.getProperty("davmail.proxyHost"); proxyPort = Settings.getIntProperty("davmail.proxyPort"); proxyUser = Settings.getProperty("davmail.proxyUser"); @@ -154,4 +184,18 @@ public class DavGatewayHttpClientFacade { // caller will need to release connection return method; } + + public static void stop() { + if (multiThreadedHttpConnectionManager != null) { + multiThreadedHttpConnectionManager.shutdown(); + multiThreadedHttpConnectionManager = null; + } + } + + public static void start() { + if (multiThreadedHttpConnectionManager == null) { + multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager(); + multiThreadedHttpConnectionManager.setMaxConnectionsPerHost(10); + } + } } diff --git a/src/java/davmail/ui/SettingsFrame.java b/src/java/davmail/ui/SettingsFrame.java index ea556b07..f24b133e 100644 --- a/src/java/davmail/ui/SettingsFrame.java +++ b/src/java/davmail/ui/SettingsFrame.java @@ -282,6 +282,7 @@ public class SettingsFrame extends JFrame { dispose(); Settings.save(); // restart listeners with new config + DavGateway.stop(); DavGateway.start(); } };