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 public final class FakeSMTP {
29 private static final Logger LOGGER = LoggerFactory.getLogger(FakeSMTP.class);
30
31 private FakeSMTP() {
32 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 ArgsHandler.INSTANCE.handleArgs(args);
55 } catch (ParseException e) {
56 ArgsHandler.INSTANCE.displayUsage();
57 return;
58 }
59
60 if (ArgsHandler.INSTANCE.shouldStartInBackground()) {
61 try {
62 SMTPServerHandler.INSTANCE.startServer(getPort(), getBindAddress());
63 } catch (NumberFormatException e) {
64 LOGGER.error("Error: Invalid port number", e);
65 } catch (UnknownHostException e) {
66 LOGGER.error("Error: Invalid bind address", e);
67 } catch (Exception e) {
68 LOGGER.error("Failed to auto-start server in background", e);
69 }
70 } else {
71 System.setProperty("mail.mime.decodetext.strict", "false");
72 Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler());
73
74 EventQueue.invokeLater(new Runnable() {
75 @Override
76 public void run() {
77 try {
78 URL envelopeImage = getClass().getResource(Configuration.INSTANCE.get("application.icon.path"));
79 if (envelopeImage != null) {
80 Application.getApplication().setDockIconImage(Toolkit.getDefaultToolkit().getImage(envelopeImage));
81 }
82 } catch (RuntimeException e) {
83 LOGGER.debug("Error: {} - This is probably because we run on a non-Mac platform and these components are not implemented", e.getMessage());
84 } catch (Exception e) {
85 LOGGER.error("", e);
86 }
87
88 System.setProperty("apple.laf.useScreenMenuBar", "true");
89 System.setProperty("com.apple.mrj.application.apple.menu.about.name", Configuration.INSTANCE.get("application.name"));
90 UIManager.put("swing.boldMetal", Boolean.FALSE);
91 try {
92 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
93 } catch (Exception e) {
94 LOGGER.error("", e);
95 }
96
97 new MainFrame();
98 }
99 });
100 }
101 }
102
103
104
105
106
107 private static int getPort() throws NumberFormatException {
108 String portStr = ArgsHandler.INSTANCE.getPort();
109 if (portStr == null) {
110 portStr = Configuration.INSTANCE.get("smtp.default.port");
111 }
112 return Integer.parseInt(portStr);
113 }
114
115
116
117
118
119 private static InetAddress getBindAddress() throws UnknownHostException {
120 String bindAddressStr = ArgsHandler.INSTANCE.getBindAddress();
121 if (bindAddressStr == null || bindAddressStr.isEmpty()) {
122 return null;
123 }
124 return InetAddress.getByName(bindAddressStr);
125 }
126 }