package com.onaro.sanscreen.client.error; import java.net.SocketException; import java.util.HashSet; import java.util.ResourceBundle; import java.util.Set; import javax.naming.NamingException; import javax.swing.JOptionPane; import com.onaro.commons.lang.OnaroExceptionUtils; import com.onaro.sanscreen.client.SessionsHelper; /** * Handles errors in the communication with the server. When such an error occurs, it notifies the * {@link SessionsHelper} the connection is lost to allow it to clear its caching of server related stuff. It provide * the user with the options to ignore the error (and try again later) or to exit the application. */ public class ServerConnectionHandler extends MessageWindowHandler { /** * Gets the mesaages and titles from the resources. Set the options to "exit" and "ignore". * * @param bundle * locale dependant resources */ public ServerConnectionHandler(ResourceBundle bundle) { super(bundle.getString("server.connection.title"), bundle.getString("server.connection.message"), //$NON-NLS-1$ //$NON-NLS-2$ JOptionPane.ERROR_MESSAGE); addActionIgnore(); addActionExit(); addActionShowException(); } /** * Handle the error if the exception is any of: * * When checking for a match, the chain of causes is also searched. If an error is handled, make sure to flush the * {@link SessionsHelper}'s cache. * * @param shortDescription * an optional short description of the error * @param exception * only the above specified errors are handled * * @return true if the error was handled */ @Override public boolean handle(String shortDescription, Throwable exception) { // possible known exception causes Set> knownExceptions = new HashSet>(); knownExceptions.add(SocketException.class); knownExceptions.add(NamingException.class); for (Class knownException : knownExceptions) { if (OnaroExceptionUtils.isCause(exception, knownException)) { return super.handle(shortDescription, exception); } } return false; } }