Skip to content

Commit

Permalink
Clean up and replace URL in Set usage with URI to avoid DNS lookups i…
Browse files Browse the repository at this point in the history
…n hash/equals
  • Loading branch information
Renanse committed Mar 1, 2024
1 parent b42554e commit 717a48b
Showing 1 changed file with 18 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
Expand Down Expand Up @@ -51,11 +53,7 @@ public static ResourceSource locateResource(final String resourceType, String re
return null;
}

try {
resourceName = URLDecoder.decode(resourceName, "UTF-8");
} catch (final UnsupportedEncodingException ex) {
ex.printStackTrace();
}
resourceName = URLDecoder.decode(resourceName, StandardCharsets.UTF_8);

synchronized (_locatorMap) {
final List<ResourceLocator> bases = _locatorMap.get(resourceType);
Expand Down Expand Up @@ -89,12 +87,7 @@ public static void addResourceLocator(final String resourceType, final ResourceL
return;
}
synchronized (_locatorMap) {
List<ResourceLocator> bases = _locatorMap.get(resourceType);
if (bases == null) {
bases = new ArrayList<>();
_locatorMap.put(resourceType, bases);
}

List<ResourceLocator> bases = _locatorMap.computeIfAbsent(resourceType, k -> new ArrayList<>());
if (!bases.contains(locator)) {
bases.add(locator);
}
Expand Down Expand Up @@ -194,31 +187,31 @@ public static String getClassPathResourceAsString(final Class<?> clazz, final St
* a class to use as a local reference.
* @param name
* the name and path of the resource.
* @return a set containing the located URLs of the named resource.
* @return a set containing the located URIs of the named resource.
*/
public static Set<URL> getClassPathResources(final Class<?> clazz, final String name) {
final Set<URL> results = new HashSet<>();
public static Set<URI> getClassPathResources(final Class<?> clazz, final String name) {
final Set<URI> results = new HashSet<>();
Enumeration<URL> urls = null;
try {
urls = Thread.currentThread().getContextClassLoader().getResources(name);
for (; urls.hasMoreElements();) {
results.add(urls.nextElement());
while (urls.hasMoreElements()) {
results.add(urls.nextElement().toURI());
}
} catch (final IOException ioe) {}
if (!Thread.currentThread().getContextClassLoader().equals(ClassLoader.getSystemClassLoader())) {
} catch (final URISyntaxException | IOException ignored) {}
if (!Thread.currentThread().getContextClassLoader().equals(ClassLoader.getSystemClassLoader())) {
try {
urls = ClassLoader.getSystemResources(name);
for (; urls.hasMoreElements();) {
results.add(urls.nextElement());
while (urls.hasMoreElements()) {
results.add(urls.nextElement().toURI());
}
} catch (final IOException ioe) {}
} catch (final URISyntaxException | IOException ignored) {}
}
try {
urls = clazz.getClassLoader().getResources(name);
for (; urls.hasMoreElements();) {
results.add(urls.nextElement());
while (urls.hasMoreElements()) {
results.add(urls.nextElement().toURI());
}
} catch (final IOException ioe) {}
} catch (final URISyntaxException | IOException ignored) {}
return results;
}
}

0 comments on commit 717a48b

Please sign in to comment.