View Javadoc
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   * Entry point of the application.
24   *
25   * @author Nilhcem
26   * @since 1.0
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  	 * Checks command line arguments, sets some specific properties, and runs the main window.
37  	 * <p>
38  	 * Before opening the main window, this method will:
39       * </p>
40  	 * <ul>
41  	 *   <li>check command line arguments, and possibly display an error dialog,</li>
42  	 *   <li>set a default uncaught exception handler to intercept every uncaught exception;</li>
43  	 *   <li>use a custom icon in the Mac Dock;</li>
44  	 *   <li>set a property for Mac OS X to take the menu bar off the JFrame;</li>
45  	 *   <li>set a property for Mac OS X to set the name of the application menu item;</li>
46  	 *   <li>turn off the bold font in all components for swing default theme;</li>
47  	 *   <li>use the platform look and feel.</li>
48  	 * </ul>
49  	 *
50  	 * @param args a list of command line parameters.
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 	 * @return either the default port, or the custom port, if specified.
105 	 * @throws NumberFormatException if the specified port cannot be parsed to an integer.
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 	 * @return an InetAddress representing the specified bind address, or null, if not specified
117 	 * @throws UnknownHostException if the bind address is invalid
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 }