This tutorial describes an absolute minimal use-case for JPF and the JPF Code Generator tool. The example system is a stripped-down version of the JPF Tutorial example. The system consists of three plugins: One core plug-in that displays a tabbed pane and offers an extension-point so that plug-ins can contribute panels on this pane and two plug-ins that provide such panels.
The system uses the default file structure for a JPF based application:
. |-> src Main application source code \-> plugins Folder for plug-ins |-> core The main plug-in that provides extension-points for others to extend. |-> plugin1 An example plug-in that provides one extension \-> plugin2 Another example plug-in that provides one extension
The most important plugin.xml is the one of the core plug-in,
since it offers the extension point for the other plug-ins to
extend. Since this is a basic tutorial we will only a very basic
extension point that requires only two parameters to be defined by
an extension. First an extending plug-in should tell us the name of
the panel it wants to contribute and then it should provide us with
a type that extends java.swing.JPanel
, so that we can
create an instance of this type to add to the tabbed pane of the
main application we want to create.
The resulting plugin.xml for the com.example.core plug-in looks like this:
<?xml version="1.0" ?> <!DOCTYPE plugin PUBLIC "-//JPF//Java Plug-in Manifest 1.0" "http://jpf.sourceforge.net/plugin_1_0.dtd"> <plugin id="com.example.core" version="1.0.0" class="com.example.core.CorePlugin"> <!-- Since the application code uses the core plug-in we do not provide a runtime reference to the class files. --> <!-- In this example, the application provides a single extension point for plug-ins to extend: --> <extension-point id="Panel"> <!-- String parameter with custom-data are used to tell the code-generator that this extension-point parameter is really used for getting objects from plug-ins that implement this extension-point. The given class name in custom-data will be used as the return-type of the function "getPanel()" that is generated for this parameter. --> <parameter-def type="string" id="panel" custom-data="javax.swing.JPanel" /> <parameter-def type="string" id="name" /> </extension-point> </plugin>
The runtime attribute is needed, so that JPF can manage the classpath of our application for us. For more details on this have a look at the JPF tutorial.
Once we have defined this extension-point, we can now create plug-ins that extend it. In our case we need to do two things for this...
...we need to create the actual panel class that we want to contribute to the system (for simplicity this is really a very minimal implementation)...
package com.example.plugin1; import java.awt.Label; import javax.swing.JPanel; public class Plugin1Panel extends JPanel { private static final long serialVersionUID = -6420919219685347035L; public Plugin1Panel(){ this.add(new Label("I am Panel 1!")); } }
...and then we need to create the plugin.xml that will contain an extension:
<?xml version="1.0" ?> <!DOCTYPE plugin PUBLIC "-//JPF//Java Plug-in Manifest 1.0" "http://jpf.sourceforge.net/plugin_1_0.dtd"> <plugin id="com.example.plugin1" version="1.0.0" class="com.example.plugin1.Plugin1"> <requires> <import plugin-id="com.example.core"/> </requires> <runtime> <library type="code" path="build/" id="src"/> </runtime> <extension id="Plugin1Panel" plugin-id="com.example.core" point-id="Panel"> <parameter id="panel" value="com.example.plugin1.Plugin1Panel" /> <parameter id="name" value="Plugin No. 1 Panel" /> </extension> </plugin>
The plugin.xml for the second plugin is similar.
Using these plugin.xml files, we can now use the JPF Code Generator to create code that will make using the plug-in system very easy.
Go to directory...
tutorials/basic/
...and run the generate
ant
-task
there:
ant generate
This will create two classes for each plug-in using the class-attribute in the plugin.xml. We will discuss this only for the interesting case of the CorePlugin (the generated classes Plugin1 and Plugin2 classes are empty at the moment since they don't provide extension points)
The class that contains the important code parts is
com.example.core.generated._CorePlugin.java
. As you
can see below it contains a method for retrieving all extensions
for the extension-point. The extensions then contains methods for
accessing the attributes panel
and name
.
Since we defined panel
to be a string-parameter with a
type as custom-data, the code generator has created a method that
returns a singleton instance of the type given in the extending
plug-in (in the case of com.example.Plugin1
this will
be an instance of com.example.plugin1.Plugin1Panel
).
The name method will return just the string value defined in the
plugin.xml of the extending plug-in (for the extension in Plugin1
this will be "Plugin No. 1 Panel").
package com.example.core.generated; ...imports... /** * Do not modify this file, as it was auto generated and will be overwritten! * User modifications should go in com.example.core.CorePlugin. */ public abstract class _CorePlugin extends Plugin { public static String getId() { return "com.example.core"; } static Log log = LogFactory.getLog(_CorePlugin.class); public List<PanelExtension> getPanelExtensions() { ExtensionPoint extPoint = getManager().getRegistry().getExtensionPoint( getId(), "Panel"); List<PanelExtension> result = new ArrayList<PanelExtension>(); for (Extension ext : extPoint.getConnectedExtensions()) { try { result.add(new PanelExtension(getManager().getPlugin( ext.getDeclaringPluginDescriptor().getId()), ext)); } catch (PluginLifecycleException e) { log.error("Failed to activate plug-in" + ext.getDeclaringPluginDescriptor().getId(), e); } } return result; } public static class PanelExtension extends RuntimeExtension { public PanelExtension(Plugin declaringPlugin, Extension wrapped) { super(declaringPlugin, wrapped); } /** * @return A singleton instance of the class parameter or null if the class could not be found! */ public javax.swing.JPanel getPanel() { return (javax.swing.JPanel) getClassParameter("panel"); } public String getName() { return getStringParameter("name"); } } }
Since this class will be complete overwritten if you re-run the
code generator you should not put any custom code into this class.
Rather the code generator has created another class that you can
customize, which will not get re-generated if it already exists.
This class is the com.example.core.CorePlugin.java
and
looks as follows:
package com.example.core; import com.example.core.generated._CorePlugin; import org.java.plugin.PluginLifecycleException; import org.java.plugin.PluginManager; public class CorePlugin extends _CorePlugin { public void doStart(){ // TODO: Will be called when plug-in is started. } public void doStop(){ // TODO: Will be called when plug-in is stopped. } /** * Retrieve the Plug-in instance from the given manager. * * @param manager * The manager from which to retrieve the plug-in instance * * @return The requested plug-in or null if not found. */ public static CorePlugin getInstance(PluginManager manager) { try { return (CorePlugin) manager .getPlugin(CorePlugin.getId()); } catch (PluginLifecycleException e) { return null; } catch (IllegalArgumentException e) { return null; } } }
In the main application source folder we can now create the
application itself com.example.Main
. The application
will initialize plug-in system, create the main window with a
tabbed pane on it and use the plug-in system to fill this tabbed
pane.
First we need to initialize the plug-in system:
// Create Plugin Manager manager = ObjectFactory.newInstance().createManager(); // Find plugins and add to registry DefaultPluginsCollector collector = new DefaultPluginsCollector(); ExtendedProperties ep = new ExtendedProperties(); ep.setProperty("org.java.plugin.boot.pluginsRepositories", "./plugins"); try { collector.configure(ep); manager.publishPlugins(collector.collectPluginLocations().toArray( new PluginLocation[] {})); } catch (Exception e) { e.printStackTrace(); System.exit(0); }
Then we will create a JFrame to show the tabbed pane:
JFrame frame = new JFrame("JPF Code Generator Basic Tutorial"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); frame.setLocation(300, 200); JTabbedPane tabbedPane = new JTabbedPane(); frame.add(tabbedPane);
Now we want to populate the tabbed pane with the Panels that the plug-ins have contributed
// Use the plugin system to find panels to add to the tabbed pane CorePlugin corePlugin = CorePlugin.getInstance(manager); // corePlugin might be null here (for instance if the plug-in is missing) if (corePlugin != null){ // Now we can get all extensions for the Panel extension point for (PanelExtension extension : corePlugin.getPanelExtensions()){ // The PanelExtension class then allows easy access to the parameters defined // for this extension-point. JPanel panel = extension.getPanel(); String name = extension.getName(); // panel might be null if an error occurred while creating the object if (panel == null){ System.out.println("Error loading panel from extension " + extension.getId()); continue; } tabbedPane.addTab(name, panel); } }
(This paragraph contains some advanced topics, you can safely
skip it:) You notice that this style of using a plug-in framework
is a little different as the one you get when using the JPF-boot
library. In contrast to JPF-boot, where everything is a plug-in and
the application is started by starting a dedicated application
plug-in, this tutorial here uses the plug-in system from a
stand-alone application. I feel that this is a more natural
approach, when first getting into contact with plug-in
architectures. If you want to achieve a pure plug-in style of
programming put both the creation of the JFrame and the hooking up
of the panels into the doStart method of the
com.example.core.CorePlugin
. Another point that is
important in this discussion is how to set the classpath. If the
core plug-in is used by the main application directly, it also
needs to be available on the classpath during building and running
the application. This means that the core plugin does not provide a
runtime plug-in attribute but instead needs to be on the classpath
of the application itself.
Finally we set this frame to visible:
frame.setVisible(true);
If you put all this code in a class body into the method
start()
your sample application is done.
package com.example; import javax.swing.*; import org.java.plugin.*; import org.java.plugin.util.ExtendedProperties; import com.example.core.CorePlugin; import com.example.core.generated._CorePlugin.PanelExtension; public class Main { public static void main(final String[] args) { // Move to non-static and swing-thread. SwingUtilities.invokeLater(new Runnable(){ public void run() { new Main().start(args); } }); } PluginManager manager; public void start(String[] args) { !!Code from above goes here!! } }
To run the example application go to the folder...
tutorials/basic/
...and run the ant
-task run
:
ant run
In this tutorial you have learned about the basics of using the Java Plug-in Framework in conjunction with the code generator to easily enable a plug-in system for your java application.
Key points to remember are:
More information on how to use the code generator is provided in its documentation (for instance if you don't want to use the class attribute to define which class gets generated) and further information about JPF in general is best found on the JPF Homepage on Sourceforge.
Copyright (C) Christopher Oezbek (2007) - oezi[at]oezi.de