diff options
author | Decebal Suiu <decebal.suiu@gmail.com> | 2019-04-20 21:30:34 +0300 |
---|---|---|
committer | Decebal Suiu <decebal.suiu@gmail.com> | 2019-04-20 21:30:34 +0300 |
commit | 65e8fe76c8da4049f58c263b651e77c80b375067 (patch) | |
tree | 9728f1a8bc3ca69f96209d254e087a4fb6901ffe /pf4j | |
parent | 8a2674c5392de62a97e4d0b33e4dd94dbaae407d (diff) | |
download | pf4j-65e8fe76c8da4049f58c263b651e77c80b375067.tar.gz pf4j-65e8fe76c8da4049f58c263b651e77c80b375067.zip |
Improve #292
Diffstat (limited to 'pf4j')
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<ExtensionWrapper<T>> extensionsWrapper = extensionFinder.find(type); List<T> extensions = new ArrayList<>(extensionsWrapper.size()); for (ExtensionWrapper<T> 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<ExtensionWrapper<T>> extensionsWrapper = extensionFinder.find(type, pluginId); List<T> extensions = new ArrayList<>(extensionsWrapper.size()); for (ExtensionWrapper<T> 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<ExtensionWrapper> 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> T create(Class<T> 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> T create(Class<T> 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<T> implements Comparable<ExtensionWrapper<T>> { } @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> T create(Class<T> 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); |