Main.java :  » J2EE » enhydra » org » enhydra » tool » common » Java Open Source

Java Open Source » J2EE » enhydra 
enhydra » org » enhydra » tool » common » Main.java

/*
 * Enhydra Java Application Server Project
 *
 * The contents of this file are subject to the Enhydra Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License on
 * the Enhydra web site ( http://www.enhydra.org/ ).
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific terms governing rights and limitations
 * under the License.
 *
 * The Initial Developer of the Enhydra Application Server is Lutris
 * Technologies, Inc. The Enhydra Application Server and portions created
 * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
 * All Rights Reserved.
 *
 * Contributor(s):
 * Paul Mahar
 *
 */
package org.enhydra.tool.common;

// ToolBox imports
import org.enhydra.tool.ToolBoxInfo;

// Standard imports
import java.io.File;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.ResourceBundle;
public class Main {

    //
    static ResourceBundle res =
        ResourceBundle.getBundle("org.enhydra.tool.common.Res");    // nores

    //
    private final String  PARAM_ARCHIVE = "-archive";               // nores
    private final String  PARAM_CONFIG = "-config";                 // nores
    private final String  PARAM_CODEGEN = "-codegen";               // nores
    private final String  PARAM_INFO = "-info";                     // nores

    //
    private boolean       archive = false;
    private boolean       config = false;
    private boolean       codeGen = false;
    private boolean       info = false;

    public Main() {
    }

    public static void main(String[] args) {
        Main main = new Main();

        main.init(args);
    }

    private String[] getToolArgs(String[] args) {
        ArrayList list = null;
        String[]  toolArgs = new String[0];

        if (args.length >= 1) {
            list = new ArrayList(Arrays.asList(args));
            list.remove(0);
            list.trimToSize();
            toolArgs = new String[list.size()];
            toolArgs = (String[]) list.toArray(toolArgs);
        }
        return toolArgs;
    }

    private void init(String[] args) {
        try {
            parseArgs(args);
            if (isArchive()) {
                org.enhydra.tool.archive.Main.main(getToolArgs(args));
            } else if (isCodeGen()) {
                org.enhydra.tool.codegen.Main.main(getToolArgs(args));
            } else if (isConfig()) {
                org.enhydra.tool.configure.Main.main(getToolArgs(args));
            } else if (isInfo()) {
                org.enhydra.tool.ToolBoxInfo.main(getToolArgs(args));
            }
        } catch (ArgumentException e) {
            System.out.println(ResUtil.format(res.getString("Error_0_"),
                                              e.getMessage()));
            showUsage();
        }
    }

    private void parseArgs(String[] args) throws ArgumentException {
        if (args.length < 1) {
            throw new ArgumentException(res.getString("Too_few_arguments"));
        } else if (!isValidTool(args[0])) {
            throw new ArgumentException(ResUtil.format(res.getString("Tool_not_found_0_"),
                                                       args[0]));
        }
    }

    private boolean isArchive() {
        return archive;
    }

    private void setArchive(boolean b) {
        archive = b;
    }

    private boolean isConfig() {
        return config;
    }

    private void setConfig(boolean b) {
        config = b;
    }

    private boolean isCodeGen() {
        return codeGen;
    }

    private void setCodeGen(boolean b) {
        codeGen = b;
    }

    private boolean isInfo() {
        return info;
    }

    private void setInfo(boolean b) {
        info = b;
    }

    private boolean isValidTool(String tool) {
        boolean valid = false;

        if (tool == null) {

            // done
        } else if (tool.equalsIgnoreCase(PARAM_ARCHIVE)) {
            setArchive(true);
            valid = true;
        } else if (tool.equalsIgnoreCase(PARAM_CONFIG)) {
            setConfig(true);
            valid = true;
        } else if (tool.equalsIgnoreCase(PARAM_CODEGEN)) {
            setCodeGen(true);
            valid = true;
        } else if (tool.equalsIgnoreCase(PARAM_INFO)) {
            setInfo(true);
            valid = true;
        }
        return valid;
    }

    private void showUsage() {
        System.out.println(res.getString("Kelp_ToolBox") + ' '
                           + ToolBoxInfo.getToolBoxVersion());
        System.out.println(ToolBoxInfo.getCopyright());
        System.out.println(res.getString("Usage1"));
        System.out.println(res.getString("Usage2"));
        System.out.println(res.getString("Usage3"));
    }

    class ArgumentException extends Exception {
        public ArgumentException(String msg) {
            super(msg);
        }

    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.