Remember previous checkConfig status to detect network down

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@560 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-05-10 23:10:48 +00:00
parent 71323e707c
commit da56920821
2 changed files with 32 additions and 20 deletions

View File

@ -1,7 +1,7 @@
package davmail.exchange; package davmail.exchange;
import davmail.Settings;
import davmail.BundleMessage; import davmail.BundleMessage;
import davmail.Settings;
import davmail.exception.DavMailException; import davmail.exception.DavMailException;
import davmail.http.DavGatewayHttpClientFacade; import davmail.http.DavGatewayHttpClientFacade;
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpClient;
@ -11,9 +11,12 @@ import org.apache.commons.httpclient.methods.GetMethod;
import java.io.IOException; import java.io.IOException;
import java.net.NetworkInterface; import java.net.NetworkInterface;
import java.net.NoRouteToHostException;
import java.net.SocketException; import java.net.SocketException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.*; import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
/** /**
* Create ExchangeSession instances. * Create ExchangeSession instances.
@ -21,6 +24,7 @@ import java.util.*;
public final class ExchangeSessionFactory { public final class ExchangeSessionFactory {
private static final Object LOCK = new Object(); private static final Object LOCK = new Object();
private static final Map<PoolKey, ExchangeSession> poolMap = new HashMap<PoolKey, ExchangeSession>(); private static final Map<PoolKey, ExchangeSession> poolMap = new HashMap<PoolKey, ExchangeSession>();
private static boolean configChecked;
static class PoolKey { static class PoolKey {
public final String url; public final String url;
@ -60,11 +64,11 @@ public final class ExchangeSessionFactory {
* @throws IOException on error * @throws IOException on error
*/ */
public static ExchangeSession getInstance(String userName, String password) throws IOException { public static ExchangeSession getInstance(String userName, String password) throws IOException {
ExchangeSession session = null;
try { try {
String baseUrl = Settings.getProperty("davmail.url"); String baseUrl = Settings.getProperty("davmail.url");
PoolKey poolKey = new PoolKey(baseUrl, userName, password); PoolKey poolKey = new PoolKey(baseUrl, userName, password);
ExchangeSession session;
synchronized (LOCK) { synchronized (LOCK) {
session = poolMap.get(poolKey); session = poolMap.get(poolKey);
} }
@ -89,14 +93,14 @@ public final class ExchangeSessionFactory {
synchronized (LOCK) { synchronized (LOCK) {
poolMap.put(poolKey, session); poolMap.put(poolKey, session);
} }
return session; // session opened, future failure will mean network down
} catch (IOException e) { configChecked = true;
if (checkNetwork()) { } catch (UnknownHostException exc) {
throw e; handleNetworkDown(exc);
} else { } catch (NoRouteToHostException exc) {
throw new NetworkDownException("EXCEPTION_NETWORK_DOWN"); handleNetworkDown(exc);
}
} }
return session;
} }
public static void checkConfig() throws IOException { public static void checkConfig() throws IOException {
@ -113,17 +117,13 @@ public final class ExchangeSessionFactory {
&& status != HttpStatus.SC_MOVED_TEMPORARILY && status != HttpStatus.SC_MOVED_PERMANENTLY) { && status != HttpStatus.SC_MOVED_TEMPORARILY && status != HttpStatus.SC_MOVED_PERMANENTLY) {
throw new DavMailException("EXCEPTION_CONNECTION_FAILED", url, status); throw new DavMailException("EXCEPTION_CONNECTION_FAILED", url, status);
} }
// session opened, future failure will mean network down
configChecked = true;
} catch (UnknownHostException exc) { } catch (UnknownHostException exc) {
if (checkNetwork()) { handleNetworkDown(exc);
BundleMessage message = new BundleMessage("EXCEPTION_UNKNOWN_HOST", exc.getMessage()); } catch (NoRouteToHostException exc) {
ExchangeSession.LOGGER.error(message); handleNetworkDown(exc);
throw new DavMailException("EXCEPTION_DAVMAIL_CONFIGURATION", message);
} else {
ExchangeSession.LOGGER.error(BundleMessage.formatLog("EXCEPTION_NETWORK_DOWN"));
throw new NetworkDownException("EXCEPTION_NETWORK_DOWN");
}
} catch (NetworkDownException exc) { } catch (NetworkDownException exc) {
throw exc; throw exc;
} catch (Exception exc) { } catch (Exception exc) {
@ -135,6 +135,17 @@ public final class ExchangeSessionFactory {
} }
private static void handleNetworkDown(Exception exc) throws DavMailException {
if (!checkNetwork() || configChecked) {
ExchangeSession.LOGGER.error(BundleMessage.formatLog("EXCEPTION_NETWORK_DOWN"));
throw new NetworkDownException("EXCEPTION_NETWORK_DOWN");
} else {
BundleMessage message = new BundleMessage("EXCEPTION_UNKNOWN_HOST", exc.getMessage());
ExchangeSession.LOGGER.error(message);
throw new DavMailException("EXCEPTION_DAVMAIL_CONFIGURATION", message);
}
}
/** /**
* Check if at least one network interface is up and active (i.e. has an address) * Check if at least one network interface is up and active (i.e. has an address)
* *
@ -160,6 +171,7 @@ public final class ExchangeSessionFactory {
} }
public static void reset() { public static void reset() {
configChecked=false;
poolMap.clear(); poolMap.clear();
} }
} }

View File

@ -57,7 +57,7 @@ public class DavGatewayTray {
protected static void displayMessage(BundleMessage message, Exception e, Priority priority) { protected static void displayMessage(BundleMessage message, Exception e, Priority priority) {
LOGGER.log(priority, BundleMessage.getExceptionLogMessage(message, e), e); LOGGER.log(priority, BundleMessage.getExceptionLogMessage(message, e), e);
if (davGatewayTray != null if (davGatewayTray != null
&& (!(e instanceof NetworkDownException) || isActive())) { && (!(e instanceof NetworkDownException))) {
davGatewayTray.displayMessage(BundleMessage.getExceptionMessage(message, e), priority); davGatewayTray.displayMessage(BundleMessage.getExceptionMessage(message, e), priority);
} }
if (davGatewayTray != null && e instanceof NetworkDownException) { if (davGatewayTray != null && e instanceof NetworkDownException) {