Understanding PF4J extensions

706 views
Skip to first unread message

t.lher...@gmail.com

unread,
Feb 25, 2016, 4:42:09 PM2/25/16
to pf4j
Im exploring the possible runtime plugin frameworks and currently this one seems to be really well assembled however Im having a few problems getting it up and running as expected. In my core host application I have exposed an interface that I would like to make available to the plugins:

public interface DispatchServices extends ExtensionPoint {
void printMsg();
}

I have then created a dummy plugin that implements that:

public class TestPlugin extends Plugin {

public TestPlugin(PluginWrapper wrapper) {
super(wrapper);
}

@Extension
public static class TestDispatcher implements DispatchServices {

@Overrides
public void printMsg() {
System.out.println("Test dispatcher called");
}
}
}

I then build the extension into a zip file using maven. That all seems to be fine(ish), the only notable problem is that my extensions.idx file is empty. I have enabled annotations inside IntelliJ but that is not the end of the world as I can manually add the package and class to the extensions.idx. So my extensions.idx file now contains:

com.example.plugin.TestDispatcher

I then basically have copied and pasted the demo code into a class in my core host application:

PluginManager pluginManager = new DefaultPluginManager(extensionDirectory);
pluginManager.loadPlugins();
pluginManager.startPlugins();

List<DispatchServices> greetings = pluginManager.getExtensions(DispatchServices.class);
System.out.println(String.format("Found %d extensions for extension point '%s'", greetings.size(), DispatchServices.class.getName()));
for (DispatchServices greeting : greetings) {
System.out.println(">>> " + greeting.toString());
}

// print extensions from classpath (non plugin)
System.out.println("Extensions added by classpath:");
Set<String> extensionClassNames = pluginManager.getExtensionClassNames(null);
for (String extension : extensionClassNames) {
System.out.println(" " + extension);
}

// print extensions for each started plugin
List<PluginWrapper> startedPlugins = pluginManager.getStartedPlugins();
for (PluginWrapper plugin : startedPlugins) {
String pluginId = plugin.getDescriptor().getPluginId();
System.out.println(String.format("Extensions added by plugin '%s':", pluginId));
extensionClassNames = pluginManager.getExtensionClassNames(pluginId);
for (String extension : extensionClassNames) {
System.out.println(" " + extension);
}
}

What I get printed to the console is:

Found 0 extensions for extension point 'com.example.api.dispatch.DispatchServices'
Extensions added by classpath:
org.restlet.ext.jackson.JacksonConverter
Extensions added by plugin 'test-plugin':
com.example.plugin.TestDispatcher

So the plugin is registered, started and the extension seems to be added by the plugin. Now what I don't understand is why I have no 0 extensions for 'com.example.api.dispatch.DispatchServices'? TestDispatcher implements DispatchServices so logically i would have thought that I should have 1 extension declared. I cant possibly look for it under the implementation name because that would defy the point of runtime extensions.

I would really appreciate any help in either my understanding or implementation.

Thanks in advance for any help.

t.lher...@gmail.com

unread,
Feb 25, 2016, 5:17:44 PM2/25/16
to pf4j, t.lher...@gmail.com
Turns out it doesn't like like the @Extension declare as an inner static class. Declaring it as a normal public class fixes the issue.

I'm loving this framework. Great project.

Decebal Suiu

unread,
Feb 26, 2016, 3:46:01 AM2/26/16
to pf4j, t.lher...@gmail.com
You must use "$" for inner class, so your extensions.idx file must contains com.example.plugin.TestPlugin$TestDispatcher

t.lher...@gmail.com

unread,
Feb 29, 2016, 10:33:27 AM2/29/16
to pf4j, t.lher...@gmail.com

Good point. Note sure why I didn't try that!

Mukarram Tailor

unread,
Jun 18, 2016, 2:09:50 AM6/18/16
to pf4j, t.lher...@gmail.com
Hi, 

I am facing a similar problem, even when I have defined extension as a separate public class. 
META-INF/extension.idx has correct path even then it is unable to detect the extension. Do you have any other insights that could lead me to the root of the problem? 

Thanks and Regards
Mukarram 
Reply all
Reply to author
Forward
0 new messages