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

Java tutorial

Introduction

Here is the source code for org.pentaho.supportutility.config.retriever.FileExtensionUtil.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.FileFilter;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.FileFileFilter;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.pentaho.supportutility.config.constant.SupportUtilConstant;

public class FileExtensionUtil {

    /**
     * Get the Server Xmls from server
     * 
     * @param biPath
     * @param dstPath
     * @return
     */
    @SuppressWarnings("deprecation")
    public static boolean getServerXmlFile(String biPath, String dstPath) {

        Boolean result = false;
        File srcDir = new File(biPath);
        File dstDir = new File(dstPath);
        IOFileFilter SuffixFilter = FileFilterUtils.suffixFileFilter(SupportUtilConstant.DOT_XML);
        IOFileFilter applySuffixFilter = FileFilterUtils.andFileFilter(FileFileFilter.FILE, SuffixFilter);
        IOFileFilter finalFilter = FileFilterUtils.orFileFilter(applySuffixFilter, DirectoryFileFilter.DIRECTORY);
        try {
            FileUtils.copyDirectory(srcDir, dstDir, finalFilter, false);
            if (deleteEmptyDir(dstDir)) {
                result = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 
     * Get the Server Properties from Server
     * 
     * @param biPath
     * @param dstPath
     * @return
     */
    @SuppressWarnings("deprecation")
    public static boolean getServerProperties(String biPath, String dstPath) {

        Boolean result = false;
        File srcDir = new File(biPath);
        File dstDir = new File(dstPath);
        IOFileFilter SuffixFilter = FileFilterUtils.suffixFileFilter(SupportUtilConstant.DOT_PROP);
        IOFileFilter hibFilter = FileFilterUtils.nameFileFilter(SupportUtilConstant.SPRING_JDBC_PROP);
        FileFilter ignorehibFilter = FileFilterUtils.notFileFilter(hibFilter);
        IOFileFilter ldapFilter = FileFilterUtils.nameFileFilter(SupportUtilConstant.SPRING_LDAP_PROP);
        FileFilter ignoreldapFilter = FileFilterUtils.notFileFilter(ldapFilter);
        IOFileFilter filter1 = FileFilterUtils.asFileFilter(ignorehibFilter);
        IOFileFilter filter2 = FileFilterUtils.asFileFilter(ignoreldapFilter);
        IOFileFilter applyIgnoreFilter = FileFilterUtils.andFileFilter(filter1, filter2);
        IOFileFilter applySuffixFilter = FileFilterUtils.andFileFilter(FileFileFilter.FILE, SuffixFilter);
        IOFileFilter applySuffixIgnore = FileFilterUtils.andFileFilter(applySuffixFilter, applyIgnoreFilter);
        IOFileFilter finalFilter = FileFilterUtils.orFileFilter(applySuffixIgnore, DirectoryFileFilter.DIRECTORY);
        try {
            FileUtils.copyDirectory(srcDir, dstDir, finalFilter, false);
            if (deleteEmptyDir(dstDir)) {
                result = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * Get the Server Batch from Server
     * 
     * @param biPath
     * @param dstPath
     * @return
     */
    @SuppressWarnings("deprecation")
    public static boolean getServerStartUpFile(String biPath, String dstPath) {

        Boolean result = false;
        File srcDir = new File(biPath);
        File dstDir = new File(dstPath);
        IOFileFilter SuffixFilter = null;
        String os = System.getProperty(SupportUtilConstant.OS_NAME).toLowerCase().substring(0, 3);
        if (os.equals("win")) {
            SuffixFilter = FileFilterUtils.suffixFileFilter(SupportUtilConstant.DOT_BAT);
        } else if (os.equals("lin") || os.equals("mac")) {
            SuffixFilter = FileFilterUtils.suffixFileFilter(SupportUtilConstant.DOT_SH);
        }

        IOFileFilter applySuffixFilter = FileFilterUtils.andFileFilter(FileFileFilter.FILE, SuffixFilter);
        IOFileFilter finalFilter = FileFilterUtils.orFileFilter(applySuffixFilter, DirectoryFileFilter.DIRECTORY);
        try {
            FileUtils.copyDirectory(srcDir, dstDir, finalFilter, false);
            if (deleteEmptyDir(dstDir)) {
                result = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * Get the Tomcat Xmls from Server
     * 
     * @param tomPath
     * @param dstPath
     * @return
     */
    @SuppressWarnings("deprecation")
    public static boolean getTomcatXmlFiles(String tomPath, String dstPath) {

        Boolean result = false;
        File srcDir = new File(tomPath);
        File dstDir = new File(dstPath);
        IOFileFilter SuffixFilter = FileFilterUtils.suffixFileFilter(SupportUtilConstant.DOT_XML);
        IOFileFilter contextFilter = FileFilterUtils.nameFileFilter(SupportUtilConstant.CONT_XML);
        FileFilter ignorecontextFilter = FileFilterUtils.notFileFilter(contextFilter);
        IOFileFilter pentahoFilter = FileFilterUtils.nameFileFilter(SupportUtilConstant.PENT_XML);
        FileFilter ignorepentahoFilter = FileFilterUtils.notFileFilter(pentahoFilter);
        IOFileFilter filter1 = FileFilterUtils.asFileFilter(ignorecontextFilter);
        IOFileFilter filter2 = FileFilterUtils.asFileFilter(ignorepentahoFilter);
        IOFileFilter applyIgnoreFilter = FileFilterUtils.andFileFilter(filter1, filter2);
        IOFileFilter applySuffixFilter = FileFilterUtils.andFileFilter(FileFileFilter.FILE, SuffixFilter);
        IOFileFilter applySuffixIgnore = FileFilterUtils.andFileFilter(applySuffixFilter, applyIgnoreFilter);
        IOFileFilter finalFilter = FileFilterUtils.orFileFilter(applySuffixIgnore, DirectoryFileFilter.DIRECTORY);
        try {
            FileUtils.copyDirectory(srcDir, dstDir, finalFilter, false);
            if (deleteEmptyDir(dstDir)) {
                result = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * deletes Empty directories
     * 
     * @param dstPath
     * @return
     */
    private static boolean deleteEmptyDir(File dstPath) {

        Boolean result = false;
        if (dstPath.exists()) {
            try {
                delete(dstPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * deletes Empty directories
     * 
     * @param file
     * @throws IOException
     */
    private static void delete(File file) throws IOException {

        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();
                }
            }
        }
    }

}