Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WW-4901 Decouples from URL.openConnection implementation of container #190

Merged
merged 2 commits into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ public class JarEntryRevision extends Revision {
private long lastModified;

public static Revision build(URL fileUrl, FileManager fileManager) {
// File within a Jar
// Find separator index of jar filename and filename within jar
JarURLConnection conn = null;
try {
conn = (JarURLConnection) fileUrl.openConnection();
conn = StrutsJarURLConnection.openConnection(fileUrl);
conn.setUseCaches(false);
URL url = fileManager.normalizeToFileProtocol(fileUrl);
if (url != null) {
Expand Down Expand Up @@ -75,7 +73,7 @@ public boolean needsReloading() {
JarURLConnection conn = null;
long lastLastModified = lastModified;
try {
conn = (JarURLConnection) jarFileURL.openConnection();
conn = StrutsJarURLConnection.openConnection(jarFileURL);
conn.setUseCaches(false);
lastLastModified = conn.getJarEntry().getTime();
} catch (IOException ignored) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.opensymphony.xwork2.util.fs;

import java.io.IOException;
import java.io.InputStream;
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;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.jar.JarFile;

/**
* 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

private JarFile jarFile;

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

@Override
public JarFile getJarFile() throws IOException {
connect();
return jarFile;
}

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

try (final InputStream in = getJarFileURL().openConnection().getInputStream()) {
jarFile = AccessController.doPrivileged(
new PrivilegedExceptionAction<JarFile>() {
public JarFile run() throws IOException {
Path tmpFile = Files.createTempFile("jar_cache", null);
try {
Files.copy(in, tmpFile, StandardCopyOption.REPLACE_EXISTING);
JarFile jarFile = new JarFile(tmpFile.toFile(), true, JarFile.OPEN_READ);
tmpFile.toFile().deleteOnExit();
return jarFile;
} catch (Throwable thr) {
try {
Files.delete(tmpFile);
} catch (IOException ioe) {
thr.addSuppressed(ioe);
}
throw thr;
} finally {
in.close();
}
}
});
connected = true;
} catch (PrivilegedActionException pae) {
throw (IOException) pae.getException();
}
}


static JarURLConnection openConnection(URL url) throws IOException {
URLConnection conn = url.openConnection();
if (conn instanceof JarURLConnection) {
return (JarURLConnection) conn;
} else {
try {
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)");
}
}
}