org.pentaho.support.cmd.CommandLineUtility.java Source code

Java tutorial

Introduction

Here is the source code for org.pentaho.support.cmd.CommandLineUtility.java

Source

/*
 * Copyright 2007 Pentaho Corporation. All rights reserved.
 * 
 * This software was developed by Pentaho Corporation and is provided under the terms
 * of the Mozilla Public License, Version 1.1, or any later version. You may not use
 * this file except in compliance with the license. If you need a copy of the license,
 * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
 * BI Platform. The Initial Developer is Pentaho Corporation.
 *
 * Software distributed under the Mozilla Public License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
 * the license for the specific language governing your rights and limitations.
 *
 * Created
 * @author
 * 
 */
package org.pentaho.support.cmd;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.pentaho.supportutil.cmd.constant.CMDConstant;
import org.pentaho.supportutility.config.factory.CofingRetrieverFactory;
import org.pentaho.supportutility.config.retriever.ConfigRetreiver;
import org.pentaho.supportutility.config.retriever.FileRetriever;
import org.pentaho.supportutility.util.SupportZipUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class CommandLineUtility {

    private static CofingRetrieverFactory factory;

    public static String PENTAHO_SOLU_PATH_MAN = null;

    public static Boolean serverXml = false;
    public static Boolean tomcatXml = false;
    public static Boolean serverBatFile = false;
    public static Boolean serverProrperties = false;

    public static int installationType = 0;
    public static int server = 0;

    static String currentWorkingDirectory = null;
    static String PENTAHO_SOLU_PATH = null;

    static StringBuilder supProp = null;
    static StringBuilder WEB_XML = null;

    public static void main(String[] args) {

        currentWorkingDirectory = System.getProperty(CMDConstant.USER_DIR);

        System.out.print(CMDConstant.NEW_LINE_SINGLE);
        System.out.print(CMDConstant.INSTALLATION_TYPE);
        System.out.print(CMDConstant.NEW_LINE_SINGLE);
        System.out.print(CMDConstant.INSTALLATION_OPTION);
        System.out.print(CMDConstant.NEW_LINE_SINGLE);

        executeInstallationType();

    }

    /**
     * accepts input command and sets installation type
     */
    private static void executeInstallationType() {

        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
        int option = 0;
        try {
            option = Integer.parseInt(buffer.readLine());
            setInstallationType(option);
        } catch (NumberFormatException ne) {
            System.out.println(CMDConstant.ERROR);
            executeInstallationType();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }

        System.out.print(CMDConstant.NEW_LINE_SINGLE);
        System.out.print(CMDConstant.FIRST_LINE);
        System.out.print(CMDConstant.NEW_LINE_SINGLE);
        System.out.print(CMDConstant.OPTION);
        System.out.print(CMDConstant.NEW_LINE_SINGLE);

        executeServerOption();

    }

    /**
     * based on selected server call respective server method
     */
    private static void executeServerOption() {

        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
        int option = 0;
        try {
            option = Integer.parseInt(buffer.readLine());
        } catch (NumberFormatException ne) {
            System.out.println(CMDConstant.ERROR);
            executeServerOption();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }

        if (option == 1) {
            executeBIServer();
        } else if (option == 2) {
            executeDIServer();
        }

    }

    /**
     * accepts input command line argument and executes BI-Service
     */
    private static void executeBIServer() {

        setServer(1);
        System.out.print(CMDConstant.NEW_LINE_SINGLE);
        System.out.print(CMDConstant.BI_SERVER);
        System.out.print(CMDConstant.NEW_LINE_SINGLE);
        System.out.print(CMDConstant.LIST_ONE);
        System.out.print(CMDConstant.LIST_TWO);
        System.out.print(CMDConstant.LIST_THREE);
        System.out.print(CMDConstant.LIST_FOUR);
        System.out.print(CMDConstant.LIST_FIVE);
        System.out.print(CMDConstant.LIST_SIX);
        System.out.print(CMDConstant.LIST_SEVEN);
        System.out.print(CMDConstant.NEW_LINE_SINGLE);
        System.out.print(CMDConstant.NEW_LINE_SINGLE);
        System.out.print(CMDConstant.ENTER_OPTION + CMDConstant.SEMICOLON);

        String selected[] = CommandLineService.processInput();
        if (selected.length == 0) {
            System.out.println(CMDConstant.ERROR);
            System.exit(0);
        } else {
            executeService(selected, CMDConstant.PENTAHO);
        }
    }

    /**
     * accepts input command line argument and executes DI-Service
     */
    private static void executeDIServer() {

        setServer(2);
        System.out.print(CMDConstant.NEW_LINE_SINGLE);
        System.out.print(CMDConstant.DI_SERVER);
        System.out.print(CMDConstant.NEW_LINE_SINGLE);
        System.out.print(CMDConstant.LIST_ONE);
        System.out.print(CMDConstant.LIST_TWO);
        System.out.print(CMDConstant.LIST_THREE);
        System.out.print(CMDConstant.LIST_FOUR);
        System.out.print(CMDConstant.LIST_FIVE);
        System.out.print(CMDConstant.LIST_SIX);
        System.out.print(CMDConstant.LIST_SEVEN);
        System.out.print(CMDConstant.NEW_LINE_SINGLE);
        System.out.print(CMDConstant.NEW_LINE_SINGLE);
        System.out.print(CMDConstant.ENTER_OPTION + CMDConstant.SEMICOLON);
        String selected[] = CommandLineService.processInput();
        executeService(selected, CMDConstant.PENTAHO_DI);

    }

    /**
     * loads the supportutil.xml file and creates instance of selected retriever
     * and executes
     * 
     * @param args
     * @param server
     */
    private static void executeService(String[] args, String server) {

        final Properties prop = loadSupportProperty();

        // if installation is manual and server is bi-server read web.xml
        if (getInstallationType() == 3 && getServer() == 1) {
            if (prop.getProperty(CMDConstant.BI_TOM_PATH) == null) {
                System.out.println(CMDConstant.ERROR_12);
                System.exit(0);
            } else {

                WEB_XML = new StringBuilder();
                WEB_XML.append(prop.getProperty(CMDConstant.BI_TOM_PATH)).append(File.separator)
                        .append(CMDConstant.WEB_APP).append(File.separator).append(CMDConstant.PENTAHO)
                        .append(File.separator).append(CMDConstant.WEB_INF).append(File.separator)
                        .append(CMDConstant.WEB_XML);

                PENTAHO_SOLU_PATH = getSolutionPath(server, WEB_XML.toString());
                prop.put(CMDConstant.PENTAHO_SOLU_PATH, PENTAHO_SOLU_PATH);
                prop.put(CMDConstant.BI_PATH, PENTAHO_SOLU_PATH);
            }

        } else if (getInstallationType() == 3 && getServer() == 2) {
            // if installation is manual and server is di-server read web.xml
            if (prop.getProperty(CMDConstant.DI_TOM_PATH) == null) {
                System.out.println(CMDConstant.ERROR_22);
                System.exit(0);
            } else {

                WEB_XML = new StringBuilder();
                WEB_XML.append(prop.getProperty(CMDConstant.DI_TOM_PATH)).append(File.separator)
                        .append(CMDConstant.WEB_APP).append(File.separator).append(CMDConstant.PENTAHO_DI)
                        .append(File.separator).append(CMDConstant.WEB_INF).append(File.separator)
                        .append(CMDConstant.WEB_XML);

                PENTAHO_SOLU_PATH = getSolutionPath(server, WEB_XML.toString());
                prop.put(CMDConstant.PENTAHO_SOLU_PATH, PENTAHO_SOLU_PATH);
                prop.put(CMDConstant.BI_PATH, PENTAHO_SOLU_PATH);
            }
        }

        if (getServer() == 1) {

            if (prop.get(CMDConstant.BI_PATH) == null) {

                System.out.println(CMDConstant.ERROR_1);
                System.exit(0);
            }
            if (prop.get(CMDConstant.BI_TOM_PATH) == null) {

                System.out.println(CMDConstant.ERROR_12);
                System.exit(0);
            } else {
                setBIServerPath(prop);
            }

        } else if (getServer() == 2) {
            if (prop.get(CMDConstant.DI_PATH) == null) {
                System.out.println(CMDConstant.ERROR_2);
                System.exit(0);
            }
            if (prop.get(CMDConstant.DI_TOM_PATH) == null) {
                System.out.println(CMDConstant.ERROR_22);
                System.exit(0);
            } else {
                setDIServerPath(prop);
            }
        }

        ApplicationContext context = new ClassPathXmlApplicationContext(CMDConstant.SPRING_FILE_NAME);
        factory = (CofingRetrieverFactory) context.getBean(CMDConstant.COFINGRETRIEVERACTORY);
        ConfigRetreiver[] config = factory.getConfigRetrevier(args);

        ExecutorService service = Executors.newFixedThreadPool(10);
        // loop for all created instance and call respective retriever
        for (final ConfigRetreiver configobj : config) {

            if (getServer() == 1) {

                configobj.setBISeverPath(prop);
                configobj.setServerName("biserver");
            } else if (getServer() == 2) {

                configobj.setDIServerPath(prop);
                configobj.setServerName("diserver");
            }

            if (getInstallationType() == 1) {

                // if installation is installer set Installer
                configobj.setInstallType("Installer");
            } else if (getInstallationType() == 2) {

                // if installation is Archive set Archive
                configobj.setInstallType("Archive");
            } else if (getInstallationType() == 3) {

                // if installation is Manual set Manual
                configobj.setInstallType("Manual");
            }

            // if instance if fileretriever sets required detail
            if (configobj instanceof FileRetriever) {

                configobj.setBidiXml(serverXml);
                configobj.setBidiBatFile(serverBatFile);
                configobj.setBidiProrperties(serverProrperties);
                configobj.setTomcatXml(tomcatXml);
            }

            service.execute(new Runnable() {
                public void run() {
                    configobj.readAndSaveConfiguration(prop);
                }
            });

        }

        try {
            service.shutdown();
            Thread.sleep(60000);

            // call for zip
            if (SupportZipUtil.zipFile(prop)) {

                File file = new File(prop.getProperty(CMDConstant.SUPP_INFO_DEST_PATH) + File.separator
                        + prop.getProperty(CMDConstant.SUPP_INF_DIR));
                if (file.exists()) {
                    // call for delete empty directory
                    delete(file);
                    System.exit(0);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * sets bi-server paths
     * 
     * @param prop
     */
    private static void setBIServerPath(Properties prop) {

        prop.put(CMDConstant.TOM_PATH, prop.getProperty(CMDConstant.BI_TOM_PATH));

        WEB_XML = new StringBuilder();
        WEB_XML.append(prop.getProperty(CMDConstant.BI_TOM_PATH)).append(File.separator).append(CMDConstant.WEB_APP)
                .append(File.separator).append(CMDConstant.PENTAHO).append(File.separator)
                .append(CMDConstant.WEB_INF).append(File.separator).append(CMDConstant.WEB_XML);

        prop.put(CMDConstant.WEB_XML_PATH, WEB_XML.toString());

        if (getInstallationType() != 3) {

            prop.put(CMDConstant.PENTAHO_SOLU_PATH,
                    prop.getProperty(CMDConstant.BI_PATH) + File.separator + CMDConstant.PENTAHO_SOL_DIR);
        }

        if (getInstallationType() == 1) {

            prop.put(CMDConstant.LOG_FILE_PATH,
                    prop.getProperty(CMDConstant.BI_TOM_PATH) + File.separator + CMDConstant.WEB_APP
                            + File.separator + CMDConstant.PENTAHO + File.separator + CMDConstant.WEB_INF
                            + File.separator + CMDConstant.CLASSES + File.separator + CMDConstant.LOG4J_XML);
        } else {

            prop.put(CMDConstant.LOG_FILE_PATH, prop.getProperty(CMDConstant.BI_TOM_PATH) + File.separator
                    + CMDConstant.TOM_LOG + File.separator + CMDConstant.LOG_FILE);
        }

    }

    /**
     * sets di-server paths
     * 
     * @param prop
     */
    private static void setDIServerPath(Properties prop) {

        prop.put(CMDConstant.TOM_PATH, prop.getProperty(CMDConstant.DI_TOM_PATH));

        WEB_XML = new StringBuilder();
        WEB_XML.append(prop.getProperty(CMDConstant.DI_TOM_PATH)).append(File.separator)
                .append(prop.getProperty(CMDConstant.PENTAHO_DI)).append(File.separator)
                .append(prop.getProperty(CMDConstant.WEB_APP)).append(File.separator)
                .append(prop.getProperty(CMDConstant.WEB_INF)).append(File.separator)
                .append(prop.getProperty(CMDConstant.WEB_XML));

        prop.put(CMDConstant.WEB_XML_PATH, WEB_XML.toString());

        if (getInstallationType() != 3) {

            prop.put(CMDConstant.PENTAHO_SOLU_PATH,
                    prop.getProperty(CMDConstant.DI_PATH) + File.separator + CMDConstant.PENTAHO_SOL_DIR);
        }

        if (getInstallationType() == 1) {

            prop.put(CMDConstant.LOG_FILE_PATH,
                    prop.getProperty(CMDConstant.DI_TOM_PATH) + File.separator + CMDConstant.WEB_APP
                            + File.separator + CMDConstant.PENTAHO_DI + File.separator + CMDConstant.WEB_INF
                            + File.separator + CMDConstant.CLASSES + File.separator + CMDConstant.LOG4J_XML);
        } else {

            prop.put(CMDConstant.LOG_FILE_PATH, prop.getProperty(CMDConstant.DI_TOM_PATH) + File.separator
                    + CMDConstant.TOM_LOG + File.separator + CMDConstant.LOG_FILE);
        }

    }

    /**
     * parses web.xml file the get pentaho-solution path if installation is
     * manual
     * 
     * @param server
     * @param webxml
     * @return
     */
    private static String getSolutionPath(String server, String webxml) {

        String pentahoSolPath = null;
        String os = System.getProperty(CMDConstant.OS).toLowerCase().substring(0, 3);

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        File xmlFile = new File(webxml);
        try {
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(xmlFile);
            NodeList paramValueList = doc.getElementsByTagName(CMDConstant.XML_PARAM_VALUE);

            for (int i = 0; i < paramValueList.getLength(); i++) {
                Node paramNode = paramValueList.item(i);
                if (paramNode.getTextContent().contains(CMDConstant.PENTAHO_SOL_DIR)) {

                    if (os.equals("win")) {
                        pentahoSolPath = paramNode.getTextContent().replace("/", "\\");
                    } else {
                        pentahoSolPath = paramNode.getTextContent();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pentahoSolPath;
    }

    /**
     * read support.properties file
     * 
     * @return
     */
    private static Properties loadSupportProperty() {

        supProp = new StringBuilder();
        supProp.append(currentWorkingDirectory).append(File.separator).append(CMDConstant.CMD_RSC)
                .append(File.separator);

        Properties props = new Properties();

        try {
            props.load(new FileInputStream(supProp.toString() + File.separator + CMDConstant.SUP_PROP_FILE));
            props.put(CMDConstant.ENV_FILE_PATH, supProp.toString() + File.separator + CMDConstant.ENV_FILE_NAME);
            props.put(CMDConstant.MD5_PATH, supProp.toString());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return props;
    }

    /**
     * deletes the empty directory
     * 
     * @param file
     */
    private static void delete(File file) {
        if (file.isDirectory()) {
            if (file.list().length == 0) {
                file.delete();
            } else {
                String files[] = file.list();
                for (String temp : files) {
                    File fileDelete = new File(file, temp);
                    delete(fileDelete);
                }
                if (file.list().length == 0) {
                    file.delete();
                }
            }
        } else {
            file.delete();
        }
    }

    /**
     * 
     * @return installationType(installer,manual,archive)
     */
    public static int getInstallationType() {
        return installationType;
    }

    /**
     * 
     * @param installationType
     *            (installer,manual,archive)
     */
    public static void setInstallationType(int installationType) {
        CommandLineUtility.installationType = installationType;
    }

    /**
     * 
     * @return server (BI or DI server)
     */
    public static int getServer() {
        return server;
    }

    /**
     * 
     * @param server
     *            (BI or Di Server)
     */
    public static void setServer(int server) {
        CommandLineUtility.server = server;
    }

}