source: josm/trunk/src/org/openstreetmap/josm/tools/ResourceProvider.java@ 15547

Last change on this file since 15547 was 15416, checked in by Don-vip, 5 years ago

fix #18190 - Handle "resource:" identical for CachedFile and images

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.io.InputStream;
5import java.net.URL;
6import java.util.Collection;
7import java.util.Collections;
8import java.util.HashSet;
9import java.util.Set;
10import java.util.function.Function;
11
12/**
13 * Unified provider that looks up for resource in various classloaders (josm, plugins, etc.).
14 * @since 15416
15 */
16public final class ResourceProvider {
17
18 /** set of class loaders to take resources from */
19 private static final Set<ClassLoader> classLoaders = Collections.synchronizedSet(new HashSet<>());
20 static {
21 try {
22 classLoaders.add(ClassLoader.getSystemClassLoader());
23 } catch (SecurityException e) {
24 Logging.log(Logging.LEVEL_ERROR, "Unable to get system classloader", e);
25 }
26 try {
27 classLoaders.add(ResourceProvider.class.getClassLoader());
28 } catch (SecurityException e) {
29 Logging.log(Logging.LEVEL_ERROR, "Unable to get application classloader", e);
30 }
31 }
32
33 private ResourceProvider() {
34 // Hide default constructor for utilities classes
35 }
36
37 /**
38 * Add an additional class loader to search image for.
39 * @param additionalClassLoader class loader to add to the internal set
40 * @return {@code true} if the set changed as a result of the call
41 */
42 public static boolean addAdditionalClassLoader(ClassLoader additionalClassLoader) {
43 return classLoaders.add(additionalClassLoader);
44 }
45
46 /**
47 * Add a collection of additional class loaders to search image for.
48 * @param additionalClassLoaders class loaders to add to the internal set
49 * @return {@code true} if the set changed as a result of the call
50 */
51 public static boolean addAdditionalClassLoaders(Collection<ClassLoader> additionalClassLoaders) {
52 return classLoaders.addAll(additionalClassLoaders);
53 }
54
55 private static <T> T getFirstNotNull(Function<ClassLoader, T> function) {
56 synchronized (classLoaders) {
57 for (ClassLoader source : classLoaders) {
58 T res = function.apply(source);
59 if (res != null)
60 return res;
61 }
62 }
63 return null;
64 }
65
66 /**
67 * Finds the resource with the given name.
68 * @param name The resource name
69 * @return A {@code URL} object for reading the resource, or {@code null} if the resource could not be found
70 * or the invoker doesn't have adequate privileges to get the resource.
71 * @see ClassLoader#getResource
72 */
73 public static URL getResource(String name) {
74 return getFirstNotNull(x -> x.getResource(name));
75 }
76
77 /**
78 * Finds a resource with a given name, with robustness to known JDK bugs.
79 * @param name name of the desired resource
80 * @return A {@link java.io.InputStream} object or {@code null} if no resource with this name is found
81 */
82 public static InputStream getResourceAsStream(String name) {
83 return getFirstNotNull(x -> Utils.getResourceAsStream(x, name));
84 }
85}
Note: See TracBrowser for help on using the repository browser.