Skip to content

Commit

Permalink
WW-4901 Decouples Struts from URL.openConnection implementation of co…
Browse files Browse the repository at this point in the history
…ntainer
  • Loading branch information
yasserzamani committed Dec 11, 2017
1 parent 80698ed commit 94758b6
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 15 deletions.
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 {
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(
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) {
}
}
}
}

0 comments on commit 94758b6

Please sign in to comment.