FELIX-4060 : Implement HTTP Service Update (RFC-189) - correct context and path handling

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1659800 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ContextHandler.java b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ContextHandler.java
index b3703b1..e134f0c 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ContextHandler.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ContextHandler.java
@@ -183,7 +183,7 @@
                 if ( so != null )
                 {
                     holder = new ContextHolder();
-                    // TODO check for null
+                    // TODO check for null of getService()
                     holder.servletContextHelper = so.getService();
                     holder.servletContext = new PerBundleServletContextImpl(bundle,
                             this.sharedContext,
@@ -194,6 +194,7 @@
                             this.getServletRequestAttributeListener());
                     this.perBundleContextMap.put(key, holder);
                 }
+                // TODO - check null for so
             }
             holder.counter++;
 
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ServletContextHelperManager.java b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ServletContextHelperManager.java
index 6960d09..81d2f0f 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ServletContextHelperManager.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ServletContextHelperManager.java
@@ -177,8 +177,6 @@
      */
     private void deactivate(final ContextHandler handler)
     {
-        this.httpService.unregisterContext(handler);
-
         // context listeners last
         final Map<ServiceReference<ServletContextListener>, ServletContextListenerInfo> listeners = new TreeMap<ServiceReference<ServletContextListener>, ServletContextListenerInfo>();
         final Iterator<Map.Entry<WhiteboardServiceInfo<?>, List<ContextHandler>>> i = this.servicesMap.entrySet().iterator();
@@ -206,6 +204,9 @@
             handler.destroyed(info);
         }
         handler.deactivate();
+
+        this.httpService.unregisterContext(handler);
+
     }
 
     /**
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardHttpService.java b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardHttpService.java
index 889f0cc..002213f 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardHttpService.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardHttpService.java
@@ -36,6 +36,7 @@
 import org.apache.felix.http.base.internal.handler.FilterHandler;
 import org.apache.felix.http.base.internal.handler.HandlerRegistry;
 import org.apache.felix.http.base.internal.handler.HttpSessionWrapper;
+import org.apache.felix.http.base.internal.handler.PerContextHandlerRegistry;
 import org.apache.felix.http.base.internal.handler.ServletHandler;
 import org.apache.felix.http.base.internal.runtime.FilterInfo;
 import org.apache.felix.http.base.internal.runtime.ResourceInfo;
@@ -185,8 +186,12 @@
                         servletInfo,
                         servlet);
                 try {
-                    this.handlerRegistry.getRegistry(contextHandler.getContextInfo()).addServlet(handler);
-                    contextHandler.addWhiteboardService(servletInfo);
+                    final PerContextHandlerRegistry registry = this.handlerRegistry.getRegistry(contextHandler.getContextInfo());
+                    if (registry != null )
+                    {
+                        registry.addServlet(handler);
+                        contextHandler.addWhiteboardService(servletInfo);
+                    }
                 } catch (final ServletException e) {
                     so.ungetService(servlet);
                     // TODO create failure DTO
@@ -202,13 +207,17 @@
      */
     public void unregisterServlet(@Nonnull final ContextHandler contextHandler, @Nonnull final ServletInfo servletInfo)
     {
-        final Servlet instance = this.handlerRegistry.getRegistry(contextHandler.getContextInfo()).removeServlet(servletInfo, true);
-        if ( instance != null )
+        final PerContextHandlerRegistry registry = this.handlerRegistry.getRegistry(contextHandler.getContextInfo());
+        if (registry != null )
         {
-            this.bundleContext.getServiceObjects(servletInfo.getServiceReference()).ungetService(instance);
-            contextHandler.ungetServletContext(servletInfo.getServiceReference().getBundle());
-            contextHandler.removeWhiteboardService(servletInfo);
+            final Servlet instance = registry.removeServlet(servletInfo, true);
+            if ( instance != null )
+            {
+                this.bundleContext.getServiceObjects(servletInfo.getServiceReference()).ungetService(instance);
+            }
         }
+        contextHandler.ungetServletContext(servletInfo.getServiceReference().getBundle());
+        contextHandler.removeWhiteboardService(servletInfo);
     }
 
     /**
@@ -228,8 +237,12 @@
                     filter,
                     filterInfo);
             try {
-                this.handlerRegistry.getRegistry(contextHandler.getContextInfo()).addFilter(handler);
-                contextHandler.addWhiteboardService(filterInfo);
+                final PerContextHandlerRegistry registry = this.handlerRegistry.getRegistry(contextHandler.getContextInfo());
+                if (registry != null )
+                {
+                    registry.addFilter(handler);
+                    contextHandler.addWhiteboardService(filterInfo);
+                }
             } catch (final ServletException e) {
                 // TODO create failure DTO
             }
@@ -243,13 +256,17 @@
      */
     public void unregisterFilter(@Nonnull final ContextHandler contextHandler, @Nonnull final FilterInfo filterInfo)
     {
-        final Filter instance = this.handlerRegistry.getRegistry(contextHandler.getContextInfo()).removeFilter(filterInfo, true);
-        if ( instance != null )
+        final PerContextHandlerRegistry registry = this.handlerRegistry.getRegistry(contextHandler.getContextInfo());
+        if (registry != null )
         {
-            this.bundleContext.getServiceObjects(filterInfo.getServiceReference()).ungetService(instance);
-            contextHandler.ungetServletContext(filterInfo.getServiceReference().getBundle());
-            contextHandler.removeWhiteboardService(filterInfo);
+            final Filter instance = registry.removeFilter(filterInfo, true);
+            if ( instance != null )
+            {
+                this.bundleContext.getServiceObjects(filterInfo.getServiceReference()).ungetService(instance);
+            }
         }
+        contextHandler.ungetServletContext(filterInfo.getServiceReference().getBundle());
+        contextHandler.removeWhiteboardService(filterInfo);
     }
 
     /**
@@ -268,8 +285,12 @@
                 servletInfo,
                 servlet);
         try {
-            this.handlerRegistry.getRegistry(contextHandler.getContextInfo()).addServlet(handler);
-            contextHandler.addWhiteboardService(resourceInfo);
+            final PerContextHandlerRegistry registry = this.handlerRegistry.getRegistry(contextHandler.getContextInfo());
+            if (registry != null )
+            {
+                registry.addServlet(handler);
+                contextHandler.addWhiteboardService(resourceInfo);
+            }
         } catch (ServletException e) {
             // TODO create failure DTO
         }
@@ -283,8 +304,13 @@
     public void unregisterResource(@Nonnull final ContextHandler contextHandler, @Nonnull final ResourceInfo resourceInfo)
     {
         final ServletInfo servletInfo = new ServletInfo(resourceInfo);
-        this.unregisterServlet(contextHandler, servletInfo);
-        contextHandler.removeWhiteboardService(resourceInfo);
+        final PerContextHandlerRegistry registry = this.handlerRegistry.getRegistry(contextHandler.getContextInfo());
+        if (registry != null )
+        {
+            registry.removeServlet(servletInfo, true);
+        }
+        contextHandler.ungetServletContext(servletInfo.getServiceReference().getBundle());
+        contextHandler.removeWhiteboardService(servletInfo);
     }
 
     public void registerContext(@Nonnull final ContextHandler contextHandler)