com.hangum.tadpole.commons.util.ApplicationArgumentUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.hangum.tadpole.commons.util.ApplicationArgumentUtils.java

Source

/*******************************************************************************
 * Copyright (c) 2013 hangum.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     hangum - initial API and implementation
 ******************************************************************************/
package com.hangum.tadpole.commons.util;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.eclipse.core.runtime.Platform;

import com.hangum.tadpole.commons.libs.core.define.PublicTadpoleDefine;
import com.hangum.tadpole.commons.libs.core.define.SystemDefine;

/**
 * System application argument utils 
 * 
 * @author hangum
 *
 */
public class ApplicationArgumentUtils {

    /** jdbc dir */
    public static String JDBC_RESOURCE_DIR = ApplicationArgumentUtils.getResourcesDir() + "driver"
            + PublicTadpoleDefine.DIR_SEPARATOR;

    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(ApplicationArgumentUtils.class);
    public static String[] applicationArgs = null;

    /**
     * engine?  .
     * 
     * @return
     */
    public static String getPasswd() {
        String passwd = "";

        try {
            passwd = getValue("-passwd");
        } catch (Exception e) {
            passwd = PublicTadpoleDefine.SYSTEM_DEFAULT_PASSWORD;
        }

        return passwd;
    }

    /**
     *     .
     * @return
     */
    public static String getResourcesDir() {
        String strResourceDir = "";

        try {
            strResourceDir = getValue("-resourcesDir");
        } catch (Exception e) {
            strResourceDir = FileUtils.getUserDirectoryPath() + IOUtils.DIR_SEPARATOR + "tadpole";
        }

        return strResourceDir + IOUtils.DIR_SEPARATOR;
    }

    /**
     * ?     ??
     * @return
     * @throws Exception
     */
    public static boolean isDBServer() {
        return checkString("-dbServer");
    }

    /**
     * <pre>
     *    ?    .
     * </pre>
     * 
     * @return
     */
    public static String getDbServer() throws Exception {
        return getValue("-dbServer");
    }

    /**
     * 
     * @return
     */
    public static boolean isDBPath() {
        return checkString("-dbPath");
    }

    /**
     *   ? path ? ?.
     * 
     * @return
     * @throws Exception
     */
    public static String getDBPath() throws Exception {
        return getValue("-dbPath");
    }

    /**
     * ?  ?
     * @return
     */
    public static boolean isUseDB() {
        return checkString("-useDB");
    }

    /**
     * ?  ?? .
     * @return
     * @throws Exception
     */
    public static String getUseDB() throws Exception {
        return getValue("-useDB");
    }

    public static boolean isDefaultDB() {
        return checkString("-defaultDB");
    }

    public static String getDefaultDB() throws Exception {
        return getValue("-defaultDB");
    }

    /**
     * <pre>
     * application  
     * 
     * application argement? -standalone  standalone 
     * <pre>
     * 
     * @return
     */
    public static boolean isStandaloneMode() {
        return checkString("-standalone");
    }

    /**
     * test ?
     * 
     * @return
     */
    public static boolean isTestMode() {
        return checkString("-test");
    }

    /**
     * testDB ?
     * 
     * @return
     */
    public static boolean isTestDBMode() {
        return checkString("-testDB");
    }

    /**
     * check debug mode
     */
    public static boolean isDebugMode() {
        return checkString("-debuglog");
    }

    /**
     * IS Google Analytics use?
     * 
     * @return
     */
    public static boolean isGAOFF() {
        return checkString("-GAOFF");
    }

    /**
     *   ? ?? ?
     * @return
     */
    public static boolean isNewUserPermit() {
        return checkString("-newUserPermit");
    }

    /**
     * ? ??    ?? .
     * 
     * @return
     */
    public static boolean getNewUserPermit() {
        if (isNewUserPermit()) {
            try {
                return PublicTadpoleDefine.YES_NO.YES.name().equals(getValue("-newUserPermit")) ? true : false;
            } catch (Exception e) {
                logger.error("check option : -newUserPermit exeception ", e);
                return false;
            }
        }

        return false;
    }

    /**
     * runtime? argument? value .
     * 
     * @param key
     * @return
     */
    private static String getValue(String key) throws Exception {
        String[] applicationArgs = getArguments();
        for (int i = 0; i < applicationArgs.length; i++) {
            String arg = StringUtils.trimToEmpty(applicationArgs[i]);
            if (arg.startsWith(key)) {
                return applicationArgs[i + 1];
            }
        }

        throw new Exception("Can't find argument. Find key is " + key);
    }

    /**
     * runtime? argument  .
     * 
     * @param checkString
     * @return
     */
    private static boolean checkString(String checkString) {
        String[] applicationArgs = getArguments();

        for (String strArg : applicationArgs) {
            strArg = StringUtils.trimToEmpty(strArg);
            if (strArg.equalsIgnoreCase(checkString))
                return true;
        }

        return false;
    }

    /**
     * application argument
     * 
     * @return
     */
    private static String[] getArguments() {
        if (applicationArgs != null)
            return applicationArgs;

        //      System.out.println("\t==[05:03]==========================================================");
        //      Properties p = System.getProperties();
        //      p.list(System.out);
        //      System.out.println("\t============================================================");

        try {
            /* is osgi */
            if (SystemDefine.isOSGIRuntime()) {

                logger.info("\t\t --> start OSGI Runtime....................................................");
                applicationArgs = Platform.getApplicationArgs();

                /* is single or test  */
            } else {
                logger.info("\t\t --> [0] start api server ....................................................");
                applicationArgs = getWebServerArguments();

            }
        } catch (Throwable e) {
            logger.info(
                    "\t\t [exception]--> [1] start api server ....................................................");
            applicationArgs = getWebServerArguments();
        }

        return applicationArgs;
    }

    /**
     * traditional web server argument
     * 
     * @return
     */
    private static String[] getWebServerArguments() {
        applicationArgs = new String[4];

        applicationArgs[0] = "-dbServer";
        applicationArgs[1] = System.getProperty("dbServer");
        if (applicationArgs[1] == null) {
            applicationArgs[0] = null;
            logger.error("**** System Initialize exception : Not found Tadpole engine db");

            return applicationArgs;
        }

        applicationArgs[2] = "-passwd";
        applicationArgs[3] = System.getProperty("passwd");
        if (applicationArgs[3] == null)
            applicationArgs[3] = PublicTadpoleDefine.SYSTEM_DEFAULT_PASSWORD;

        return applicationArgs;
    }
}