From 06ac6d5b0a030df47fa4f9fcc52e8685c0b9997b Mon Sep 17 00:00:00 2001 From: Matthew Vivian Date: Mon, 4 Sep 2023 11:54:53 +0100 Subject: [PATCH] Removed WebAppLoaderFix as it is now redundant Thanks to GregDThomas for spotting: IIRC it only worked on older versions of JRE/Jetty, so it's probably long redundant now. Originally created to fix https://igniterealtime.atlassian.net/browse/OF-1522 in #1228 (with comment "Note; we may need to revisit this come Java 9.") --- .../eclipse/jetty/util/WebAppLoaderFix.java | 232 ------------------ .../openfire/http/HttpBindManager.java | 9 +- 2 files changed, 1 insertion(+), 240 deletions(-) delete mode 100644 xmppserver/src/main/java/org/eclipse/jetty/util/WebAppLoaderFix.java diff --git a/xmppserver/src/main/java/org/eclipse/jetty/util/WebAppLoaderFix.java b/xmppserver/src/main/java/org/eclipse/jetty/util/WebAppLoaderFix.java deleted file mode 100644 index d12d98c30a..0000000000 --- a/xmppserver/src/main/java/org/eclipse/jetty/util/WebAppLoaderFix.java +++ /dev/null @@ -1,232 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.util; - -import java.io.IOException; -import java.lang.reflect.Field; -import java.net.URLClassLoader; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.jar.JarFile; - -public class WebAppLoaderFix -{ - public static void checkAndClose(ClassLoader classLoader) - { - if (!isWindows() || !(classLoader instanceof URLClassLoader)) - { - return; - } - HashSet leakedJarNames = preClose((URLClassLoader)classLoader); - cleanupJarFileFactory(leakedJarNames); - } - - private static boolean isWindows() - { - String osProp = System.getProperty("os.name").toLowerCase(); - return osProp.indexOf("win") >= 0; - } - - private static HashSet preClose(URLClassLoader loader) - { - HashSet leakedJarNames = new HashSet<>(); - Field f = getClassField(URLClassLoader.class, "ucp"); - if (f != null) - { - Object obj = null; - try - { - obj = f.get(loader); - final Object ucp = obj; - f = getClassField(ucp.getClass(), "loaders"); - if (f != null) - { - ArrayList loaders = null; - try - { - loaders = (ArrayList) f.get(ucp); - } - catch (IllegalAccessException ex) - { - } - for (int i = 0; loaders != null && i < loaders.size(); i++) - { - obj = loaders.get(i); - f = getClassField(obj.getClass(), "jar"); - if (f != null) - { - try - { - obj = f.get(obj); - } catch (IllegalAccessException ex) - { - } - if (obj instanceof JarFile) - { - final JarFile jarFile = (JarFile) obj; - leakedJarNames.add(jarFile.getName()); - } - } - } - } - } - catch (IllegalAccessException ex) - { - } - } - return leakedJarNames; - } - - private static Field getClassField(Class clz, String fieldName) - { - Field field = null; - try - { - field = clz.getDeclaredField(fieldName); - field.setAccessible(true); - } - catch (Exception e) - { - } - return field; - } - - private static void cleanupJarFileFactory(HashSet leakedJarNames) { - - Class classJarURLConnection = null; - try - { - classJarURLConnection = Class.forName("sun.net.www.protocol.jar.JarURLConnection"); - } - catch (ClassNotFoundException ex) - { - return; - } - - Field f = getClassField(classJarURLConnection, "factory"); - - if (f == null) - { - return; - } - Object obj = null; - try - { - obj = f.get(null); - } catch (IllegalAccessException ex) - { - return; - } - - Class classJarFileFactory = obj.getClass(); - HashMap fileCache = null; - f = getClassField(classJarFileFactory, "fileCache"); - if (f == null) - { - return; - } - try - { - obj = f.get(null); - if (obj instanceof HashMap) - { - fileCache = (HashMap) obj; - } - } - catch (IllegalAccessException ex) - { - } - HashMap urlCache = null; - f = getClassField(classJarFileFactory, "urlCache"); - if (f == null) - { - return; - } - try - { - obj = f.get(null); - if (obj instanceof HashMap) - { - urlCache = (HashMap) obj; - } - } - catch (IllegalAccessException ex) - { - } - - if (urlCache != null) - { - HashMap urlCacheTmp = (HashMap) urlCache.clone(); - Iterator it = urlCacheTmp.keySet().iterator(); - while (it.hasNext()) - { - obj = it.next(); - if (!(obj instanceof JarFile)) - { - continue; - } - JarFile jarFile = (JarFile) obj; - if (leakedJarNames.contains(jarFile.getName())) - { - try - { - jarFile.close(); - } - catch (IOException ex) - { - } - if (fileCache != null) - { - fileCache.remove(urlCache.get(jarFile)); - } - urlCache.remove(jarFile); - } - } - } - else if (fileCache != null) - { - HashMap fileCacheTmp = (HashMap) fileCache.clone(); - Iterator it = fileCacheTmp.keySet().iterator(); - while (it.hasNext()) - { - Object key = it.next(); - obj = fileCache.get(key); - if (!(obj instanceof JarFile)) - { - continue; - } - JarFile jarFile = (JarFile) obj; - if (leakedJarNames.contains(jarFile.getName())) - { - try - { - jarFile.close(); - } - catch (IOException ex) - { - } - fileCache.remove(key); - } - } - } - leakedJarNames.clear(); - } -} \ No newline at end of file diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/http/HttpBindManager.java b/xmppserver/src/main/java/org/jivesoftware/openfire/http/HttpBindManager.java index aea194dcbc..30c0c224fe 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/http/HttpBindManager.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/http/HttpBindManager.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2008 Jive Software, Ignite Realtime Foundation 2022. All rights reserved. + * Copyright (C) 2005-2008 Jive Software, Ignite Realtime Foundation 2022-2023. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,6 @@ import org.eclipse.jetty.server.handler.gzip.GzipHandler; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; -import org.eclipse.jetty.util.WebAppLoaderFix; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.webapp.WebAppContext; @@ -766,12 +765,6 @@ public void addJettyHandler( Handler handler ) */ public void removeJettyHandler( Handler handler ) { - if (handler instanceof WebAppContext) { - // A work-around of the Jetty bug described at https://github.com/eclipse/jetty.project/issues/1425 - // NOTE: According to some comments on WebAppLoaderFix, this may stop working on Java 9. - // Hopefully the Jetty team will have fixed the underlying bug by then - WebAppLoaderFix.checkAndClose(((WebAppContext) handler).getClassLoader()); - } extensionHandlers.removeHandler( handler ); if ( handler.isStarted() ) {