| 1 | |
package com.nilhcem.fakesmtp; |
| 2 | |
|
| 3 | |
import java.awt.EventQueue; |
| 4 | |
import java.awt.Toolkit; |
| 5 | |
import java.net.InetAddress; |
| 6 | |
import java.net.URL; |
| 7 | |
import java.net.UnknownHostException; |
| 8 | |
|
| 9 | |
import javax.swing.UIManager; |
| 10 | |
|
| 11 | |
import org.apache.commons.cli.ParseException; |
| 12 | |
import org.slf4j.Logger; |
| 13 | |
import org.slf4j.LoggerFactory; |
| 14 | |
|
| 15 | |
import com.apple.eawt.Application; |
| 16 | |
import com.nilhcem.fakesmtp.core.ArgsHandler; |
| 17 | |
import com.nilhcem.fakesmtp.core.Configuration; |
| 18 | |
import com.nilhcem.fakesmtp.core.exception.UncaughtExceptionHandler; |
| 19 | |
import com.nilhcem.fakesmtp.gui.MainFrame; |
| 20 | |
import com.nilhcem.fakesmtp.server.SMTPServerHandler; |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | 0 | public final class FakeSMTP { |
| 29 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(FakeSMTP.class); |
| 30 | |
|
| 31 | 0 | private FakeSMTP() { |
| 32 | 0 | throw new UnsupportedOperationException(); |
| 33 | |
} |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public static void main(final String[] args) { |
| 53 | |
try { |
| 54 | 0 | ArgsHandler.INSTANCE.handleArgs(args); |
| 55 | 0 | } catch (ParseException e) { |
| 56 | 0 | ArgsHandler.INSTANCE.displayUsage(); |
| 57 | 0 | return; |
| 58 | 0 | } |
| 59 | |
|
| 60 | 0 | if (ArgsHandler.INSTANCE.shouldStartInBackground()) { |
| 61 | |
try { |
| 62 | 0 | SMTPServerHandler.INSTANCE.startServer(getPort(), getBindAddress()); |
| 63 | 0 | } catch (NumberFormatException e) { |
| 64 | 0 | LOGGER.error("Error: Invalid port number", e); |
| 65 | 0 | } catch (UnknownHostException e) { |
| 66 | 0 | LOGGER.error("Error: Invalid bind address", e); |
| 67 | 0 | } catch (Exception e) { |
| 68 | 0 | LOGGER.error("Failed to auto-start server in background", e); |
| 69 | 0 | } |
| 70 | |
} else { |
| 71 | 0 | System.setProperty("mail.mime.decodetext.strict", "false"); |
| 72 | 0 | Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler()); |
| 73 | |
|
| 74 | 0 | EventQueue.invokeLater(new Runnable() { |
| 75 | |
@Override |
| 76 | |
public void run() { |
| 77 | |
try { |
| 78 | 0 | URL envelopeImage = getClass().getResource(Configuration.INSTANCE.get("application.icon.path")); |
| 79 | 0 | if (envelopeImage != null) { |
| 80 | 0 | Application.getApplication().setDockIconImage(Toolkit.getDefaultToolkit().getImage(envelopeImage)); |
| 81 | |
} |
| 82 | 0 | } catch (RuntimeException e) { |
| 83 | 0 | LOGGER.debug("Error: {} - This is probably because we run on a non-Mac platform and these components are not implemented", e.getMessage()); |
| 84 | 0 | } catch (Exception e) { |
| 85 | 0 | LOGGER.error("", e); |
| 86 | 0 | } |
| 87 | |
|
| 88 | 0 | System.setProperty("apple.laf.useScreenMenuBar", "true"); |
| 89 | 0 | System.setProperty("com.apple.mrj.application.apple.menu.about.name", Configuration.INSTANCE.get("application.name")); |
| 90 | 0 | UIManager.put("swing.boldMetal", Boolean.FALSE); |
| 91 | |
try { |
| 92 | 0 | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
| 93 | 0 | } catch (Exception e) { |
| 94 | 0 | LOGGER.error("", e); |
| 95 | 0 | } |
| 96 | |
|
| 97 | 0 | new MainFrame(); |
| 98 | 0 | } |
| 99 | |
}); |
| 100 | |
} |
| 101 | 0 | } |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
private static int getPort() throws NumberFormatException { |
| 108 | 0 | String portStr = ArgsHandler.INSTANCE.getPort(); |
| 109 | 0 | if (portStr == null) { |
| 110 | 0 | portStr = Configuration.INSTANCE.get("smtp.default.port"); |
| 111 | |
} |
| 112 | 0 | return Integer.parseInt(portStr); |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
private static InetAddress getBindAddress() throws UnknownHostException { |
| 120 | 0 | String bindAddressStr = ArgsHandler.INSTANCE.getBindAddress(); |
| 121 | 0 | if (bindAddressStr == null || bindAddressStr.isEmpty()) { |
| 122 | 0 | return null; |
| 123 | |
} |
| 124 | 0 | return InetAddress.getByName(bindAddressStr); |
| 125 | |
} |
| 126 | |
} |