This is an automated email from the ASF dual-hosted git repository. madhan pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/atlas.git
The following commit(s) were added to refs/heads/master by this push: new de0996b8f ATLAS-4964: atlas-plugin-classloader module: update for code readabil… (#292) de0996b8f is described below commit de0996b8ff28e6be9bfa07ac5424b6158553d858 Author: jayendrap <jayen...@freestoneinfotech.com> AuthorDate: Sun Feb 16 23:54:49 2025 +0530 ATLAS-4964: atlas-plugin-classloader module: update for code readabil… (#292) --- plugin-classloader/pom.xml | 5 + .../plugin/classloader/AtlasPluginClassLoader.java | 174 ++++++++------------- .../classloader/AtlasPluginClassLoaderUtil.java | 63 +++----- .../classloader/AtlasPluginClassLoaderTest.java | 19 ++- 4 files changed, 100 insertions(+), 161 deletions(-) diff --git a/plugin-classloader/pom.xml b/plugin-classloader/pom.xml index 049bfe936..7e82ff2c7 100644 --- a/plugin-classloader/pom.xml +++ b/plugin-classloader/pom.xml @@ -31,6 +31,11 @@ <name>Apache Atlas Plugin Classloader</name> <description>Apache Atlas Plugin Classloader</description> + <properties> + <checkstyle.failOnViolation>true</checkstyle.failOnViolation> + <checkstyle.skip>false</checkstyle.skip> + </properties> + <dependencies> <dependency> <groupId>org.testng</groupId> diff --git a/plugin-classloader/src/main/java/org/apache/atlas/plugin/classloader/AtlasPluginClassLoader.java b/plugin-classloader/src/main/java/org/apache/atlas/plugin/classloader/AtlasPluginClassLoader.java index 0a71bf160..884693ba5 100644 --- a/plugin-classloader/src/main/java/org/apache/atlas/plugin/classloader/AtlasPluginClassLoader.java +++ b/plugin-classloader/src/main/java/org/apache/atlas/plugin/classloader/AtlasPluginClassLoader.java @@ -6,9 +6,9 @@ * 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 - * + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> * 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. @@ -17,7 +17,6 @@ */ package org.apache.atlas.plugin.classloader; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -67,11 +66,11 @@ public final class AtlasPluginClassLoader extends URLClassLoader { ret = pluginClassLoaders.get(pluginType); if (ret == null) { - ret = AccessController.doPrivileged(new PrivilegedExceptionAction<AtlasPluginClassLoader>() { - public AtlasPluginClassLoader run() throws URISyntaxException { - return new AtlasPluginClassLoader(pluginType, pluginClass); - } - }); + ret = AccessController.doPrivileged(new PrivilegedExceptionAction<AtlasPluginClassLoader>() { + public AtlasPluginClassLoader run() throws URISyntaxException { + return new AtlasPluginClassLoader(pluginType, pluginClass); + } + }); if (ret != null) { pluginClassLoaders.put(pluginType, ret); @@ -84,17 +83,13 @@ public final class AtlasPluginClassLoader extends URLClassLoader { @Override public Class<?> findClass(String name) throws ClassNotFoundException { - if (LOG.isTraceEnabled()) { - LOG.trace("==> AtlasPluginClassLoader.findClass({})", name); - } + LOG.trace("==> AtlasPluginClassLoader.findClass({})", name); Class<?> ret = null; try { // first try to find the class in pluginClassloader - if (LOG.isTraceEnabled()) { - LOG.trace("AtlasPluginClassLoader.findClass({}): calling pluginClassLoader.findClass()", name); - } + LOG.trace("AtlasPluginClassLoader.findClass({}): calling pluginClassLoader.findClass()", name); ret = super.findClass(name); } catch (Throwable e) { @@ -102,66 +97,23 @@ public final class AtlasPluginClassLoader extends URLClassLoader { MyClassLoader savedClassLoader = getComponentClassLoader(); if (savedClassLoader != null) { - if (LOG.isTraceEnabled()) { - LOG.trace("AtlasPluginClassLoader.findClass({}): calling componentClassLoader.findClass()", name); - } + LOG.trace("AtlasPluginClassLoader.findClass({}): calling componentClassLoader.findClass()", name); ret = savedClassLoader.findClass(name); } } - if (LOG.isTraceEnabled()) { - LOG.trace("<== AtlasPluginClassLoader.findClass({}): {}", name, ret); - } - - return ret; - } - - @Override - public Class<?> loadClass(String name) throws ClassNotFoundException { - if (LOG.isTraceEnabled()) { - LOG.trace("==> AtlasPluginClassLoader.loadClass({})", name); - } - - Class<?> ret = null; - - try { - // first try to load the class from pluginClassloader - if (LOG.isTraceEnabled()) { - LOG.trace("AtlasPluginClassLoader.loadClass({}): calling pluginClassLoader.loadClass()", name); - } - - ret = super.loadClass(name); - } catch (Throwable e) { - // on failure to load from pluginClassLoader, try to load from componentClassLoader - MyClassLoader savedClassLoader = getComponentClassLoader(); - - if (savedClassLoader != null) { - if (LOG.isTraceEnabled()) { - LOG.trace("AtlasPluginClassLoader.loadClass({}): calling componentClassLoader.loadClass()", name); - } - - ret = savedClassLoader.loadClass(name); - } - } - - if (LOG.isTraceEnabled()) { - LOG.trace("<== AtlasPluginClassLoader.loadClass({}): {}", name, ret); - } + LOG.trace("<== AtlasPluginClassLoader.findClass({}): {}", name, ret); return ret; } @Override public URL findResource(String name) { - if (LOG.isDebugEnabled()) { - LOG.debug("==> AtlasPluginClassLoader.findResource({}) ", name); - } + LOG.debug("==> AtlasPluginClassLoader.findResource({}) ", name); // first try to find the resource from pluginClassloader - if (LOG.isDebugEnabled()) { - LOG.debug("AtlasPluginClassLoader.findResource({}): calling pluginClassLoader.findResource()", name); - } + LOG.debug("AtlasPluginClassLoader.findResource({}): calling pluginClassLoader.findResource()", name); URL ret = super.findResource(name); @@ -169,30 +121,23 @@ public final class AtlasPluginClassLoader extends URLClassLoader { MyClassLoader savedClassLoader = getComponentClassLoader(); if (savedClassLoader != null) { - if (LOG.isDebugEnabled()) { - LOG.debug("AtlasPluginClassLoader.findResource({}): calling componentClassLoader.getResource()", name); - } + LOG.debug("AtlasPluginClassLoader.findResource({}): calling componentClassLoader.getResource()", name); ret = savedClassLoader.getResource(name); } } - if (LOG.isDebugEnabled()) { - LOG.debug("<== AtlasPluginClassLoader.findResource({}): {}", name, ret); - } + LOG.debug("<== AtlasPluginClassLoader.findResource({}): {}", name, ret); return ret; } @Override public Enumeration<URL> findResources(String name) throws IOException { - if (LOG.isDebugEnabled()) { - LOG.debug("==> AtlasPluginClassLoader.findResources({})", name); - } + LOG.debug("==> AtlasPluginClassLoader.findResources({})", name); - Enumeration<URL> ret = null; - - Enumeration<URL> resourcesInPluginClsLoader = findResourcesUsingPluginClassLoader(name); + Enumeration<URL> ret; + Enumeration<URL> resourcesInPluginClsLoader = findResourcesUsingPluginClassLoader(name); Enumeration<URL> resourcesInComponentClsLoader = findResourcesUsingComponentClassLoader(name); if (resourcesInPluginClsLoader != null && resourcesInComponentClsLoader != null) { @@ -203,31 +148,50 @@ public final class AtlasPluginClassLoader extends URLClassLoader { ret = resourcesInComponentClsLoader; } - if (LOG.isDebugEnabled()) { - LOG.debug("<== AtlasPluginClassLoader.findResources({}): {}", name, ret); + LOG.debug("<== AtlasPluginClassLoader.findResources({}): {}", name, ret); + + return ret; + } + + @Override + public Class<?> loadClass(String name) throws ClassNotFoundException { + LOG.trace("==> AtlasPluginClassLoader.loadClass({})", name); + + Class<?> ret = null; + + try { + // first try to load the class from pluginClassloader + LOG.trace("AtlasPluginClassLoader.loadClass({}): calling pluginClassLoader.loadClass()", name); + + ret = super.loadClass(name); + } catch (Throwable e) { + // on failure to load from pluginClassLoader, try to load from componentClassLoader + MyClassLoader savedClassLoader = getComponentClassLoader(); + + if (savedClassLoader != null) { + LOG.trace("AtlasPluginClassLoader.loadClass({}): calling componentClassLoader.loadClass()", name); + + ret = savedClassLoader.loadClass(name); + } } + LOG.trace("<== AtlasPluginClassLoader.loadClass({}): {}", name, ret); + return ret; } public void activate() { - if (LOG.isDebugEnabled()) { - LOG.debug("==> AtlasPluginClassLoader.activate()"); - } + LOG.debug("==> AtlasPluginClassLoader.activate()"); preActivateClassLoader.set(Thread.currentThread().getContextClassLoader()); Thread.currentThread().setContextClassLoader(this); - if (LOG.isDebugEnabled()) { - LOG.debug("<== AtlasPluginClassLoader.activate()"); - } + LOG.debug("<== AtlasPluginClassLoader.activate()"); } public void deactivate() { - if (LOG.isDebugEnabled()) { - LOG.debug("==> AtlasPluginClassLoader.deactivate()"); - } + LOG.debug("==> AtlasPluginClassLoader.deactivate()"); ClassLoader classLoader = preActivateClassLoader.get(); @@ -235,6 +199,7 @@ public final class AtlasPluginClassLoader extends URLClassLoader { preActivateClassLoader.remove(); } else { MyClassLoader savedClassLoader = getComponentClassLoader(); + if (savedClassLoader != null && savedClassLoader.getParent() != null) { classLoader = savedClassLoader.getParent(); } @@ -243,13 +208,10 @@ public final class AtlasPluginClassLoader extends URLClassLoader { if (classLoader != null) { Thread.currentThread().setContextClassLoader(classLoader); } else { - LOG.warn("AtlasPluginClassLoader.deactivate() was not successful.Couldn't not get the saved " - + "componentClassLoader..."); + LOG.warn("AtlasPluginClassLoader.deactivate() was not successful.Couldn't not get the saved componentClassLoader..."); } - if (LOG.isDebugEnabled()) { - LOG.debug("<== AtlasPluginClassLoader.deactivate()"); - } + LOG.debug("<== AtlasPluginClassLoader.deactivate()"); } private MyClassLoader getComponentClassLoader() { @@ -257,9 +219,7 @@ public final class AtlasPluginClassLoader extends URLClassLoader { } private Enumeration<URL> findResourcesUsingPluginClassLoader(String name) { - if (LOG.isDebugEnabled()) { - LOG.debug("==> AtlasPluginClassLoader.findResourcesUsingPluginClassLoader({})", name); - } + LOG.debug("==> AtlasPluginClassLoader.findResourcesUsingPluginClassLoader({})", name); Enumeration<URL> ret = null; @@ -267,22 +227,16 @@ public final class AtlasPluginClassLoader extends URLClassLoader { ret = super.findResources(name); } catch (Throwable excp) { // Ignore exceptions - if (LOG.isDebugEnabled()) { - LOG.debug("AtlasPluginClassLoader.findResourcesUsingPluginClassLoader({}): resource not found in plugin", name, excp); - } + LOG.debug("AtlasPluginClassLoader.findResourcesUsingPluginClassLoader({}): resource not found in plugin", name, excp); } - if (LOG.isDebugEnabled()) { - LOG.debug("<== AtlasPluginClassLoader.findResourcesUsingPluginClassLoader({}): {}", name, ret); - } + LOG.debug("<== AtlasPluginClassLoader.findResourcesUsingPluginClassLoader({}): {}", name, ret); return ret; } private Enumeration<URL> findResourcesUsingComponentClassLoader(String name) { - if (LOG.isDebugEnabled()) { - LOG.debug("==> AtlasPluginClassLoader.findResourcesUsingComponentClassLoader({})", name); - } + LOG.debug("==> AtlasPluginClassLoader.findResourcesUsingComponentClassLoader({})", name); Enumeration<URL> ret = null; @@ -290,28 +244,22 @@ public final class AtlasPluginClassLoader extends URLClassLoader { MyClassLoader savedClassLoader = getComponentClassLoader(); if (savedClassLoader != null) { - if (LOG.isDebugEnabled()) { - LOG.debug("AtlasPluginClassLoader.findResourcesUsingComponentClassLoader({}): calling componentClassLoader.getResources()", name); - } + LOG.debug("AtlasPluginClassLoader.findResourcesUsingComponentClassLoader({}): calling componentClassLoader.getResources()", name); ret = savedClassLoader.getResources(name); } - if (LOG.isDebugEnabled()) { - LOG.debug("<== AtlasPluginClassLoader.findResourcesUsingComponentClassLoader({}): {}", name, ret); - } + LOG.debug("<== AtlasPluginClassLoader.findResourcesUsingComponentClassLoader({}): {}", name, ret); } catch (Throwable t) { - if (LOG.isDebugEnabled()) { - LOG.debug("AtlasPluginClassLoader.findResourcesUsingComponentClassLoader({}): class not found in componentClassLoader.", name, t); - } + LOG.debug("AtlasPluginClassLoader.findResourcesUsingComponentClassLoader({}): class not found in componentClassLoader.", name, t); } return ret; } static class MergeEnumeration implements Enumeration<URL> { //NOPMD - private Enumeration<URL> e1 = null; - private Enumeration<URL> e2 = null; + private Enumeration<URL> e1; + private Enumeration<URL> e2; public MergeEnumeration(Enumeration<URL> e1, Enumeration<URL> e2) { this.e1 = e1; diff --git a/plugin-classloader/src/main/java/org/apache/atlas/plugin/classloader/AtlasPluginClassLoaderUtil.java b/plugin-classloader/src/main/java/org/apache/atlas/plugin/classloader/AtlasPluginClassLoaderUtil.java index e6f868be7..45e0bf950 100644 --- a/plugin-classloader/src/main/java/org/apache/atlas/plugin/classloader/AtlasPluginClassLoaderUtil.java +++ b/plugin-classloader/src/main/java/org/apache/atlas/plugin/classloader/AtlasPluginClassLoaderUtil.java @@ -6,9 +6,9 @@ * 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 - * + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> * 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. @@ -17,7 +17,6 @@ */ package org.apache.atlas.plugin.classloader; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,7 +29,6 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; - /** * AtlasPluginClassLoaderUtil used by AtlasPluginClassLoader. */ @@ -39,12 +37,10 @@ final class AtlasPluginClassLoaderUtil { private static final String ATLAS_PLUGIN_LIBDIR = "atlas-%-plugin-impl"; - private AtlasPluginClassLoaderUtil(){ } + private AtlasPluginClassLoaderUtil() { } public static URL[] getFilesInDirectories(String[] libDirs) { - if (LOG.isDebugEnabled()) { - LOG.debug("==> AtlasPluginClassLoaderUtil.getFilesInDirectories()"); - } + LOG.debug("==> AtlasPluginClassLoaderUtil.getFilesInDirectories()"); List<URL> ret = new ArrayList<>(); @@ -52,17 +48,25 @@ final class AtlasPluginClassLoaderUtil { getFilesInDirectory(libDir, ret); } - if (LOG.isDebugEnabled()) { - LOG.debug("<== AtlasPluginClassLoaderUtil.getFilesInDirectories(): {} files", ret.size()); - } + LOG.debug("<== AtlasPluginClassLoaderUtil.getFilesInDirectories(): {} files", ret.size()); + + return ret.toArray(new URL[] {}); + } + + public static String[] getPluginImplLibPath(String pluginType, Class<?> pluginClass) throws URISyntaxException { + LOG.debug("==> AtlasPluginClassLoaderUtil.getPluginImplLibPath for Class ({})", pluginClass.getName()); + + URI uri = pluginClass.getProtectionDomain().getCodeSource().getLocation().toURI(); + Path path = Paths.get(URI.create(uri.toString())); + String ret = path.getParent().toString() + File.separatorChar + ATLAS_PLUGIN_LIBDIR.replaceAll("%", pluginType); + + LOG.debug("<== AtlasPluginClassLoaderUtil.getPluginImplLibPath for Class {}): {})", pluginClass.getName(), ret); - return ret.toArray(new URL[]{}); + return new String[] {ret}; } private static void getFilesInDirectory(String dirPath, List<URL> files) { - if (LOG.isDebugEnabled()) { - LOG.debug("==> AtlasPluginClassLoaderUtil.getPluginFiles()"); - } + LOG.debug("==> AtlasPluginClassLoaderUtil.getPluginFiles()"); if (dirPath != null) { try { @@ -73,14 +77,11 @@ final class AtlasPluginClassLoaderUtil { try { URL jarPath = dirFile.toURI().toURL(); - if (LOG.isDebugEnabled()) { - LOG.debug("getFilesInDirectory('{}'): adding {}", dirPath, dirFile.getAbsolutePath()); - } + LOG.debug("getFilesInDirectory('{}'): adding {}", dirPath, dirFile.getAbsolutePath()); files.add(jarPath); } catch (Exception excp) { - LOG.warn("getFilesInDirectory('{}'): failed to get URI for file {}", dirPath, dirFile - .getAbsolutePath(), excp); + LOG.warn("getFilesInDirectory('{}'): failed to get URI for file {}", dirPath, dirFile.getAbsolutePath(), excp); } } } @@ -91,24 +92,6 @@ final class AtlasPluginClassLoaderUtil { LOG.warn("getFilesInDirectory('{}'): could not find directory in path {}", dirPath, dirPath); } - if (LOG.isDebugEnabled()) { - LOG.debug("<== AtlasPluginClassLoaderUtil.getFilesInDirectory({})", dirPath); - } - } - - public static String[] getPluginImplLibPath(String pluginType, Class<?> pluginClass) throws URISyntaxException { - if (LOG.isDebugEnabled()) { - LOG.debug("==> AtlasPluginClassLoaderUtil.getPluginImplLibPath for Class ({})", pluginClass.getName()); - } - - URI uri = pluginClass.getProtectionDomain().getCodeSource().getLocation().toURI(); - Path path = Paths.get(URI.create(uri.toString())); - String ret = path.getParent().toString() + File.separatorChar + ATLAS_PLUGIN_LIBDIR.replaceAll("%", pluginType); - - if (LOG.isDebugEnabled()) { - LOG.debug("<== AtlasPluginClassLoaderUtil.getPluginImplLibPath for Class {}): {})", pluginClass.getName(), ret); - } - - return new String[] { ret }; + LOG.debug("<== AtlasPluginClassLoaderUtil.getFilesInDirectory({})", dirPath); } } diff --git a/plugin-classloader/src/test/java/org/apache/atlas/plugin/classloader/AtlasPluginClassLoaderTest.java b/plugin-classloader/src/test/java/org/apache/atlas/plugin/classloader/AtlasPluginClassLoaderTest.java index e12584f92..3731c86ff 100644 --- a/plugin-classloader/src/test/java/org/apache/atlas/plugin/classloader/AtlasPluginClassLoaderTest.java +++ b/plugin-classloader/src/test/java/org/apache/atlas/plugin/classloader/AtlasPluginClassLoaderTest.java @@ -6,9 +6,9 @@ * 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 - * + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> * 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. @@ -17,30 +17,31 @@ */ package org.apache.atlas.plugin.classloader; -import org.testng.Assert; import org.testng.annotations.Test; +import static org.testng.Assert.fail; public class AtlasPluginClassLoaderTest { - @Test public void testClassLoader() throws Exception { String cls = "org.apache.atlas.service.Services"; try { loadClass(cls, null); - Assert.fail("Expected ClassNotFoundException"); + + fail("Expected ClassNotFoundException"); } catch (ClassNotFoundException e) { //expected } - AtlasPluginClassLoader classLoader = new AtlasPluginClassLoader(new String[]{ "../common/target" }, this.getClass()); + AtlasPluginClassLoader classLoader = new AtlasPluginClassLoader(new String[] {"../common/target"}, this.getClass()); classLoader.activate(); //org.apache.atlas.service.Services class should be loadable now //should also load org.apache.atlas.service.Service Class<?> servicesCls = loadClass(cls, null); + loadClass("org.apache.atlas.service.Service", servicesCls.getClassLoader()); //Fall back to current class loader should also work @@ -51,7 +52,8 @@ public class AtlasPluginClassLoaderTest { //After disable, class loading should fail again try { loadClass(cls, null); - Assert.fail("Expected ClassNotFoundException"); + + fail("Expected ClassNotFoundException"); } catch (ClassNotFoundException e) { //expected } @@ -61,6 +63,7 @@ public class AtlasPluginClassLoaderTest { if (classLoader == null) { classLoader = Thread.currentThread().getContextClassLoader(); } + return Class.forName(cls, true, classLoader); } }