1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 19:22:22 -05:00

Fix connection pool handling on restart and prepare system proxy setting

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@219 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2008-12-08 13:49:21 +00:00
parent 146c29c136
commit fa1400a59c
4 changed files with 63 additions and 10 deletions

View File

@ -7,6 +7,7 @@ import davmail.ldap.LdapServer;
import davmail.pop.PopServer; import davmail.pop.PopServer;
import davmail.smtp.SmtpServer; import davmail.smtp.SmtpServer;
import davmail.tray.DavGatewayTray; import davmail.tray.DavGatewayTray;
import davmail.exchange.ExchangeSessionFactory;
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.GetMethod;
@ -33,6 +34,8 @@ public class DavGateway {
* @param args command line parameter config file path * @param args command line parameter config file path
*/ */
public static void main(String[] args) { public static void main(String[] args) {
// enable system proxy setup
System.setProperty("java.net.useSystemProxies","true");
if (args.length >= 1) { if (args.length >= 1) {
Settings.setConfigFilePath(args[0]); Settings.setConfigFilePath(args[0]);
@ -45,10 +48,9 @@ public class DavGateway {
} }
public static void start() { public static void start() {
// first stop existing servers
DavGateway.stop();
try { try {
// prepare HTTP connection pool
DavGatewayHttpClientFacade.start();
smtpServer = new SmtpServer(Settings.getIntProperty("davmail.smtpPort")); smtpServer = new SmtpServer(Settings.getIntProperty("davmail.smtpPort"));
popServer = new PopServer(Settings.getIntProperty("davmail.popPort")); popServer = new PopServer(Settings.getIntProperty("davmail.popPort"));
caldavServer = new CaldavServer(Settings.getIntProperty("davmail.caldavPort")); caldavServer = new CaldavServer(Settings.getIntProperty("davmail.caldavPort"));
@ -94,6 +96,10 @@ public class DavGateway {
stopServer(popServer); stopServer(popServer);
stopServer(caldavServer); stopServer(caldavServer);
stopServer(ldapServer); stopServer(ldapServer);
// close pooled connections
DavGatewayHttpClientFacade.stop();
// clear session cache
ExchangeSessionFactory.reset();
} }
public static String getCurrentVersion() { public static String getCurrentVersion() {

View File

@ -69,7 +69,7 @@ public class ExchangeSessionFactory {
public void clean() { public void clean() {
while (!isEmpty()) { while (!isEmpty()) {
pop().close(); pop();
} }
} }
} }
@ -101,7 +101,6 @@ public class ExchangeSessionFactory {
if (session != null && session.isExpired()) { if (session != null && session.isExpired()) {
ExchangeSession.LOGGER.debug("Session " + session + " expired"); ExchangeSession.LOGGER.debug("Session " + session + " expired");
session.close();
session = null; session = null;
} }
@ -134,8 +133,7 @@ public class ExchangeSessionFactory {
sessionStack = new ExchangeSessionStack(); sessionStack = new ExchangeSessionStack();
poolMap.put(poolKey, sessionStack); poolMap.put(poolKey, sessionStack);
} }
// keep httpClient, but close HTTP connection // keep httpClient
session.close();
sessionStack.push(session); sessionStack.push(session);
ExchangeSession.LOGGER.debug("Pooled session: " + session); ExchangeSession.LOGGER.debug("Pooled session: " + session);
} }
@ -218,4 +216,8 @@ public class ExchangeSessionFactory {
} }
return up; return up;
} }
public static void reset() {
poolMap.clear();
}
} }

View File

@ -6,15 +6,18 @@ import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.GetMethod;
import java.io.IOException; import java.io.IOException;
import java.net.*;
import java.net.URI;
import java.util.List;
/** /**
* Create HttpClient instance according to DavGateway Settings * Create HttpClient instance according to DavGateway Settings
*/ */
public class DavGatewayHttpClientFacade { public class DavGatewayHttpClientFacade {
static final MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager(); static MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager;
static { static {
multiThreadedHttpConnectionManager.setMaxConnectionsPerHost(10); DavGatewayHttpClientFacade.start();
// force XML response with Internet Explorer header // 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)"); 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); httpClient.getState().setAuthenticationPreemptive(false);
boolean enableProxy = Settings.getBooleanProperty("davmail.enableProxy"); boolean enableProxy = Settings.getBooleanProperty("davmail.enableProxy");
boolean systemProxy = Settings.getBooleanProperty("davmail.systemProxy");
String proxyHost = null; String proxyHost = null;
int proxyPort = 0; int proxyPort = 0;
String proxyUser = null; String proxyUser = null;
String proxyPassword = null; String proxyPassword = null;
if (enableProxy) { if (systemProxy) {
String url = Settings.getProperty("davmail.url");
try {
List<Proxy> 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"); proxyHost = Settings.getProperty("davmail.proxyHost");
proxyPort = Settings.getIntProperty("davmail.proxyPort"); proxyPort = Settings.getIntProperty("davmail.proxyPort");
proxyUser = Settings.getProperty("davmail.proxyUser"); proxyUser = Settings.getProperty("davmail.proxyUser");
@ -154,4 +184,18 @@ public class DavGatewayHttpClientFacade {
// caller will need to release connection // caller will need to release connection
return method; 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);
}
}
} }

View File

@ -282,6 +282,7 @@ public class SettingsFrame extends JFrame {
dispose(); dispose();
Settings.save(); Settings.save();
// restart listeners with new config // restart listeners with new config
DavGateway.stop();
DavGateway.start(); DavGateway.start();
} }
}; };