2.1. Creating the extension provider
We will create an Eclipse RCP application which allows to be extended by other plugins via an extension point. The application will be a simple console application without UI.
Create a new project "de.vogella.extensionpoint.definition" (see Creating Eclipse RCP application for details). This example will not make contribution to the UI, select this setting in the wizard. Use the "Headless Hello RCP" as a template. The option of the activator should be active.
Select the file "MANIFEST.MF" and the tab "Extension Points". Press Add.
Use the ID "de.vogella.extensionpoint.definition.greeter" and the name "Greeter". The schema name will be generated for you.
You should now in the schema editor. Switch to the "Definition" Tab.
The definition for extension is already created. Adds attributes to the extension point. Click therefore on the "New Element" button. Give the new element the name "client".
Select the "client" element and press "New Attribute". Give the new element the name class and the type java. The interface should be "de.vogella.extensionpoint.definition.IGreeter".
Press on the hyperlink "Implements" to create this interface "IGreeter".
package de.vogella.extensionpoint.definition;
public interface IGreeter {
void greet();
}
Select the file "MANIFEST.MF" switch to the "Runtime" tab and export the package which contains IGreeter.
Go back to your extension point definition and add a choice to the extension point. This defines how often the extension "client" can be provided by contributing plugins. We will set no restrictions (unbound).
The plugin which defines an extension point is also responisble for reading and using the provided extensions. It is up to you where in your coding you do this. The following will do it directly in Application.java.
We are using ISafeRunnable. This interface allows to protect yourself from malfunction extensions. If the extension throws an exception it will be caught and the remaining extensions will still get executed.
package de.vogella.extensionpoint.definition;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
/**
* This class controls all aspects of the application's execution
*/
public class Application implements IApplication {
// This is the ID from your extension point
private static final String IGREETER_ID = "de.vogella.extensionpoint.definition.greeter";
public Object start(IApplicationContext context) throws Exception {
System.out.println("Starting");
runGreeterExtension();
return IApplication.EXIT_OK;
}
public void stop() {
//nothing to do
}
private void runGreeterExtension() {
IConfigurationElement[] config = Platform.getExtensionRegistry()
.getConfigurationElementsFor(IGREETER_ID);
try {
for (IConfigurationElement e : config) {
System.out.println("Evaluating extension");
final Object o = e.createExecutableExtension("class");
if (o instanceof IGreeter) {
ISafeRunnable runnable = new ISafeRunnable() {
@Override
public void handleException(Throwable exception) {
System.out.println("Exception in client");
}
@Override
public void run() throws Exception {
((IGreeter) o).greet();
}
};
SafeRunner.run(runnable);
}
}
} catch (CoreException ex) {
System.out.println(ex.getMessage());
}
}
}
Have a look at the generated file "schema/de.vogella.extensionpoint.definition.greeter.exsd" and the reference to this file in "plugin.xml".
Run your application. It should write "Starting" to the console. You application is finished and it ready to get extended by other plugins!
Create a new headless plugin "de.vogella.extensionpoint.contribution". This plugin should not be an RCP application. Use no template to create it.
Select "MANIFEST.MF" and the tab "Dependencies". Add "de.vogella.extensionpoint.definition" and "org.eclipse.core.runtime" as a dependency. Make sure your plugin has the singleton flag set.
Switch to the "Extensions" tab and select "Add". Select your extension point and press Finish.
Add a client to your extension point.
Create the class "de.vogella.extensionpoint.contribution.GreeterGerman" with the following coding.
package de.vogella.extensionpoint.contribution;
import de.vogella.extensionpoint.definition.IGreeter;
public class GreeterGerman implements IGreeter {
@Override
public void greet() {
System.out.println("Moin Jungs!");
}
}
0 comments:
Post a Comment