ExpandWarpublic class ExpandWar extends Object Expand out a WAR in a Host's appBase. |
Fields Summary |
---|
private static org.apache.juli.logging.Log | log | protected static final org.apache.catalina.util.StringManager | smThe string resources for this package. |
Methods Summary |
---|
public static boolean | copy(java.io.File src, java.io.File dest)Copy the specified file or directory to the destination.
boolean result = true;
String files[] = null;
if (src.isDirectory()) {
files = src.list();
result = dest.mkdir();
} else {
files = new String[1];
files[0] = "";
}
if (files == null) {
files = new String[0];
}
for (int i = 0; (i < files.length) && result; i++) {
File fileSrc = new File(src, files[i]);
File fileDest = new File(dest, files[i]);
if (fileSrc.isDirectory()) {
result = copy(fileSrc, fileDest);
} else {
FileChannel ic = null;
FileChannel oc = null;
try {
ic = (new FileInputStream(fileSrc)).getChannel();
oc = (new FileOutputStream(fileDest)).getChannel();
ic.transferTo(0, ic.size(), oc);
} catch (IOException e) {
log.error(sm.getString
("expandWar.copy", fileSrc, fileDest), e);
result = false;
} finally {
if (ic != null) {
try {
ic.close();
} catch (IOException e) {
}
}
if (oc != null) {
try {
oc.close();
} catch (IOException e) {
}
}
}
}
}
return result;
| public static boolean | delete(java.io.File dir)Delete the specified directory, including all of its contents and
subdirectories recursively.
if (dir.isDirectory()) {
return deleteDir(dir);
} else {
return dir.delete();
}
| public static boolean | deleteDir(java.io.File dir)Delete the specified directory, including all of its contents and
subdirectories recursively.
String files[] = dir.list();
if (files == null) {
files = new String[0];
}
for (int i = 0; i < files.length; i++) {
File file = new File(dir, files[i]);
if (file.isDirectory()) {
deleteDir(file);
} else {
file.delete();
}
}
return dir.delete();
| public static java.lang.String | expand(org.apache.catalina.Host host, java.net.URL war)Expand the WAR file found at the specified URL into an unpacked
directory structure, and return the absolute pathname to the expanded
directory.
// Calculate the directory name of the expanded directory
if (host.getLogger().isDebugEnabled()) {
host.getLogger().debug("expand(" + war.toString() + ")");
}
String pathname = war.toString().replace('\\", '/");
if (pathname.endsWith("!/")) {
pathname = pathname.substring(0, pathname.length() - 2);
}
int period = pathname.lastIndexOf('.");
if (period >= pathname.length() - 4)
pathname = pathname.substring(0, period);
int slash = pathname.lastIndexOf('/");
if (slash >= 0) {
pathname = pathname.substring(slash + 1);
}
if (host.getLogger().isDebugEnabled()) {
host.getLogger().debug(" Proposed directory name: " + pathname);
}
return expand(host, war, pathname);
| public static java.lang.String | expand(org.apache.catalina.Host host, java.net.URL war, java.lang.String pathname)Expand the WAR file found at the specified URL into an unpacked
directory structure, and return the absolute pathname to the expanded
directory.
// Make sure that there is no such directory already existing
File appBase = new File(host.getAppBase());
if (!appBase.isAbsolute()) {
appBase = new File(System.getProperty("catalina.base"),
host.getAppBase());
}
if (!appBase.exists() || !appBase.isDirectory()) {
throw new IOException
(sm.getString("hostConfig.appBase",
appBase.getAbsolutePath()));
}
File docBase = new File(appBase, pathname);
if (docBase.exists()) {
// War file is already installed
return (docBase.getAbsolutePath());
}
// Create the new document base directory
docBase.mkdir();
// Expand the WAR into the new document base directory
JarURLConnection juc = (JarURLConnection) war.openConnection();
juc.setUseCaches(false);
JarFile jarFile = null;
InputStream input = null;
try {
jarFile = juc.getJarFile();
Enumeration jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
JarEntry jarEntry = (JarEntry) jarEntries.nextElement();
String name = jarEntry.getName();
int last = name.lastIndexOf('/");
if (last >= 0) {
File parent = new File(docBase,
name.substring(0, last));
parent.mkdirs();
}
if (name.endsWith("/")) {
continue;
}
input = jarFile.getInputStream(jarEntry);
// Bugzilla 33636
File expandedFile = expand(input, docBase, name);
long lastModified = jarEntry.getTime();
if ((lastModified != -1) && (lastModified != 0) && (expandedFile != null)) {
expandedFile.setLastModified(lastModified);
}
input.close();
input = null;
}
} catch (IOException e) {
// If something went wrong, delete expanded dir to keep things
// clean
deleteDir(docBase);
throw e;
} finally {
if (input != null) {
try {
input.close();
} catch (Throwable t) {
;
}
input = null;
}
if (jarFile != null) {
try {
jarFile.close();
} catch (Throwable t) {
;
}
jarFile = null;
}
}
// Return the absolute path to our new document base directory
return (docBase.getAbsolutePath());
| protected static java.io.File | expand(java.io.InputStream input, java.io.File docBase, java.lang.String name)Expand the specified input stream into the specified directory, creating
a file named from the specified relative path.
File file = new File(docBase, name);
BufferedOutputStream output = null;
try {
output =
new BufferedOutputStream(new FileOutputStream(file));
byte buffer[] = new byte[2048];
while (true) {
int n = input.read(buffer);
if (n <= 0)
break;
output.write(buffer, 0, n);
}
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
// Ignore
}
}
}
return file;
|
|