Refactoring, java 1.5 compatibility with custom socket factory

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@151 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2008-11-03 10:56:57 +00:00
parent cd2996f8a1
commit 3df9d6d8da
12 changed files with 85 additions and 59 deletions

View File

@ -60,24 +60,25 @@ public class ExchangeSessionFactory {
}
} catch (UnknownHostException exc) {
String message = "DavMail configuration exception: \n Unknown host ";
String message = "DavMail configuration exception: \n";
if (checkNetwork()) {
message += exc.getMessage();
message += "Unknown host " + exc.getMessage();
} else {
message += "All network interfaces down !";
}
ExchangeSession.LOGGER.error(message, exc);
throw new IOException(message, exc);
throw new IOException(message);
} catch (Exception exc) {
ExchangeSession.LOGGER.error("DavMail configuration exception: \n" + exc.getMessage(), exc);
throw new IOException("DavMail configuration exception: \n" + exc.getMessage(), exc);
throw new IOException("DavMail configuration exception: \n" + exc.getMessage());
}
}
/**
* Check if at least one network interface is up and active (i.e. has an address)
*
* @return true if network available
*/
protected static boolean checkNetwork() {
@ -90,6 +91,9 @@ public class ExchangeSessionFactory {
up = networkInterface.isUp() && !networkInterface.isLoopback()
&& networkInterface.getInetAddresses().hasMoreElements();
}
} catch (NoSuchMethodError error) {
ExchangeSession.LOGGER.debug("Unable to test network interfaces (not available under Java 1.5)");
up = true;
} catch (SocketException exc) {
ExchangeSession.LOGGER.error("DavMail configuration exception: \n Error listing network interfaces " + exc.getMessage(), exc);
}

View File

@ -47,10 +47,7 @@ public class DavGatewaySSLProtocolSocketFactory extends SSLProtocolSocketFactory
private SSLContext createSSLContext() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
SSLContext context = SSLContext.getInstance("SSL");
context.init(
null,
new TrustManager[]{new DavGatewayX509TrustManager()},
null);
context.init(null, new TrustManager[]{new DavGatewayX509TrustManager()}, null);
return context;
}
@ -64,48 +61,39 @@ public class DavGatewaySSLProtocolSocketFactory extends SSLProtocolSocketFactory
public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException {
try {
return getSSLContext().getSocketFactory().createSocket(
host,
port,
clientHost,
clientPort
);
return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
} catch (NoSuchAlgorithmException e) {
throw new IOException(e);
throw new IOException(e+" "+e.getMessage());
} catch (KeyManagementException e) {
throw new IOException(e);
throw new IOException(e+" "+e.getMessage());
} catch (KeyStoreException e) {
throw new IOException(e);
throw new IOException(e+" "+e.getMessage());
}
}
public Socket createSocket(String host, int port) throws IOException {
try {
return getSSLContext().getSocketFactory().createSocket(
host,
port
);
return getSSLContext().getSocketFactory().createSocket(host, port);
} catch (NoSuchAlgorithmException e) {
throw new IOException(e);
throw new IOException(e+" "+e.getMessage());
} catch (KeyManagementException e) {
throw new IOException(e);
throw new IOException(e+" "+e.getMessage());
} catch (KeyStoreException e) {
throw new IOException(e);
throw new IOException(e+" "+e.getMessage());
}
}
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {
try {
return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose
);
return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
} catch (NoSuchAlgorithmException e) {
throw new IOException(e);
throw new IOException(e+" "+e.getMessage());
} catch (KeyManagementException e) {
throw new IOException(e);
throw new IOException(e+" "+e.getMessage());
} catch (KeyStoreException e) {
throw new IOException(e);
throw new IOException(e+" "+e.getMessage());
}
}
}

View File

@ -121,7 +121,6 @@ public class PopConnection extends AbstractConnection {
message = "Authentication failed: "+e.toString();
}
DavGatewayTray.error(message);
message = message.replaceAll("\\n", " ");
sendERR(message);
}
}
@ -240,7 +239,7 @@ public class PopConnection extends AbstractConnection {
}
public void sendERR(String message) throws IOException {
sendClient("-ERR ", message);
sendClient("-ERR ", message.replaceAll("\\n", " "));
}
/**

View File

@ -1,8 +1,8 @@
package davmail.tray;
import davmail.AboutFrame;
import davmail.Settings;
import davmail.SettingsFrame;
import davmail.ui.AboutFrame;
import davmail.ui.SettingsFrame;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.apache.log4j.lf5.LF5Appender;
@ -10,10 +10,12 @@ 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
@ -29,6 +31,10 @@ public class AwtGatewayTray implements DavGatewayTrayInterface {
private static Image image = null;
private static Image image2 = null;
public Image getFrameIcon() {
return image;
}
public void switchIcon() {
synchronized (LOCK) {
if (trayIcon.getImage() == image) {
@ -77,15 +83,24 @@ public class AwtGatewayTray implements DavGatewayTrayInterface {
SystemTray tray = SystemTray.getSystemTray();
// load an image
ClassLoader classloader = DavGatewayTray.class.getClassLoader();
URL imageUrl = classloader.getResource("tray.png");
image = Toolkit.getDefaultToolkit().getImage(imageUrl);
URL imageUrl2 = classloader.getResource("tray2.png");
image2 = Toolkit.getDefaultToolkit().getImage(imageUrl2);
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);
}
// create a popup menu
PopupMenu popup = new PopupMenu();
final AboutFrame aboutFrame = new AboutFrame();
aboutFrame.setIconImage(image);
// create an action settingsListener to listen for settings action executed on the tray icon
ActionListener aboutListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
@ -98,7 +113,6 @@ public class AwtGatewayTray implements DavGatewayTrayInterface {
popup.add(aboutItem);
final SettingsFrame settingsFrame = new SettingsFrame();
settingsFrame.setIconImage(image);
// create an action settingsListener to listen for settings action executed on the tray icon
ActionListener settingsListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {

View File

@ -18,6 +18,14 @@ public class DavGatewayTray {
static DavGatewayTrayInterface davGatewayTray;
public static Image getFrameIcon() {
Image icon = null;
if (davGatewayTray != null) {
icon = davGatewayTray.getFrameIcon();
}
return icon;
}
public static void switchIcon() {
if (davGatewayTray != null) {
davGatewayTray.switchIcon();

View File

@ -2,12 +2,15 @@ package davmail.tray;
import org.apache.log4j.Priority;
import java.awt.*;
/**
* Gateway tray interface common to SWT and pure java implementations
*/
public interface DavGatewayTrayInterface {
void switchIcon();
void resetIcon();
Image getFrameIcon();
void displayMessage(String message, Priority priority);
void init();
}

View File

@ -1,8 +1,8 @@
package davmail.tray;
import davmail.AboutFrame;
import davmail.Settings;
import davmail.SettingsFrame;
import davmail.ui.AboutFrame;
import davmail.ui.SettingsFrame;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.apache.log4j.lf5.LF5Appender;
@ -20,8 +20,8 @@ import org.eclipse.swt.widgets.ToolTip;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.net.URL;
@ -36,11 +36,16 @@ public class SwtGatewayTray implements DavGatewayTrayInterface {
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 Display display;
private static Shell shell;
public java.awt.Image getFrameIcon() {
return awtImage;
}
public void switchIcon() {
display.syncExec(
new Runnable() {
@ -120,11 +125,10 @@ public class SwtGatewayTray implements DavGatewayTrayInterface {
// load an image
ClassLoader classloader = DavGatewayTray.class.getClassLoader();
java.awt.Image awtImage = null;
try {
URL imageUrl = classloader.getResource("tray.png");
image = new Image(display, imageUrl.openStream());
awtImage = Toolkit.getDefaultToolkit().getImage(imageUrl);
awtImage = ImageIO.read(imageUrl);
} catch (IOException e) {
DavGatewayTray.warn("Unable to load image", e);
}
@ -154,9 +158,6 @@ public class SwtGatewayTray implements DavGatewayTrayInterface {
MenuItem aboutItem = new MenuItem(popup, SWT.PUSH);
aboutItem.setText("About...");
final AboutFrame aboutFrame = new AboutFrame();
if (awtImage != null) {
aboutFrame.setIconImage(awtImage);
}
aboutItem.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
display.asyncExec(
@ -169,9 +170,6 @@ public class SwtGatewayTray implements DavGatewayTrayInterface {
});
final SettingsFrame settingsFrame = new SettingsFrame();
if (awtImage != null) {
settingsFrame.setIconImage(awtImage);
}
trayItem.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event event) {
display.asyncExec(

View File

@ -1,4 +1,4 @@
package davmail;
package davmail.ui;
import davmail.tray.DavGatewayTray;
@ -20,7 +20,8 @@ import java.net.URL;
public class AboutFrame extends JFrame {
public AboutFrame() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setTitle("About DavMail");
setTitle("About DavMail Gateway");
setIconImage(DavGatewayTray.getFrameIcon());
try {
JLabel imageLabel = new JLabel();
ClassLoader classloader = this.getClass().getClassLoader();

View File

@ -49,8 +49,8 @@ public class AcceptCertificateDialog extends JDialog {
public String getFormattedSerial(X509Certificate certificate) {
StringBuilder builder = new StringBuilder();
String serial = certificate.getSerialNumber().toString(16);
for (int i = 0;i<serial.length();i++) {
if (i > 0 && i%2 == 0) {
for (int i = 0; i < serial.length(); i++) {
if (i > 0 && i % 2 == 0) {
builder.append(' ');
}
builder.append(serial.charAt(i));
@ -59,6 +59,7 @@ public class AcceptCertificateDialog extends JDialog {
}
public AcceptCertificateDialog(X509Certificate certificate) {
setAlwaysOnTop(true);
String sha1Hash;
try {
sha1Hash = DavGatewayX509TrustManager.getFormattedHash(certificate);
@ -69,7 +70,12 @@ public class AcceptCertificateDialog extends JDialog {
}
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
setTitle("Accept certificate ?");
setTitle("DavMail: Accept certificate ?");
try {
setIconImage(DavGatewayTray.getFrameIcon());
} catch (NoSuchMethodError error) {
DavGatewayTray.debug("Unable to set JDialog icon image (not available under Java 1.5)");
}
JPanel subjectPanel = new JPanel();
subjectPanel.setLayout(new BoxLayout(subjectPanel, BoxLayout.Y_AXIS));

View File

@ -1,4 +1,4 @@
package davmail;
package davmail.ui;
import java.io.IOException;
import java.net.URI;

View File

@ -1,4 +1,8 @@
package davmail;
package davmail.ui;
import davmail.Settings;
import davmail.DavGateway;
import davmail.tray.DavGatewayTray;
import javax.swing.*;
import java.awt.event.ActionListener;
@ -128,7 +132,8 @@ public class SettingsFrame extends JFrame {
public SettingsFrame() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setTitle("DavMail Settings");
setTitle("DavMail Gateway Settings");
setIconImage(DavGatewayTray.getFrameIcon());
JTabbedPane tabbedPane = new JTabbedPane();

View File

@ -1,4 +1,4 @@
package davmail;
package davmail.ui;
import org.eclipse.swt.program.Program;