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 1 commit
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 @@ -23,7 +23,6 @@
import org.apache.logging.log4j.LogManager;

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

/**
Expand All @@ -37,11 +36,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;
StrutsJarURLConnection conn = null;
try {
conn = (JarURLConnection) fileUrl.openConnection();
conn = new StrutsJarURLConnection(fileUrl);
conn.setUseCaches(false);
URL url = fileManager.normalizeToFileProtocol(fileUrl);
if (url != null) {
Expand All @@ -55,10 +52,7 @@ public static Revision build(URL fileUrl, FileManager fileManager) {
}
finally {
if(null != conn) {
try {
conn.getInputStream().close();
} catch (IOException ignored) {
}
conn.disconnect();
}
}
}
Expand All @@ -72,20 +66,17 @@ private JarEntryRevision(URL jarFileURL, long lastModified) {
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.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 Struts from {@link URL#openConnection()} implementation of container (e.g. 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;

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(
Copy link
Member

Choose a reason for hiding this comment

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

could you add something like if (!getJarFileURL().openConnection() instanceof JarURLConnection) to avoid copiing of file if not necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you very much for your attention 👍 fixed :)

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();
}
}

void disconnect() {
if (connected) {
connected = false;
try {
jarFile.close();
} catch (IOException ignored) {
}
}
}
}