com.bluexml.side.util.deployer.war.DirectWebAppsDeployer.java Source code

Java tutorial

Introduction

Here is the source code for com.bluexml.side.util.deployer.war.DirectWebAppsDeployer.java

Source

/*
Copyright (C) 2007-20013  BlueXML - www.bluexml.com
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.
    
You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.bluexml.side.util.deployer.war;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import com.bluexml.side.util.componentmonitor.MonitorWriter;
import com.bluexml.side.util.libs.FileHelper;
import com.bluexml.side.util.libs.IFileHelper;
import com.bluexml.side.util.libs.zip.ZipManager;

public abstract class DirectWebAppsDeployer extends WarDeployer {
    boolean cleanned = false;
    protected List<String> deployedFiles = new ArrayList<String>();

    public DirectWebAppsDeployer(String cleanKey, String webappName, String webappKeyName, String packageExt) {
        super(cleanKey, null, webappName, webappKeyName);
        this.packageExt = packageExt;
        //      tzh = new TrueZipHelper(packageExt);
    }

    //   protected TrueZipHelper tzh = null;
    protected File wkdir = null;
    protected String packageExt = null;
    protected boolean incremental = true;
    protected boolean webappReloading = false;

    public File getWorkingDir() throws Exception {
        if (wkdir == null) {
            throw new Exception(Activator.Messages.getString("DirectWebAppsDeployer.1")); //$NON-NLS-1$
        }
        return wkdir;
    }

    @Override
    protected void clean(File fileToDeploy) throws Exception {
        monitor.beginTask(Activator.Messages.getString("DirectWebAppsDeployer.0")); //$NON-NLS-1$
        boolean result = true;
        // delete exploded webapps
        if (getDeployedWebbAppFolder().exists()) {
            monitor.getLog().addInfoLog(Activator.Messages.getString("WarDeployer.1"), //$NON-NLS-1$
                    Activator.Messages.getString("WarDeployer.2", getDeployedWebbAppFolder().getName()), ""); //$NON-NLS-1$
            FileHelper.deleteFile(getDeployedWebbAppFolder(), false);
        }

        // unzip .war or .org if exist
        if (getBackupWarFile().exists()) {
            monitor.getLog().addInfoLog(Activator.Messages.getString("WarDeployer.1"), //$NON-NLS-1$
                    Activator.Messages.getString("DirectWebAppsDeployer.4", getWarToPatchFile().getName()), ""); //$NON-NLS-1$

            ZipManager.unzip(getBackupWarFile(), getDeployedWebbAppFolder(), true, true);
            //         TrueZipHelper tzh2 = new TrueZipHelper(backupWarExt);
            //         result = tzh2.copyFiles(getBackupWarFile(), getDeployedWebbAppFolder(), true);
        } else if (getWarToPatchFile().exists()) {
            monitor.getLog().addInfoLog(Activator.Messages.getString("WarDeployer.5"), //$NON-NLS-1$
                    Activator.Messages.getString("DirectWebAppsDeployer.4", getWarToPatchFile().getName()), ""); //$NON-NLS-1$
            ZipManager.unzip(getWarToPatchFile(), getDeployedWebbAppFolder(), true, true);
            //         TrueZipHelper tzh2 = new TrueZipHelper(warToPatchExt);
            //         result = tzh2.copyFiles(getWarToPatchFile(), getDeployedWebbAppFolder(), true);
        } else {
            throw new Exception(
                    Activator.Messages.getString("DirectWebAppsDeployer.7") + getWarToPatchFile().getAbsolutePath() //$NON-NLS-1$
                            + Activator.Messages.getString("DirectWebAppsDeployer.9")); //$NON-NLS-1$
        }
        if (!result) {
            throw new Exception(Activator.Messages.getString("DirectWebAppsDeployer.10")); //$NON-NLS-1$
        }
        cleanned = true;
        monitor.taskDone(Activator.Messages.getString("DirectWebAppsDeployer.2")); //$NON-NLS-1$
    }

    public FileFilter getFileFilter(final DeployMode mode) {
        FileFilter incrementalFileFilter = new FileFilter() {

            public boolean accept(File pathname) {
                if (pathname.isFile()) {
                    try {
                        String fileExt = FileHelper.getFileExt(pathname);
                        if (fileExt.equals(packageExt)) {
                            // test if package is newer than the deployed webapp
                            if (cleanned || !incremental || mode.equals(DeployMode.CUSTOM)) {
                                return true;
                            } else {
                                // incremental allowed
                                long module = pathname.lastModified();
                                File deployedWebbAppFolder = getIncrementalLastDeployedFlag();
                                long webapp = deployedWebbAppFolder.lastModified();

                                if (module > webapp) {
                                    System.out.println("module is newer ");
                                    // module must be deployed
                                    return true;
                                } else {
                                    System.out.println("module skipped by incremental deployer :" + pathname);
                                    Date dateModule = new Date(module);
                                    Date dateWebapp = new Date(webapp);
                                    System.out.println("module :" + pathname + "[" + dateModule + "]");
                                    System.out.println("webapp :" + deployedWebbAppFolder + "[" + dateWebapp + "]");
                                }
                            }
                        }

                    } catch (Exception e) {
                        System.out.println("fileName :" + pathname);
                        e.printStackTrace();
                    }
                }

                return false;
            }
        };

        return incrementalFileFilter;
    }

    @Override
    protected void deployProcess(File fileToDeploy, DeployMode mode) throws Exception {
        System.out.println("DirectWebAppsDeployer.deployProcess() :" + this);
        FileFilter fileFilter = getFileFilter(mode);
        File deployedWebbAppFolder = getDeployedWebbAppFolder();
        if (!deployedWebbAppFolder.exists()) {
            this.clean(fileToDeploy);
        }
        if (fileToDeploy.exists() && fileToDeploy.isDirectory()) {
            for (File f : fileToDeploy.listFiles(fileFilter)) {
                List<String> list = deployFile(f).getList();

                deployedFiles.addAll(list);

                // check if files are copied more than one time, this detect file collision
                //            for (String file : deployedFiles) {
                //               int frequency = Collections.frequency(deployedFiles, file);
                //               if (frequency > 1) {
                //                  // conflict detected ...
                //                  monitor.addWarningTextAndLog("Beware the file "+file+"have been overrided by module :"+fileToDeploy, "");
                //               }
                //            }

            }
        } else if (fileToDeploy.exists() && fileToDeploy.isFile() && fileFilter.accept(fileToDeploy)) {
            deployedFiles.addAll(deployFile(fileToDeploy).getList());
        } else {
            monitor.addWarningTextAndLog(Activator.Messages.getString("WarDeployer.5"), "");
        }

        // deploy process is done, we need to mark the deployed webapp      
        File incrementalLastDeploeydFlag = getIncrementalLastDeployedFlag();
        System.out.println("DirectWebAppsDeployer.deployProcess() touch " + incrementalLastDeploeydFlag);
        FileUtils.touch(incrementalLastDeploeydFlag);

        if (webappReloading) {
            System.out.println("DirectWebAppsDeployer.deployProcess() touch " + getWebAppXMLFile());
            // we touch web.xml too to let tomcat reload the webapp, some webapps should not be restarted
            FileUtils.touch(getWebAppXMLFile());
        }

    }

    private MonitorWriter deployFile(File f) throws Exception {
        monitor.getLog().addInfoLog(Activator.Messages.getString("WarDeployer.6"), //$NON-NLS-1$
                Activator.Messages.getString("WarDeployer.7", f.getName()), ""); //$NON-NLS-1$
        File explodedPackage = new File(getWorkingDir(), f.getName().replaceAll("\\." + packageExt, "")); //$NON-NLS-1$ //$NON-NLS-2$

        explodedPackage.mkdirs();
        // unzip files in tmp
        //      tzh.copyFiles(f, explodedPackage, true);
        ZipManager.unzip(f, explodedPackage, true, true);

        Map<String, File> map = createMapper(explodedPackage);
        List<File> fileList = FileHelper.listAll(explodedPackage);

        return dispatchFiles(fileList, map);
    }

    @Override
    protected void preProcess(File fileToDeploy) throws Exception {
        File out = IFileHelper.getFile(IFileHelper.getIFolder(getTargetPath()));
        this.wkdir = new File(out, "deployer_" + getClass().getName());
        if (getWorkingDir().exists()) {
            monitor.getLog().addInfoLog(Activator.Messages.getString("DirectWebAppsDeployer.5"), //$NON-NLS-1$
                    Activator.Messages.getString("DirectWebAppsDeployer.17", getWorkingDir().getName()), ""); //$NON-NLS-1$
            FileHelper.deleteFile(getWorkingDir(), false);
        }
        monitor.getLog().addInfoLog(Activator.Messages.getString("DirectWebAppsDeployer.5"), //$NON-NLS-1$
                Activator.Messages.getString("WarDeployer.19", getWorkingDir().getName()), ""); //$NON-NLS-1$
        getWorkingDir().mkdirs();
    }

    public Map<String, File> createMapper(File packageDirectory) {
        File packageFolder = getDeployedWebbAppFolder();

        Map<String, File> mapper = new HashMap<String, File>();
        // all files just copied in same directory organization, so filter is on
        // packageDirectory
        addToMap(mapper, packageDirectory.getAbsolutePath(), packageFolder, "/");
        return mapper;
    }

    protected void addToMap(Map<String, File> map, String key, File ampRoot, String target) {
        map.put(key.replace("/", File.separator), //$NON-NLS-1$
                createTargetFolders(ampRoot, target.replace("/", File.separator))); //$NON-NLS-1$
    }

    protected File createTargetFolders(File ampRoot, String p) {
        File dir = new File(ampRoot, p);
        dir.mkdirs();
        return dir;
    }

    protected MonitorWriter dispatchFiles(List<File> files, Map<String, File> mapper) throws Exception {
        List<String> copyFiles = new ArrayList<String>();

        MonitorWriter mw = new MonitorWriter(monitor, Activator.Messages.getString("DirectWebAppsDeployer.3"), ""); //$NON-NLS-1$ //$NON-NLS-2$

        for (File f : files) {
            for (Map.Entry<String, File> ent : mapper.entrySet()) {
                if (f.getAbsolutePath().indexOf(ent.getKey()) != -1) {
                    String path = f.getAbsolutePath();
                    String pathIn = ent.getValue().getAbsolutePath() + File.separator
                            + path.substring(path.indexOf(ent.getKey()) + ent.getKey().length());
                    File dest = new File(pathIn);
                    dest.getParentFile().mkdirs();

                    copyFiles.addAll(FileHelper.copyFiles(f, dest, true, mw));
                }
            }
        }
        mw.write("OVERRIDEN FILES" + copyFiles.size());
        for (String string : copyFiles) {
            mw.write("\t" + string);
        }
        return mw;
    }

    public File getWebAppXMLFile() {
        String path = FilenameUtils.separatorsToSystem("/WEB-INF/web.xml");
        return new File(getDeployedWebbAppFolder().getAbsolutePath() + path);
    }

    public File getIncrementalLastDeployedFlag() {
        String path = FilenameUtils.separatorsToSystem("/META-INF/lastDeployed.txt");
        return new File(getDeployedWebbAppFolder().getAbsolutePath() + path);
    }

}