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