Friday, September 5, 2014

Equinox Transform Revealed

Equinox Transform Revealed

Hey!!!!!

Working with Eclipse plugins is really awesome and challenging.
While working with OSGI stuffs, there are many situation when
* we want to suppress some of the functionality provided by any plugin or
* we want to modify the extension point at runtime to put in our own functionality on top of it.

One answer could be directly go and modify the stuff, may be plugin. xml or Manifest.mf.

But what would we do if we don't have any control on those code? That could be a third party plugin, but I want to suppress its some views to be visible in my application.

Potential Solution

Then there are 2 best solutions to it:

1. Equinox Transform: I will talk on it more.

2. Eclipse Activities : You can get it in details here.


Equinox Transform
As the name 'Transform' tells that it is going to transform. Yes, We can transform the stuffs what we have in Eclipse plugin development.

I will put in some scenario when we could need this.

i) I have Extension point given by a third party, but I don't want this to be in my application.
ii) I want that 3rd party perspective, but I want to give it my name.
iii) I want to add/remove some of the views to that 3rd party Perspective
iv) Modify the bundles name etc

many more.....

We can do anything we want.Nothing is fixed. everything is modifiable.
You can read theories on it here Eclipse wiki .
We will go step by step to do it with an example.

Ensure that you have these plugins in your target. If not you can update with latest OSGI.
  • org.eclipse.equinox.transforms.xslt
  • org.eclipse.equinox.transforms.hook

Step 1: Create Eclipse plugin 1



Step 2: Create a Sample View in it.
Step 3: Create another plugin 2 with a Sample view. (Assume this is a 3rd party plugin)

Now we want to suppress or remove the view contributed by plugin 2.

Step 4:  create a file named 'transform.csv' in plugin 1 with following pattern:

<pluginid>,<resource file>,<xslt file>

<pluginid> : The plugin id whose file is to modified.
<resource file> : Name of the resource that need to be modified.
<xslt file> : location of xslt file which is used for transforming the <resource file>
eg: 
plugin id: com.test.sample.plugin
resource file : plugin.xml
xslt file : xslt/tes.xslt

then transform.csv will have value like the this: 
com\.test\.sample\.plugin,plugin\.xml,/xslt/test.xslt


Step 5: create the xslt file. which exactly modifies the resource file mentioned in transform.csv
Sample content

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions">


<xsl:template match="extension[@point='org.eclipse.ui.views']/view[@id='com.test.sample.plugin.views.SampleView']">
</xsl:template>
<!-- identity transformation - copy everything (nodes and attributes) without 
modifications -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

Now your project structure will look something like this.



Step 6 : Now we need to register our 'transform.csv' with proper transformerType in eclipse services.
For this open the Activator.start() of Plugin 1 and all following line of codes:

Hashtable<String, String> properties = new Hashtable<>();
properties.put("equinox.transformerType", "xslt");
registration= context.registerService(URL.class.getName(), context.getBundle().getEntry("/transform.csv"), properties);


Step 7 : Now include plugin 2(3rd party plugin)  in plugin 1 dependencies.

Step 8 : Now its time to run the application.

Please ensure following to be set in run configuration or config.ini file, before running the application:

i)osgi.bundles=org.eclipse.equinox.common@2:start,org.eclipse.core.runtime@start,org.eclipse.equinox.transforms.xslt@1:start
ii) osgi.bundles.defaultStartLevel=4
iii) osgi.framework=org.eclipse.osgi
iv) osgi.framework.extensions=org.eclipse.equinox.transforms.hook


If we do above mentioned steps correctly , we will see that it has removed the view contributed by plugin 2.

Complete Sample Code:

Plugin 1 : https://github.com/sanjar/Day_Today/tree/master/TestEquinoxTransform
Plugin 2(3rd Party) : https://github.com/sanjar/Day_Today/tree/master/com.test.sample.plugin 

Note: I have added a launch file (in Plugin 1) for help to set the proper plugins and its start levels.

Hope This will help in understanding Equinox (Magic) Transform. :)

Please comment your views and suggestions on it.

CHEERS!!!



No comments:

Post a Comment