geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r776274 - /geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/DefaultBlueprintContextEventSender.java
Date Tue, 19 May 2009 11:20:13 GMT
Author: gnodet
Date: Tue May 19 11:20:12 2009
New Revision: 776274

URL: http://svn.apache.org/viewvc?rev=776274&view=rev
Log:
Make sure newly registered listeners have last event for all bundles

Modified:
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/DefaultBlueprintContextEventSender.java

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/DefaultBlueprintContextEventSender.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/DefaultBlueprintContextEventSender.java?rev=776274&r1=776273&r2=776274&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/DefaultBlueprintContextEventSender.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/DefaultBlueprintContextEventSender.java
Tue May 19 11:20:12 2009
@@ -31,11 +31,13 @@
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.framework.Version;
+import org.osgi.framework.ServiceReference;
 import org.osgi.service.blueprint.context.BlueprintContextListener;
 import org.osgi.service.blueprint.context.EventConstants;
 import org.osgi.service.event.Event;
 import org.osgi.service.event.EventAdmin;
 import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -55,16 +57,33 @@
     private final ServiceTracker contextListenerTracker;
     private final Map<Bundle, Object> states;
 
-    public DefaultBlueprintContextEventSender(BundleContext bundleContext) {
+    public DefaultBlueprintContextEventSender(final BundleContext bundleContext) {
         this.extenderBundle = bundleContext.getBundle();
         this.eventAdminServiceTracker = new ServiceTracker(bundleContext, EventAdmin.class.getName(),
null);
         this.eventAdminServiceTracker.open();
-        this.contextListenerTracker = new ServiceTracker(bundleContext, BlueprintContextListener.class.getName(),
null);
+        this.contextListenerTracker = new ServiceTracker(bundleContext, BlueprintContextListener.class.getName(),
new ServiceTrackerCustomizer() {
+            public Object addingService(ServiceReference reference) {
+                BlueprintContextListener listener = (BlueprintContextListener) bundleContext.getService(reference);
+                sendInitialEvents(listener);
+                return listener;
+            }
+            public void modifiedService(ServiceReference reference, Object service) {
+            }
+            public void removedService(ServiceReference reference, Object service) {
+                bundleContext.ungetService(reference);
+            }
+        });
         this.contextListenerTracker.open();
         this.states = new ConcurrentHashMap<Bundle, Object>();
         this.registration = bundleContext.registerService(BlueprintStateManager.class.getName(),
this, null);
     }
 
+    protected void sendInitialEvents(BlueprintContextListener listener) {
+        for (Map.Entry<Bundle, Object> entry : states.entrySet()) {
+            callListener(listener, entry.getKey(), entry.getValue());
+        }
+    }
+
     public int getState(Bundle bundle) {
         Object obj = states.get(bundle);
         if (obj instanceof Integer) {
@@ -165,17 +184,26 @@
             Object[] listeners = contextListenerTracker.getServices();
             if (listeners != null) {
                 for (Object listener : listeners) {
-                    try {
-                        if (created) {
-                            ((BlueprintContextListener) listener).contextCreated(bundle);
-                        } else {
-                            ((BlueprintContextListener) listener).contextCreationFailed(bundle,
cause);
-                        }
-                    } catch (Throwable t) {
-                        LOGGER.info("Error calling blueprint context listener", t);
-                    }
+                    callListener((BlueprintContextListener) listener, bundle, created ? CREATED
: cause != null ? cause : FAILED);
+                }
+            }
+        }
+    }
+
+    private void callListener(BlueprintContextListener listener, Bundle bundle, Object state)
{
+        // TODO: listener is missing a few methods to replay the state (at least WAITING
events)
+        try {
+            if (state instanceof Integer) {
+                if ((Integer) state == CREATED) {
+                    listener.contextCreated(bundle);
+                } else if ((Integer) state == FAILED) {
+                    listener.contextCreationFailed(bundle, null);
                 }
+            } else if (state instanceof Throwable) {
+                listener.contextCreationFailed(bundle, (Throwable) state);
             }
+        } catch (Throwable t) {
+            LOGGER.info("Error calling blueprint context listener", t);
         }
     }
 



Mime
View raw message