001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018:
019: package org.columba.core.gui.exception;
020:
021: import java.io.IOException;
022: import java.io.PrintWriter;
023: import java.io.StringWriter;
024: import java.net.ConnectException;
025: import java.net.NoRouteToHostException;
026: import java.net.PortUnreachableException;
027: import java.net.SocketException;
028: import java.net.SocketTimeoutException;
029: import java.net.UnknownHostException;
030: import java.text.MessageFormat;
031: import java.util.logging.Logger;
032:
033: import org.columba.api.exception.IExceptionListener;
034: import org.columba.core.connectionstate.ConnectionStateImpl;
035: import org.columba.core.gui.dialog.ErrorDialog;
036: import org.columba.core.resourceloader.GlobalResourceLoader;
037: import org.columba.ristretto.imap.IMAPDisconnectedException;
038: import org.columba.ristretto.imap.IMAPException;
039: import org.columba.ristretto.io.ConnectionDroppedException;
040:
041: import sun.net.ConnectionResetException;
042:
043: /**
044: * Handles all exceptions catched by Worker.construct(). Opens error dialogs.
045: *
046: * @author fdietz
047: */
048: public class ExceptionHandler implements IExceptionListener {
049: private static final String RESOURCE_PATH = "org.columba.core.i18n.dialog";
050:
051: /** JDK 1.4+ logging framework logger, used for logging. */
052: private static final Logger LOG = Logger
053: .getLogger("org.columba.api.command");
054:
055: /**
056: * Handle all kinds of exceptions.
057: *
058: * @param e
059: * exception to process
060: */
061: public void processException(Exception e) {
062: // Print the stacktrace to our log file.
063: StringWriter error = new StringWriter();
064: e.printStackTrace(new PrintWriter(error));
065: LOG.severe(error.toString());
066:
067: if (e instanceof SocketException) {
068: processSocketException((SocketException) e);
069: ConnectionStateImpl.getInstance().setOnline(false);
070: } else if (e instanceof IOException) {
071: processIOException((IOException) e);
072: } else if (e instanceof IMAPException) {
073: processIMAPExcpetion((IMAPException) e);
074: } else {
075: // show error dialog, with exception message and stack-trace
076: // -> dialog also provides a button for the user to easily
077: // -> report a bug
078: showErrorDialog(e.getMessage(), e);
079: }
080: }
081:
082: /**
083: * @param exception
084: */
085: private void processIMAPExcpetion(IMAPException exception) {
086: String errorMessage = "";
087: String serverResponse = "";
088:
089: if (exception.getResponse() != null) {
090: serverResponse = ": "
091: + exception.getResponse().getResponseMessage();
092: }
093:
094: if (exception instanceof IMAPDisconnectedException) {
095: errorMessage = GlobalResourceLoader.getString(
096: RESOURCE_PATH, "error", "imap_disconnected_error");
097: } else {
098: errorMessage = GlobalResourceLoader.getString(
099: RESOURCE_PATH, "error", "imap_error")
100: + serverResponse;
101: }
102:
103: showErrorDialog(errorMessage, exception);
104: }
105:
106: /**
107: * Handle all java.net.SocketException
108: *
109: * @param e
110: * a socket exception
111: */
112: private void processSocketException(SocketException e) {
113: String errorMessage = "";
114:
115: if (e instanceof ConnectException) {
116: errorMessage = GlobalResourceLoader.getString(
117: RESOURCE_PATH, "error", "connect_error");
118: } else if (e instanceof NoRouteToHostException) {
119: errorMessage = GlobalResourceLoader.getString(
120: RESOURCE_PATH, "error", "no_route_to_host_error");
121: } else if (e instanceof PortUnreachableException) {
122: errorMessage = GlobalResourceLoader.getString(
123: RESOURCE_PATH, "error", "port_unreachable_error");
124: } else if (e instanceof ConnectionResetException) {
125: errorMessage = GlobalResourceLoader.getString(
126: RESOURCE_PATH, "error", "connection_reset");
127: } else {
128: errorMessage = GlobalResourceLoader.getString(
129: RESOURCE_PATH, "error", "generic_socket_error");
130: }
131:
132: showErrorDialog(errorMessage, e);
133: }
134:
135: /**
136: * Handle all java.io.IOExceptions
137: *
138: * @param e
139: * io exception to process
140: */
141: private void processIOException(IOException e) {
142: String errorMessage = e.getMessage();
143:
144: if (e instanceof SocketTimeoutException) {
145: errorMessage = GlobalResourceLoader.getString(
146: RESOURCE_PATH, "error", "socket_timeout_error");
147: } else if (e instanceof UnknownHostException) {
148: errorMessage = MessageFormat.format(GlobalResourceLoader
149: .getString(RESOURCE_PATH, "error",
150: "unknown_host_error"), new Object[] { e
151: .getMessage() });
152: } else if (e instanceof ConnectionDroppedException) {
153: errorMessage = GlobalResourceLoader.getString(
154: RESOURCE_PATH, "error", "connection_dropped_error");
155: } else {
156: errorMessage = GlobalResourceLoader.getString(
157: RESOURCE_PATH, "error", "generic_io_error");
158: }
159:
160: showErrorDialog(errorMessage, e);
161: }
162:
163: /**
164: * Show error dialog.
165: *
166: * @param errorMessage
167: * human-readable error message
168: * @param e
169: * exception to process
170: */
171: private void showErrorDialog(String details, Exception e) {
172:
173: if (details == null)
174: details = e.toString();
175:
176: ErrorDialog.createDialog(details, e);
177:
178: }
179:
180: public void exceptionOccured(Exception e) {
181: processException(e);
182: }
183: }
|