From 290c1470ed0dcce4f3b74492a8145e56cbbe0de9 Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Fri, 25 Sep 2015 17:48:45 +0300 Subject: [PATCH] make DefaultExtensionFinder testable --- .../fortsoft/pf4j/DefaultExtensionFinder.java | 71 +++++++++++-------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java index 2df14ae..281b6a5 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java @@ -12,14 +12,21 @@ */ package ro.fortsoft.pf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; -import java.util.*; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; /** * The default implementation for ExtensionFinder. @@ -32,9 +39,9 @@ public class DefaultExtensionFinder implements ExtensionFinder, PluginStateListe private static final Logger log = LoggerFactory.getLogger(DefaultExtensionFinder.class); - private PluginManager pluginManager; - private ExtensionFactory extensionFactory; - private volatile Map> entries; // cache by pluginId + protected PluginManager pluginManager; + protected ExtensionFactory extensionFactory; + protected volatile Map> entries; // cache by pluginId public DefaultExtensionFinder(PluginManager pluginManager, ExtensionFactory extensionFactory) { this.pluginManager = pluginManager; @@ -51,7 +58,7 @@ public class DefaultExtensionFinder implements ExtensionFinder, PluginStateListe } log.debug("Finding extensions for extension point '{}'", type.getName()); - readIndexFiles(); + Map> entries = getEntries(); List> result = new ArrayList<>(); for (Map.Entry> entry : entries.entrySet()) { @@ -64,9 +71,7 @@ public class DefaultExtensionFinder implements ExtensionFinder, PluginStateListe } } - Set extensionClassNames = entry.getValue(); - - for (String className : extensionClassNames) { + for (String className : entry.getValue()) { try { ClassLoader classLoader; if (pluginId != null) { @@ -100,7 +105,7 @@ public class DefaultExtensionFinder implements ExtensionFinder, PluginStateListe if (entries.isEmpty()) { log.debug("No extensions found for extension point '{}'", type.getName()); } else { - log.debug("Found {} extensions for extension point '{}'", entries.size(), type.getName()); + log.debug("Found {} extensions for extension point '{}'", result.size(), type.getName()); } // sort by "ordinal" property @@ -111,8 +116,7 @@ public class DefaultExtensionFinder implements ExtensionFinder, PluginStateListe @Override public Set findClassNames(String pluginId) { - readIndexFiles(); - return entries.get(pluginId); + return getEntries().get(pluginId); } @Override @@ -122,23 +126,20 @@ public class DefaultExtensionFinder implements ExtensionFinder, PluginStateListe entries = null; } - private Map> readIndexFiles() { - // checking cache - if (entries != null) { - return entries; - } + protected Map> readIndexFiles() { + Map> result = new LinkedHashMap<>(); - entries = new LinkedHashMap<>(); + result.putAll(readClasspathIndexFiles()); + result.putAll(readPluginsIndexFiles()); - readClasspathIndexFiles(); - readPluginsIndexFiles(); - - return entries; + return result; } - private void readClasspathIndexFiles() { + private Map> readClasspathIndexFiles() { log.debug("Reading extensions index files from classpath"); + Map> result = new LinkedHashMap<>(); + Set bucket = new HashSet<>(); try { Enumeration urls = getClass().getClassLoader().getResources(ExtensionsIndexer.EXTENSIONS_RESOURCE); @@ -158,15 +159,19 @@ public class DefaultExtensionFinder implements ExtensionFinder, PluginStateListe } } - entries.put(null, bucket); + result.put(null, bucket); } catch (IOException e) { log.error(e.getMessage(), e); } + + return result; } - private void readPluginsIndexFiles() { + private Map> readPluginsIndexFiles() { log.debug("Reading extensions index files from plugins"); + Map> result = new LinkedHashMap<>(); + List plugins = pluginManager.getPlugins(); for (PluginWrapper plugin : plugins) { String pluginId = plugin.getDescriptor().getPluginId(); @@ -192,15 +197,25 @@ public class DefaultExtensionFinder implements ExtensionFinder, PluginStateListe } } - entries.put(pluginId, bucket); + result.put(pluginId, bucket); } catch (IOException e) { log.error(e.getMessage(), e); } } + + return result; } private boolean isExtensionPoint(Class type) { return ExtensionPoint.class.isAssignableFrom(type); } + private Map> getEntries() { + if (entries == null) { + entries = readIndexFiles(); + } + + return entries; + } + }