komandor.posmonitor.config.ArgsConfig.java Source code

Java tutorial

Introduction

Here is the source code for komandor.posmonitor.config.ArgsConfig.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package komandor.posmonitor.config;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 *
 * @author slava
 */
public class ArgsConfig {

    private String loggerConfigPath;
    private String appConfigPath;

    public ArgsConfig(String[] args) {
        this.loggerConfigPath = "logger.xml";
        this.appConfigPath = "./config.xml";

        try {
            parser(args);
        } catch (ParseException ex) {
            System.out.println("   ? : "
                    + ex.getMessage());
            ex.printStackTrace();
        }
    }

    private void parser(String[] args) throws ParseException {
        Options opts = new Options();
        opts.addOption("c", "config", true, "The configration file");
        opts.addOption("l", "log", true, "The logger configuration file");

        CommandLineParser parser = new BasicParser();
        CommandLine cmd = parser.parse(opts, args);

        if (cmd.hasOption("c")) {
            this.appConfigPath = cmd.getOptionValue("c");
        }

        if (cmd.hasOption("l")) {
            this.loggerConfigPath = cmd.getOptionValue("l");
        }
    }

    public void setLoggerConfigPath(String path) {
        this.loggerConfigPath = path;
    }

    public String getLoggerConfigPath() {
        return this.loggerConfigPath;
    }

    public void setAppConfigPath(String path) {
        this.appConfigPath = path;
    }

    public String getAppConfigPath() {
        return this.appConfigPath;
    }
}