Explain how to use the Extension Point and Extensions.
Refer1
Refer2
Refer3
1. Introduction
- Extensions are the central mechanism for contribute behavior to the platform. We used it to contribute functionality to a certain type of API.
- Extension points define new functions points for the platform that other plug-ins can plug into.
- E.g. When you want to create modular that can be added or removed at runtime, you can use the OSGi service.
- These extensions and extension points are defined via the
plugin.xml
file.
2. Using Extension points/ Extensions
2.1. Create an EXTENSION POINT
- A plugin which declares an extension point must declare the extension point in its plugin.xml.
- It will contain code to evaluate the extensions.
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension-point id="com.extensionpoint.greeters"
name="Greeters"
schema="schema/com.extensionpoint.greeters.exsd" />
</plugin>
-
It has three attributes:
id
: a required attr, simple name idname
: a required attr, sime name stringschema
: optional attr, describe
-
Examples: Create a plugin named
com.extensionpoint.definition
contained the a extension point.
//MANIFEST
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: A Plugin for the Extension Point Definition
Bundle-SymbolicName: com.extensionpoint.definition;singleton:=true
Bundle-Version: 1.0.0.qualifier
Export-Package: com.extensionpoint.definition
Bundle-Vendor: Phong
Automatic-Module-Name: com.extensionpoint.definition
Bundle-RequiredExecutionEnvironment: JavaSE-21
Import-Package: org.eclipse.core.runtime;version="[3.7.0,4.0.0)",
org.eclipse.e4.core.di.annotations;version="[1.6.0,2.0.0)"
// plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension-point id="com.vogella.extensionpoint.definition.greeter" name="Greeter (Extension Point Name)" schema="schema/com.vogella.extensionpoint.definition.greeter.exsd"/>
</plugin>
// exsd
</documentation>
</annotation>
</attribute>
<attribute name="id" type="string">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="name" type="string">
<annotation>
<documentation>
</documentation>
<appinfo>
<meta.attribute translatable="true"/>
</appinfo>
</annotation>
</attribute>
</complexType>
</documentation>
<appinfo>
<meta.attribute kind="java"/>
</appinfo>
</annotation>
</attribute>
</complexType>
// Evaluation class
package com.extensionpoint.definition;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.e4.core.di.annotations.Execute;
public class EvaluateContributionsHandler {
private static final String IGREETER_ID =
"com.extensionpoint.definition.greeter";
@Execute
public void execute(IExtensionRegistry registry) {
IConfigurationElement[] config =
registry.getConfigurationElementsFor(IGREETER_ID);
try {
for (IConfigurationElement e : config) {
System.out.println("Evaluating extension");
final Object o =
e.createExecutableExtension("class");
if (o instanceof IGreeter) {
executeExtension(o);
}
}
} catch (CoreException ex) {
System.out.println(ex.getMessage());
}
}
private void executeExtension(final Object o) {
ISafeRunnable runnable = new ISafeRunnable() {
@Override
public void handleException(Throwable e) {
System.out.println("Exception in client");
}
@Override
public void run() throws Exception {
((IGreeter) o).greet();
}
};
SafeRunner.run(runnable);
}
}
// Attribute class
package com.vogella.extensionpoint.definition;
public interface IGreeter {
void greet();
}
2.2. Create/adding EXTENSIONS to EXTENSION POINTS
- Examples: Create a plugin named
com.extension.contribution
contained the a extension.
// MANIFEST
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Contribution
Bundle-SymbolicName: com.vogella.extensionpoint.contribution;singleton:=true
Bundle-Version: 1.0.0.qualifier
Require-Bundle: org.eclipse.core.runtime;bundle-version="3.32.0",
com.vogella.extensionpoint.definition;bundle-version="1.0.0"
Bundle-Vendor: VOGELLA
Automatic-Module-Name: com.vogella.extensionpoint.contribution
Bundle-RequiredExecutionEnvironment: JavaSE-21
// plugin.xml
// The client class that implemented attribute class
package com.vogella.extensionpoint.contribution;
import com.vogella.extensionpoint.definition.IGreeter;
public class GreeterExensionContribution implements IGreeter {
@Override
public void greet() {
System.err.println("This call from GreeterExensionContribution");
}
}
2.3. Accessing EXTENSIONS
- The information about the available extensions point and the provided extensions are stored in a class of type
IExtensionRegistry
. - We can use the dependency injection mechanism to get the
IExtensionRegistry
class that injected. IExtensionRegistry
contains all extensions of all extensions point.
// The following will read all existing extensions
// for the defined ID of the extension point
Platform.getExtensionRegistry().
getConfigurationElementsFor("yourextensionpointid");
IConfigurationElement[] elements = Platform.getExtensionRegistry()
.getConfigurationElementsFor("com.renesas.cdt.devicesupport.api.builderDeviceSupport");
for (IConfigurationElement ele : elements) {
}