MCPcopy Create free account
hub / github.com/apache/sirona / ClassLoaders

Class ClassLoaders

api/src/main/java/org/apache/sirona/util/ClassLoaders.java:30–193  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28import java.util.Set;
29
30public class ClassLoaders {
31 private static final boolean DONT_USE_GET_URLS = Boolean.getBoolean("xbean.finder.use.get-resources");
32 private static final ClassLoader SYSTEM = ClassLoader.getSystemClassLoader();
33 private static final boolean UNIX = !System.getProperty("os.name").toLowerCase().contains("win");
34 private static final URL[] NO_URL = new URL[0];
35
36 public static ClassLoader current() {
37 final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
38 if (tccl != null) {
39 return tccl;
40 }
41 return ClassLoaders.class.getClassLoader();
42 }
43
44 public static URL[] findUrls(final ClassLoader classLoader) throws IOException {
45 if (classLoader == null || (SYSTEM.getParent() != null && classLoader == SYSTEM.getParent())) {
46 return NO_URL;
47 }
48
49 final Set<URL> urls = new HashSet<URL>();
50
51 if (URLClassLoader.class.isInstance(classLoader) && !DONT_USE_GET_URLS) {
52 if (!isSurefire(classLoader)) {
53 for (final URL[] item : new URL[][] { URLClassLoader.class.cast(classLoader).getURLs(), findUrls(classLoader.getParent()) }) {
54 for (final URL url : item) {
55 addIfNotSo(urls, url);
56 }
57 }
58 } else { // http://jira.codehaus.org/browse/SUREFIRE-928 - we could reuse findUrlFromResources but this seems faster
59 urls.addAll(fromClassPath());
60 }
61 }
62
63 // DONT_USE_GET_URLS || java -jar xxx.jar and use MANIFEST.MF Class-Path?
64 // here perf is not an issue since we would either miss all the classpath or we have a single jar
65 if (urls.size() <= 1) {
66 final Set<URL> urlFromResources = findUrlFromResources(classLoader);
67 if (!urls.isEmpty()) {
68 final URL theUrl = urls.iterator().next();
69 if ("file".equals(theUrl.getProtocol())) { // theUrl can be file:xxxx but it is the same entry actually
70 urlFromResources.remove(new URL("jar:" + theUrl.toExternalForm() + "!/"));
71 }
72 }
73 urls.addAll(urlFromResources);
74 }
75
76 return urls.toArray(new URL[urls.size()]);
77 }
78
79 private static void addIfNotSo(final Set<URL> urls, final URL url) {
80 if (UNIX && isNative(url)) {
81 return;
82 }
83
84 urls.add(url);
85 }
86
87 public static boolean isNative(final URL url) {

Callers

nothing calls this directly

Calls 1

getPropertyMethod · 0.80

Tested by

no test coverage detected