mirror of
https://github.com/apache/struts.git
synced 2026-08-07 07:37:20 +00:00
delete temp files on close instead of on JVM exists
This commit is contained in:
@@ -37,9 +37,7 @@ public class JarEntryRevision extends Revision {
|
|||||||
private long lastModified;
|
private long lastModified;
|
||||||
|
|
||||||
public static Revision build(URL fileUrl, FileManager fileManager) {
|
public static Revision build(URL fileUrl, FileManager fileManager) {
|
||||||
StrutsJarURLConnection conn = null;
|
try (StrutsJarURLConnection conn = StrutsJarURLConnection.openConnection(fileUrl)) {
|
||||||
try {
|
|
||||||
conn = StrutsJarURLConnection.openConnection(fileUrl);
|
|
||||||
conn.setUseCaches(false);
|
conn.setUseCaches(false);
|
||||||
URL url = fileManager.normalizeToFileProtocol(fileUrl);
|
URL url = fileManager.normalizeToFileProtocol(fileUrl);
|
||||||
if (url != null) {
|
if (url != null) {
|
||||||
@@ -51,14 +49,6 @@ public class JarEntryRevision extends Revision {
|
|||||||
LOG.warn("Could not create JarEntryRevision for [{}]!", fileUrl, e);
|
LOG.warn("Could not create JarEntryRevision for [{}]!", fileUrl, e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
if(null != conn) {
|
|
||||||
try {
|
|
||||||
conn.getInputStream().close();
|
|
||||||
} catch (IOException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JarEntryRevision(URL jarFileURL, long lastModified) {
|
private JarEntryRevision(URL jarFileURL, long lastModified) {
|
||||||
@@ -70,21 +60,12 @@ public class JarEntryRevision extends Revision {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean needsReloading() {
|
public boolean needsReloading() {
|
||||||
StrutsJarURLConnection conn = null;
|
|
||||||
long lastLastModified = lastModified;
|
long lastLastModified = lastModified;
|
||||||
try {
|
try (StrutsJarURLConnection conn = StrutsJarURLConnection.openConnection(jarFileURL)) {
|
||||||
conn = StrutsJarURLConnection.openConnection(jarFileURL);
|
|
||||||
conn.setUseCaches(false);
|
conn.setUseCaches(false);
|
||||||
lastLastModified = conn.getJarEntry().getTime();
|
lastLastModified = conn.getJarEntry().getTime();
|
||||||
} catch (IOException ignored) {
|
} catch (Throwable e) {
|
||||||
}
|
LOG.warn("Could not check if needsReloading for [{}]!", jarFileURL, e);
|
||||||
finally {
|
|
||||||
if(null != conn) {
|
|
||||||
try {
|
|
||||||
conn.getInputStream().close();
|
|
||||||
} catch (IOException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return lastModified < lastLastModified;
|
return lastModified < lastLastModified;
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ import java.util.jar.JarFile;
|
|||||||
* While {@link JarURLConnection#parseSpecs(URL)} is private, then we had to extend {@link URLConnection} instead
|
* While {@link JarURLConnection#parseSpecs(URL)} is private, then we had to extend {@link URLConnection} instead
|
||||||
* @since 2.5.15
|
* @since 2.5.15
|
||||||
*/
|
*/
|
||||||
class StrutsJarURLConnection extends URLConnection {
|
class StrutsJarURLConnection extends URLConnection implements AutoCloseable {
|
||||||
private static final String FILE_URL_PREFIX = "file:";
|
private static final String FILE_URL_PREFIX = "file:";
|
||||||
|
|
||||||
private JarURLConnection jarURLConnection;
|
private JarURLConnection jarURLConnection;
|
||||||
@@ -123,8 +123,8 @@ class StrutsJarURLConnection extends URLConnection {
|
|||||||
Path tmpFile = Files.createTempFile("jar_cache", null);
|
Path tmpFile = Files.createTempFile("jar_cache", null);
|
||||||
try {
|
try {
|
||||||
Files.copy(in, tmpFile, StandardCopyOption.REPLACE_EXISTING);
|
Files.copy(in, tmpFile, StandardCopyOption.REPLACE_EXISTING);
|
||||||
JarFile jarFile = new JarFile(tmpFile.toFile(), true, JarFile.OPEN_READ);
|
JarFile jarFile = new JarFile(tmpFile.toFile(), true, JarFile.OPEN_READ
|
||||||
tmpFile.toFile().deleteOnExit();
|
| JarFile.OPEN_DELETE);
|
||||||
return jarFile;
|
return jarFile;
|
||||||
} catch (Throwable thr) {
|
} catch (Throwable thr) {
|
||||||
try {
|
try {
|
||||||
@@ -171,6 +171,20 @@ class StrutsJarURLConnection extends URLConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws Exception {
|
||||||
|
try {
|
||||||
|
getInputStream().close();
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
|
if (jarURLConnection == null) {
|
||||||
|
try {
|
||||||
|
jarFile.close();
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static StrutsJarURLConnection openConnection(URL url) throws IOException {
|
static StrutsJarURLConnection openConnection(URL url) throws IOException {
|
||||||
return new StrutsJarURLConnection(url);
|
return new StrutsJarURLConnection(url);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,9 @@ package com.opensymphony.xwork2.util.fs;
|
|||||||
import com.opensymphony.xwork2.FileManager;
|
import com.opensymphony.xwork2.FileManager;
|
||||||
import com.opensymphony.xwork2.FileManagerFactory;
|
import com.opensymphony.xwork2.FileManagerFactory;
|
||||||
import com.opensymphony.xwork2.XWorkTestCase;
|
import com.opensymphony.xwork2.XWorkTestCase;
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.apache.commons.io.filefilter.WildcardFileFilter;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
@@ -30,6 +32,8 @@ import java.io.InputStream;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.net.URLStreamHandler;
|
import java.net.URLStreamHandler;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.jar.Attributes;
|
import java.util.jar.Attributes;
|
||||||
import java.util.jar.JarOutputStream;
|
import java.util.jar.JarOutputStream;
|
||||||
import java.util.jar.Manifest;
|
import java.util.jar.Manifest;
|
||||||
@@ -113,6 +117,20 @@ public class JarEntryRevisionTest extends XWorkTestCase {
|
|||||||
assertTrue(entry.needsReloading());
|
assertTrue(entry.needsReloading());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void tearDown() throws Exception {
|
||||||
|
Path tmpFile = Files.createTempFile("jar_cache", null);
|
||||||
|
Path tmpFolder = tmpFile.getParent();
|
||||||
|
int count = FileUtils.listFiles(tmpFolder.toFile(), new WildcardFileFilter("jar_cache*"),
|
||||||
|
null).size();
|
||||||
|
if (tmpFile.toFile().delete()) {
|
||||||
|
count--;
|
||||||
|
}
|
||||||
|
assertEquals(0, count);
|
||||||
|
|
||||||
|
super.tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* WW-4901 Simulating container implementation of {@link URL#openConnection()}
|
* WW-4901 Simulating container implementation of {@link URL#openConnection()}
|
||||||
|
|||||||
Reference in New Issue
Block a user