Skip to content

Commit

Permalink
[#9414] Refer classpath for querying available libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
acafela authored and smilu97 committed Nov 16, 2022
1 parent 2813dde commit bb9a5d7
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

package com.navercorp.pinpoint.test.plugin;

import com.navercorp.pinpoint.common.annotations.VisibleForTesting;
import com.navercorp.pinpoint.common.util.SystemProperty;
import com.navercorp.pinpoint.test.plugin.util.ArrayUtils;
import com.navercorp.pinpoint.test.plugin.util.CodeSourceUtils;
import com.navercorp.pinpoint.test.plugin.util.StringUtils;
import com.navercorp.pinpoint.test.plugin.util.TestLogger;
import com.navercorp.pinpoint.test.plugin.util.TestPluginVersion;

import org.junit.internal.runners.statements.RunAfters;
import org.junit.internal.runners.statements.RunBefores;
import org.junit.runner.Runner;
Expand All @@ -35,11 +36,11 @@
import java.lang.management.ManagementFactory;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -92,21 +93,13 @@ public AbstractPinpointPluginTestSuite(Class<?> testClass) throws Initialization
Repository repository = testClass.getAnnotation(Repository.class);
this.repositoryUrls = getRepository(repository);

List<ClassLoaderLib> classLoaderLibs = collectLib(getClass().getClassLoader());
if (logger.isDebugEnabled()) {
for (ClassLoaderLib classLoaderLib : classLoaderLibs) {
logger.debug("classLoader:{}", classLoaderLib.getClassLoader());
for (URL lib : classLoaderLib.getLibs()) {
logger.debug("-> {}", classLoaderLib.getClassLoader(), lib);
}
}
}
List<String> libs = collectLibs(getClass().getClassLoader());

final LibraryFilter requiredLibraryFilter = new LibraryFilter(
LibraryFilter.createContainsMatcher(PluginClassLoading.getContainsCheckClassPath()),
LibraryFilter.createGlobMatcher(PluginClassLoading.getGlobMatchesCheckClassPath()));

this.requiredLibraries = filterLib(classLoaderLibs, requiredLibraryFilter);
this.requiredLibraries = filterLibs(libs, requiredLibraryFilter);
if (logger.isDebugEnabled()) {
for (String requiredLibrary : requiredLibraries) {
logger.debug("requiredLibraries :{}", requiredLibrary);
Expand All @@ -116,10 +109,10 @@ public AbstractPinpointPluginTestSuite(Class<?> testClass) throws Initialization
final LibraryFilter mavenDependencyLibraryFilter = new LibraryFilter(
LibraryFilter.createContainsMatcher(PluginClassLoading.MAVEN_DEPENDENCY_CLASS_PATHS));

this.mavenDependencyLibraries = filterLib(classLoaderLibs, mavenDependencyLibraryFilter);
this.mavenDependencyLibraries = filterLibs(libs, mavenDependencyLibraryFilter);
if (logger.isDebugEnabled()) {
for (String mavenDependencyLibrary : mavenDependencyLibraries) {
logger.debug("mavenDependencyLibraries :{}", mavenDependencyLibrary);
logger.debug("mavenDependencyLibraries: {}", mavenDependencyLibrary);
}
}
this.testClassLocation = resolveTestClassLocation(testClass);
Expand Down Expand Up @@ -187,54 +180,86 @@ private String resolveTestClassLocation(Class<?> testClass) {
return toPathString(testClassLocation);
}

private static class ClassLoaderLib {
private final ClassLoader cl;
private final List<URL> libs;
private List<String> filterLibs(List<String> classPaths, LibraryFilter classPathFilter) {
final Set<String> result = new LinkedHashSet<>();
for (String classPath: classPaths) {
if (classPathFilter.filter(classPath)) {
result.add(classPath);
}
}
return new ArrayList<>(result);
}

private List<String> collectLibs(ClassLoader sourceCl) {
List<String> result = new ArrayList<>();
final ClassLoader termCl = ClassLoader.getSystemClassLoader().getParent();
for (ClassLoader cl : iterateClassLoaderChain(sourceCl, termCl)) {
final List<String> libs = extractLibrariesFromClassLoader(cl);
if (libs != null) {
result.addAll(libs);
if (logger.isDebugEnabled()) {
logger.debug("classLoader: {}", cl);
for (String lib : libs) {
logger.debug(" -> {}", lib);
}
}
}
}
return result;
}

public ClassLoaderLib(ClassLoader cl, List<URL> libs) {
this.cl = cl;
this.libs = libs;
private static Iterable<ClassLoader> iterateClassLoaderChain(ClassLoader src, ClassLoader term) {
final List<ClassLoader> classLoaders = new ArrayList<>(8);
ClassLoader cl = src;
while (cl != term) {
classLoaders.add(cl);
if (cl == Object.class.getClassLoader()) {
break;
}
cl = cl.getParent();
}
return classLoaders;
}

public ClassLoader getClassLoader() {
return cl;
private static List<String> extractLibrariesFromClassLoader(ClassLoader cl) {
if (cl instanceof URLClassLoader) {
return extractLibrariesFromURLClassLoader((URLClassLoader) cl);
}
if (cl == ClassLoader.getSystemClassLoader()) {
return extractLibrariesFromSystemClassLoader();
}
return null;
}

public List<URL> getLibs() {
return libs;
private static List<String> extractLibrariesFromURLClassLoader(URLClassLoader cl) {
final URL[] urls = cl.getURLs();
final List<String> paths = new ArrayList<>(urls.length);
for (URL url: urls) {
paths.add(normalizePath(toPathString(url)));
}
return paths;
}

private List<String> filterLib(List<ClassLoaderLib> classLoaderLibs, LibraryFilter classPathFilter) {
Set<String> result = new HashSet<>();
for (ClassLoaderLib classLoaderLib : classLoaderLibs) {
List<URL> libs = classLoaderLib.getLibs();
for (URL lib : libs) {
if (classPathFilter.filter(lib)) {
final String filterLibs = toPathString(lib);
if (filterLibs != null) {
result.add(filterLibs);
}
}
}
private static List<String> extractLibrariesFromSystemClassLoader() {
final String classPath = SystemProperty.INSTANCE.getProperty("java.class.path");
if (StringUtils.isEmpty(classPath)) {
return Collections.emptyList();
}
List<String> libs = new ArrayList<>(result);
libs.sort(Comparator.naturalOrder());
return libs;
final List<String> paths = Arrays.asList(classPath.split(":"));
return normalizePaths(paths);
}

private List<ClassLoaderLib> collectLib(ClassLoader cl) {
List<ClassLoaderLib> libs = new ArrayList<>();
while (cl != null) {
if (cl instanceof URLClassLoader) {
URLClassLoader ucl = ((URLClassLoader) cl);
URL[] urLs = ucl.getURLs();
libs.add(new ClassLoaderLib(cl, Arrays.asList(urLs)));
}
@VisibleForTesting
static String normalizePath(String classPath) {
return Paths.get(classPath).toAbsolutePath().normalize().toString();
}

cl = cl.getParent();
private static List<String> normalizePaths(List<String> classPaths) {
final List<String> result = new ArrayList<>(classPaths.size());
for (String cp: classPaths) {
result.add(normalizePath(cp));
}
return libs;
return result;
}

private static String toPathString(URL url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ public LibraryFilter(LibraryMatcher... matchers) {
this.matchers = Objects.requireNonNull(matchers, "matchers");
}

public boolean filter(URL url) {
public boolean filter(String cp) {
for (LibraryMatcher matcher : matchers) {
if (matcher.include(url.getFile())) {
if (matcher.include(cp)) {
return true;
}
}
return false;
}

public boolean filter(URL url) {
return filter(url.getFile());
}

interface LibraryMatcher {
boolean include(String filePath);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2022 NAVER Corp.
*
* Licensed 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.navercorp.pinpoint.test.plugin;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author youngjin.kim2
*/
@ExtendWith(MockitoExtension.class)
public class AbstractPinpointPluginTestSuiteTest {

@Test
public void normalizeShouldWork() {
final String result = AbstractPinpointPluginTestSuite.normalizePath("./foo/bar/../zoo");
final String expect = System.getProperty("user.dir") + "/foo/zoo";
assertThat(result).isEqualTo(expect);
}

}

0 comments on commit bb9a5d7

Please sign in to comment.