Search in sources :

Example 1 with DescriptorUpdater

use of com.centurylink.mdw.plugin.project.extensions.DescriptorUpdater in project mdw-designer by CenturyLinkCloud.

the class ProjectUpdater method addWebLibs.

/**
 * Update WEB-INF/lib jars in MDWWeb.war with contents of zip from the MDW
 * Release site. (Assumes a clean MDWWeb.war without any extensions.)
 */
public void addWebLibs(String zipFile, String webAppWarName, List<DescriptorUpdater> descriptorUpdaters, boolean doDownload, boolean deleteZip, IProgressMonitor monitor) throws InvocationTargetException, InterruptedException, CoreException, IOException {
    IFile webAppWar;
    if (workflowProject.isCloudProject()) {
        project = workflowProject.getSourceProject();
        localFolder = workflowProject.getSourceProject().getFolder(MdwPlugin.getSettings().getTempResourceLocation());
        IFolder deployEarFolder = workflowProject.getSourceProject().getFolder(new Path("deploy/ear"));
        webAppWar = deployEarFolder.getFile(webAppWarName);
    } else {
        project = workflowProject.getEarProject();
        localFolder = workflowProject.getEarContentFolder();
        webAppWar = localFolder.getFile(webAppWarName);
    }
    if (!webAppWar.exists())
        // may not exist in some scenarios
        return;
    file = project.getFile(localFolder.getProjectRelativePath().toString() + "/" + zipFile);
    IFile newWebAppWar = localFolder.getFile(webAppWarName + ".tmp");
    ZipFile zip = null;
    JarFile warArchive = null;
    JarOutputStream jarOut = null;
    BufferedOutputStream bufferedOut = null;
    monitor.beginTask("Building " + webAppWarName, 200);
    try {
        monitor.worked(5);
        if (doDownload)
            PluginUtil.downloadIntoProject(project, getFileUrl(), localFolder, file, "Download", // 75 ticks
            monitor);
        else
            monitor.worked(75);
        File outFile = new File(newWebAppWar.getLocationURI());
        bufferedOut = new BufferedOutputStream(new FileOutputStream(outFile));
        jarOut = new JarOutputStream(bufferedOut);
        int read = 0;
        byte[] buf = new byte[1024];
        monitor.subTask("Add existing entries to WAR file");
        SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 50);
        // lots of
        subMonitor.beginTask("Add existing entries", 500);
        // entries
        subMonitor.worked(5);
        warArchive = new JarFile(new File(webAppWar.getLocationURI()));
        for (Enumeration<?> entriesEnum = warArchive.entries(); entriesEnum.hasMoreElements(); ) {
            JarEntry jarEntry = (JarEntry) entriesEnum.nextElement();
            DescriptorUpdater dUpdater = null;
            if (descriptorUpdaters != null) {
                for (DescriptorUpdater descriptorUpdater : descriptorUpdaters) {
                    if (descriptorUpdater.getFilePath().equals(jarEntry.getName())) {
                        dUpdater = descriptorUpdater;
                        break;
                    }
                }
            }
            InputStream warIn = warArchive.getInputStream(jarEntry);
            if (dUpdater == null) {
                // straight pass-through
                jarOut.putNextEntry(jarEntry);
                while ((read = warIn.read(buf)) != -1) jarOut.write(buf, 0, read);
            } else {
                jarOut.putNextEntry(new JarEntry(jarEntry.getName()));
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                // files)
                while ((read = warIn.read(buf)) != -1) baos.write(buf, 0, read);
                baos.flush();
                byte[] contents = dUpdater.processContents(new String(baos.toByteArray()), monitor).getBytes();
                jarOut.write(contents, 0, contents.length);
            }
            warIn.close();
            subMonitor.worked(1);
        }
        subMonitor.done();
        monitor.subTask("Add new entries to WAR file");
        subMonitor = new SubProgressMonitor(monitor, 50);
        // potentially lots of
        subMonitor.beginTask("Add new entries", 500);
        // entries
        subMonitor.worked(5);
        zip = new ZipFile(new File(file.getLocationURI()));
        for (Enumeration<?> entriesEnum = zip.entries(); entriesEnum.hasMoreElements(); ) {
            ZipEntry zipEntry = (ZipEntry) entriesEnum.nextElement();
            try {
                jarOut.putNextEntry(zipEntry);
            } catch (ZipException ex) {
                // ignore duplicate entries
                if (ex.getMessage().startsWith("duplicate entry:"))
                    PluginMessages.log(ex.getMessage());
                else
                    throw ex;
            }
            InputStream zipIn = zip.getInputStream(zipEntry);
            while ((read = zipIn.read(buf)) != -1) jarOut.write(buf, 0, read);
            zipIn.close();
            subMonitor.worked(1);
        }
        subMonitor.done();
    } finally {
        if (zip != null)
            zip.close();
        if (warArchive != null)
            warArchive.close();
        if (jarOut != null)
            jarOut.close();
        if (bufferedOut != null)
            bufferedOut.close();
        SubProgressMonitor subMon = monitor.isCanceled() ? null : new SubProgressMonitor(monitor, 25);
        monitor.subTask("Cleaning up");
        if (deleteZip) {
            file.refreshLocal(1, subMon);
            file.delete(true, subMon);
        }
        webAppWar.refreshLocal(1, subMon);
        if (webAppWar.exists())
            webAppWar.delete(true, subMon);
        newWebAppWar.refreshLocal(1, subMon);
        if (newWebAppWar.exists())
            newWebAppWar.move(webAppWar.getFullPath(), true, subMon);
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) InputStream(java.io.InputStream) DescriptorUpdater(com.centurylink.mdw.plugin.project.extensions.DescriptorUpdater) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) ZipException(java.util.zip.ZipException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) ZipFile(java.util.zip.ZipFile) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IFolder(org.eclipse.core.resources.IFolder)

Example 2 with DescriptorUpdater

use of com.centurylink.mdw.plugin.project.extensions.DescriptorUpdater in project mdw-designer by CenturyLinkCloud.

the class ProjectUpdater method removeWebLibs.

/**
 * Update WEB-INF/lib jars in MDWWeb.war to remove contents of zip from the
 * MDW Release site. (Assumes a clean MDWWeb.war without any extensions.)
 */
public void removeWebLibs(String zipFile, String webAppWarName, List<DescriptorUpdater> descriptorUpdaters, boolean doDownload, boolean deleteZip, IProgressMonitor monitor) throws InvocationTargetException, InterruptedException, CoreException, IOException {
    IFile webAppWar;
    if (workflowProject.isCloudProject()) {
        project = workflowProject.getSourceProject();
        localFolder = workflowProject.getSourceProject().getFolder(MdwPlugin.getSettings().getTempResourceLocation());
        IFolder deployEarFolder = workflowProject.getSourceProject().getFolder(new Path("deploy/ear"));
        webAppWar = deployEarFolder.getFile(webAppWarName);
    } else {
        project = workflowProject.getEarProject();
        localFolder = workflowProject.getEarContentFolder();
        webAppWar = localFolder.getFile(webAppWarName);
    }
    file = project.getFile(localFolder.getProjectRelativePath().toString() + "/" + zipFile);
    IFile newWebAppWar = localFolder.getFile(webAppWarName + ".tmp");
    ZipFile zip = null;
    JarFile warArchive = null;
    JarOutputStream jarOut = null;
    BufferedOutputStream bufferedOut = null;
    monitor.beginTask("Remove Web Libraries from " + webAppWarName, 200);
    try {
        monitor.worked(5);
        if (doDownload)
            PluginUtil.downloadIntoProject(project, getFileUrl(), localFolder, file, "Check", // 75 ticks
            monitor);
        else
            monitor.worked(75);
        File outFile = new File(newWebAppWar.getLocationURI());
        monitor.subTask("Updating: " + outFile.getName());
        int read = 0;
        byte[] buf = new byte[1024];
        monitor.subTask("Find WAR entries to remove");
        SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 50);
        // potentially
        subMonitor.beginTask("Find entries to remove", 500);
        // lots of
        // entries
        subMonitor.worked(5);
        List<String> zipFileList = new ArrayList<String>();
        zip = new ZipFile(new File(file.getLocationURI()));
        for (Enumeration<?> entriesEnum = zip.entries(); entriesEnum.hasMoreElements(); ) {
            ZipEntry zipEntry = (ZipEntry) entriesEnum.nextElement();
            zipFileList.add(zipEntry.getName());
            subMonitor.worked(1);
        }
        subMonitor.done();
        bufferedOut = new BufferedOutputStream(new FileOutputStream(outFile));
        jarOut = new JarOutputStream(bufferedOut);
        monitor.subTask("Rebuild webapp WAR file");
        subMonitor = new SubProgressMonitor(monitor, 50);
        // lots of entries
        subMonitor.beginTask("Rebuild webapp war", 500);
        subMonitor.worked(5);
        warArchive = new JarFile(new File(webAppWar.getLocationURI()));
        for (Enumeration<?> entriesEnum = warArchive.entries(); entriesEnum.hasMoreElements(); ) {
            JarEntry jarEntry = (JarEntry) entriesEnum.nextElement();
            // exclude those from the zip list when repackaging
            if (!zipFileList.contains(jarEntry.getName())) {
                DescriptorUpdater dUpdater = null;
                if (descriptorUpdaters != null) {
                    for (DescriptorUpdater descriptorUpdater : descriptorUpdaters) {
                        if (descriptorUpdater.getFilePath().equals(jarEntry.getName())) {
                            dUpdater = descriptorUpdater;
                            break;
                        }
                    }
                }
                InputStream warIn = warArchive.getInputStream(jarEntry);
                if (dUpdater == null) {
                    // straight pass-through
                    jarOut.putNextEntry(jarEntry);
                    while ((read = warIn.read(buf)) != -1) jarOut.write(buf, 0, read);
                } else {
                    jarOut.putNextEntry(new JarEntry(jarEntry.getName()));
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    // files)
                    while ((read = warIn.read(buf)) != -1) baos.write(buf, 0, read);
                    baos.flush();
                    byte[] contents = dUpdater.processContents(new String(baos.toByteArray()), monitor).getBytes();
                    jarOut.write(contents, 0, contents.length);
                }
                warIn.close();
            }
            subMonitor.worked(1);
        }
        subMonitor.done();
    } finally {
        if (zip != null)
            zip.close();
        if (warArchive != null)
            warArchive.close();
        if (jarOut != null)
            jarOut.close();
        if (bufferedOut != null)
            bufferedOut.close();
        SubProgressMonitor subMon = monitor.isCanceled() ? null : new SubProgressMonitor(monitor, 25);
        monitor.subTask("Cleaning up");
        if (deleteZip) {
            file.refreshLocal(1, subMon);
            file.delete(true, subMon);
        }
        webAppWar.refreshLocal(1, subMon);
        if (webAppWar.exists())
            webAppWar.delete(true, subMon);
        newWebAppWar.refreshLocal(1, subMon);
        if (newWebAppWar.exists())
            newWebAppWar.move(webAppWar.getFullPath(), true, subMon);
        monitor.done();
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) DescriptorUpdater(com.centurylink.mdw.plugin.project.extensions.DescriptorUpdater) ArrayList(java.util.ArrayList) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) ZipFile(java.util.zip.ZipFile) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

DescriptorUpdater (com.centurylink.mdw.plugin.project.extensions.DescriptorUpdater)2 BufferedOutputStream (java.io.BufferedOutputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 JarEntry (java.util.jar.JarEntry)2 JarFile (java.util.jar.JarFile)2 JarOutputStream (java.util.jar.JarOutputStream)2 ZipEntry (java.util.zip.ZipEntry)2 ZipFile (java.util.zip.ZipFile)2 IFile (org.eclipse.core.resources.IFile)2 IFolder (org.eclipse.core.resources.IFolder)2 Path (org.eclipse.core.runtime.Path)2 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)2 ArrayList (java.util.ArrayList)1 ZipException (java.util.zip.ZipException)1