org.pentaho.support.di.server.DISupportUtilityServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.pentaho.support.di.server.DISupportUtilityServiceImpl.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.di.server;

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

import org.pentaho.platform.engine.core.system.PentahoSystem;

import org.pentaho.support.di.shared.DIConstant;
import org.pentaho.support.di.shared.DISelectedItem;
import org.pentaho.support.di.client.DISupportUtilityService;

import org.pentaho.supportutility.util.SupportZipUtil;
import org.pentaho.supportutility.config.retriever.FileRetriever;
import org.pentaho.supportutility.config.retriever.ConfigRetreiver;
import org.pentaho.supportutility.config.factory.CofingRetrieverFactory;
import org.pentaho.supportutility.config.retriever.BrowserInfoRetriever;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class DISupportUtilityServiceImpl extends RemoteServiceServlet implements DISupportUtilityService {

    private static final long serialVersionUID = 1L;

    String PENTAHO_SOLU_PATH = PentahoSystem.getApplicationContext().getSolutionRootPath();
    DISelectedItem selected = null;

    String DI_PATH = PENTAHO_SOLU_PATH + File.separator + DIConstant.DOT;

    String TOM_PATH = DI_PATH + File.separator + DIConstant.TOMCAT_DIR;

    String ENV_FILE_PATH = TOM_PATH + File.separator + DIConstant.WEB_APP + File.separator + DIConstant.PENTAHO_DI
            + File.separator + DIConstant.SUPPORTUTILITYDI + File.separator + DIConstant.RESOURCE + File.separator
            + DIConstant.ENV_PROP_FILE;

    String MD5_PATH = TOM_PATH + File.separator + DIConstant.WEB_APP + File.separator + DIConstant.PENTAHO_DI
            + File.separator + DIConstant.SUPPORTUTILITYDI + File.separator + DIConstant.RESOURCE;

    String WEB_XML_PATH = TOM_PATH + File.separator + DIConstant.WEB_APP + File.separator + DIConstant.PENTAHO_DI
            + File.separator + DIConstant.WEB_INF + File.separator + DIConstant.WEB_XML;;

    String LOG_FILE_PATH = null;

    /**
     * reads the support.properties and sets the required path
     */
    @Override
    public boolean readandsaveSelectedConfiguration(List<String> selectedItem, DISelectedItem seleObj) {

        selected = seleObj;

        Boolean result = false;
        Properties supProp = loadSupportProperty();
        // based on installation type sets required log path
        if (selected.getInstallType().equalsIgnoreCase(DIConstant.BI_INSTALLER)) {

            LOG_FILE_PATH = TOM_PATH + File.separator + DIConstant.WEB_APP + File.separator + DIConstant.PENTAHO_DI
                    + File.separator + DIConstant.WEB_INF + File.separator + DIConstant.CLASSES + File.separator
                    + DIConstant.LOG4J_XML;
        } else if (selected.getInstallType().equalsIgnoreCase(DIConstant.BI_ARCHIVE)
                || selected.getInstallType().equalsIgnoreCase(DIConstant.BI_MANUAL)) {

            LOG_FILE_PATH = TOM_PATH + File.separator + DIConstant.LOG_DIR + File.separator + DIConstant.LOG_FILE;
        }

        supProp.put(DIConstant.DI_PATH, DI_PATH);
        supProp.put(DIConstant.TOM_PATH, TOM_PATH);
        supProp.put(DIConstant.MD5_PATH, MD5_PATH);
        supProp.put(DIConstant.ENV_FILE_PATH, ENV_FILE_PATH);
        supProp.put(DIConstant.WEB_XML_PATH, WEB_XML_PATH);
        supProp.put(DIConstant.LOG_FILE_PATH, LOG_FILE_PATH);
        supProp.put(DIConstant.PENTAHO_SOLU_PATH, PENTAHO_SOLU_PATH);

        // invokes executeService method
        if (executeService((String[]) selectedItem.toArray(new String[selectedItem.size()]), supProp)) {

            selected.setBidiXml(false);
            selected.setBidiBatFile(false);
            selected.setBidiProrperties(false);
            selected.setTomcatXml(false);
            selected.setInstallType(null);
            result = true;
        }

        return result;
    }

    /**
     * loads spring configuration SupportUtil.xml file and creates instance of
     * selected retriever
     * 
     * @param args
     * @param prop
     * @return
     */
    private boolean executeService(String[] args, final Properties prop) {
        Boolean result = false;
        String SPRING_CONFIG_CLASS = "cofingRetrieverFactory";

        try {

            ApplicationContext context = new ClassPathXmlApplicationContext(DIConstant.SPRING_FILE_NAME);
            final CofingRetrieverFactory factory = (CofingRetrieverFactory) context.getBean(SPRING_CONFIG_CLASS);
            ConfigRetreiver[] config = factory.getConfigRetrevier(args);

            ExecutorService service = Executors.newFixedThreadPool(10);

            // loop through created retriever instance and calls respective
            // retriever
            for (final ConfigRetreiver configobj : config) {

                configobj.setDIServerPath(prop);
                configobj.setServerName(selected.getServerName());
                configobj.setInstallType(selected.getInstallType());

                // if retriever instance is FileRetriever, sets required detail
                if (configobj instanceof FileRetriever) {

                    configobj.setBidiXml(selected.getBidiXml());
                    configobj.setBidiBatFile(selected.getBidiBatFile());
                    configobj.setBidiProrperties(selected.getBidiProrperties());
                    configobj.setTomcatXml(selected.getTomcatXml());
                }

                // if retriever instance is BrowserInfoRetriever, sets Browser
                // info detail
                if (configobj instanceof BrowserInfoRetriever) {
                    configobj.setBrowserInfo(selected.getBrowserInfo());
                }

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

            }

            service.shutdown();
            Thread.sleep(75000);

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

                File file = new File(prop.getProperty(DIConstant.SUPP_INFO_DEST_PATH) + File.separator
                        + prop.getProperty(DIConstant.SUPP_INF_DIR));
                if (file.exists()) {
                    // call delete
                    delete(file);
                }

                result = true;
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    // load the support.properties
    private Properties loadSupportProperty() {

        String propPath = MD5_PATH + File.separator + DIConstant.SUPPORT_PROP_FILE;

        Properties props = new Properties();

        try {

            props.load(new FileInputStream(propPath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return props;
    }

    // delete the empty directory
    private 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();
        }
    }

}