From 65e8fe76c8da4049f58c263b651e77c80b375067 Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Sat, 20 Apr 2019 21:30:34 +0300 Subject: [PATCH] Improve #292 --- .../java/org/pf4j/AbstractPluginManager.java | 19 ++++++++++--- .../org/pf4j/DefaultExtensionFactory.java | 10 +++---- .../main/java/org/pf4j/ExtensionFactory.java | 2 +- .../main/java/org/pf4j/ExtensionWrapper.java | 2 +- .../org/pf4j/SingletonExtensionFactory.java | 7 ++--- .../org/pf4j/DefaultExtensionFactoryTest.java | 27 ++++++++++++------- .../pf4j/SingletonExtensionFactoryTest.java | 4 +-- 7 files changed, 45 insertions(+), 26 deletions(-) diff --git a/pf4j/src/main/java/org/pf4j/AbstractPluginManager.java b/pf4j/src/main/java/org/pf4j/AbstractPluginManager.java index 4fa1d38..a7333ca 100644 --- a/pf4j/src/main/java/org/pf4j/AbstractPluginManager.java +++ b/pf4j/src/main/java/org/pf4j/AbstractPluginManager.java @@ -521,6 +521,7 @@ public abstract class AbstractPluginManager implements PluginManager { Class c = extensionWrapper.getDescriptor().extensionClass; extensionClasses.add(c); } + return extensionClasses; } @@ -555,7 +556,11 @@ public abstract class AbstractPluginManager implements PluginManager { List> extensionsWrapper = extensionFinder.find(type); List extensions = new ArrayList<>(extensionsWrapper.size()); for (ExtensionWrapper extensionWrapper : extensionsWrapper) { - extensions.add(extensionWrapper.getExtension()); + try { + extensions.add(extensionWrapper.getExtension()); + } catch (PluginException e) { + log.error("Cannot retrieve extension", e); + } } return extensions; @@ -566,7 +571,11 @@ public abstract class AbstractPluginManager implements PluginManager { List> extensionsWrapper = extensionFinder.find(type, pluginId); List extensions = new ArrayList<>(extensionsWrapper.size()); for (ExtensionWrapper extensionWrapper : extensionsWrapper) { - extensions.add(extensionWrapper.getExtension()); + try { + extensions.add(extensionWrapper.getExtension()); + } catch (PluginException e) { + log.error("Cannot retrieve extension", e); + } } return extensions; @@ -578,7 +587,11 @@ public abstract class AbstractPluginManager implements PluginManager { List extensionsWrapper = extensionFinder.find(pluginId); List extensions = new ArrayList<>(extensionsWrapper.size()); for (ExtensionWrapper extensionWrapper : extensionsWrapper) { - extensions.add(extensionWrapper.getExtension()); + try { + extensions.add(extensionWrapper.getExtension()); + } catch (PluginException e) { + log.error("Cannot retrieve extension", e); + } } return extensions; diff --git a/pf4j/src/main/java/org/pf4j/DefaultExtensionFactory.java b/pf4j/src/main/java/org/pf4j/DefaultExtensionFactory.java index bd67032..fbcba27 100644 --- a/pf4j/src/main/java/org/pf4j/DefaultExtensionFactory.java +++ b/pf4j/src/main/java/org/pf4j/DefaultExtensionFactory.java @@ -29,20 +29,16 @@ public class DefaultExtensionFactory implements ExtensionFactory { private static final Logger log = LoggerFactory.getLogger(DefaultExtensionFactory.class); /** - * Creates an extension instance. If an error occurs than that error is logged and the method returns {@code null}. - * @param extensionClass - * @return + * Creates an extension instance. */ @Override - public Object create(Class extensionClass) { + public T create(Class extensionClass) throws PluginException { log.debug("Create instance for extension '{}'", extensionClass.getName()); try { return extensionClass.newInstance(); } catch (Exception e) { - log.error(e.getMessage(), e); + throw new PluginException(e); } - - return null; } } diff --git a/pf4j/src/main/java/org/pf4j/ExtensionFactory.java b/pf4j/src/main/java/org/pf4j/ExtensionFactory.java index 177d489..42b0de8 100644 --- a/pf4j/src/main/java/org/pf4j/ExtensionFactory.java +++ b/pf4j/src/main/java/org/pf4j/ExtensionFactory.java @@ -20,6 +20,6 @@ package org.pf4j; */ public interface ExtensionFactory { - Object create(Class extensionClass); + T create(Class extensionClass) throws PluginException; } diff --git a/pf4j/src/main/java/org/pf4j/ExtensionWrapper.java b/pf4j/src/main/java/org/pf4j/ExtensionWrapper.java index 3899d5f..85c5b91 100644 --- a/pf4j/src/main/java/org/pf4j/ExtensionWrapper.java +++ b/pf4j/src/main/java/org/pf4j/ExtensionWrapper.java @@ -32,7 +32,7 @@ public class ExtensionWrapper implements Comparable> { } @SuppressWarnings("unchecked") - public T getExtension() { + public T getExtension() throws PluginException { if (extension == null) { extension = (T) extensionFactory.create(descriptor.extensionClass); } diff --git a/pf4j/src/main/java/org/pf4j/SingletonExtensionFactory.java b/pf4j/src/main/java/org/pf4j/SingletonExtensionFactory.java index d339921..3da97d0 100644 --- a/pf4j/src/main/java/org/pf4j/SingletonExtensionFactory.java +++ b/pf4j/src/main/java/org/pf4j/SingletonExtensionFactory.java @@ -39,13 +39,14 @@ public class SingletonExtensionFactory extends DefaultExtensionFactory { } @Override - public Object create(Class extensionClass) { + @SuppressWarnings("unchecked") + public T create(Class extensionClass) throws PluginException { String extensionClassName = extensionClass.getName(); if (cache.containsKey(extensionClassName)) { - return cache.get(extensionClassName); + return (T) cache.get(extensionClassName); } - Object extension = super.create(extensionClass); + T extension = super.create(extensionClass); if (extensionClassNames.isEmpty() || extensionClassNames.contains(extensionClassName)) { cache.put(extensionClassName, extension); } diff --git a/pf4j/src/test/java/org/pf4j/DefaultExtensionFactoryTest.java b/pf4j/src/test/java/org/pf4j/DefaultExtensionFactoryTest.java index 13327cc..207c178 100644 --- a/pf4j/src/test/java/org/pf4j/DefaultExtensionFactoryTest.java +++ b/pf4j/src/test/java/org/pf4j/DefaultExtensionFactoryTest.java @@ -15,27 +15,38 @@ */ package org.pf4j; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.pf4j.plugin.FailTestExtension; import org.pf4j.plugin.TestExtension; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; /** - * * @author Mario Franco */ public class DefaultExtensionFactoryTest { + private ExtensionFactory extensionFactory; + + @BeforeEach + public void setUp() { + extensionFactory = new DefaultExtensionFactory(); + } + + @AfterEach + public void tearDown() { + extensionFactory = null; + } + /** * Test of create method, of class DefaultExtensionFactory. */ @Test - public void testCreate() { - DefaultExtensionFactory instance = new DefaultExtensionFactory(); - Object result = instance.create(TestExtension.class); - assertNotNull(result); + public void testCreate() throws PluginException { + assertNotNull(extensionFactory.create(TestExtension.class)); } /** @@ -43,9 +54,7 @@ public class DefaultExtensionFactoryTest { */ @Test public void testCreateFailConstructor() { - DefaultExtensionFactory instance = new DefaultExtensionFactory(); - Object result = instance.create(FailTestExtension.class); - assertNull(result); + assertThrows(PluginException.class, () -> extensionFactory.create(FailTestExtension.class)); } } diff --git a/pf4j/src/test/java/org/pf4j/SingletonExtensionFactoryTest.java b/pf4j/src/test/java/org/pf4j/SingletonExtensionFactoryTest.java index 80e7ab0..97e98cf 100644 --- a/pf4j/src/test/java/org/pf4j/SingletonExtensionFactoryTest.java +++ b/pf4j/src/test/java/org/pf4j/SingletonExtensionFactoryTest.java @@ -28,7 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertSame; public class SingletonExtensionFactoryTest { @Test - public void create() { + public void create() throws PluginException { ExtensionFactory extensionFactory = new SingletonExtensionFactory(); Object extensionOne = extensionFactory.create(TestExtension.class); Object extensionTwo = extensionFactory.create(TestExtension.class); @@ -36,7 +36,7 @@ public class SingletonExtensionFactoryTest { } @Test - public void createNewEachTime() { + public void createNewEachTime() throws PluginException { ExtensionFactory extensionFactory = new SingletonExtensionFactory(FailTestExtension.class.getName()); Object extensionOne = extensionFactory.create(TestExtension.class); Object extensionTwo = extensionFactory.create(TestExtension.class);