Changeset 15416 in josm


Ignore:
Timestamp:
2019-10-05T00:50:17+02:00 (5 years ago)
Author:
Don-vip
Message:

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

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/CachedFile.java

    r15329 r15416  
    2424import java.util.List;
    2525import java.util.Map;
     26import java.util.Optional;
    2627import java.util.concurrent.ConcurrentHashMap;
    2728import java.util.concurrent.TimeUnit;
     
    3536import org.openstreetmap.josm.tools.Pair;
    3637import org.openstreetmap.josm.tools.PlatformManager;
     38import org.openstreetmap.josm.tools.ResourceProvider;
    3739import org.openstreetmap.josm.tools.Utils;
    3840
     
    4143 *
    4244 * Supports URLs, local files, and a custom scheme (<code>resource:</code>) to get
    43  * resources from the current *.jar file. (Local caching is only done for URLs.)
     45 * resources from the current JOSM *.jar file as well as plugins *.jar files.
     46 * (Local caching is only done for URLs.)
    4447 * <p>
    4548 * The mirrored file is only downloaded if it has been more than 7 days since
     
    225228        if (file == null) {
    226229            if (name != null && name.startsWith("resource://")) {
    227                 String resourceName = name.substring("resource:/".length());
    228                 InputStream is = Utils.getResourceAsStream(getClass(), resourceName);
    229                 if (is == null) {
    230                     throw new IOException(tr("Failed to open input stream for resource ''{0}''", name));
    231                 }
    232                 return is;
     230                return Optional.ofNullable(ResourceProvider.getResourceAsStream(name.substring("resource:/".length())))
     231                        .orElseThrow(() -> new IOException(tr("Failed to open input stream for resource ''{0}''", name)));
    233232            } else {
    234233                throw new IOException("No file found for: "+name);
  • trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java

    r15381 r15416  
    7373import org.openstreetmap.josm.tools.ImageProvider;
    7474import org.openstreetmap.josm.tools.Logging;
     75import org.openstreetmap.josm.tools.ResourceProvider;
    7576import org.openstreetmap.josm.tools.SubclassFilteredCollection;
    7677import org.openstreetmap.josm.tools.Utils;
     
    886887
    887888            extendJoinedPluginResourceCL(toLoad);
    888             ImageProvider.addAdditionalClassLoaders(getResourceClassLoaders());
     889            ResourceProvider.addAdditionalClassLoaders(getResourceClassLoaders());
    889890            monitor.setTicksCount(toLoad.size());
    890891            for (PluginInformation info : toLoad) {
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r15408 r15416  
    3535import java.util.Base64;
    3636import java.util.Collection;
    37 import java.util.Collections;
    3837import java.util.EnumMap;
    3938import java.util.HashMap;
    40 import java.util.HashSet;
    4139import java.util.Hashtable;
    4240import java.util.Iterator;
     
    4543import java.util.Map;
    4644import java.util.Objects;
    47 import java.util.Set;
    4845import java.util.TreeSet;
    4946import java.util.concurrent.CompletableFuture;
     
    263260    public static final String PROP_TRANSPARENCY_COLOR = "josm.transparency.color";
    264261
    265     /** set of class loaders to take images from */
    266     private static final Set<ClassLoader> classLoaders = Collections.synchronizedSet(new HashSet<>());
    267     static {
    268         try {
    269             classLoaders.add(ClassLoader.getSystemClassLoader());
    270         } catch (SecurityException e) {
    271             Logging.log(Logging.LEVEL_ERROR, "Unable to get system classloader", e);
    272         }
    273         try {
    274             classLoaders.add(ImageProvider.class.getClassLoader());
    275         } catch (SecurityException e) {
    276             Logging.log(Logging.LEVEL_ERROR, "Unable to get application classloader", e);
    277         }
    278     }
    279 
    280262    /** directories in which images are searched */
    281263    protected Collection<String> dirs;
     
    617599     * @return {@code true} if the set changed as a result of the call
    618600     * @since 12870
    619      */
     601     * @deprecated Use ResourceProvider#addAdditionalClassLoader
     602     */
     603    @Deprecated
    620604    public static boolean addAdditionalClassLoader(ClassLoader additionalClassLoader) {
    621         return classLoaders.add(additionalClassLoader);
     605        return ResourceProvider.addAdditionalClassLoader(additionalClassLoader);
    622606    }
    623607
     
    627611     * @return {@code true} if the set changed as a result of the call
    628612     * @since 12870
    629      */
     613     * @deprecated Use ResourceProvider#addAdditionalClassLoaders
     614     */
     615    @Deprecated
    630616    public static boolean addAdditionalClassLoaders(Collection<ClassLoader> additionalClassLoaders) {
    631         return classLoaders.addAll(additionalClassLoaders);
     617        return ResourceProvider.addAdditionalClassLoaders(additionalClassLoaders);
    632618    }
    633619
     
    12121198    private static URL getImageUrl(String path, String name) {
    12131199        if (path != null && path.startsWith("resource://")) {
    1214             String p = path.substring("resource://".length());
    1215             synchronized (classLoaders) {
    1216                 for (ClassLoader source : classLoaders) {
    1217                     URL res;
    1218                     if ((res = source.getResource(p + name)) != null)
    1219                         return res;
    1220                 }
    1221             }
     1200            return ResourceProvider.getResource(path.substring("resource://".length()) + name);
    12221201        } else {
    12231202            File f = new File(path, name);
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r15007 r15416  
    18831883    /**
    18841884     * Finds a resource with a given name, with robustness to known JDK bugs.
    1885      * @param klass class on which {@link Class#getResourceAsStream} will be called
     1885     * @param klass class on which {@link ClassLoader#getResourceAsStream} will be called
    18861886     * @param path name of the desired resource
    18871887     * @return  A {@link java.io.InputStream} object or {@code null} if no resource with this name is found
     
    18891889     */
    18901890    public static InputStream getResourceAsStream(Class<?> klass, String path) {
     1891        return getResourceAsStream(klass.getClassLoader(), path);
     1892    }
     1893
     1894    /**
     1895     * Finds a resource with a given name, with robustness to known JDK bugs.
     1896     * @param cl classloader on which {@link ClassLoader#getResourceAsStream} will be called
     1897     * @param path name of the desired resource
     1898     * @return  A {@link java.io.InputStream} object or {@code null} if no resource with this name is found
     1899     * @since 15416
     1900     */
     1901    public static InputStream getResourceAsStream(ClassLoader cl, String path) {
    18911902        try {
    1892             return klass.getResourceAsStream(path);
     1903            if (path != null && path.startsWith("/")) {
     1904                path = path.substring(1); // See Class#resolveName
     1905            }
     1906            return cl.getResourceAsStream(path);
    18931907        } catch (InvalidPathException e) {
    18941908            Logging.error("Cannot open {0}: {1}", path, e.getMessage());
    18951909            Logging.trace(e);
    18961910            try {
    1897                 URL betterUrl = betterJarUrl(klass.getResource(path));
     1911                URL betterUrl = betterJarUrl(cl.getResource(path));
    18981912                if (betterUrl != null) {
    18991913                    return betterUrl.openStream();
Note: See TracChangeset for help on using the changeset viewer.