From 58f7c85ab90327b2305f8ad777fd2ff5daba1d8f Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Fri, 28 Dec 2018 23:17:34 +0200 Subject: [PATCH] Polishing (extract duplicate code in internal method, remove unneeded null check) #262 --- .../java/org/pf4j/LegacyExtensionFinder.java | 32 ++++++++--------- .../pf4j/ServiceProviderExtensionFinder.java | 35 +++++++++---------- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/pf4j/src/main/java/org/pf4j/LegacyExtensionFinder.java b/pf4j/src/main/java/org/pf4j/LegacyExtensionFinder.java index 9995d89..5e682f3 100644 --- a/pf4j/src/main/java/org/pf4j/LegacyExtensionFinder.java +++ b/pf4j/src/main/java/org/pf4j/LegacyExtensionFinder.java @@ -15,9 +15,9 @@ */ package org.pf4j; +import org.pf4j.processor.LegacyExtensionStorage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.pf4j.processor.LegacyExtensionStorage; import java.io.IOException; import java.io.InputStreamReader; @@ -53,13 +53,7 @@ public class LegacyExtensionFinder extends AbstractExtensionFinder { Set bucket = new HashSet<>(); try { Enumeration urls = getClass().getClassLoader().getResources(getExtensionsResource()); - while (urls.hasMoreElements()) { - URL url = urls.nextElement(); - log.debug("Read '{}'", url.getFile()); - try (Reader reader = new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)) { - LegacyExtensionStorage.read(reader, bucket); - } - } + collectExtensions(urls, bucket); debugExtensions(bucket); @@ -84,16 +78,10 @@ public class LegacyExtensionFinder extends AbstractExtensionFinder { try { Enumeration urls = ((PluginClassLoader) plugin.getPluginClassLoader()).findResources(getExtensionsResource()); - if (urls == null || !urls.hasMoreElements()) { - log.debug("Cannot find '{}'", getExtensionsResource()); + if (urls.hasMoreElements()) { + collectExtensions(urls, bucket); } else { - while (urls.hasMoreElements()) { - URL url = urls.nextElement(); - log.debug("Read '{}'", url.getFile()); - try (Reader reader = new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)) { - LegacyExtensionStorage.read(reader, bucket); - } - } + log.debug("Cannot find '{}'", getExtensionsResource()); } debugExtensions(bucket); @@ -107,6 +95,16 @@ public class LegacyExtensionFinder extends AbstractExtensionFinder { return result; } + private void collectExtensions(Enumeration urls, Set bucket) throws IOException { + while (urls.hasMoreElements()) { + URL url = urls.nextElement(); + log.debug("Read '{}'", url.getFile()); + try (Reader reader = new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)) { + LegacyExtensionStorage.read(reader, bucket); + } + } + } + private static String getExtensionsResource() { return LegacyExtensionStorage.EXTENSIONS_RESOURCE; } diff --git a/pf4j/src/main/java/org/pf4j/ServiceProviderExtensionFinder.java b/pf4j/src/main/java/org/pf4j/ServiceProviderExtensionFinder.java index bfa8f5a..66efb8d 100644 --- a/pf4j/src/main/java/org/pf4j/ServiceProviderExtensionFinder.java +++ b/pf4j/src/main/java/org/pf4j/ServiceProviderExtensionFinder.java @@ -63,14 +63,7 @@ public class ServiceProviderExtensionFinder extends AbstractExtensionFinder { try { URL url = getClass().getClassLoader().getResource(getExtensionsResource()); if (url != null) { - Path extensionPath; - if (url.toURI().getScheme().equals("jar")) { - extensionPath = FileUtils.getPath(url.toURI(), getExtensionsResource()); - } else { - extensionPath = Paths.get(url.toURI()); - } - - bucket.addAll(readExtensions(extensionPath)); + collectExtensions(url, bucket); } debugExtensions(bucket); @@ -96,20 +89,13 @@ public class ServiceProviderExtensionFinder extends AbstractExtensionFinder { try { Enumeration urls = ((PluginClassLoader) plugin.getPluginClassLoader()).findResources(getExtensionsResource()); - if (urls == null || !urls.hasMoreElements()) { - log.debug("Cannot find '{}'", getExtensionsResource()); - } else { + if (urls.hasMoreElements()) { while (urls.hasMoreElements()) { URL url = urls.nextElement(); - Path extensionPath; - if (url.toURI().getScheme().equals("jar")) { - extensionPath = FileUtils.getPath(url.toURI(), getExtensionsResource()); - } else { - extensionPath = Paths.get(url.toURI()); - } - - bucket.addAll(readExtensions(extensionPath)); + collectExtensions(url, bucket); } + } else { + log.debug("Cannot find '{}'", getExtensionsResource()); } debugExtensions(bucket); @@ -123,6 +109,17 @@ public class ServiceProviderExtensionFinder extends AbstractExtensionFinder { return result; } + private void collectExtensions(URL url, Set bucket) throws URISyntaxException, IOException { + Path extensionPath; + if (url.toURI().getScheme().equals("jar")) { + extensionPath = FileUtils.getPath(url.toURI(), getExtensionsResource()); + } else { + extensionPath = Paths.get(url.toURI()); + } + + bucket.addAll(readExtensions(extensionPath)); + } + private static String getExtensionsResource() { return ServiceProviderExtensionStorage.EXTENSIONS_RESOURCE; } -- 2.39.5