struts-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (WW-4901) ClassCastException in JarEntryRevision
Date Thu, 14 Dec 2017 08:19:03 GMT

    [ https://issues.apache.org/jira/browse/WW-4901?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16290519#comment-16290519
] 

ASF GitHub Bot commented on WW-4901:
------------------------------------

lukaszlenart closed pull request #190: WW-4901 Decouples from URL.openConnection implementation
of container
URL: https://github.com/apache/struts/pull/190
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/core/src/main/java/com/opensymphony/xwork2/util/fs/JarEntryRevision.java b/core/src/main/java/com/opensymphony/xwork2/util/fs/JarEntryRevision.java
index 3356fbb5a..dc5f0a757 100644
--- a/core/src/main/java/com/opensymphony/xwork2/util/fs/JarEntryRevision.java
+++ b/core/src/main/java/com/opensymphony/xwork2/util/fs/JarEntryRevision.java
@@ -37,11 +37,9 @@
     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) {
@@ -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) {
diff --git a/core/src/main/java/com/opensymphony/xwork2/util/fs/StrutsJarURLConnection.java
b/core/src/main/java/com/opensymphony/xwork2/util/fs/StrutsJarURLConnection.java
new file mode 100644
index 000000000..8c1b71a79
--- /dev/null
+++ b/core/src/main/java/com/opensymphony/xwork2/util/fs/StrutsJarURLConnection.java
@@ -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 {
+    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;
+    }
+}
diff --git a/core/src/test/java/com/opensymphony/xwork2/util/fs/JarEntryRevisionTest.java
b/core/src/test/java/com/opensymphony/xwork2/util/fs/JarEntryRevisionTest.java
index 4764d0410..5fd4215d1 100644
--- a/core/src/test/java/com/opensymphony/xwork2/util/fs/JarEntryRevisionTest.java
+++ b/core/src/test/java/com/opensymphony/xwork2/util/fs/JarEntryRevisionTest.java
@@ -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;
@@ -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)");
+        }
+    }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> ClassCastException in JarEntryRevision
> --------------------------------------
>
>                 Key: WW-4901
>                 URL: https://issues.apache.org/jira/browse/WW-4901
>             Project: Struts 2
>          Issue Type: Bug
>    Affects Versions: 2.5.14.1
>         Environment: Websphere 8.5.5.12
>            Reporter: Michael Hum
>            Assignee: Yasser Zamani
>            Priority: Critical
>             Fix For: 2.5.15
>
>
> After upgrading to struts 2.5.14.1 we are unable to startup the application on websphere.
The logs show class cast exceptions in the JarEntryRevision class:
> {code}
> [12/7/17 16:50:18:323 EST] 00000502 JarEntryRevis W com.opensymphony.xwork2.util.fs.JarEntryRevision
build Could not create JarEntryRevision for [wsjar:file:/icosdata/IBM/WebSphere/AppServer/profiles/CmpAppSrv01/installedApps/CMPDMDEVCell01/icos-dev.ear/icos-web.war/WEB-INF/lib/struts2-core-2.5.14.1.jar!/struts-default.xml]!
>                                  java.lang.ClassCastException: com.ibm.ws.classloader.Handler$ClassLoaderURLConnection
incompatible with java.net.JarURLConnection
>     at com.opensymphony.xwork2.util.fs.JarEntryRevision.build(JarEntryRevision.java:44)
>     at com.opensymphony.xwork2.util.fs.DefaultFileManager.monitorFile(DefaultFileManager.java:94)
>     at com.opensymphony.xwork2.util.fs.DefaultFileManager.loadFile(DefaultFileManager.java:73)
>     at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadConfigurationFiles(XmlConfigurationProvider.java:1054)
>     at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadDocuments(XmlConfigurationProvider.java:198)
>     at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.init(XmlConfigurationProvider.java:165)
>     at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:166)
>     at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
>     at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:957)
>     at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:463)
>     at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:496)
>     at org.apache.struts2.dispatcher.InitOperations.initDispatcher(InitOperations.java:73)
>     at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:61)
>     at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.init(FilterInstanceWrapper.java:145)
>     at com.ibm.ws.webcontainer.filter.WebAppFilterManager._loadFilter(WebAppFilterManager.java:607)
>     at com.ibm.ws.webcontainer.filter.WebAppFilterManager.loadFilter(WebAppFilterManager.java:514)
>     at com.ibm.ws.webcontainer.filter.WebAppFilterManager.getFilterInstanceWrapper(WebAppFilterManager.java:319)
>     at com.ibm.ws.webcontainer.filter.WebAppFilterManager.getFilterChain(WebAppFilterManager.java:392)
>     at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:931)
>     at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1107)
>     at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3980)
>     at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
>     at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1016)
>     at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817)
>     at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)
>     at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:463)
>     at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:530)
>     at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:316)
>     at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:287)
>     at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
>     at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
>     at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
>     at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
>     at com.ibm.io.async.AsyncChannelFuture$1.run(AsyncChannelFuture.java:205)
>     at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1892)
> {code}
> We took a look at tracked it down to WW-4869 which modified the code to cast to JarURLConnection:
> {code:java}
>  JarURLConnection conn = null;
>         try {
>             conn = (JarURLConnection) fileUrl.openConnection();
> ...
> {code}
> Unfortunately the URLConnection in websphere doesn't inherit from JarURLConnection:
> {code:java}
> static class ClassLoaderURLConnection extends URLConnection { ... }
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Mime
View raw message