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;
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;

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);
/**
* 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;
}
}

2
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;
}

2
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);
}

7
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);
}

27
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));
}
}

4
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);

Loading…
Cancel
Save