org.geoint.geoserver.configuration.manager.GeoserverConfigCli.java Source code

Java tutorial

Introduction

Here is the source code for org.geoint.geoserver.configuration.manager.GeoserverConfigCli.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 org.geoint.geoserver.configuration.manager;

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

/**
 *
 * @author joel_harris
 */
public class GeoserverConfigCli {

    /**
     *
     * @param args
     * @throws MalformedURLException arg[0] = source server URL arg[1] = path
     * where you want to save the zip of configuration arg[2.....] = as many
     * destination servers as you want
     */
    private static final String ZIP = ".zip";
    private static final String GEOSERVERCONFIG = "geoserver-config-";
    static HelpFormatter formatter = new HelpFormatter();
    private static String username;
    private static String password;
    private static String sourceUrl;
    private static String zipPath;
    private static String[] destinations;
    private static final String OPTION_COLLECTANDDISTRIBUTE = "collectanddistribute";
    private static final String OPTION_DISTRIBUTE = "distribute";
    private static final String OPTION_COLLECT = "collect";
    private static final String OPTION_D = "d";
    private static final String OPTION_Z = "z";
    private static final String OPTION_U = "u";
    private static final String OPTION_HELP = "help";
    private static final String OPTION_S = "S";
    private static final String OPTION_P = "p";
    private static boolean debug = false;

    public static CommandLine parse(String[] args) throws ParseException {

        CommandLineParser parser = new GnuParser();

        CommandLine commandLine = parser.parse(getOptions(), args);
        return commandLine;
    }

    public static Options getOptions() {
        Option debug_option = OptionBuilder.withDescription("sets logger to all").create("debug");
        Option userName_option = OptionBuilder.withArgName("username").hasArg().isRequired(true)
                .withDescription("(required) domain username").create(OPTION_U);
        Option password_option = OptionBuilder.withArgName("password").isRequired(true).hasArg()
                .withDescription("(required) domain Password").create(OPTION_P);
        Option sourceServer_option = OptionBuilder.withArgName("sourceServer").hasArg()
                .withDescription("The source server URL").create(OPTION_S);
        Option zipLocation_option = OptionBuilder.withArgName("ziplocation").hasArg().isRequired(true)
                .withDescription(
                        "(required) the path to the ZIP file. This can be where you want it, or the source for distribution")
                .create(OPTION_Z);
        Option collect_option = OptionBuilder.withArgName(OPTION_COLLECT)
                .withDescription("Collect configuration from server").create(OPTION_COLLECT);
        Option distribute_option = OptionBuilder.withArgName(OPTION_DISTRIBUTE)
                .withDescription("distribute configuration to other geoserver instances").create(OPTION_DISTRIBUTE);
        Option collectanddistribute_option = OptionBuilder.withArgName("collectanddistribute_option")
                .withDescription("collect and distribute configuration to other geoserver instances")
                .create(OPTION_COLLECTANDDISTRIBUTE);
        Option destinationServers_option = OptionBuilder.hasArgs(12)
                .withDescription("a list of destination server URLs comma separated").withValueSeparator(',')
                .create(OPTION_D);
        Option help_option = OptionBuilder.withDescription("prints help").create(OPTION_HELP);

        Options options = new Options();

        options.addOption(debug_option);
        options.addOption(userName_option);
        options.addOption(password_option);
        options.addOption(sourceServer_option);
        options.addOption(zipLocation_option);
        options.addOption(collect_option);
        options.addOption(distribute_option);
        options.addOption(collectanddistribute_option);
        options.addOption(destinationServers_option);
        options.addOption(help_option);

        return options;
    }
}