Monday, September 29, 2014

Using Aspects in Equinox Environment.


 Using Aspects in Equinox Environment.



Aspects can be used to weave into the original code and manipulate it according to our requirement.
Let’s see how we can use Aspects in Equinox environment.
Let us understand when you can need to use Aspect when you are doing some kind of eclipse/Equinox/osgi development.
I will take a very simple example. Suppose Your application shows a view which is provided by  some 3rd party and this view shows some Treeviewer. This TreeViewer has its own content provider and label provider. Now you have a requirement in which you have change the image show for every tree node at runtime depending on some business requirement.
To work on above scenario you would need to have control on label provide or you would like to override it and then do some kind of tweak to provide images at runtime and show it.
I have a better way to do this. Using Aspects!!!!!
What I will do is write an aspect where I will use the pointcut , getImage() method of that original label provider and provide my own image to it. And I am done.
Following Setup is required for  this:
  1. Install the latest version of AJDT from the AJDT update site.
  2. latest build of Equinox Aspects, which contains plugins regarding equinox weaving with aspectJ
Steps to understand this with an example:
  1. Create a sample plugin project, having a view and a treeviewer with label provider.


  1. Export  the above sample project as deployable plugin, so that is can be used as 3rd party jar/contribution of view.
  1. Now put the above sample deployable jar as target in your eclipse.
 
  1. Create a new plugin project, were we will use the 3rd party sample plugin to show its view and it tree.
  2. Make some changes in Menifest.mf to add some dependencies  and attributes:
  • Export the package which contains aspects and provide the name of those aspects(Here ViewAspect is the name of Aspect which we are going to use).
  • ‘Eclipse-SupplimentBundle’ attribute conatins bundle which are to be weaved.

  1. Create the ViewAspect.aj in package we have mentioned in Manifest.mf.
Here you can see, I have created a pointcut which is concerned about getImage() method of ViewLabelProvider. I am returning my image, instead of original image for Tree node.
  1. Now its Time to run our application.
Configure your runtime
  • Tell Equinox to use org.eclipse.equinox.weaving.hook as a framework extension (in the "VM arguments" of your launch configuration,  add                                                -Dosgi.framework.extensions=org.eclipse.equinox.weaving.hook).
  • The AspectJ weaving service (bundle org.eclipse.equinox.weaving.aspectj) must be started before any classes are loaded from any bundles targeted for weaving

  1. We are done now. We can run this application, We will see that Sample view Treeviewer will take my custom image now.
You can Access the code related to above mentioned scenario at GitHub:
Feel Free to comment and provide your responses to understand the topic more.
CHEERS!!!

No comments:

Post a Comment