com.sangachy.license.LicenseHelperCommand.java Source code

Java tutorial

Introduction

Here is the source code for com.sangachy.license.LicenseHelperCommand.java

Source

package com.sangachy.license;

import com.sangachy.license.task.ApplyLicenseTask;
import com.sangachy.license.task.SingleTask;
import com.sangachy.license.task.UploadLicenseTask;
import org.apache.commons.cli.*;

import java.util.Hashtable;

/**
 * Created with IntelliJ IDEA.
 * User: ShouZhi Zhang
 * Date: 13-6-2
 * Time: ?6:43
 */
public class LicenseHelperCommand {
    private Options options = new Options();

    private void printHelp() {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("Options", options);
    }

    public static void main(String[] args) {
        String[] arg = { "-Dsfa", "afsd" };
        LicenseHelperCommand licenseHelperCommand = new LicenseHelperCommand();
        licenseHelperCommand.parser(arg);
    }

    public SingleTask parser(String[] args) {

        options.addOption("h", "help", false, "?");

        options.addOption("u", "upload", false, "License");
        options.addOption("a", "apply", false, "License");

        options.addOption("tp", "temp-path", true, "License ?");
        options.addOption("lp", "lic-path", true, "License ?");

        options.addOption("e", "esn-key", true, "ESN ?");
        options.addOption("ek", "env-key", true, "");

        PosixParser basicParser = new PosixParser();
        CommandLine commandLine;

        try {
            commandLine = basicParser.parse(options, args);

            if (commandLine.getOptions().length <= 0) {
                System.out.println("??0?");
                printHelp();
                return null;
            }

            if (commandLine.getOptions().length > 0) {

                if (commandLine.hasOption("h")) {
                    printHelp();
                    return null;
                }

                if (!commandLine.hasOption("a") && !commandLine.hasOption("u")) {
                    System.out.println("????");
                    printHelp();
                    return null;
                }

                if (commandLine.hasOption("a")) {
                    return getApplyLicenseTask(commandLine);
                }

                if (commandLine.hasOption("u")) {
                    return getUploadTask(commandLine);
                }

            }
            return null;
        } catch (ParseException e) {
            System.out.println(e.getMessage());
            printHelp();
            return null;
        }
    }

    public SingleTask getUploadTask(CommandLine commandLine) {
        String licensePath = commandLine.getOptionValue("lp");
        if (null == licensePath) {
            System.out.println("License ");
            return null;
        }

        String envKey = commandLine.getOptionValue("ek");
        if (null == envKey) {
            System.out.println("");
            //envKey = "";
        }
        Hashtable hashTable = new Hashtable();
        //hashTable.put("", licensePath);
        //hashTable.put(" ", envKey);
        SingleTask task = new UploadLicenseTask();
        task.preExecute(hashTable);
        task.excute();
        task.aftExcute();
        return task;
    }

    public SingleTask getApplyLicenseTask(CommandLine commandLine) {
        String templatePath = commandLine.getOptionValue("tp");
        if (null == templatePath) {
            System.out.println("License ?,");
            // templatePath = "";
        }

        String esnKey = commandLine.getOptionValue("e");
        if (null == esnKey) {
            System.out.println("PS 10.0 ESN");
            // esnKey = "";
        }
        Hashtable hashTable = new Hashtable();
        //hashTable.put("", templatePath);
        //hashTable.put(" ", esnKey);
        SingleTask task = new ApplyLicenseTask();

        task.preExecute(hashTable);
        task.excute();
        task.aftExcute();
        return task;
    }
}