diff --git a/src/java/davmail/exchange/ExchangeSessionFactory.java b/src/java/davmail/exchange/ExchangeSessionFactory.java index 72baa643..a3904049 100644 --- a/src/java/davmail/exchange/ExchangeSessionFactory.java +++ b/src/java/davmail/exchange/ExchangeSessionFactory.java @@ -34,7 +34,7 @@ public class ExchangeSessionFactory { if (checkNetwork()) { throw e; } else { - throw new IOException("All network interfaces down !"); + throw new NetworkDownException("All network interfaces down !"); } } } @@ -64,12 +64,16 @@ public class ExchangeSessionFactory { String message = "DavMail configuration exception: \n"; if (checkNetwork()) { message += "Unknown host " + exc.getMessage(); + ExchangeSession.LOGGER.error(message, exc); + throw new IOException(message); } else { message += "All network interfaces down !"; + ExchangeSession.LOGGER.error(message, exc); + throw new NetworkDownException(message); } - ExchangeSession.LOGGER.error(message, exc); - throw new IOException(message); + } catch (NetworkDownException exc) { + throw exc; } catch (Exception exc) { ExchangeSession.LOGGER.error("DavMail configuration exception: \n" + exc.getMessage(), exc); throw new IOException("DavMail configuration exception: \n" + exc.getMessage()); diff --git a/src/java/davmail/exchange/NetworkDownException.java b/src/java/davmail/exchange/NetworkDownException.java new file mode 100644 index 00000000..df0f8e29 --- /dev/null +++ b/src/java/davmail/exchange/NetworkDownException.java @@ -0,0 +1,12 @@ +package davmail.exchange; + +import java.io.IOException; + +/** + * Custom exception to mark network down case. + */ +public class NetworkDownException extends IOException { + public NetworkDownException(String message) { + super(message); + } +} diff --git a/src/java/davmail/imap/ImapConnection.java b/src/java/davmail/imap/ImapConnection.java index 8bbac62e..f9f9078b 100644 --- a/src/java/davmail/imap/ImapConnection.java +++ b/src/java/davmail/imap/ImapConnection.java @@ -56,7 +56,7 @@ public class ImapConnection extends AbstractConnection { sendClient(commandId + " OK Authenticated"); state = AUTHENTICATED; } catch (Exception e) { - DavGatewayTray.error(e.getMessage()); + DavGatewayTray.error(e); sendClient(commandId + " NO LOGIN failed"); state = INITIAL; } diff --git a/src/java/davmail/pop/PopConnection.java b/src/java/davmail/pop/PopConnection.java index c1cde79f..7a0aed07 100644 --- a/src/java/davmail/pop/PopConnection.java +++ b/src/java/davmail/pop/PopConnection.java @@ -75,7 +75,6 @@ public class PopConnection extends AbstractConnection { break; } - tokens = new StringTokenizer(line); if (tokens.hasMoreTokens()) { String command = tokens.nextToken(); @@ -117,12 +116,8 @@ public class PopConnection extends AbstractConnection { // can not send error to client after a socket exception DavGatewayTray.warn("Client closed connection ", e); } catch (Exception e) { - String message = e.getMessage(); - if (message == null) { - message = "Authentication failed: "+e.toString(); - } - DavGatewayTray.error(message); - sendERR(message); + DavGatewayTray.error(e); + sendERR(e); } } } else if ("CAPA".equalsIgnoreCase(command)) { @@ -223,7 +218,7 @@ public class PopConnection extends AbstractConnection { os.flush(); } } catch (IOException e) { - DavGatewayTray.error(e.getMessage()); + DavGatewayTray.error(e); try { sendERR(e.getMessage()); } catch (IOException e2) { @@ -239,6 +234,14 @@ public class PopConnection extends AbstractConnection { sendClient("+OK ", message); } + public void sendERR(Exception e) throws IOException { + String message = e.getMessage(); + if (message == null) { + message = e.toString(); + } + sendERR(message); + } + public void sendERR(String message) throws IOException { sendClient("-ERR ", message.replaceAll("\\n", " ")); } diff --git a/src/java/davmail/smtp/SmtpConnection.java b/src/java/davmail/smtp/SmtpConnection.java index 26b581fe..b9dd9139 100644 --- a/src/java/davmail/smtp/SmtpConnection.java +++ b/src/java/davmail/smtp/SmtpConnection.java @@ -3,8 +3,6 @@ package davmail.smtp; import davmail.AbstractConnection; import davmail.exchange.ExchangeSessionFactory; import davmail.tray.DavGatewayTray; -import sun.misc.BASE64Decoder; -import sun.misc.BASE64Encoder; import java.io.IOException; import java.net.Socket; @@ -21,7 +19,7 @@ public class SmtpConnection extends AbstractConnection { protected static final int RECIPIENT = 3; protected static final int MAILDATA = 4; protected static final int LOGIN = 5; - protected static final int PASSWORD = 6; + public static final int PASSWORD = 6; // Initialize the streams and start the thread public SmtpConnection(Socket clientSocket) { @@ -147,11 +145,11 @@ public class SmtpConnection extends AbstractConnection { sendClient("235 OK Authenticated"); state = AUTHENTICATED; } catch (Exception e) { + DavGatewayTray.error(e); String message = e.getMessage(); if (message == null) { message = e.toString(); } - DavGatewayTray.error(message); message = message.replaceAll("\\n", " "); sendClient("554 Authenticated failed " + message); state = INITIAL; @@ -159,16 +157,6 @@ public class SmtpConnection extends AbstractConnection { } - protected String base64Encode(String value) { - BASE64Encoder encoder = new BASE64Encoder(); - return encoder.encode(value.getBytes()); - } - - protected String base64Decode(String value) throws IOException { - BASE64Decoder decoder = new BASE64Decoder(); - return new String(decoder.decodeBuffer(value)); - } - /** * Decode SMTP credentials * diff --git a/src/java/davmail/tray/AwtGatewayTray.java b/src/java/davmail/tray/AwtGatewayTray.java index 259dd574..d38fa2db 100644 --- a/src/java/davmail/tray/AwtGatewayTray.java +++ b/src/java/davmail/tray/AwtGatewayTray.java @@ -10,12 +10,9 @@ import org.apache.log4j.lf5.LogLevel; import org.apache.log4j.lf5.viewer.LogBrokerMonitor; import javax.swing.*; -import javax.imageio.ImageIO; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.net.URL; -import java.io.IOException; /** * Tray icon handler based on java 1.6 @@ -24,35 +21,53 @@ public class AwtGatewayTray implements DavGatewayTrayInterface { protected AwtGatewayTray() { } - // LOCK for synchronized block - protected static final Object LOCK = new Object(); - private static TrayIcon trayIcon = null; private static Image image = null; private static Image image2 = null; + private static Image inactiveImage = null; + private boolean isActive = true; public Image getFrameIcon() { return image; } public void switchIcon() { - synchronized (LOCK) { - if (trayIcon.getImage() == image) { - trayIcon.setImage(image2); - } else { - trayIcon.setImage(image); + isActive = true; + SwingUtilities.invokeLater(new Runnable() { + public void run() { + if (trayIcon.getImage() == image) { + trayIcon.setImage(image2); + } else { + trayIcon.setImage(image); + } } - } + }); } public void resetIcon() { - synchronized (LOCK) { - trayIcon.setImage(image); - } + SwingUtilities.invokeLater(new Runnable() { + public void run() { + trayIcon.setImage(image); + } + }); } - public void displayMessage(String message, Priority priority) { - synchronized (LOCK) { + public void inactiveIcon() { + isActive = false; + SwingUtilities.invokeLater(new Runnable() { + public void run() { + trayIcon.setImage(inactiveImage); + } + }); + } + + public boolean isActive() { + return isActive; + } + + public void displayMessage(final String message, final Priority priority) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { if (trayIcon != null) { TrayIcon.MessageType messageType = null; if (priority == Priority.INFO) { @@ -67,8 +82,8 @@ public class AwtGatewayTray implements DavGatewayTrayInterface { } trayIcon.setToolTip("DavMail gateway \n" + message); } - } - + } + }); } public void init() { @@ -79,7 +94,9 @@ public class AwtGatewayTray implements DavGatewayTrayInterface { }); } - public void createAndShowGUI() { + + + protected void createAndShowGUI() { // set native look and feel try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); @@ -89,21 +106,9 @@ public class AwtGatewayTray implements DavGatewayTrayInterface { // get the SystemTray instance SystemTray tray = SystemTray.getSystemTray(); - // load an image - ClassLoader classloader = DavGatewayTray.class.getClassLoader(); - try { - URL imageUrl = classloader.getResource("tray.png"); - image = ImageIO.read(imageUrl); - } catch (IOException e) { - DavGatewayTray.warn("Unable to load image", e); - } - - try { - URL imageUrl2 = classloader.getResource("tray2.png"); - image2 = ImageIO.read(imageUrl2); - } catch (IOException e) { - DavGatewayTray.warn("Unable to load image", e); - } + image = DavGatewayTray.loadImage("tray.png"); + image2 = DavGatewayTray.loadImage("tray.png"); + inactiveImage = DavGatewayTray.loadImage("trayinactive.png"); // create a popup menu PopupMenu popup = new PopupMenu(); diff --git a/src/java/davmail/tray/DavGatewayTray.java b/src/java/davmail/tray/DavGatewayTray.java index 9388e7bc..a8f99f4b 100644 --- a/src/java/davmail/tray/DavGatewayTray.java +++ b/src/java/davmail/tray/DavGatewayTray.java @@ -1,10 +1,14 @@ package davmail.tray; import davmail.Settings; +import davmail.exchange.NetworkDownException; import org.apache.log4j.Logger; import org.apache.log4j.Priority; +import javax.imageio.ImageIO; import java.awt.*; +import java.io.IOException; +import java.net.URL; /** @@ -33,11 +37,21 @@ public class DavGatewayTray { } public static void resetIcon() { - if (davGatewayTray != null) { + if (davGatewayTray != null && isActive()) { davGatewayTray.resetIcon(); } } + public static void inactiveIcon() { + if (davGatewayTray != null) { + davGatewayTray.inactiveIcon(); + } + } + + public static boolean isActive() { + return davGatewayTray == null || davGatewayTray.isActive(); + } + protected static void displayMessage(String message, Priority priority) { LOGGER.log(priority, message); if (davGatewayTray != null) { @@ -47,8 +61,21 @@ public class DavGatewayTray { protected static void displayMessage(String message, Exception e, Priority priority) { LOGGER.log(priority, message, e); - if (davGatewayTray != null) { - davGatewayTray.displayMessage(message + " " + e + " " + e.getMessage(), priority); + if (davGatewayTray != null + && (!(e instanceof NetworkDownException) || isActive())) { + StringBuilder buffer = new StringBuilder(); + if (message != null) { + buffer.append(message).append(" "); + } + if (e.getMessage() != null) { + buffer.append(e.getMessage()); + } else { + buffer.append(e.toString()); + } + davGatewayTray.displayMessage(buffer.toString(), priority); + } + if (davGatewayTray != null && e instanceof NetworkDownException) { + davGatewayTray.inactiveIcon(); } } @@ -68,6 +95,10 @@ public class DavGatewayTray { displayMessage(message, Priority.ERROR); } + public static void error(Exception e) { + displayMessage(null, e, Priority.ERROR); + } + public static void debug(String message, Exception e) { displayMessage(message, e, Priority.DEBUG); } @@ -113,4 +144,22 @@ public class DavGatewayTray { } } } + + /** + * Load image with current class loader. + * + * @param fileName image resource file name + * @return image + */ + public static Image loadImage(String fileName) { + Image result = null; + try { + ClassLoader classloader = DavGatewayTray.class.getClassLoader(); + URL imageUrl = classloader.getResource(fileName); + result = ImageIO.read(imageUrl); + } catch (IOException e) { + DavGatewayTray.warn("Unable to load image", e); + } + return result; + } } diff --git a/src/java/davmail/tray/DavGatewayTrayInterface.java b/src/java/davmail/tray/DavGatewayTrayInterface.java index 3e62ff9a..141af376 100644 --- a/src/java/davmail/tray/DavGatewayTrayInterface.java +++ b/src/java/davmail/tray/DavGatewayTrayInterface.java @@ -10,6 +10,8 @@ import java.awt.*; public interface DavGatewayTrayInterface { void switchIcon(); void resetIcon(); + void inactiveIcon(); + boolean isActive(); Image getFrameIcon(); void displayMessage(String message, Priority priority); void init(); diff --git a/src/java/davmail/tray/SwtGatewayTray.java b/src/java/davmail/tray/SwtGatewayTray.java index 98ad0b64..2348366b 100644 --- a/src/java/davmail/tray/SwtGatewayTray.java +++ b/src/java/davmail/tray/SwtGatewayTray.java @@ -10,17 +10,8 @@ import org.apache.log4j.lf5.LogLevel; import org.apache.log4j.lf5.viewer.LogBrokerMonitor; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.widgets.Event; -import org.eclipse.swt.widgets.Listener; -import org.eclipse.swt.widgets.Menu; -import org.eclipse.swt.widgets.MenuItem; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.swt.widgets.ToolTip; -import org.eclipse.swt.widgets.Tray; -import org.eclipse.swt.widgets.TrayItem; +import org.eclipse.swt.widgets.*; -import javax.imageio.ImageIO; import javax.swing.*; import java.io.IOException; import java.net.URL; @@ -32,71 +23,96 @@ public class SwtGatewayTray implements DavGatewayTrayInterface { protected SwtGatewayTray() { } - // LOCK for synchronized block - protected static final Object LOCK = new Object(); - private static TrayItem trayItem = null; private static java.awt.Image awtImage = null; private static Image image = null; private static Image image2 = null; + private static Image inactiveImage = null; private static Display display; private static Shell shell; + private boolean isActive = true; public java.awt.Image getFrameIcon() { return awtImage; } public void switchIcon() { - display.syncExec( - new Runnable() { - public void run() { - if (trayItem.getImage() == image) { - trayItem.setImage(image2); - } else { - trayItem.setImage(image); - } - } - }); + isActive = true; + display.syncExec(new Runnable() { + public void run() { + if (trayItem.getImage() == image) { + trayItem.setImage(image2); + } else { + trayItem.setImage(image); + } + } + }); } public void resetIcon() { - display.syncExec( - new Runnable() { - public void run() { - trayItem.setImage(image); - } - }); + display.syncExec(new Runnable() { + public void run() { + trayItem.setImage(image); + } + }); + } + + public void inactiveIcon() { + isActive = false; + display.syncExec(new Runnable() { + public void run() { + trayItem.setImage(inactiveImage); + } + }); + } + + public boolean isActive() { + return isActive; } public void displayMessage(final String message, final Priority priority) { - synchronized (LOCK) { - if (trayItem != null) { - display.asyncExec( - new Runnable() { - public void run() { - int messageType = 0; - if (priority == Priority.INFO) { - messageType = SWT.ICON_INFORMATION; - } else if (priority == Priority.WARN) { - messageType = SWT.ICON_WARNING; - } else if (priority == Priority.ERROR) { - messageType = SWT.ICON_ERROR; - } - if (messageType == 0) { - trayItem.setToolTipText("DavMail gateway \n" + message); - } else { - final ToolTip toolTip = new ToolTip(shell, SWT.BALLOON | messageType); - toolTip.setText("DavMail gateway"); - toolTip.setMessage(message); - trayItem.setToolTip(toolTip); - toolTip.setVisible(true); - } - } - }); - } + if (trayItem != null) { + display.asyncExec(new Runnable() { + public void run() { + int messageType = 0; + if (priority == Priority.INFO) { + messageType = SWT.ICON_INFORMATION; + } else if (priority == Priority.WARN) { + messageType = SWT.ICON_WARNING; + } else if (priority == Priority.ERROR) { + messageType = SWT.ICON_ERROR; + } + if (messageType == 0) { + trayItem.setToolTipText("DavMail gateway \n" + message); + } else { + final ToolTip toolTip = new ToolTip(shell, SWT.BALLOON | messageType); + toolTip.setText("DavMail gateway"); + toolTip.setMessage(message); + trayItem.setToolTip(toolTip); + toolTip.setVisible(true); + } + } + }); } + } + /** + * Load image with current class loader. + * + * @param fileName image resource file name + * @return image + */ + public static Image loadSwtImage(String fileName) { + Image result = null; + try { + ClassLoader classloader = DavGatewayTray.class.getClassLoader(); + URL imageUrl = classloader.getResource(fileName); + result = new Image(display, imageUrl.openStream()); + } catch (IOException e) { + DavGatewayTray.warn("Unable to load image", e); + } + return result; } public void init() { @@ -123,22 +139,10 @@ public class SwtGatewayTray implements DavGatewayTrayInterface { trayItem = new TrayItem(tray, SWT.NONE); trayItem.setToolTipText("DavMail gateway"); - // load an image - ClassLoader classloader = DavGatewayTray.class.getClassLoader(); - try { - URL imageUrl = classloader.getResource("tray.png"); - image = new Image(display, imageUrl.openStream()); - awtImage = ImageIO.read(imageUrl); - } catch (IOException e) { - DavGatewayTray.warn("Unable to load image", e); - } - - try { - URL imageUrl2 = classloader.getResource("tray2.png"); - image2 = new Image(display, imageUrl2.openStream()); - } catch (IOException e) { - DavGatewayTray.warn("Unable to load image", e); - } + awtImage = DavGatewayTray.loadImage("tray.png"); + image = loadSwtImage("tray.png"); + image2 = loadSwtImage("tray2.png"); + inactiveImage = loadSwtImage("trayinactive.png"); trayItem.setImage(image); diff --git a/src/java/trayinactive.png b/src/java/trayinactive.png new file mode 100644 index 00000000..13163c5c Binary files /dev/null and b/src/java/trayinactive.png differ