org.pentaho.supportutility.config.retriever.ConfigRetreiver.java Source code

Java tutorial

Introduction

Here is the source code for org.pentaho.supportutility.config.retriever.ConfigRetreiver.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.supportutility.config.retriever;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.apache.commons.io.FileUtils;

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

import org.pentaho.supportutility.config.constant.SupportUtilConstant;

public abstract class ConfigRetreiver extends ConfigCommonRetriever {

    protected Boolean bidiXml;
    protected Boolean tomcatXml;
    protected Boolean bidiBatFile;
    protected Boolean bidiProrperties;

    protected String serverName;
    protected String installType;
    protected String browserInfo;

    /**
     * 
     * @return installType(archive or manual or installer)
     */
    public String getInstallType() {
        return installType;
    }

    /**
     * 
     * @param installType
     *            (archive or manual or installer)
     */
    public void setInstallType(String installType) {
        this.installType = installType;
    }

    /**
     * 
     * @return bidiXml(return true if BI Server or DI Server Xml is checked at
     *         client)
     */
    public Boolean getBidiXml() {
        return bidiXml;
    }

    /**
     * 
     * @param bidiXml
     *            (set true if BI Server or DI Server Xml is checked at client)
     */
    public void setBidiXml(Boolean bidiXml) {
        this.bidiXml = bidiXml;
    }

    /**
     * 
     * @return bidiProrperties(return true if BI Server or DI Server Properties
     *         is checked at client)
     */
    public Boolean getBidiProrperties() {
        return bidiProrperties;
    }

    /**
     * 
     * @param bidiProrperties
     *            (set true if BI Server or DI Server Properties is checked at
     *            client)
     */
    public void setBidiProrperties(Boolean bidiProrperties) {
        this.bidiProrperties = bidiProrperties;
    }

    /**
     * 
     * @return bidiBatFile(return true if BI Server or DI Server Batch is
     *         checked at client)
     */
    public Boolean getBidiBatFile() {
        return bidiBatFile;
    }

    /**
     * 
     * @param bidiBatFile
     *            (set true if BI Server or DI Server Properties is checked at
     *            client)
     */
    public void setBidiBatFile(Boolean bidiBatFile) {
        this.bidiBatFile = bidiBatFile;
    }

    /**
     * 
     * @return tomcatXml(return true if BI Server or DI Server Tomcat XML is
     *         checked at client)
     */
    public Boolean getTomcatXml() {
        return tomcatXml;
    }

    /**
     * 
     * @param tomcatXml
     *            (set true if BI Server or DI Server Tomcat XMl is checked at
     *            client)
     */
    public void setTomcatXml(Boolean tomcatXml) {
        this.tomcatXml = tomcatXml;
    }

    /**
     * 
     * @return serverName(BI-Server or DI-Server or Spoon)
     */
    public String getServerName() {
        return serverName;
    }

    /**
     * 
     * @param serverName
     *            (BI-Server or DI-Server or Spoon)
     * 
     */
    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    /**
     * 
     * @return browserInfo(Browser text)
     */
    public String getBrowserInfo() {
        return browserInfo;
    }

    /**
     * 
     * @param browserInfo
     *            (Browser text)
     */
    public void setBrowserInfo(String browserInfo) {
        this.browserInfo = browserInfo;
    }

    /**
     * This is called from client to read and save selected configuration
     * 
     * @param props
     */
    public void readAndSaveConfiguration(Properties props) {
        readConfiguration(props);
    }

    /**
     * abstract method to read configuration
     * 
     * BrowserInfoRetriever - creates and writes browser information to a
     * browser.txt
     * 
     * ConnectivityConfigRetriever - creates and writes connectivity details to
     * a respective files
     * 
     * DataSourceRetriever - creates and writes data-source details to a
     * respective files
     * 
     * EnvironmentRetriever - creates and writes environment details to a
     * respective file
     * 
     * FileRetriever - creates and copies selected directory files
     * 
     * LicenseRetriever - creates and writes License details
     * 
     * LogRetriever - creates and copies log files
     * 
     * MD5Retriever - creates and writes checksum data to respective file
     * 
     * StructureRetriever - creates and writes structure details
     * 
     * TaskRetriever - creates and writes system process details
     * 
     * @param props
     *            -support properties
     */
    protected abstract void readConfiguration(Properties props);

    /**
     * create directory at specified path and name
     * 
     * @param destDir
     * @param dirName
     * @return boolean
     */
    protected boolean createDestDirectory(String destDir, String dirName) {

        Boolean result = false;
        File mkDirectory = new File(destDir);
        if (!mkDirectory.exists()) {
            try {
                FileUtils.forceMkdir(mkDirectory);
            } catch (IOException e) {
            }
            result = true;
        } else {
            result = true;
        }
        return result;
    }

    /**
     * create file at specified path
     * 
     * @param props
     * @param destDir
     * @param fileName
     * @return destination file object
     */
    protected File createDestFile(Properties props, String destDir, String fileName) {
        File file = null;
        String dirPath;
        if (destDir == null) {
            dirPath = props.getProperty(SupportUtilConstant.SUPP_INFO_DEST_PATH) + File.separator
                    + props.getProperty(SupportUtilConstant.SUPP_INF_DIR);
        } else {
            dirPath = props.getProperty(SupportUtilConstant.SUPP_INFO_DEST_PATH) + File.separator
                    + props.getProperty(SupportUtilConstant.SUPP_INF_DIR) + File.separator + destDir;
        }
        if (createDestDirectory(dirPath, null)) {
            if (fileName != null) {
                file = new File(dirPath + File.separator + fileName);
            } else {
                file = new File(dirPath);
            }
        }
        return file;
    }

    /**
     * This will be called from client to set checked configuration
     * 
     * @param serverXml
     * @param serverProperty
     * @param serverBat
     * @param tomcatXml
     */
    public void setSelectedFiles(boolean serverXml, boolean serverProperty, boolean serverBat, boolean tomcatXml) {
        this.bidiXml = serverXml;
        this.bidiProrperties = serverProperty;
        this.bidiBatFile = serverBat;
        this.tomcatXml = tomcatXml;
    }

    /**
     * To read and parse context.xml to get database connection details
     * 
     * @param filePath
     * @return database nodelist
     */
    protected NodeList parseContextXml(String filePath) {
        NodeList resourceNList = null;
        File contextXml = new File(filePath);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder;
        try {
            dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(contextXml);
            doc.getDocumentElement().normalize();
            resourceNList = doc.getElementsByTagName(SupportUtilConstant.RESOURCE);
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resourceNList;
    }

}