Author: markt Date: Fri Sep 1 15:04:45 2017 New Revision: 1806973 URL: http://svn.apache.org/viewvc?rev=1806973&view=rev Log: Java 9 allows us to be more selective with the JRE memory leak protection.
Modified: tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java tomcat/trunk/java/org/apache/tomcat/util/compat/Jre9Compat.java tomcat/trunk/java/org/apache/tomcat/util/compat/JreCompat.java tomcat/trunk/webapps/docs/changelog.xml Modified: tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java?rev=1806973&r1=1806972&r2=1806973&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java Fri Sep 1 15:04:45 2017 @@ -20,7 +20,6 @@ package org.apache.catalina.core; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.net.URL; import java.net.URLConnection; import java.sql.DriverManager; import java.util.StringTokenizer; @@ -288,19 +287,17 @@ public class JreMemoryLeakPreventionList * - javax.xml.bind.JAXBContext.newInstance() * * https://bugs.openjdk.java.net/browse/JDK-8163449 + * + * Java 9 onwards disables caching for JAR URLConnections + * Java 8 and earlier disables caching for all URLConnections */ // Set the default URL caching policy to not to cache if (urlCacheProtection) { try { - // Doesn't matter that this JAR doesn't exist - just as - // long as the URL is well-formed - URL url = new URL("jar:file://dummy.jar!/"); - URLConnection uConn = url.openConnection(); - uConn.setDefaultUseCaches(false); + JreCompat.getInstance().disableCachingForJarUrlConnections(); } catch (IOException e) { - log.error(sm.getString( - "jreLeakListener.jarUrlConnCacheFail"), e); + log.error(sm.getString("jreLeakListener.jarUrlConnCacheFail"), e); } } Modified: tomcat/trunk/java/org/apache/tomcat/util/compat/Jre9Compat.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/compat/Jre9Compat.java?rev=1806973&r1=1806972&r2=1806973&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/compat/Jre9Compat.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/compat/Jre9Compat.java Fri Sep 1 15:04:45 2017 @@ -16,8 +16,10 @@ */ package org.apache.tomcat.util.compat; +import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.net.URLConnection; import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLParameters; @@ -27,16 +29,19 @@ class Jre9Compat extends JreCompat { private static final Class<?> inaccessibleObjectExceptionClazz; private static final Method setApplicationProtocolsMethod; private static final Method getApplicationProtocolMethod; + private static final Method setDefaultUseCaches; static { Class<?> c1 = null; Method m2 = null; Method m3 = null; + Method m4 = null; try { c1 = Class.forName("java.lang.reflect.InaccessibleObjectException"); m2 = SSLParameters.class.getMethod("setApplicationProtocols", String[].class); m3 = SSLEngine.class.getMethod("getApplicationProtocol"); + m4 = URLConnection.class.getMethod("setDefaultUseCaches", String.class, boolean.class); } catch (SecurityException | NoSuchMethodException e) { // Should never happen } catch (ClassNotFoundException e) { @@ -45,6 +50,7 @@ class Jre9Compat extends JreCompat { inaccessibleObjectExceptionClazz = c1; setApplicationProtocolsMethod = m2; getApplicationProtocolMethod = m3; + setDefaultUseCaches = m4; } @@ -80,5 +86,15 @@ class Jre9Compat extends JreCompat { } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new UnsupportedOperationException(e); } + } + + + @Override + public void disableCachingForJarUrlConnections() throws IOException { + try { + setDefaultUseCaches.invoke(null, "JAR", Boolean.FALSE); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + throw new UnsupportedOperationException(e); + } } } Modified: tomcat/trunk/java/org/apache/tomcat/util/compat/JreCompat.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/compat/JreCompat.java?rev=1806973&r1=1806972&r2=1806973&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/compat/JreCompat.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/compat/JreCompat.java Fri Sep 1 15:04:45 2017 @@ -16,6 +16,10 @@ */ package org.apache.tomcat.util.compat; +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; + import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLParameters; @@ -96,4 +100,19 @@ public class JreCompat { public String getApplicationProtocol(SSLEngine sslEngine) { throw new UnsupportedOperationException(sm.getString("jreCompat.noApplicationProtocol")); } + + + /** + * Disables caching for JAR URL connections. For Java 8 and earlier, this also disables + * caching for ALL URL connections. + * + * @throws IOException If a dummy JAR URLConnection can not be created + */ + public void disableCachingForJarUrlConnections() throws IOException { + // Doesn't matter that this JAR doesn't exist - just as + // long as the URL is well-formed + URL url = new URL("jar:file://dummy.jar!/"); + URLConnection uConn = url.openConnection(); + uConn.setDefaultUseCaches(false); + } } Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1806973&r1=1806972&r2=1806973&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Fri Sep 1 15:04:45 2017 @@ -70,6 +70,13 @@ running under a <code>SecurityManager</code> and using <code>Subject.doAs()</code>. (markt) </fix> + <add> + When running under Java 9 or later, and the + <code>urlCacheProtection</code> option of the + <code>JreMemoryLeakPreventionListener</code> is enabled, use the API + added in Java 9 to only disable the caching for JAR URL connections. + (markt) + </add> </changelog> </subsection> <subsection name="Coyote"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org