PDA

View Full Version : How to receive war bundles deploy events?



essam
May 8th, 2009, 02:35 PM
I'm using dm Server 1.0.2 and need to somehow get notified when a war module is deployed. My wars use the shared libraries way as described in the developer's guide (http://static.springsource.com/projects/dm-server/1.0.x/programmer-guide/html/ch04.html#architecture-shared-libraries-war).

I first followed the "Listening to Extender events" section @ http://static.springframework.org/osgi/docs/1.1.0-m2/reference/html/app-deploy.html#app-deploy:extender-configuration:events, and implemented a Spring DM OsgiBundleApplicationContextListener as follows:

import org.springframework.osgi.context.event.OsgiBundleA pplicationContextEvent;
import org.springframework.osgi.context.event.OsgiBundleA pplicationContextListener;
import org.springframework.osgi.context.event.OsgiBundleC ontextRefreshedEvent;

public class MyBundleApplicationContextListener implements OsgiBundleApplicationContextListener {

@Override
public void onOsgiApplicationEvent(OsgiBundleApplicationContex tEvent event) {
if (event instanceof OsgiBundleContextRefreshedEvent) {
System.out.println(event);
}
}

and added the following to one of my (non-war) bundles:

<bean name="contextListenerImpl"
class="com.sample.impl.MyBundleApplicationContextListener"/>

<osgi:service id="contextListener" ref="contextListenerImpl"
interface="org.springframework.osgi.context.event.OsgiBundleA pplicationContextListener" />

But the listener gets called only for non-war bundles, which I guess makes sense since dm server does not use the Spring DM web extender (or something like that).

Is there a way to get notified when war bundles get deployed?

Thanks!

essam
May 12th, 2009, 11:22 AM
One way of receiving bundle events is to have a BundleActivator and add a BundleListener to the BundleContext.
For example:

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleListener;

public class MyBundleActivator implements BundleActivator {
@Override
public void start(BundleContext bc) throws Exception {
bc.addBundleListener(new BundleListener() {
public void bundleChanged(BundleEvent event) {
if (event.getType()== BundleEvent.STARTED) {
handleBundle(event.getBundle());
}
}
});
}
}

In this case you get the bundle event for all bundle kinds including war bundles (which means that the listener added in the original post would not be needed if you use the activator mechanism).