Coverage Report - com.nilhcem.fakesmtp.FakeSMTP
 
Classes in this File Line Coverage Branch Coverage Complexity
FakeSMTP
0%
0/30
0%
0/8
4
FakeSMTP$1
0%
0/18
0%
0/2
4
 
 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  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  
          * 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  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  
          * @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  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  
          * @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  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  
 }