com.chezzverse.timelogger.server.TimeLoggerServerMain.java Source code

Java tutorial

Introduction

Here is the source code for com.chezzverse.timelogger.server.TimeLoggerServerMain.java

Source

/***************************************************************************************************
 *Copyright (c)2015 Charles Freedman
 *
 *Permission is hereby granted, free of charge, to any person obtaining a copy
 *of this software and associated documentation files (the "Software"), to deal
 *in the Software without restriction, including without limitation the rights
 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *copies of the Software, and to permit persons to whom the Software is
 *furnished to do so, subject to the following conditions:
 *
 *The above copyright notice and this permission notice shall be included in
 *all copies or substantial portions of the Software.
 *
 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *THE SOFTWARE.
 **************************************************************************************************/

package com.chezzverse.timelogger.server;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class TimeLoggerServerMain {

    private static final String _DEFAULT_CONFIG_FILE = System.getProperty("user.dir") + "/config/tlconfig.ini";

    public static void main(String[] args) {

        // Initialize the default configuration file to the hard coded default path value.
        String configFileName = _DEFAULT_CONFIG_FILE;

        // Create all the available options
        Option portOpt = new Option("p", "port", true, "Set the listen port for the http server " + "instance.");
        portOpt.setArgName("number");

        Option htmlOpt = new Option("h", "html-root", true,
                "Set the root directory that " + "contains the required html files.");
        htmlOpt.setArgName("directory");

        Option configOpt = new Option("c", "config", true,
                "Get configuration options from the " + "specified file");
        Option backlogOpt = new Option("b", "backlog", true,
                "Set the number of connections to " + "queue for the server.");

        Options opts = new Options();
        opts.addOption(portOpt);
        opts.addOption(htmlOpt);
        opts.addOption(configOpt);
        opts.addOption(backlogOpt);

        CommandLineParser parser = new DefaultParser();
        CommandLine cmd = null;
        HelpFormatter formatter = new HelpFormatter();

        try {
            // Parse the command line arguments.
            cmd = parser.parse(opts, args);

            // Fist check to see if there is a custom defined config file, and load the config file.
            if (cmd.hasOption("c")) {
                configFileName = cmd.getOptionValue("c");
            }

            TimeLoggerConfig.getInstance().LoadConfigFile(configFileName);

            // Process the rest of the args and override any settings in the config file.
            if (cmd.hasOption("h")) {
                TimeLoggerConfig.getInstance().htmlRoot = cmd.getOptionValue("h");
            }

            if (cmd.hasOption("p")) {
                TimeLoggerConfig.getInstance().port = Integer.parseInt(cmd.getOptionValue("p"));
            }

            if (cmd.hasOption("b")) {
                TimeLoggerConfig.getInstance().maxBackLog = Integer.parseInt(cmd.getOptionValue("b"));
            }

        } catch (ParseException e) {
            System.out.println("Invalid argument.");
            formatter.printHelp("TimeLoggerServer", opts);
            System.exit(1);
        } catch (NumberFormatException e) {
            System.out.println("Invalid argument.");
            formatter.printHelp("TimeLoggerServer", opts);
            System.exit(1);
        } catch (Exception e) {
            e.printStackTrace();
        }

        TimeLoggerApp app = new TimeLoggerApp();

        if (app != null) {
            app.run();
        } else {
            System.exit(3); // Strange termination
        }
    }

    /**
     * Process command line arguments.
     * @param args Array of arguments.
     * @return An instantiated TimeLoggerApp object.
     */
    public static TimeLoggerApp processArgs(String[] args) {

        return new TimeLoggerApp();
    }
}