/* Obtaining the last modified time opens an InputStream and there is no *explicitclosemethod.Wehavetoobtainandthenclosethe *InputStreamtoavoidafileleakandtheassociatedlockedfile.
*/
JarURLConnection juc = (JarURLConnection) war.openConnection();
juc.setUseCaches(false);
URL jarFileUrl = juc.getJarFileURL();
URLConnection jfuc = jarFileUrl.openConnection();
boolean success = false;
File docBase = new File(host.getAppBaseFile(), pathname);
File warTracker = new File(host.getAppBaseFile(), pathname + Constants.WarTracker); long warLastModified = -1;
try (InputStream is = jfuc.getInputStream()) { // Get the last modified time for the WAR
warLastModified = jfuc.getLastModified();
}
// Check to see of the WAR has been expanded previously if (docBase.exists()) { // A WAR was expanded. Tomcat will have set the last modified // time of warTracker file to the last modified time of the WAR so // changes to the WAR while Tomcat is stopped can be detected if (!warTracker.exists() || warTracker.lastModified() == warLastModified) { // No (detectable) changes to the WAR
success = true; return docBase.getAbsolutePath();
}
// WAR must have been modified. Remove expanded directory.
log.info(sm.getString("expandWar.deleteOld", docBase)); if (!delete(docBase)) { thrownew IOException(sm.getString("expandWar.deleteFailed", docBase));
}
}
// Create the new document base directory if(!docBase.mkdir() && !docBase.isDirectory()) { thrownew IOException(sm.getString("expandWar.createFailed", docBase));
}
// Expand the WAR into the new document base directory
Path canonicalDocBasePath = docBase.getCanonicalFile().toPath();
// Creating war tracker parent (normally META-INF)
File warTrackerParent = warTracker.getParentFile(); if (!warTrackerParent.isDirectory() && !warTrackerParent.mkdirs()) { thrownew IOException(sm.getString("expandWar.createFailed", warTrackerParent.getAbsolutePath()));
}
try (JarFile jarFile = juc.getJarFile()) {
Enumeration<JarEntry> jarEntries = jarFile.entries(); while (jarEntries.hasMoreElements()) {
JarEntry jarEntry = jarEntries.nextElement();
String name = jarEntry.getName();
File expandedFile = new File(docBase, name); if (!expandedFile.getCanonicalFile().toPath().startsWith(canonicalDocBasePath)) { // Trying to expand outside the docBase // Throw an exception to stop the deployment thrownew IllegalArgumentException(
sm.getString("expandWar.illegalPath",war, name,
expandedFile.getCanonicalPath(),
canonicalDocBasePath));
} int last = name.lastIndexOf('/'); if (last >= 0) {
File parent = new File(docBase,
name.substring(0, last)); if (!parent.mkdirs() && !parent.isDirectory()) { thrownew IOException(
sm.getString("expandWar.createFailed", parent));
}
} if (name.endsWith("/")) { continue;
}
// Bugzilla 33636
expand(input, expandedFile); long lastModified = jarEntry.getTime(); if ((lastModified != -1) && (lastModified != 0)) { if (!expandedFile.setLastModified(lastModified)) { thrownew IOException(
sm.getString("expandWar.lastModifiedFailed", expandedFile));
}
}
}
}
// Create the warTracker file and align the last modified time // with the last modified time of the WAR if (!warTracker.createNewFile()) { thrownew IOException(sm.getString("expandWar.createFileFailed", warTracker));
} if (!warTracker.setLastModified(warLastModified)) { thrownew IOException(sm.getString("expandWar.lastModifiedFailed", warTracker));
}
success = true;
} catch (IOException e) { throw e;
} finally { if (!success) { // If something went wrong, delete expanded dir to keep things // clean
deleteDir(docBase);
}
}
// Return the absolute path to our new document base directory return docBase.getAbsolutePath();
}
String files[] = dir.list(); if (files == null) {
files = new String[0];
} for (String s : files) {
File file = new File(dir, s); if (file.isDirectory()) {
deleteDir(file, logFailure);
} else {
file.delete();
}
}
boolean result; if (dir.exists()) {
result = dir.delete();
} else {
result = true;
}
if (logFailure && !result) {
log.error(sm.getString( "expandWar.deleteFailed", dir.getAbsolutePath()));
}
return result;
}
/** *Expandthespecifiedinputstreamintothespecifiedfile. * *@paraminputInputStreamtobecopied *@paramfileThefiletobecreated * *@exceptionIOExceptionifaninput/outputerroroccurs
*/ privatestaticvoid expand(InputStream input, File file) throws IOException { try (BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file))) { byte buffer[] = newbyte[2048]; while (true) { int n = input.read(buffer); if (n <= 0) { break;
}
output.write(buffer, 0, n);
}
}
}
}
Messung V0.5 in Prozent
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.22Bemerkung:
(vorverarbeitet am 2026-06-10)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.