[jboss-cvs] JBossAS SVN: r93180 - projects/demos/microcontainer/trunk/bundle_user/src/main/java/org/jboss/demos/buser.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Sep 3 12:23:37 EDT 2009


Author: alesj
Date: 2009-09-03 12:23:36 -0400 (Thu, 03 Sep 2009)
New Revision: 93180

Modified:
   projects/demos/microcontainer/trunk/bundle_user/src/main/java/org/jboss/demos/buser/EventActivator.java
Log:
Poor man's service tracker.

Modified: projects/demos/microcontainer/trunk/bundle_user/src/main/java/org/jboss/demos/buser/EventActivator.java
===================================================================
--- projects/demos/microcontainer/trunk/bundle_user/src/main/java/org/jboss/demos/buser/EventActivator.java	2009-09-03 16:15:57 UTC (rev 93179)
+++ projects/demos/microcontainer/trunk/bundle_user/src/main/java/org/jboss/demos/buser/EventActivator.java	2009-09-03 16:23:36 UTC (rev 93180)
@@ -23,10 +23,13 @@
 
 import java.util.logging.Logger;
 
+import org.jboss.demos.bundle.services.EventService;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
 import org.osgi.framework.ServiceReference;
-import org.jboss.demos.bundle.services.EventService;
 
 /**
  * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
@@ -34,10 +37,38 @@
 public class EventActivator implements BundleActivator
 {
    private Logger log = Logger.getLogger(EventActivator.class.getName());
+   private BundleContext bundleContext;
+   private ServiceListener listener;
 
    public void start(BundleContext bundleContext) throws Exception
    {
       ServiceReference reference = bundleContext.getServiceReference(EventService.class.getName());
+      if (reference != null)
+      {
+         doWelcome(bundleContext, reference);
+      }
+      else
+      {
+         this.bundleContext = bundleContext;
+         this.listener = new EventListener();
+
+         bundleContext.addServiceListener(listener, "(" + Constants.OBJECTCLASS + "=" + EventService.class.getName() + ")");
+      }
+   }
+
+   public void stop(BundleContext bundleContext) throws Exception
+   {
+      if (this.bundleContext != null)
+      {
+         this.bundleContext.removeServiceListener(listener);
+
+         this.bundleContext = null;
+         this.listener = null;
+      }
+   }
+
+   private void doWelcome(BundleContext bundleContext, ServiceReference reference)
+   {
       try
       {
          EventService es = (EventService)bundleContext.getService(reference);
@@ -49,7 +80,14 @@
       }
    }
 
-   public void stop(BundleContext bundleContext) throws Exception
+   private class EventListener implements ServiceListener
    {
+      public void serviceChanged(ServiceEvent event)
+      {
+         if (event.getType() == ServiceEvent.REGISTERED)
+         {
+            doWelcome(bundleContext, event.getServiceReference());
+         }
+      }
    }
 }




More information about the jboss-cvs-commits mailing list