You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2015/02/16 10:22:38 UTC

svn commit: r1660058 - in /felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal: runtime/AbstractInfo.java whiteboard/ContextHandler.java

Author: cziegeler
Date: Mon Feb 16 09:22:38 2015
New Revision: 1660058

URL: http://svn.apache.org/r1660058
Log:
FELIX-4060 : Implement HTTP Service Update (RFC-189) - simplify service object getting/ungetting

Modified:
    felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/AbstractInfo.java
    felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ContextHandler.java

Modified: felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/AbstractInfo.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/AbstractInfo.java?rev=1660058&r1=1660057&r2=1660058&view=diff
==============================================================================
--- felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/AbstractInfo.java (original)
+++ felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/AbstractInfo.java Mon Feb 16 09:22:38 2015
@@ -22,7 +22,9 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.osgi.framework.Bundle;
 import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceObjects;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
 
@@ -195,11 +197,41 @@ public abstract class AbstractInfo<T> im
         return this.serviceId;
     }
 
+    public String getTarget()
+    {
+        return this.target;
+    }
+
     public ServiceReference<T> getServiceReference()
     {
         return this.serviceReference;
     }
 
+    public T getService(final Bundle bundle)
+    {
+        if ( this.serviceReference != null )
+        {
+            final ServiceObjects<T> so = bundle.getBundleContext().getServiceObjects(this.serviceReference);
+            if ( so != null )
+            {
+                return so.getService();
+            }
+        }
+        return null;
+    }
+
+    public void ungetService(final Bundle bundle, final T service)
+    {
+        if ( this.serviceReference != null )
+        {
+            final ServiceObjects<T> so = bundle.getBundleContext().getServiceObjects(this.serviceReference);
+            if ( so != null )
+            {
+                so.ungetService(service);
+            }
+        }
+    }
+
     @Override
     public int hashCode()
     {
@@ -227,9 +259,4 @@ public abstract class AbstractInfo<T> im
             return false;
         return true;
     }
-
-    public String getTarget()
-    {
-        return this.target;
-    }
 }

Modified: felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ContextHandler.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ContextHandler.java?rev=1660058&r1=1660057&r2=1660058&view=diff
==============================================================================
--- felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ContextHandler.java (original)
+++ felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ContextHandler.java Mon Feb 16 09:22:38 2015
@@ -136,19 +136,15 @@ public final class ContextHandler implem
 
     public void initialized(@Nonnull final ServletContextListenerInfo listenerInfo)
     {
-        final ServiceObjects<ServletContextListener> so = bundle.getBundleContext().getServiceObjects(listenerInfo.getServiceReference());
-        if ( so != null )
+        final ServletContextListener listener = listenerInfo.getService(bundle);
+        if ( listener != null)
         {
-            final ServletContextListener listener = so.getService();
-            if ( listener != null)
-            {
-                // no need to sync map - initialized is called in sync
-                this.listeners.put(listenerInfo.getServiceId(), listener);
+            // no need to sync map - initialized is called in sync
+            this.listeners.put(listenerInfo.getServiceId(), listener);
 
-                final ServletContext context = this.getServletContext(listenerInfo.getServiceReference().getBundle());
+            final ServletContext context = this.getServletContext(listenerInfo.getServiceReference().getBundle());
 
-                listener.contextInitialized(new ServletContextEvent(context));
-            }
+            listener.contextInitialized(new ServletContextEvent(context));
         }
     }
 
@@ -163,11 +159,7 @@ public final class ContextHandler implem
             // call unget twice, once for the call in initialized and once for the call in this method(!)
             this.ungetServletContext(listenerInfo.getServiceReference().getBundle());
             this.ungetServletContext(listenerInfo.getServiceReference().getBundle());
-            final ServiceObjects<ServletContextListener> so = bundle.getBundleContext().getServiceObjects(listenerInfo.getServiceReference());
-            if ( so != null )
-            {
-                so.ungetService(listener);
-            }
+            listenerInfo.ungetService(bundle, listener);
         }
     }
 
@@ -230,14 +222,10 @@ public final class ContextHandler implem
      */
     public void addListener(@Nonnull final ServletContextAttributeListenerInfo info)
     {
-        final ServiceObjects<ServletContextAttributeListener> so =  bundle.getBundleContext().getServiceObjects(info.getServiceReference());
-        if ( so != null )
+        final  ServletContextAttributeListener service = info.getService(bundle);
+        if ( service != null )
         {
-            final  ServletContextAttributeListener service = bundle.getBundleContext().getServiceObjects(info.getServiceReference()).getService();
-            if ( service != null )
-            {
-                this.contextAttributeListeners.put(info.getServiceReference(), service);
-            }
+            this.contextAttributeListeners.put(info.getServiceReference(), service);
         }
     }
 
@@ -250,10 +238,7 @@ public final class ContextHandler implem
         final  ServletContextAttributeListener service = this.contextAttributeListeners.remove(info.getServiceReference());
         if ( service != null )
         {
-            final ServiceObjects<ServletContextAttributeListener> so =  bundle.getBundleContext().getServiceObjects(info.getServiceReference());
-            if ( so != null ) {
-                so.ungetService(service);
-            }
+            info.ungetService(bundle, service);
         }
     }
 
@@ -263,14 +248,10 @@ public final class ContextHandler implem
      */
     public void addListener(@Nonnull final HttpSessionAttributeListenerInfo info)
     {
-        final ServiceObjects<HttpSessionAttributeListener> so =  bundle.getBundleContext().getServiceObjects(info.getServiceReference());
-        if ( so != null )
+        final  HttpSessionAttributeListener service = info.getService(bundle);
+        if ( service != null )
         {
-            final  HttpSessionAttributeListener service = bundle.getBundleContext().getServiceObjects(info.getServiceReference()).getService();
-            if ( service != null )
-            {
-                this.sessionAttributeListeners.put(info.getServiceReference(), service);
-            }
+            this.sessionAttributeListeners.put(info.getServiceReference(), service);
         }
     }
 
@@ -283,10 +264,7 @@ public final class ContextHandler implem
         final  HttpSessionAttributeListener service = this.sessionAttributeListeners.remove(info.getServiceReference());
         if ( service != null )
         {
-            final ServiceObjects<HttpSessionAttributeListener> so =  bundle.getBundleContext().getServiceObjects(info.getServiceReference());
-            if ( so != null ) {
-                so.ungetService(service);
-            }
+            info.ungetService(bundle, service);
         }
     }
 
@@ -296,14 +274,10 @@ public final class ContextHandler implem
      */
     public void addListener(@Nonnull final HttpSessionListenerInfo info)
     {
-        final ServiceObjects<HttpSessionListener> so =  bundle.getBundleContext().getServiceObjects(info.getServiceReference());
-        if ( so != null )
+        final  HttpSessionListener service = info.getService(bundle);
+        if ( service != null )
         {
-            final  HttpSessionListener service = bundle.getBundleContext().getServiceObjects(info.getServiceReference()).getService();
-            if ( service != null )
-            {
-                this.sessionListeners.put(info.getServiceReference(), service);
-            }
+            this.sessionListeners.put(info.getServiceReference(), service);
         }
     }
 
@@ -316,10 +290,7 @@ public final class ContextHandler implem
         final  HttpSessionListener service = this.sessionListeners.remove(info.getServiceReference());
         if ( service != null )
         {
-            final ServiceObjects<HttpSessionListener> so =  bundle.getBundleContext().getServiceObjects(info.getServiceReference());
-            if ( so != null ) {
-                so.ungetService(service);
-            }
+            info.ungetService(bundle, service);
         }
     }
 
@@ -329,14 +300,10 @@ public final class ContextHandler implem
      */
     public void addListener(@Nonnull final ServletRequestListenerInfo info)
     {
-        final ServiceObjects<ServletRequestListener> so =  bundle.getBundleContext().getServiceObjects(info.getServiceReference());
-        if ( so != null )
+        final  ServletRequestListener service = info.getService(bundle);
+        if ( service != null )
         {
-            final  ServletRequestListener service = bundle.getBundleContext().getServiceObjects(info.getServiceReference()).getService();
-            if ( service != null )
-            {
-                this.requestListeners.put(info.getServiceReference(), service);
-            }
+            this.requestListeners.put(info.getServiceReference(), service);
         }
     }
 
@@ -349,10 +316,7 @@ public final class ContextHandler implem
         final ServletRequestListener service = this.requestListeners.remove(info.getServiceReference());
         if ( service != null )
         {
-            final ServiceObjects<ServletRequestListener> so =  bundle.getBundleContext().getServiceObjects(info.getServiceReference());
-            if ( so != null ) {
-                so.ungetService(service);
-            }
+            info.ungetService(bundle, service);
         }
     }
 
@@ -362,14 +326,10 @@ public final class ContextHandler implem
      */
     public void addListener(@Nonnull final ServletRequestAttributeListenerInfo info)
     {
-        final ServiceObjects<ServletRequestAttributeListener> so =  bundle.getBundleContext().getServiceObjects(info.getServiceReference());
-        if ( so != null )
+        final  ServletRequestAttributeListener service = info.getService(bundle);
+        if ( service != null )
         {
-            final  ServletRequestAttributeListener service = bundle.getBundleContext().getServiceObjects(info.getServiceReference()).getService();
-            if ( service != null )
-            {
-                this.requestAttributeListeners.put(info.getServiceReference(), service);
-            }
+            this.requestAttributeListeners.put(info.getServiceReference(), service);
         }
     }
 
@@ -382,10 +342,7 @@ public final class ContextHandler implem
         final ServletRequestAttributeListener service = this.requestAttributeListeners.remove(info.getServiceReference());
         if ( service != null )
         {
-            final ServiceObjects<ServletRequestAttributeListener> so =  bundle.getBundleContext().getServiceObjects(info.getServiceReference());
-            if ( so != null ) {
-                so.ungetService(service);
-            }
+            info.ungetService(bundle, service);
         }
     }