From cd8f5bbd228956abcb800675b92ded075924a201 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Mon, 2 Jan 2023 16:46:39 -0600 Subject: [PATCH] [NO JIRA] Remove unused ClassLoaderUtils --- .../sonar/server/util/ClassLoaderUtils.java | 114 ------------------ .../server/platform/ClassLoaderUtilsTest.java | 76 ------------ 2 files changed, 190 deletions(-) delete mode 100644 server/sonar-webserver-core/src/main/java/org/sonar/server/util/ClassLoaderUtils.java delete mode 100644 server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ClassLoaderUtilsTest.java diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/util/ClassLoaderUtils.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/util/ClassLoaderUtils.java deleted file mode 100644 index 7a787ea25ac..00000000000 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/util/ClassLoaderUtils.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.util; - -import com.google.common.base.Throwables; -import java.net.URL; -import java.net.URLDecoder; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Enumeration; -import java.util.function.Predicate; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; -import javax.annotation.Nullable; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.utils.log.Loggers; - -import static java.nio.charset.StandardCharsets.UTF_8; - -public class ClassLoaderUtils { - - private ClassLoaderUtils() { - // only static methods - } - - /** - * Finds files within a given directory and its subdirectories - * - * @param classLoader - * @param rootPath the root directory, for example org/sonar/sqale - * @return a list of relative paths, for example {"org/sonar/sqale/foo/bar.txt}. Never null. - */ - public static Collection listFiles(ClassLoader classLoader, String rootPath) { - return listResources(classLoader, rootPath, path -> !StringUtils.endsWith(path, "/")); - } - - /** - * Finds directories and files within a given directory and its subdirectories. - * - * @param classLoader - * @param rootPath the root directory, for example org/sonar/sqale, or a file in this root directory, for example org/sonar/sqale/index.txt - * @param predicate - * @return a list of relative paths, for example {"org/sonar/sqale", "org/sonar/sqale/foo", "org/sonar/sqale/foo/bar.txt}. Never null. - */ - public static Collection listResources(ClassLoader classLoader, String rootPath, Predicate predicate) { - String jarPath = null; - JarFile jar = null; - try { - Collection paths = new ArrayList<>(); - URL root = classLoader.getResource(rootPath); - if (root != null) { - checkJarFile(root); - - // Path of the root directory - // Examples : - // org/sonar/sqale/index.txt -> rootDirectory is org/sonar/sqale - // org/sonar/sqale/ -> rootDirectory is org/sonar/sqale - // org/sonar/sqale -> rootDirectory is org/sonar/sqale - String rootDirectory = rootPath; - if (StringUtils.substringAfterLast(rootPath, "/").indexOf('.') >= 0) { - rootDirectory = StringUtils.substringBeforeLast(rootPath, "/"); - } - // strip out only the JAR file - jarPath = root.getPath().substring(5, root.getPath().indexOf('!')); - jar = new JarFile(URLDecoder.decode(jarPath, UTF_8.name())); - Enumeration entries = jar.entries(); - while (entries.hasMoreElements()) { - String name = entries.nextElement().getName(); - if (name.startsWith(rootDirectory) && predicate.test(name)) { - paths.add(name); - } - } - } - return paths; - } catch (Exception e) { - throw Throwables.propagate(e); - } finally { - closeJar(jar, jarPath); - } - } - - private static void closeJar(@Nullable JarFile jar, String jarPath) { - if (jar != null) { - try { - jar.close(); - } catch (Exception e) { - Loggers.get(ClassLoaderUtils.class).error("Fail to close JAR file: " + jarPath, e); - } - } - } - - private static void checkJarFile(URL root) { - if (!"jar".equals(root.getProtocol())) { - throw new IllegalStateException("Unsupported protocol: " + root.getProtocol()); - } - } -} diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ClassLoaderUtilsTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ClassLoaderUtilsTest.java deleted file mode 100644 index 3f8215ee8ff..00000000000 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ClassLoaderUtilsTest.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.platform; - -import java.net.URL; -import java.net.URLClassLoader; -import java.util.Collection; -import org.junit.Before; -import org.junit.Test; -import org.sonar.server.util.ClassLoaderUtils; - -import static org.apache.commons.lang.StringUtils.endsWith; -import static org.assertj.core.api.Assertions.assertThat; - -public class ClassLoaderUtilsTest { - - private ClassLoader classLoader; - - @Before - public void prepareClassLoader() { - // This JAR file has the three following files : - // org/sonar/sqale/app/copyright.txt - // org/sonar/sqale/app/README.md - // org/sonar/other/other.txt - URL jarUrl = getClass().getResource("/org/sonar/server/platform/ClassLoaderUtilsTest/ClassLoaderUtilsTest.jar"); - classLoader = new URLClassLoader(new URL[] {jarUrl}, /* no parent classloader */null); - } - - @Test - public void listResources_unknown_root() { - Collection strings = ClassLoaderUtils.listResources(classLoader, "unknown/directory", s -> true); - assertThat(strings).isEmpty(); - } - - @Test - public void listResources_all() { - Collection strings = ClassLoaderUtils.listResources(classLoader, "org/sonar/sqale", s -> true); - assertThat(strings).containsOnly( - "org/sonar/sqale/", - "org/sonar/sqale/app/", - "org/sonar/sqale/app/copyright.txt", - "org/sonar/sqale/app/README.md"); - } - - @Test - public void listResources_use_predicate() { - Collection strings = ClassLoaderUtils.listResources(classLoader, "org/sonar/sqale", s -> endsWith(s, "md")); - assertThat(strings).containsOnly("org/sonar/sqale/app/README.md"); - } - - @Test - public void listFiles() { - Collection strings = ClassLoaderUtils.listFiles(classLoader, "org/sonar/sqale"); - assertThat(strings).containsOnly( - "org/sonar/sqale/app/copyright.txt", - "org/sonar/sqale/app/README.md"); - } - -} -- 2.39.5