summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDecebal Suiu <decebal.suiu@gmail.com>2013-10-10 16:48:53 +0300
committerDecebal Suiu <decebal.suiu@gmail.com>2013-10-10 16:48:53 +0300
commitabf4672d97529d3122848c762d3269311a9f281f (patch)
tree8314cf3eee7c01a4aaae25ded9d45a287361cbc9
parent78d959eb50b4606cc65e2f08cbf24d6441bc43bc (diff)
downloadpf4j-abf4672d97529d3122848c762d3269311a9f281f.tar.gz
pf4j-abf4672d97529d3122848c762d3269311a9f281f.zip
add possibility to override extension creation in DefaultExtensionFinder
-rw-r--r--pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java48
1 files changed, 40 insertions, 8 deletions
diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java
index 5ab989b..f61195b 100644
--- a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java
+++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultExtensionFinder.java
@@ -38,10 +38,12 @@ public class DefaultExtensionFinder implements ExtensionFinder {
private static final Logger log = LoggerFactory.getLogger(DefaultExtensionFinder.class);
private ClassLoader classLoader;
+ private ExtensionFactory extensionFactory;
private volatile Set<String> entries;
public DefaultExtensionFinder(ClassLoader classLoader) {
this.classLoader = classLoader;
+ this.extensionFactory = createExtensionFactory();
}
@Override
@@ -57,21 +59,17 @@ public class DefaultExtensionFinder implements ExtensionFinder {
Class<?> extensionType = classLoader.loadClass(entry);
log.debug("Checking extension type {}", extensionType.getName());
if (type.isAssignableFrom(extensionType)) {
- Object instance = extensionType.newInstance();
+ Object instance = extensionFactory.create(extensionType);
if (instance != null) {
Extension extension = extensionType.getAnnotation(Extension.class);
- log.debug("Added extension {} with ordinal {}", extensionType, extension.ordinal());
+ log.debug("Added extension {} with ordinal {}", extensionType.getName(), extension.ordinal());
result.add(new ExtensionWrapper<T>(type.cast(instance), extension.ordinal()));
}
} else {
- log.warn("{} is not an extension for extension point {}", extensionType, type.getName());
+ log.warn("{} is not an extension for extension point {}", extensionType.getName(), type.getName());
}
} catch (ClassNotFoundException e) {
- log.error(e.getMessage(), e);
- } catch (InstantiationException e) {
- log.error(e.getMessage(), e);
- } catch (IllegalAccessException e) {
- log.error(e.getMessage(), e);
+ log.error(e.getMessage(), e);
}
}
@@ -87,6 +85,31 @@ public class DefaultExtensionFinder implements ExtensionFinder {
return result;
}
+ /**
+ * Add the possibility to override the ExtensionFactory.
+ * The default implementation uses Class.newInstance() method.
+ */
+ protected ExtensionFactory createExtensionFactory() {
+ return new ExtensionFactory() {
+
+ @Override
+ public Object create(Class<?> extensionType) {
+ log.debug("Create instance for extension {}", extensionType.getName());
+
+ try {
+ return extensionType.newInstance();
+ } catch (InstantiationException e) {
+ log.error(e.getMessage(), e);
+ } catch (IllegalAccessException e) {
+ log.error(e.getMessage(), e);
+ }
+
+ return null;
+ }
+
+ };
+ }
+
private Set<String> readIndexFiles() {
log.debug("Reading extensions index files");
Set<String> entries = new HashSet<String>();
@@ -110,4 +133,13 @@ public class DefaultExtensionFinder implements ExtensionFinder {
return entries;
}
+ /**
+ * Creates an extension instance.
+ */
+ public static interface ExtensionFactory {
+
+ public Object create(Class<?> extensionType);
+
+ }
+
}