import org.apache.commons.cli.*;

import javax.mail.*;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Main {

    private static String appName = "malooer";

    /**
     * Required parameters:
     * -port <PORT_NUMBER>
     * -host <HOST>
     * -user <USER>
     * -pwd <PASSWORD>
     *
     * optional parameters:
     * -o <EXECUTE_ONCE>
     *
     * @param args
     */
    public static void main(String[] args) {
        HelpFormatter hFormatter = new HelpFormatter();
        CommandLine cl;

        if (args.length < 1) {
            hFormatter.printHelp(appName, getOptions());
            return;
        }

        try {
            cl = getCommandline(args);
        } catch (ParseException e) {
            hFormatter.printHelp(appName, getOptions());
            return;
        }

        if (!validateCmdLine(cl)) {
            hFormatter.printHelp(appName, getOptions());
            return;
        }

        final Integer port = Integer.valueOf(cl.getOptionValue("port"));
        final String host = cl.getOptionValue("host");
        final String user = cl.getOptionValue("user");
        final String pwd = cl.getOptionValue("pwd");
        boolean onlyOnce = cl.hasOption("o");


        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");

        Session session = Session.getInstance(props);

        try {
            final Transport transport = session.getTransport("smtp");
            Runnable runnable = new Runnable() {
                public void run() {
                    Date date = new Date();
                    Timestamp timestamp = new Timestamp(date.getTime());
                    try {
                        transport.connect(host, port, user, pwd);
                        transport.close();
                        System.out.println(timestamp + ": [OK] Connection established - " + host);
                    } catch (AuthenticationFailedException e) {
                        System.out.println(timestamp + ": [FAIL] Authentication failed");
                    } catch (MessagingException e) {
                        System.out.println(timestamp + ": [FAIL] Unable to connect to server");
                    }
                }
            };

            ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();

            if(onlyOnce){
                service.execute(runnable);
                service.shutdown();
            }else{
                service.scheduleAtFixedRate(runnable, 0, 5, TimeUnit.MINUTES);
            }
        } catch (NoSuchProviderException e) {
            System.out.println("No such provider");
            return;
        }
    }

    private static boolean validateCmdLine(CommandLine cl) {
        List<Object> inputList = new ArrayList<Object>();
        inputList.add(cl.getOptionValue("port"));
        inputList.add(cl.getOptionValue("host"));
        inputList.add(cl.getOptionValue("user"));
        inputList.add(cl.getOptionValue("pwd"));

        for (Object obj : inputList) {
            if (obj == null) {
                return false;
            }
        }
        return true;
    }

    private static CommandLine getCommandline(String[] args) throws ParseException {
        Options options = getOptions();
        CommandLineParser clParser = new DefaultParser();

        return clParser.parse(options, args);
    }

    private static Options getOptions() {
        Options options = new Options();

        Option portOption = new Option("port", true, "port of server");
        Option hostOption = new Option("host", true, "the host");
        Option userOption = new Option("user", true, "the user");
        Option passOption = new Option("pwd", true, "the password");
        Option executeOnceOption = new Option("o", false, "establish connection only once");

        options.addOption(portOption);
        options.addOption(hostOption);
        options.addOption(userOption);
        options.addOption(passOption);
        options.addOption(executeOnceOption);

        return options;
    }
}
@@ -532,32 +532,53 @@
 nal parameters:%0A
+     * -i %3CINTERVAL%3E%0A
      * -o %3CEXECU
@@ -586,16 +586,16 @@
 E_ONCE%3E%0A
-
      *%0A 
@@ -1450,16 +1450,270 @@
 (%22o%22);%0A%0A
+        Integer interval;%0A        try %7B%0A            interval = getInterval(cl);%0A        %7D catch (Exception e) %7B%0A            hFormatter.printHelp(appName, getOptions());%0A            return;%0A        %7D%0A        interval = interval != null ? interval : 300;%0A%0A
 %0A       
@@ -3009,9 +3009,16 @@
  0, 
-5
+interval
 , Ti
@@ -3028,14 +3028,14 @@
 nit.
-MINUTE
+SECOND
 S);%0A
@@ -3175,32 +3175,294 @@
        %7D%0A    %7D%0A%0A
+    private static Integer getInterval(CommandLine cl) throws Exception %7B%0A        String interval = cl.getOptionValue(%22i%22);%0A%0A        if (interval != null) %7B%0A            return Integer.valueOf(interval);%0A        %7D else %7B%0A            return null;%0A        %7D%0A    %7D%0A%0A
     private stat
@@ -4506,24 +4506,111 @@
 password%22);%0A
+        Option intervalOption = new Option(%22i%22, true, %22interval between connections%22);%0A
         Opti
@@ -4800,32 +4800,32 @@
 on(userOption);%0A
-
         options.
@@ -4839,32 +4839,75 @@
 on(passOption);%0A
+        options.addOption(intervalOption);%0A
         options.

Add option: interval between connections