Use a resource attribute instead of a param.

This commit is contained in:
Michael Bayne
2018-11-30 11:25:54 -08:00
parent 37b6575ca7
commit d7d937c2a1
4 changed files with 32 additions and 36 deletions
@@ -431,7 +431,7 @@ public class Application
try { try {
URL remote = new URL(createVAppBase(_targetVersion), encodePath(_javaLocation)); URL remote = new URL(createVAppBase(_targetVersion), encodePath(_javaLocation));
return new Resource(vmfile, remote, getLocalPath(vmfile), return new Resource(vmfile, remote, getLocalPath(vmfile),
EnumSet.of(Resource.Attr.UNPACK)); EnumSet.of(Resource.Attr.UNPACK, Resource.Attr.CLEAN));
} catch (Exception e) { } catch (Exception e) {
log.warning("Failed to create VM resource", "vmfile", vmfile, "appbase", _appbase, log.warning("Failed to create VM resource", "vmfile", vmfile, "appbase", _appbase,
"tvers", _targetVersion, "javaloc", _javaLocation, "error", e); "tvers", _targetVersion, "javaloc", _javaLocation, "error", e);
@@ -31,6 +31,9 @@ public class Resource implements Comparable<Resource>
public static enum Attr { public static enum Attr {
/** Indicates that the resource should be unpacked. */ /** Indicates that the resource should be unpacked. */
UNPACK, UNPACK,
/** If present, when unpacking a resource, any directories created by the newly
* unpacked resource will first be cleared of files before unpacking. */
CLEAN,
/** Indicates that the resource should be marked executable. */ /** Indicates that the resource should be marked executable. */
EXEC EXEC
}; };
@@ -254,21 +257,20 @@ public class Resource implements Comparable<Resource>
/** /**
* Installs the {@code getLocalNew} version of this resource to {@code getLocal}. * Installs the {@code getLocalNew} version of this resource to {@code getLocal}.
*/ */
public void install (boolean cleanExistingDirs) throws IOException { public void install () throws IOException {
File source = getLocalNew(), dest = getLocal(); File source = getLocalNew(), dest = getLocal();
log.info("- " + source); log.info("- " + source);
if (!FileUtil.renameTo(source, dest)) { if (!FileUtil.renameTo(source, dest)) {
throw new IOException("Failed to rename " + source + " to " + dest); throw new IOException("Failed to rename " + source + " to " + dest);
} }
applyAttrs(cleanExistingDirs); applyAttrs();
markAsValid(); markAsValid();
} }
/** /**
* Unpacks this resource file into the directory that contains it. * Unpacks this resource file into the directory that contains it.
*/ */
public void unpack (boolean cleanExistingDirs) throws IOException public void unpack () throws IOException
{ {
// sanity check // sanity check
if (!_isJar && !_isPacked200Jar) { if (!_isJar && !_isPacked200Jar) {
@@ -276,35 +278,26 @@ public class Resource implements Comparable<Resource>
} }
if (_isJar) { if (_isJar) {
try (JarFile jar = new JarFile(_local)) { try (JarFile jar = new JarFile(_local)) {
FileUtil.unpackJar(jar, _unpacked, cleanExistingDirs); FileUtil.unpackJar(jar, _unpacked, _attrs.contains(Attr.CLEAN));
} }
} else { } else {
FileUtil.unpackPacked200Jar(_local, _unpacked); FileUtil.unpackPacked200Jar(_local, _unpacked);
} }
} }
public void unpack () throws IOException
{
unpack(false);
}
/** /**
* Applies this resources special attributes: unpacks this resource if needed, marks it as * Applies this resources special attributes: unpacks this resource if needed, marks it as
* executable if needed. * executable if needed.
*/ */
public void applyAttrs (boolean cleanExistingDirs) throws IOException { public void applyAttrs () throws IOException {
if (shouldUnpack()) { if (shouldUnpack()) {
unpack(cleanExistingDirs); unpack();
} }
if (_attrs.contains(Attr.EXEC)) { if (_attrs.contains(Attr.EXEC)) {
FileUtil.makeExecutable(_local); FileUtil.makeExecutable(_local);
} }
} }
public void applyAttrs () throws IOException {
applyAttrs(false);
}
/** /**
* Wipes this resource file along with any "validated" marker file that may be associated with * Wipes this resource file along with any "validated" marker file that may be associated with
* it. * it.
@@ -93,26 +93,29 @@ public class FileUtil
/** /**
* Unpacks the specified jar file into the specified target directory. * Unpacks the specified jar file into the specified target directory.
* @param cleanExistingDirs if true, all files in all directories contained in {@code jar} will
* be deleted prior to unpacking the jar.
*/ */
public static void unpackJar (JarFile jar, File target, boolean cleanExistingDirs) throws IOException public static void unpackJar (JarFile jar, File target, boolean cleanExistingDirs)
throws IOException
{ {
Enumeration<?> entries = jar.entries(); if (cleanExistingDirs) {
if (cleanExistingDirs) Enumeration<?> entries = jar.entries();
{ while (entries.hasMoreElements()) {
while (entries.hasMoreElements()) { JarEntry entry = (JarEntry)entries.nextElement();
JarEntry entry = (JarEntry) entries.nextElement(); if (entry.isDirectory()) {
if (entry.isDirectory()) { File efile = new File(target, entry.getName());
File efile = new File(target, entry.getName()); if (efile.exists()) {
if (efile.exists()) { for (File f : efile.listFiles()) {
for (File f : efile.listFiles()) { if (!f.isDirectory())
if (!f.isDirectory()) f.delete();
f.delete(); }
} }
} }
} }
}
} }
entries = jar.entries();
Enumeration<?> entries = jar.entries();
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
JarEntry entry = (JarEntry)entries.nextElement(); JarEntry entry = (JarEntry)entries.nextElement();
File efile = new File(target, entry.getName()); File efile = new File(target, entry.getName());
@@ -116,7 +116,7 @@ public abstract class Getdown extends Thread
} else if (_readyToInstall) { } else if (_readyToInstall) {
log.info("Installing " + _toInstallResources.size() + " downloaded resources:"); log.info("Installing " + _toInstallResources.size() + " downloaded resources:");
for (Resource resource : _toInstallResources) { for (Resource resource : _toInstallResources) {
resource.install(false); resource.install();
} }
_toInstallResources.clear(); _toInstallResources.clear();
_readyToInstall = false; _readyToInstall = false;
@@ -589,7 +589,7 @@ public abstract class Getdown extends Thread
reportTrackingEvent("jvm_unpack", -1); reportTrackingEvent("jvm_unpack", -1);
updateStatus("m.unpacking_java"); updateStatus("m.unpacking_java");
vmjar.install(true); vmjar.install();
// these only run on non-Windows platforms, so we use Unix file separators // these only run on non-Windows platforms, so we use Unix file separators
String localJavaDir = LaunchUtil.LOCAL_JAVA_DIR + "/"; String localJavaDir = LaunchUtil.LOCAL_JAVA_DIR + "/";