aboutsummaryrefslogtreecommitdiffstats
path: root/pf4j
diff options
context:
space:
mode:
authorDecebal Suiu <decebal.suiu@gmail.com>2016-08-11 23:07:58 +0300
committerDecebal Suiu <decebal.suiu@gmail.com>2016-08-11 23:07:58 +0300
commitf595eb5dfaea617af739df9e8e8de94a0a44a67a (patch)
tree187daf89d3b2c487cba3f9dc00e21e36d7c095d9 /pf4j
parentd6f9ccb1196d12c96400889369a01122ed684160 (diff)
downloadpf4j-f595eb5dfaea617af739df9e8e8de94a0a44a67a.tar.gz
pf4j-f595eb5dfaea617af739df9e8e8de94a0a44a67a.zip
Return a list of all extensions from a plugin and optional for an extension point
Diffstat (limited to 'pf4j')
-rw-r--r--pf4j/src/main/java/ro/fortsoft/pf4j/AbstractExtensionFinder.java176
-rw-r--r--pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java21
-rw-r--r--pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java23
-rw-r--r--pf4j/src/main/java/ro/fortsoft/pf4j/ExtensionFinder.java10
-rw-r--r--pf4j/src/main/java/ro/fortsoft/pf4j/ExtensionWrapper.java3
-rw-r--r--pf4j/src/main/java/ro/fortsoft/pf4j/LegacyExtensionFinder.java18
-rw-r--r--pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java4
-rw-r--r--pf4j/src/main/java/ro/fortsoft/pf4j/ServiceProviderExtensionFinder.java57
8 files changed, 216 insertions, 96 deletions
diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/AbstractExtensionFinder.java b/pf4j/src/main/java/ro/fortsoft/pf4j/AbstractExtensionFinder.java
index f54a2ab..cdfeb38 100644
--- a/pf4j/src/main/java/ro/fortsoft/pf4j/AbstractExtensionFinder.java
+++ b/pf4j/src/main/java/ro/fortsoft/pf4j/AbstractExtensionFinder.java
@@ -48,67 +48,131 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin
public <T> List<ExtensionWrapper<T>> find(Class<T> type) {
log.debug("Finding extensions of extension point '{}'", type.getName());
Map<String, Set<String>> entries = getEntries();
+ List<ExtensionWrapper<T>> result = new ArrayList<>();
+
+ // add extensions found in classpath
+ List<ExtensionWrapper<T>> classpathExtensions = find(type, null);
+ result.addAll(classpathExtensions);
+
+ // add extensions found in each plugin
+ for (String pluginId : entries.keySet()) {
+ List<ExtensionWrapper<T>> pluginExtensions = find(type, pluginId);
+ result.addAll(pluginExtensions);
+ }
+
+ if (entries.isEmpty()) {
+ log.debug("No extensions found for extension point '{}'", type.getName());
+ } else {
+ log.debug("Found {} extensions for extension point '{}'", result.size(), type.getName());
+ }
+
+ // sort by "ordinal" property
+ Collections.sort(result);
+
+ return result;
+ }
+ @Override
+ @SuppressWarnings("unchecked")
+ public <T> List<ExtensionWrapper<T>> find(Class<T> type, String pluginId) {
+ log.debug("Finding extensions of extension point '{}' for plugin '{}'", type.getName(), pluginId);
List<ExtensionWrapper<T>> result = new ArrayList<>();
- for (Map.Entry<String, Set<String>> entry : entries.entrySet()) {
- if (entry.getValue().isEmpty()) {
- continue;
+
+ Set<String> classNames = findClassNames(pluginId);
+ if (classNames.isEmpty()) {
+ return result;
+ }
+
+ if (pluginId != null) {
+ PluginWrapper pluginWrapper = pluginManager.getPlugin(pluginId);
+ if (PluginState.STARTED != pluginWrapper.getPluginState()) {
+ return result;
}
- String pluginId = entry.getKey();
+ log.trace("Checking extensions from plugin '{}'", pluginId);
+ } else {
+ log.trace("Checking extensions from classpath");
+ }
+
+ ClassLoader classLoader = (pluginId != null) ? pluginManager.getPluginClassLoader(pluginId) : getClass().getClassLoader();
- if (pluginId != null) {
- PluginWrapper pluginWrapper = pluginManager.getPlugin(pluginId);
- if (PluginState.STARTED != pluginWrapper.getPluginState()) {
- continue;
+ for (String className : classNames) {
+ try {
+ log.debug("Loading class '{}' using class loader '{}'", className, classLoader);
+ Class<?> extensionClass = classLoader.loadClass(className);
+
+ log.debug("Checking extension type '{}'", className);
+ if (type.isAssignableFrom(extensionClass)) {
+ ExtensionWrapper extensionWrapper = createExtensionWrapper(extensionClass);
+ result.add(extensionWrapper);
+ log.debug("Added extension '{}' with ordinal {}", className, extensionWrapper.getOrdinal());
+ } else {
+ log.trace("'{}' is not an extension for extension point '{}'", className, type.getName());
}
+ } catch (ClassNotFoundException e) {
+ log.error(e.getMessage(), e);
+ }
+ }
+
+ if (result.isEmpty()) {
+ log.debug("No extensions found for extension point '{}'", type.getName());
+ } else {
+ log.debug("Found {} extensions for extension point '{}'", result.size(), type.getName());
+ }
- log.trace("Checking extensions from plugin '{}'", pluginId);
- } else {
- log.trace("Checking extensions from classpath");
+ // sort by "ordinal" property
+ Collections.sort(result);
+
+ return result;
+ }
+
+ @Override
+ public List<ExtensionWrapper> find(String pluginId) {
+ log.debug("Finding extensions from plugin '{}'", pluginId);
+ List<ExtensionWrapper> result = new ArrayList<>();
+
+ Set<String> classNames = findClassNames(pluginId);
+ if (classNames.isEmpty()) {
+ return result;
+ }
+
+ if (pluginId != null) {
+ PluginWrapper pluginWrapper = pluginManager.getPlugin(pluginId);
+ if (PluginState.STARTED != pluginWrapper.getPluginState()) {
+ return result;
}
- ClassLoader classLoader = (pluginId != null) ? pluginManager.getPluginClassLoader(pluginId) : getClass().getClassLoader();
-
- for (String className : entry.getValue()) {
- try {
- log.debug("Loading class '{}' using class loader '{}'", className, classLoader);
- Class<?> extensionClass = classLoader.loadClass(className);
-
- log.debug("Checking extension type '{}'", className);
- if (type.isAssignableFrom(extensionClass)) {
- ExtensionDescriptor descriptor = new ExtensionDescriptor();
- int ordinal = 0;
- if (extensionClass.isAnnotationPresent(Extension.class)) {
- ordinal = extensionClass.getAnnotation(Extension.class).ordinal();
- }
- descriptor.setOrdinal(ordinal);
- descriptor.setExtensionClass(extensionClass);
-
- ExtensionWrapper extensionWrapper = new ExtensionWrapper<>(descriptor);
- extensionWrapper.setExtensionFactory(pluginManager.getExtensionFactory());
- result.add(extensionWrapper);
- log.debug("Added extension '{}' with ordinal {}", className, ordinal);
- } else {
- log.trace("'{}' is not an extension for extension point '{}'", className, type.getName());
- }
- } catch (ClassNotFoundException e) {
- log.error(e.getMessage(), e);
- }
+ log.trace("Checking extensions from plugin '{}'", pluginId);
+ } else {
+ log.trace("Checking extensions from classpath");
+ }
+
+ ClassLoader classLoader = (pluginId != null) ? pluginManager.getPluginClassLoader(pluginId) : getClass().getClassLoader();
+
+ for (String className : classNames) {
+ try {
+ log.debug("Loading class '{}' using class loader '{}'", className, classLoader);
+ Class<?> extensionClass = classLoader.loadClass(className);
+
+ ExtensionWrapper extensionWrapper = createExtensionWrapper(extensionClass);
+ result.add(extensionWrapper);
+ log.debug("Added extension '{}' with ordinal {}", className, extensionWrapper.getOrdinal());
+ } catch (ClassNotFoundException e) {
+ log.error(e.getMessage(), e);
}
}
- if (entries.isEmpty()) {
- log.debug("No extensions found for extension point '{}'", type.getName());
+ if (result.isEmpty()) {
+ log.debug("No extensions found for plugin '{}'", pluginId);
} else {
- log.debug("Found {} extensions for extension point '{}'", result.size(), type.getName());
+ log.debug("Found {} extensions for plugin '{}'", result.size(), pluginId);
}
// sort by "ordinal" property
Collections.sort(result);
- return result;
- }
+ return result;
+ }
@Override
public Set<String> findClassNames(String pluginId) {
@@ -122,6 +186,17 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin
entries = null;
}
+ protected void logExtensions(Set<String> extensions) {
+ if (extensions.isEmpty()) {
+ log.debug("No extensions found");
+ } else {
+ log.debug("Found possible {} extensions:", extensions.size());
+ for (String extension : extensions) {
+ log.debug(" " + extension);
+ }
+ }
+ }
+
private Map<String, Set<String>> readStorages() {
Map<String, Set<String>> result = new LinkedHashMap<>();
@@ -139,4 +214,19 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin
return entries;
}
+ private ExtensionWrapper createExtensionWrapper(Class<?> extensionClass) {
+ ExtensionDescriptor descriptor = new ExtensionDescriptor();
+ int ordinal = 0;
+ if (extensionClass.isAnnotationPresent(Extension.class)) {
+ ordinal = extensionClass.getAnnotation(Extension.class).ordinal();
+ }
+ descriptor.setOrdinal(ordinal);
+ descriptor.setExtensionClass(extensionClass);
+
+ ExtensionWrapper extensionWrapper = new ExtensionWrapper<>(descriptor);
+ extensionWrapper.setExtensionFactory(pluginManager.getExtensionFactory());
+
+ return extensionWrapper;
+ }
+
}
diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java
index 66fb952..952f6d4 100644
--- a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java
+++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java
@@ -51,6 +51,27 @@ public class DefaultExtensionFinder implements ExtensionFinder, PluginStateListe
}
@Override
+ public <T> List<ExtensionWrapper<T>> find(Class<T> type, String pluginId) {
+ List<ExtensionWrapper<T>> extensions = new ArrayList<>();
+ for (ExtensionFinder finder : finders) {
+ extensions.addAll(finder.find(type, pluginId));
+ }
+
+ return extensions;
+ }
+
+ @Override
+ public List<ExtensionWrapper> find(String pluginId) {
+ List<ExtensionWrapper> extensions = new ArrayList<>();
+ for (ExtensionFinder finder : finders) {
+ extensions.addAll(finder.find(pluginId));
+ }
+
+ return extensions;
+ }
+
+
+ @Override
public Set<String> findClassNames(String pluginId) {
Set<String> classNames = new HashSet<>();
for (ExtensionFinder finder : finders) {
diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
index af85f1f..60b09e8 100644
--- a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
+++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
@@ -562,6 +562,29 @@ public class DefaultPluginManager implements PluginManager {
}
@Override
+ public <T> List<T> getExtensions(Class<T> type, String pluginId) {
+ List<ExtensionWrapper<T>> extensionsWrapper = extensionFinder.find(type, pluginId);
+ List<T> extensions = new ArrayList<>(extensionsWrapper.size());
+ for (ExtensionWrapper<T> extensionWrapper : extensionsWrapper) {
+ extensions.add(extensionWrapper.getExtension());
+ }
+
+ return extensions;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public List getExtensions(String pluginId) {
+ List<ExtensionWrapper> extensionsWrapper = extensionFinder.find(pluginId);
+ List extensions = new ArrayList<>(extensionsWrapper.size());
+ for (ExtensionWrapper extensionWrapper : extensionsWrapper) {
+ extensions.add(extensionWrapper.getExtension());
+ }
+
+ return extensions;
+ }
+
+ @Override
public Set<String> getExtensionClassNames(String pluginId) {
return extensionFinder.findClassNames(pluginId);
}
diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/ExtensionFinder.java b/pf4j/src/main/java/ro/fortsoft/pf4j/ExtensionFinder.java
index 3dc5149..d00e838 100644
--- a/pf4j/src/main/java/ro/fortsoft/pf4j/ExtensionFinder.java
+++ b/pf4j/src/main/java/ro/fortsoft/pf4j/ExtensionFinder.java
@@ -29,6 +29,16 @@ public interface ExtensionFinder {
<T> List<ExtensionWrapper<T>> find(Class<T> type);
/**
+ * Retrieves a list with all extensions found for an extension point and a plugin.
+ */
+ <T> List<ExtensionWrapper<T>> find(Class<T> type, String pluginId);
+
+ /**
+ * Retrieves a list with all extensions found for a plugin
+ */
+ List<ExtensionWrapper> find(String pluginId);
+
+ /**
* Retrieves a list with all extension class names found for a plugin.
*/
Set<String> findClassNames(String pluginId);
diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/ExtensionWrapper.java b/pf4j/src/main/java/ro/fortsoft/pf4j/ExtensionWrapper.java
index 8ee99c7..f150c0d 100644
--- a/pf4j/src/main/java/ro/fortsoft/pf4j/ExtensionWrapper.java
+++ b/pf4j/src/main/java/ro/fortsoft/pf4j/ExtensionWrapper.java
@@ -30,7 +30,8 @@ public class ExtensionWrapper<T> implements Comparable<ExtensionWrapper<T>> {
this.descriptor = descriptor;
}
- public T getExtension() {
+ @SuppressWarnings("unchecked")
+ public T getExtension() {
if (extension == null) {
extension = (T) extensionFactory.create(descriptor.getExtensionClass());
}
diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/LegacyExtensionFinder.java b/pf4j/src/main/java/ro/fortsoft/pf4j/LegacyExtensionFinder.java
index 1e6e5db..dc47d0e 100644
--- a/pf4j/src/main/java/ro/fortsoft/pf4j/LegacyExtensionFinder.java
+++ b/pf4j/src/main/java/ro/fortsoft/pf4j/LegacyExtensionFinder.java
@@ -59,14 +59,7 @@ public class LegacyExtensionFinder extends AbstractExtensionFinder {
LegacyExtensionStorage.read(reader, bucket);
}
- if (bucket.isEmpty()) {
- log.debug("No extensions found");
- } else {
- log.debug("Found possible {} extensions:", bucket.size());
- for (String entry : bucket) {
- log.debug(" " + entry);
- }
- }
+ logExtensions(bucket);
result.put(null, bucket);
} catch (IOException e) {
@@ -97,14 +90,7 @@ public class LegacyExtensionFinder extends AbstractExtensionFinder {
log.debug("Cannot find '{}'", getExtensionsResource());
}
- if (bucket.isEmpty()) {
- log.debug("No extensions found");
- } else {
- log.debug("Found possible {} extensions:", bucket.size());
- for (String entry : bucket) {
- log.debug(" " + entry);
- }
- }
+ logExtensions(bucket);
result.put(pluginId, bucket);
} catch (IOException e) {
diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java
index d079a64..5f23e5c 100644
--- a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java
+++ b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java
@@ -134,6 +134,10 @@ public interface PluginManager {
<T> List<T> getExtensions(Class<T> type);
+ <T> List<T> getExtensions(Class<T> type, String pluginId);
+
+ List getExtensions(String pluginId);
+
Set<String> getExtensionClassNames(String pluginId);
ExtensionFactory getExtensionFactory();
diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/ServiceProviderExtensionFinder.java b/pf4j/src/main/java/ro/fortsoft/pf4j/ServiceProviderExtensionFinder.java
index 2a32c55..1794f69 100644
--- a/pf4j/src/main/java/ro/fortsoft/pf4j/ServiceProviderExtensionFinder.java
+++ b/pf4j/src/main/java/ro/fortsoft/pf4j/ServiceProviderExtensionFinder.java
@@ -70,27 +70,11 @@ public class ServiceProviderExtensionFinder extends AbstractExtensionFinder {
} else {
extensionPath = Paths.get(url.toURI());
}
- Files.walkFileTree(extensionPath, Collections.<FileVisitOption>emptySet(), 1, new SimpleFileVisitor<Path>() {
-
- @Override
- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- log.debug("Read '{}'", file);
- Reader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8);
- ServiceProviderExtensionStorage.read(reader, bucket);
- return FileVisitResult.CONTINUE;
- }
- });
+ bucket.addAll(readExtensions(extensionPath));
}
- if (bucket.isEmpty()) {
- log.debug("No extensions found");
- } else {
- log.debug("Found possible {} extensions:", bucket.size());
- for (String entry : bucket) {
- log.debug(" " + entry);
- }
- }
+ logExtensions(bucket);
result.put(null, bucket);
} catch (IOException | URISyntaxException e) {
@@ -121,29 +105,13 @@ public class ServiceProviderExtensionFinder extends AbstractExtensionFinder {
} else {
extensionPath = Paths.get(url.toURI());
}
- Files.walkFileTree(extensionPath, Collections.<FileVisitOption>emptySet(), 1, new SimpleFileVisitor<Path>() {
-
- @Override
- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- log.debug("Read '{}'", file);
- Reader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8);
- ServiceProviderExtensionStorage.read(reader, bucket);
- return FileVisitResult.CONTINUE;
- }
- });
+ bucket.addAll(readExtensions(extensionPath));
} else {
log.debug("Cannot find '{}'", getExtensionsResource());
}
- if (bucket.isEmpty()) {
- log.debug("No extensions found");
- } else {
- log.debug("Found possible {} extensions:", bucket.size());
- for (String entry : bucket) {
- log.debug(" " + entry);
- }
- }
+ logExtensions(bucket);
result.put(pluginId, bucket);
} catch (IOException | URISyntaxException e) {
@@ -158,4 +126,21 @@ public class ServiceProviderExtensionFinder extends AbstractExtensionFinder {
return ServiceProviderExtensionStorage.EXTENSIONS_RESOURCE;
}
+ private Set<String> readExtensions(Path extensionPath) throws IOException {
+ final Set<String> result = new HashSet<>();
+ Files.walkFileTree(extensionPath, Collections.<FileVisitOption>emptySet(), 1, new SimpleFileVisitor<Path>() {
+
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+ log.debug("Read '{}'", file);
+ Reader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8);
+ ServiceProviderExtensionStorage.read(reader, result);
+ return FileVisitResult.CONTINUE;
+ }
+
+ });
+
+ return result;
+ }
+
}