Skip to content

Commit

Permalink
WW-4901 Falls back if JarURLConnection was present already
Browse files Browse the repository at this point in the history
  • Loading branch information
yasserzamani committed Dec 12, 2017
1 parent 94758b6 commit d082aac
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.logging.log4j.LogManager;

import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;

/**
Expand All @@ -36,9 +37,9 @@ public class JarEntryRevision extends Revision {
private long lastModified;

public static Revision build(URL fileUrl, FileManager fileManager) {
StrutsJarURLConnection conn = null;
JarURLConnection conn = null;
try {
conn = new StrutsJarURLConnection(fileUrl);
conn = StrutsJarURLConnection.openConnection(fileUrl);
conn.setUseCaches(false);
URL url = fileManager.normalizeToFileProtocol(fileUrl);
if (url != null) {
Expand All @@ -52,7 +53,10 @@ public static Revision build(URL fileUrl, FileManager fileManager) {
}
finally {
if(null != conn) {
conn.disconnect();
try {
conn.getInputStream().close();
} catch (IOException ignored) {
}
}
}
}
Expand All @@ -66,17 +70,20 @@ private JarEntryRevision(URL jarFileURL, long lastModified) {
}

public boolean needsReloading() {
StrutsJarURLConnection conn = null;
JarURLConnection conn = null;
long lastLastModified = lastModified;
try {
conn = new StrutsJarURLConnection(jarFileURL);
conn = StrutsJarURLConnection.openConnection(jarFileURL);
conn.setUseCaches(false);
lastLastModified = conn.getJarEntry().getTime();
} catch (IOException ignored) {
}
finally {
if(null != conn) {
conn.disconnect();
try {
conn.getInputStream().close();
} catch (IOException ignored) {
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
Expand All @@ -32,13 +33,14 @@
import java.util.jar.JarFile;

/**
* WW-4901 Decouples Struts from {@link URL#openConnection()} implementation of container (e.g. IBM WebSphere com.ibm.ws.classloader.Handler$ClassLoaderURLConnection)
* WW-4901 Decouples from underlying implementation of {@link URL#openConnection()}
* e.g. from IBM WebSphere com.ibm.ws.classloader.Handler$ClassLoaderURLConnection
* @since 2.5.15
*/
class StrutsJarURLConnection extends JarURLConnection {
private JarFile jarFile;

StrutsJarURLConnection(URL url) throws MalformedURLException {
private StrutsJarURLConnection(URL url) throws MalformedURLException {
super(url);
}

Expand All @@ -50,8 +52,9 @@ public JarFile getJarFile() throws IOException {

@Override
public void connect() throws IOException {
if (connected)
if (connected) {
return;
}

try (final InputStream in = getJarFileURL().openConnection().getInputStream()) {
jarFile = AccessController.doPrivileged(
Expand Down Expand Up @@ -81,13 +84,19 @@ public JarFile run() throws IOException {
}
}

void disconnect() {
if (connected) {
connected = false;

static JarURLConnection openConnection(URL url) throws IOException {
URLConnection conn = url.openConnection();
if (conn instanceof JarURLConnection) {
return (JarURLConnection) conn;
} else {
try {
jarFile.close();
conn.getInputStream().close();
} catch (IOException ignored) {
}
}

StrutsJarURLConnection result = new StrutsJarURLConnection(url);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import org.apache.commons.io.IOUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.jar.Attributes;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
Expand Down Expand Up @@ -69,4 +72,48 @@ public void testNeedsReloading() throws Exception {
createJarFile(now + 60000);
assertTrue(entry.needsReloading());
}

public void testNeedsReloadingWithContainerProvidedURLConnection() throws Exception {
long now = System.currentTimeMillis();

createJarFile(now);
URL url = new URL(null,
"jar:file:target/JarEntryRevisionTest_testNeedsReloading.jar!/com/opensymphony/xwork2/util/fs/JarEntryRevisionTest.class",
new ContainerProvidedURLStreamHandler());
Revision entry = JarEntryRevision.build(url, fileManager);
assertFalse(entry.needsReloading());

createJarFile(now + 60000);
assertTrue(entry.needsReloading());
}


/**
* WW-4901 Simulating container implementation of {@link URL#openConnection()}
* @since 2.5.15
*/
private class ContainerProvidedURLStreamHandler extends URLStreamHandler {

@Override
protected URLConnection openConnection(URL u) throws IOException {
return new ContainerProvidedURLConnection(u);
}
}

/**
* WW-4901 Simulating container implementation of {@link URLConnection}
* e.g. like IBM WebSphere com.ibm.ws.classloader.Handler$ClassLoaderURLConnection
* @since 2.5.15
*/
private class ContainerProvidedURLConnection extends URLConnection {

protected ContainerProvidedURLConnection(URL url) {
super(url);
}

@Override
public void connect() throws IOException {
throw new IllegalStateException("This is not expected (should not coupled to underlying implementation)");
}
}
}

0 comments on commit d082aac

Please sign in to comment.