Browse Source

Improve #292

pull/320/head
Decebal Suiu 6 years ago
parent
commit
65e8fe76c8
  1. 19
      pf4j/src/main/java/org/pf4j/AbstractPluginManager.java
  2. 10
      pf4j/src/main/java/org/pf4j/DefaultExtensionFactory.java
  3. 2
      pf4j/src/main/java/org/pf4j/ExtensionFactory.java
  4. 2
      pf4j/src/main/java/org/pf4j/ExtensionWrapper.java
  5. 7
      pf4j/src/main/java/org/pf4j/SingletonExtensionFactory.java
  6. 27
      pf4j/src/test/java/org/pf4j/DefaultExtensionFactoryTest.java
  7. 4
      pf4j/src/test/java/org/pf4j/SingletonExtensionFactoryTest.java

19
pf4j/src/main/java/org/pf4j/AbstractPluginManager.java

@ -521,6 +521,7 @@ public abstract class AbstractPluginManager implements PluginManager {
Class<?> c = extensionWrapper.getDescriptor().extensionClass; Class<?> c = extensionWrapper.getDescriptor().extensionClass;
extensionClasses.add(c); extensionClasses.add(c);
} }
return extensionClasses; return extensionClasses;
} }
@ -555,7 +556,11 @@ public abstract class AbstractPluginManager implements PluginManager {
List<ExtensionWrapper<T>> extensionsWrapper = extensionFinder.find(type); List<ExtensionWrapper<T>> extensionsWrapper = extensionFinder.find(type);
List<T> extensions = new ArrayList<>(extensionsWrapper.size()); List<T> extensions = new ArrayList<>(extensionsWrapper.size());
for (ExtensionWrapper<T> extensionWrapper : extensionsWrapper) { 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; return extensions;
@ -566,7 +571,11 @@ public abstract class AbstractPluginManager implements PluginManager {
List<ExtensionWrapper<T>> extensionsWrapper = extensionFinder.find(type, pluginId); List<ExtensionWrapper<T>> extensionsWrapper = extensionFinder.find(type, pluginId);
List<T> extensions = new ArrayList<>(extensionsWrapper.size()); List<T> extensions = new ArrayList<>(extensionsWrapper.size());
for (ExtensionWrapper<T> extensionWrapper : extensionsWrapper) { 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; return extensions;
@ -578,7 +587,11 @@ public abstract class AbstractPluginManager implements PluginManager {
List<ExtensionWrapper> extensionsWrapper = extensionFinder.find(pluginId); List<ExtensionWrapper> extensionsWrapper = extensionFinder.find(pluginId);
List extensions = new ArrayList<>(extensionsWrapper.size()); List extensions = new ArrayList<>(extensionsWrapper.size());
for (ExtensionWrapper extensionWrapper : extensionsWrapper) { for (ExtensionWrapper extensionWrapper : extensionsWrapper) {
extensions.add(extensionWrapper.getExtension()); try {
extensions.add(extensionWrapper.getExtension());
} catch (PluginException e) {
log.error("Cannot retrieve extension", e);
}
} }
return extensions; return extensions;

10
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); 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}. * Creates an extension instance.
* @param extensionClass
* @return
*/ */
@Override @Override
public Object create(Class<?> extensionClass) { public <T> T create(Class<T> extensionClass) throws PluginException {
log.debug("Create instance for extension '{}'", extensionClass.getName()); log.debug("Create instance for extension '{}'", extensionClass.getName());
try { try {
return extensionClass.newInstance(); return extensionClass.newInstance();
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); throw new PluginException(e);
} }
return null;
} }
} }

2
pf4j/src/main/java/org/pf4j/ExtensionFactory.java

@ -20,6 +20,6 @@ package org.pf4j;
*/ */
public interface ExtensionFactory { public interface ExtensionFactory {
Object create(Class<?> extensionClass); <T> T create(Class<T> extensionClass) throws PluginException;
} }

2
pf4j/src/main/java/org/pf4j/ExtensionWrapper.java

@ -32,7 +32,7 @@ public class ExtensionWrapper<T> implements Comparable<ExtensionWrapper<T>> {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public T getExtension() { public T getExtension() throws PluginException {
if (extension == null) { if (extension == null) {
extension = (T) extensionFactory.create(descriptor.extensionClass); extension = (T) extensionFactory.create(descriptor.extensionClass);
} }

7
pf4j/src/main/java/org/pf4j/SingletonExtensionFactory.java

@ -39,13 +39,14 @@ public class SingletonExtensionFactory extends DefaultExtensionFactory {
} }
@Override @Override
public Object create(Class<?> extensionClass) { @SuppressWarnings("unchecked")
public <T> T create(Class<T> extensionClass) throws PluginException {
String extensionClassName = extensionClass.getName(); String extensionClassName = extensionClass.getName();
if (cache.containsKey(extensionClassName)) { 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)) { if (extensionClassNames.isEmpty() || extensionClassNames.contains(extensionClassName)) {
cache.put(extensionClassName, extension); cache.put(extensionClassName, extension);
} }

27
pf4j/src/test/java/org/pf4j/DefaultExtensionFactoryTest.java

@ -15,27 +15,38 @@
*/ */
package org.pf4j; package org.pf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pf4j.plugin.FailTestExtension; import org.pf4j.plugin.FailTestExtension;
import org.pf4j.plugin.TestExtension; import org.pf4j.plugin.TestExtension;
import static org.junit.jupiter.api.Assertions.assertNotNull; 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 * @author Mario Franco
*/ */
public class DefaultExtensionFactoryTest { 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 of create method, of class DefaultExtensionFactory.
*/ */
@Test @Test
public void testCreate() { public void testCreate() throws PluginException {
DefaultExtensionFactory instance = new DefaultExtensionFactory(); assertNotNull(extensionFactory.create(TestExtension.class));
Object result = instance.create(TestExtension.class);
assertNotNull(result);
} }
/** /**
@ -43,9 +54,7 @@ public class DefaultExtensionFactoryTest {
*/ */
@Test @Test
public void testCreateFailConstructor() { public void testCreateFailConstructor() {
DefaultExtensionFactory instance = new DefaultExtensionFactory(); assertThrows(PluginException.class, () -> extensionFactory.create(FailTestExtension.class));
Object result = instance.create(FailTestExtension.class);
assertNull(result);
} }
} }

4
pf4j/src/test/java/org/pf4j/SingletonExtensionFactoryTest.java

@ -28,7 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertSame;
public class SingletonExtensionFactoryTest { public class SingletonExtensionFactoryTest {
@Test @Test
public void create() { public void create() throws PluginException {
ExtensionFactory extensionFactory = new SingletonExtensionFactory(); ExtensionFactory extensionFactory = new SingletonExtensionFactory();
Object extensionOne = extensionFactory.create(TestExtension.class); Object extensionOne = extensionFactory.create(TestExtension.class);
Object extensionTwo = extensionFactory.create(TestExtension.class); Object extensionTwo = extensionFactory.create(TestExtension.class);
@ -36,7 +36,7 @@ public class SingletonExtensionFactoryTest {
} }
@Test @Test
public void createNewEachTime() { public void createNewEachTime() throws PluginException {
ExtensionFactory extensionFactory = new SingletonExtensionFactory(FailTestExtension.class.getName()); ExtensionFactory extensionFactory = new SingletonExtensionFactory(FailTestExtension.class.getName());
Object extensionOne = extensionFactory.create(TestExtension.class); Object extensionOne = extensionFactory.create(TestExtension.class);
Object extensionTwo = extensionFactory.create(TestExtension.class); Object extensionTwo = extensionFactory.create(TestExtension.class);

Loading…
Cancel
Save