Sunday, October 14, 2007

Playing with hooks is nice

As you may know, eclipse provides a set of non-OSGi standard ways to developers to intervene in the framework intricate internal lifecycle (that is, they work only with Equinox). The need I have is to modify the classes bytecode when they are loaded so that I can perform manipulations of my own (let's say AOP-transformations).

From Java 5 you can use the -javaagent:agent.jar option and provide an instrumentor you like. Sadly, I'm still hard to understand how it works (maybe I'm just plain stupid with such things): in my case I only wanted to add the AspectJ weaver but after this modification it complys it cannot find classes in the AspectJ runtime library. So I give up as I want the eclipse-way for doing such things (and to learn about eclipse internals).

The instructions on the wiki page are quite precise (geez!, this is a wiki :))), so I'm only writing about what I've done: maybe it will be helpful for you out there too.
  1. I've imported the org.eclipse.osgi (binary+source) within my workspace, since this is a requirement for adaptors framework to work.
  2. I've create a new fragment for org.eclipse.osgi.
  3. I've written a HookConfigurator-derived class within this fragment and implemented the addHooks( HookRegistry registry ) method for adding my own hooks (Do not forget to export the package in the manifest file!).
    public class SpringHooksConfigurator implements HookConfigurator
    public void addHooks( HookRegistry registry ) {
    SpringInjectorHook springInjector = new SpringInjectorHook();

    registry.addWatcher( springInjector );
    registry.addClassLoadingHook( springInjector );
    }
    }
    (My hook implements both ClassLoadingHook and BundleWatcher interfaces).

  4. I've created a hookconfigurators.properties files to specify the configurators (as explained in the wiki page, the framework will look for such files and load the specified hooks).
    ##
    ## Hooks configuration
    ##
    ## This files describes to Equinox which adaptor hooks are provided by this fragment.
    ## Note that we only provide a HookConfigurator that will add its own hooks by code.
    ##
    hook.configurators = it.uniba.di.cdg.spring.hooks.SpringHooksConfigurator


  5. I've added the -Dosgi.framework.extensions option to the VM arguments and make it point to my own fragment.
Done. Have fun with your ... hooks :)

No comments: