org.pentaho.support.bi.server.BISupportUtilityServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.pentaho.support.bi.server.BISupportUtilityServiceImpl.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.bi.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.bi.client.BISupportUtilityService;
import org.pentaho.support.bi.shared.BIConstant;
import org.pentaho.support.bi.shared.BISelectedItem;
import org.pentaho.supportutility.config.factory.CofingRetrieverFactory;
import org.pentaho.supportutility.config.retriever.BrowserInfoRetriever;
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 com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class BISupportUtilityServiceImpl extends RemoteServiceServlet implements BISupportUtilityService {

    private static final long serialVersionUID = 1L;

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

    String BI_PATH = PENTAHO_SOLU_PATH + File.separator + BIConstant.DOT;

    String TOM_PATH = BI_PATH + File.separator + BIConstant.TOMCAT_DIR;

    String ENV_FILE_PATH = PENTAHO_SOLU_PATH + File.separator + BIConstant.SYSTEM + File.separator
            + BIConstant.SUPPORTUTILITYBI + File.separator + BIConstant.RESOURCE + File.separator
            + BIConstant.ENV_PROP_FILE;

    String MD5_PATH = PENTAHO_SOLU_PATH + File.separator + BIConstant.SYSTEM + File.separator
            + BIConstant.SUPPORTUTILITYBI + File.separator + BIConstant.RESOURCE;

    String WEB_XML_PATH = TOM_PATH + File.separator + BIConstant.WEB_APP + File.separator + BIConstant.PENTAHO
            + File.separator + BIConstant.WEB_INF + File.separator + BIConstant.WEB_XML;;

    String LOG_FILE_PATH = null;

    /*
     * (non-Javadoc)
     * 
     * @see org.pentaho.support.bi.client.BISupportUtilityService#
     * readandsaveSelectedConfiguration(java.util.List,
     * org.pentaho.support.bi.shared.BISelectedItem)
     */
    @Override
    public boolean readandsaveSelectedConfiguration(List<String> selectedItem, BISelectedItem seleObj) {

        selected = seleObj;

        Boolean result = false;
        Properties supProp = loadSupportProperty();

        if (selected.getInstallType().equalsIgnoreCase(BIConstant.BI_INSTALLER)) {

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

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

        supProp.put(BIConstant.BI_PATH, BI_PATH);
        supProp.put(BIConstant.TOM_PATH, TOM_PATH);
        supProp.put(BIConstant.MD5_PATH, MD5_PATH);
        supProp.put(BIConstant.ENV_FILE_PATH, ENV_FILE_PATH);
        supProp.put(BIConstant.WEB_XML_PATH, WEB_XML_PATH);
        supProp.put(BIConstant.LOG_FILE_PATH, LOG_FILE_PATH);
        supProp.put(BIConstant.PENTAHO_SOLU_PATH, PENTAHO_SOLU_PATH);

        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(BIConstant.SPRING_FILE_NAME);
            final CofingRetrieverFactory factory = (CofingRetrieverFactory) context.getBean(SPRING_CONFIG_CLASS);
            ConfigRetreiver[] config = factory.getConfigRetrevier(args);

            ExecutorService service = Executors.newFixedThreadPool(10);

            // based on the instance created respective retriever is called
            for (final ConfigRetreiver configobj : config) {

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

                // if file retriever instance set required detail
                if (configobj instanceof FileRetriever) {

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

                // if browser retriever instance set required detail
                if (configobj instanceof BrowserInfoRetriever) {
                    configobj.setBrowserInfo(selected.getBrowserInfo());
                }

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

            }

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

            // calls ziputility for zip
            if (SupportZipUtil.zipFile(prop)) {

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

                result = true;
            }

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

        return result;
    }

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

        String propPath = PENTAHO_SOLU_PATH + File.separator + BIConstant.SYSTEM + File.separator
                + BIConstant.SUPPORTUTILITYBI + File.separator + BIConstant.RESOURCE + File.separator
                + BIConstant.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 file
     * 
     * @param file
     */
    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();
        }

    }

}