FELIX-4603 : Whiteboard registration might get lost

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1616812 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/http/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManager.java b/http/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManager.java
index 7adbbcf..346117e 100644
--- a/http/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManager.java
+++ b/http/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManager.java
@@ -36,13 +36,16 @@
 
 public final class ExtenderManager
 {
+    static final String TYPE_FILTER = "f";
+    static final String TYPE_SERVLET = "s";
+
     private HttpService httpService;
-    private final HashMap<ServiceReference, AbstractMapping> mapping;
+    private final Map<String, AbstractMapping> mapping;
     private final HttpContextManager contextManager;
 
     public ExtenderManager()
     {
-        this.mapping = new HashMap<ServiceReference, AbstractMapping>();
+        this.mapping = new HashMap<String, AbstractMapping>();
         this.contextManager = new HttpContextManager();
     }
 
@@ -159,7 +162,7 @@
         FilterMapping mapping = new FilterMapping(ref.getBundle(), service, pattern, ranking);
         getHttpContext(mapping, ref);
         addInitParams(ref, mapping);
-        addMapping(ref, mapping);
+        addMapping(TYPE_FILTER, ref, mapping);
     }
 
     public void add(Servlet service, ServiceReference ref)
@@ -175,12 +178,17 @@
         ServletMapping mapping = new ServletMapping(ref.getBundle(), service, alias);
         getHttpContext(mapping, ref);
         addInitParams(ref, mapping);
-        addMapping(ref, mapping);
+        addMapping(TYPE_SERVLET, ref, mapping);
     }
 
-    public void remove(ServiceReference ref)
+    public void removeFilter(ServiceReference ref)
     {
-        removeMapping(ref);
+        removeMapping(TYPE_FILTER, ref);
+    }
+
+    public void removeServlet(ServiceReference ref)
+    {
+        removeMapping(TYPE_SERVLET, ref);
     }
 
     public synchronized void setHttpService(HttpService service)
@@ -237,15 +245,15 @@
     	}
     }
 
-    private synchronized void addMapping(ServiceReference ref, AbstractMapping mapping)
+    private synchronized void addMapping(final String servType, ServiceReference ref, AbstractMapping mapping)
     {
-        this.mapping.put(ref, mapping);
+        this.mapping.put(ref.getProperty(Constants.SERVICE_ID).toString() + servType, mapping);
         this.registerMapping(mapping);
     }
 
-    private synchronized void removeMapping(ServiceReference ref)
+    private synchronized void removeMapping(final String servType, ServiceReference ref)
     {
-        AbstractMapping mapping = this.mapping.remove(ref);
+        AbstractMapping mapping = this.mapping.remove(ref.getProperty(Constants.SERVICE_ID).toString() + servType);
         if (mapping != null)
         {
             ungetHttpContext(mapping, ref);
@@ -295,11 +303,11 @@
     /**
      * Returns mappings indexed by there owning OSGi service.
      */
-    Map<Object, AbstractMapping> getMappings()
+    Map<String, AbstractMapping> getMappings()
     {
         synchronized (this)
         {
-            return new HashMap<Object, AbstractMapping>(this.mapping);
+            return new HashMap<String, AbstractMapping>(this.mapping);
         }
     }
 }
diff --git a/http/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/FilterTracker.java b/http/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/FilterTracker.java
index ba4fa58..9ce1aec 100644
--- a/http/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/FilterTracker.java
+++ b/http/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/FilterTracker.java
@@ -17,6 +17,7 @@
 package org.apache.felix.http.whiteboard.internal.tracker;
 
 import javax.servlet.Filter;
+
 import org.apache.felix.http.whiteboard.internal.manager.ExtenderManager;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
@@ -32,19 +33,22 @@
         this.manager = manager;
     }
 
+    @Override
     protected void added(Filter service, ServiceReference ref)
     {
         this.manager.add(service, ref);
     }
 
+    @Override
     protected void modified(Filter service, ServiceReference ref)
     {
         removed(service, ref);
         added(service, ref);
     }
 
+    @Override
     protected void removed(Filter service, ServiceReference ref)
     {
-        this.manager.remove(ref);
+        this.manager.removeFilter(ref);
     }
 }
diff --git a/http/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/ServletTracker.java b/http/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/ServletTracker.java
index 7853c0d..fe5d6e1 100644
--- a/http/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/ServletTracker.java
+++ b/http/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/ServletTracker.java
@@ -16,10 +16,11 @@
  */
 package org.apache.felix.http.whiteboard.internal.tracker;
 
+import javax.servlet.Servlet;
+
 import org.apache.felix.http.whiteboard.internal.manager.ExtenderManager;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
-import javax.servlet.Servlet;
 
 public final class ServletTracker
     extends AbstractTracker<Servlet>
@@ -32,19 +33,22 @@
         this.manager = manager;
     }
 
+    @Override
     protected void added(Servlet service, ServiceReference ref)
     {
         this.manager.add(service, ref);
     }
 
+    @Override
     protected void modified(Servlet service, ServiceReference ref)
     {
         removed(service, ref);
         added(service, ref);
     }
 
+    @Override
     protected void removed(Servlet service, ServiceReference ref)
     {
-        this.manager.remove(ref);
+        this.manager.removeServlet(ref);
     }
 }
diff --git a/http/whiteboard/src/test/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManagerTest.java b/http/whiteboard/src/test/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManagerTest.java
index a8ad61b..310583f 100644
--- a/http/whiteboard/src/test/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManagerTest.java
+++ b/http/whiteboard/src/test/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManagerTest.java
@@ -38,6 +38,7 @@
 import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.http.HttpContext;
 
@@ -106,6 +107,9 @@
     private ServiceReference filter2Reference;
 
     @Mock
+    private ServiceReference filterAndServletReference;
+
+    @Mock
     private ServiceReference httpContextReference;
 
     @Before
@@ -118,28 +122,40 @@
         when(servlet1Reference.getBundle()).thenReturn(bundle1);
         when(servlet1Reference.getPropertyKeys()).thenReturn(new String[0]);
         when(servlet1Reference.getProperty(HttpWhiteboardConstants.ALIAS)).thenReturn(SERVLET_1_ALIAS);
+        when(servlet1Reference.getProperty(Constants.SERVICE_ID)).thenReturn(1L);
 
         when(servlet1_1Reference.getBundle()).thenReturn(bundle1);
         when(servlet1_1Reference.getPropertyKeys()).thenReturn(new String[0]);
         when(servlet1_1Reference.getProperty(HttpWhiteboardConstants.ALIAS)).thenReturn(SERVLET_1_1_ALIAS);
+        when(servlet1_1Reference.getProperty(Constants.SERVICE_ID)).thenReturn(2L);
 
         when(servlet2Reference.getBundle()).thenReturn(bundle2);
         when(servlet2Reference.getPropertyKeys()).thenReturn(new String[0]);
         when(servlet2Reference.getProperty(HttpWhiteboardConstants.ALIAS)).thenReturn(SERVLET_2_ALIAS);
         when(servlet2Reference.getProperty(HttpWhiteboardConstants.CONTEXT_ID)).thenReturn(SAMPLE_CONTEXT_ID);
+        when(servlet2Reference.getProperty(Constants.SERVICE_ID)).thenReturn(3L);
 
         when(filter1Reference.getBundle()).thenReturn(bundle1);
         when(filter1Reference.getPropertyKeys()).thenReturn(new String[0]);
         when(filter1Reference.getProperty(HttpWhiteboardConstants.PATTERN)).thenReturn(SERVLET_1_ALIAS);
+        when(filter1Reference.getProperty(Constants.SERVICE_ID)).thenReturn(47L);
 
         when(filter1_1Reference.getBundle()).thenReturn(bundle1);
         when(filter1_1Reference.getPropertyKeys()).thenReturn(new String[0]);
         when(filter1_1Reference.getProperty(HttpWhiteboardConstants.PATTERN)).thenReturn(SERVLET_1_1_ALIAS);
+        when(filter1_1Reference.getProperty(Constants.SERVICE_ID)).thenReturn(5L);
 
         when(filter2Reference.getBundle()).thenReturn(bundle2);
         when(filter2Reference.getPropertyKeys()).thenReturn(new String[0]);
         when(filter2Reference.getProperty(HttpWhiteboardConstants.PATTERN)).thenReturn(SERVLET_2_ALIAS);
         when(filter2Reference.getProperty(HttpWhiteboardConstants.CONTEXT_ID)).thenReturn(SAMPLE_CONTEXT_ID);
+        when(filter2Reference.getProperty(Constants.SERVICE_ID)).thenReturn(6L);
+
+        when(filterAndServletReference.getBundle()).thenReturn(bundle1);
+        when(filterAndServletReference.getPropertyKeys()).thenReturn(new String[0]);
+        when(filterAndServletReference.getProperty(HttpWhiteboardConstants.PATTERN)).thenReturn(SERVLET_2_ALIAS);
+        when(filterAndServletReference.getProperty(HttpWhiteboardConstants.ALIAS)).thenReturn(SERVLET_2_ALIAS);
+        when(filterAndServletReference.getProperty(Constants.SERVICE_ID)).thenReturn(7L);
 
         this.httpService = new MockExtHttpService();
     }
@@ -193,7 +209,7 @@
         em.add(servlet1, servlet1Reference);
 
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(servlet1, ((ServletMapping) em.getMappings().get(servlet1Reference)).getServlet());
+        TestCase.assertSame(servlet1, getServletMapping(em, servlet1Reference).getServlet());
         TestCase.assertEquals(1, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getServlets().size());
         TestCase.assertSame(servlet1, this.httpService.getServlets().get(SERVLET_1_ALIAS));
@@ -204,27 +220,27 @@
         em.add(servlet2, servlet2Reference);
 
         TestCase.assertEquals(2, em.getMappings().size());
-        TestCase.assertSame(servlet2, ((ServletMapping) em.getMappings().get(servlet2Reference)).getServlet());
+        TestCase.assertSame(servlet2, getServletMapping(em, servlet2Reference).getServlet());
         TestCase.assertEquals(1, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getServlets().size());
         TestCase.assertNull(this.httpService.getServlets().get(SERVLET_2_ALIAS));
         TestCase.assertEquals(1, em.getOrphanMappings().size());
         TestCase.assertEquals(1, em.getOrphanMappings().get(SAMPLE_CONTEXT_ID).size());
         TestCase.assertTrue(em.getOrphanMappings().get(SAMPLE_CONTEXT_ID)
-            .contains(em.getMappings().get(servlet2Reference)));
+            .contains(getServletMapping(em, servlet2Reference)));
 
         // unregister servlet2
-        em.remove(servlet2Reference);
+        em.removeServlet(servlet2Reference);
 
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(servlet1, ((ServletMapping) em.getMappings().get(servlet1Reference)).getServlet());
+        TestCase.assertSame(servlet1, getServletMapping(em, servlet1Reference).getServlet());
         TestCase.assertEquals(1, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getServlets().size());
         TestCase.assertSame(servlet1, this.httpService.getServlets().get(SERVLET_1_ALIAS));
         TestCase.assertTrue(em.getOrphanMappings().isEmpty());
 
         // unregister servlet1
-        em.remove(servlet1Reference);
+        em.removeServlet(servlet1Reference);
         TestCase.assertTrue(em.getMappings().isEmpty());
         TestCase.assertTrue(em.getHttpContexts().get(id).getMappings().isEmpty());
         TestCase.assertEquals(0, this.httpService.getServlets().size());
@@ -260,7 +276,7 @@
         em.add(servlet1, servlet1Reference);
 
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(servlet1, ((ServletMapping) em.getMappings().get(servlet1Reference)).getServlet());
+        TestCase.assertSame(servlet1, getServletMapping(em, servlet1Reference).getServlet());
         TestCase.assertEquals(1, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getServlets().size());
         TestCase.assertSame(servlet1, this.httpService.getServlets().get(SERVLET_1_ALIAS));
@@ -271,7 +287,7 @@
         em.add(servlet2, servlet2Reference);
 
         TestCase.assertEquals(2, em.getMappings().size());
-        TestCase.assertSame(servlet2, ((ServletMapping) em.getMappings().get(servlet2Reference)).getServlet());
+        TestCase.assertSame(servlet2, getServletMapping(em, servlet2Reference).getServlet());
         TestCase.assertEquals(2, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(2, this.httpService.getServlets().size());
         TestCase.assertSame(servlet2, this.httpService.getServlets().get(SERVLET_2_ALIAS));
@@ -279,17 +295,17 @@
         TestCase.assertSame(sampleContext, servlet2.getHttpContext());
 
         // unregister servlet2
-        em.remove(servlet2Reference);
+        em.removeServlet(servlet2Reference);
 
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(servlet1, ((ServletMapping) em.getMappings().get(servlet1Reference)).getServlet());
+        TestCase.assertSame(servlet1, getServletMapping(em, servlet1Reference).getServlet());
         TestCase.assertEquals(1, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getServlets().size());
         TestCase.assertSame(servlet1, this.httpService.getServlets().get(SERVLET_1_ALIAS));
         TestCase.assertTrue(em.getOrphanMappings().isEmpty());
 
         // unregister servlet1
-        em.remove(servlet1Reference);
+        em.removeServlet(servlet1Reference);
         TestCase.assertTrue(em.getMappings().isEmpty());
         TestCase.assertTrue(em.getHttpContexts().get(id).getMappings().isEmpty());
         TestCase.assertEquals(0, this.httpService.getServlets().size());
@@ -320,7 +336,7 @@
         em.add(servlet1, servlet1Reference);
 
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(servlet1, ((ServletMapping) em.getMappings().get(servlet1Reference)).getServlet());
+        TestCase.assertSame(servlet1, getServletMapping(em, servlet1Reference).getServlet());
         TestCase.assertEquals(1, em.getHttpContexts().get(id1).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getServlets().size());
         TestCase.assertSame(servlet1, this.httpService.getServlets().get(SERVLET_1_ALIAS));
@@ -332,7 +348,7 @@
         em.add(servlet2, servlet2Reference);
 
         TestCase.assertEquals(2, em.getMappings().size());
-        TestCase.assertSame(servlet2, ((ServletMapping) em.getMappings().get(servlet2Reference)).getServlet());
+        TestCase.assertSame(servlet2, getServletMapping(em, servlet2Reference).getServlet());
         TestCase.assertEquals(1, em.getHttpContexts().get(id2).getMappings().size());
         TestCase.assertEquals(2, this.httpService.getServlets().size());
         TestCase.assertSame(servlet2, this.httpService.getServlets().get(SERVLET_2_ALIAS));
@@ -346,7 +362,7 @@
         em.add(servlet1_1, servlet1_1Reference);
 
         TestCase.assertEquals(3, em.getMappings().size());
-        TestCase.assertSame(servlet1_1, ((ServletMapping) em.getMappings().get(servlet1_1Reference)).getServlet());
+        TestCase.assertSame(servlet1_1, getServletMapping(em, servlet1_1Reference).getServlet());
         TestCase.assertEquals(2, em.getHttpContexts().get(id1).getMappings().size());
         TestCase.assertEquals(3, this.httpService.getServlets().size());
         TestCase.assertSame(servlet1_1, this.httpService.getServlets().get(SERVLET_1_1_ALIAS));
@@ -374,14 +390,14 @@
 
         // servlet not registered with HttpService yet
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(servlet1, ((ServletMapping) em.getMappings().get(servlet1Reference)).getServlet());
+        TestCase.assertSame(servlet1, getServletMapping(em, servlet1Reference).getServlet());
         TestCase.assertEquals(0, em.getHttpContexts().size());
         TestCase.assertEquals(0, this.httpService.getServlets().size());
         TestCase.assertNull(this.httpService.getServlets().get(SERVLET_1_ALIAS));
         TestCase.assertEquals(1, em.getOrphanMappings().size());
         TestCase.assertEquals(1, em.getOrphanMappings().get(SAMPLE_CONTEXT_ID).size());
         TestCase.assertTrue(em.getOrphanMappings().get(SAMPLE_CONTEXT_ID)
-            .contains(em.getMappings().get(servlet1Reference)));
+            .contains(getServletMapping(em, servlet1Reference)));
 
         // set up a context with context ID and not shared
         when(httpContextReference.getProperty(HttpWhiteboardConstants.CONTEXT_ID)).thenReturn(SAMPLE_CONTEXT_ID);
@@ -390,7 +406,7 @@
 
         // servlet registered with HttpService
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(servlet1, ((ServletMapping) em.getMappings().get(servlet1Reference)).getServlet());
+        TestCase.assertSame(servlet1, getServletMapping(em, servlet1Reference).getServlet());
         TestCase.assertEquals(1, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getServlets().size());
         TestCase.assertSame(servlet1, this.httpService.getServlets().get(SERVLET_1_ALIAS));
@@ -400,17 +416,17 @@
         // unregister context
         em.remove(sampleContext);
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(servlet1, ((ServletMapping) em.getMappings().get(servlet1Reference)).getServlet());
+        TestCase.assertSame(servlet1, getServletMapping(em, servlet1Reference).getServlet());
         TestCase.assertEquals(0, em.getHttpContexts().size());
         TestCase.assertEquals(0, this.httpService.getServlets().size());
         TestCase.assertNull(this.httpService.getServlets().get(SERVLET_1_ALIAS));
         TestCase.assertEquals(1, em.getOrphanMappings().size());
         TestCase.assertEquals(1, em.getOrphanMappings().get(SAMPLE_CONTEXT_ID).size());
         TestCase.assertTrue(em.getOrphanMappings().get(SAMPLE_CONTEXT_ID)
-            .contains(em.getMappings().get(servlet1Reference)));
+            .contains(getServletMapping(em, servlet1Reference)));
 
         // unregister servlet1
-        em.remove(servlet1Reference);
+        em.removeServlet(servlet1Reference);
         TestCase.assertTrue(em.getMappings().isEmpty());
         TestCase.assertTrue(em.getHttpContexts().isEmpty());
         TestCase.assertEquals(0, this.httpService.getServlets().size());
@@ -434,14 +450,14 @@
 
         // servlet not registered with HttpService yet
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(servlet1, ((ServletMapping) em.getMappings().get(servlet1Reference)).getServlet());
+        TestCase.assertSame(servlet1, getServletMapping(em, servlet1Reference).getServlet());
         TestCase.assertEquals(0, em.getHttpContexts().size());
         TestCase.assertEquals(0, this.httpService.getServlets().size());
         TestCase.assertNull(this.httpService.getServlets().get(SERVLET_1_ALIAS));
         TestCase.assertEquals(1, em.getOrphanMappings().size());
         TestCase.assertEquals(1, em.getOrphanMappings().get(SAMPLE_CONTEXT_ID).size());
         TestCase.assertTrue(em.getOrphanMappings().get(SAMPLE_CONTEXT_ID)
-            .contains(em.getMappings().get(servlet1Reference)));
+            .contains(getServletMapping(em, servlet1Reference)));
 
         // set up a context with context ID and not shared
         when(httpContextReference.getProperty(HttpWhiteboardConstants.CONTEXT_ID)).thenReturn(SAMPLE_CONTEXT_ID);
@@ -451,7 +467,7 @@
 
         // servlet registered with HttpService
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(servlet1, ((ServletMapping) em.getMappings().get(servlet1Reference)).getServlet());
+        TestCase.assertSame(servlet1, getServletMapping(em, servlet1Reference).getServlet());
         TestCase.assertEquals(1, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getServlets().size());
         TestCase.assertSame(servlet1, this.httpService.getServlets().get(SERVLET_1_ALIAS));
@@ -461,17 +477,17 @@
         // unregister context
         em.remove(sampleContext);
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(servlet1, ((ServletMapping) em.getMappings().get(servlet1Reference)).getServlet());
+        TestCase.assertSame(servlet1, getServletMapping(em, servlet1Reference).getServlet());
         TestCase.assertEquals(0, em.getHttpContexts().size());
         TestCase.assertEquals(0, this.httpService.getServlets().size());
         TestCase.assertNull(this.httpService.getServlets().get(SERVLET_1_ALIAS));
         TestCase.assertEquals(1, em.getOrphanMappings().size());
         TestCase.assertEquals(1, em.getOrphanMappings().get(SAMPLE_CONTEXT_ID).size());
         TestCase.assertTrue(em.getOrphanMappings().get(SAMPLE_CONTEXT_ID)
-            .contains(em.getMappings().get(servlet1Reference)));
+            .contains(getServletMapping(em, servlet1Reference)));
 
         // unregister servlet1
-        em.remove(servlet1Reference);
+        em.removeServlet(servlet1Reference);
         TestCase.assertTrue(em.getMappings().isEmpty());
         TestCase.assertTrue(em.getHttpContexts().isEmpty());
         TestCase.assertEquals(0, this.httpService.getServlets().size());
@@ -503,7 +519,7 @@
         em.add(filter1, filter1Reference);
 
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(filter1, ((FilterMapping) em.getMappings().get(filter1Reference)).getFilter());
+        TestCase.assertSame(filter1, getFilterMapping(em, filter1Reference).getFilter());
         TestCase.assertEquals(1, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getFilters().size());
         TestCase.assertSame(filter1, this.httpService.getFilters().get(SERVLET_1_ALIAS));
@@ -514,27 +530,27 @@
         em.add(filter2, filter2Reference);
 
         TestCase.assertEquals(2, em.getMappings().size());
-        TestCase.assertSame(filter2, ((FilterMapping) em.getMappings().get(filter2Reference)).getFilter());
+        TestCase.assertSame(filter2, getFilterMapping(em, filter2Reference).getFilter());
         TestCase.assertEquals(1, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getFilters().size());
         TestCase.assertNull(this.httpService.getFilters().get(SERVLET_2_ALIAS));
         TestCase.assertEquals(1, em.getOrphanMappings().size());
         TestCase.assertEquals(1, em.getOrphanMappings().get(SAMPLE_CONTEXT_ID).size());
         TestCase.assertTrue(em.getOrphanMappings().get(SAMPLE_CONTEXT_ID)
-            .contains(em.getMappings().get(filter2Reference)));
+            .contains(getFilterMapping(em, filter2Reference)));
 
         // unregister filter2
-        em.remove(filter2Reference);
+        em.removeFilter(filter2Reference);
 
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(filter1, ((FilterMapping) em.getMappings().get(filter1Reference)).getFilter());
+        TestCase.assertSame(filter1, getFilterMapping(em, filter1Reference).getFilter());
         TestCase.assertEquals(1, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getFilters().size());
         TestCase.assertSame(filter1, this.httpService.getFilters().get(SERVLET_1_ALIAS));
         TestCase.assertTrue(em.getOrphanMappings().isEmpty());
 
         // unregister filter1
-        em.remove(filter1Reference);
+        em.removeFilter(filter1Reference);
         TestCase.assertTrue(em.getMappings().isEmpty());
         TestCase.assertTrue(em.getHttpContexts().get(id).getMappings().isEmpty());
         TestCase.assertEquals(0, this.httpService.getFilters().size());
@@ -569,7 +585,7 @@
         em.add(filter1, filter1Reference);
 
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(filter1, ((FilterMapping) em.getMappings().get(filter1Reference)).getFilter());
+        TestCase.assertSame(filter1, getFilterMapping(em, filter1Reference).getFilter());
         TestCase.assertEquals(1, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getFilters().size());
         TestCase.assertSame(filter1, this.httpService.getFilters().get(SERVLET_1_ALIAS));
@@ -580,7 +596,7 @@
         em.add(filter2, filter2Reference);
 
         TestCase.assertEquals(2, em.getMappings().size());
-        TestCase.assertSame(filter2, ((FilterMapping) em.getMappings().get(filter2Reference)).getFilter());
+        TestCase.assertSame(filter2, getFilterMapping(em, filter2Reference).getFilter());
         TestCase.assertEquals(2, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(2, this.httpService.getFilters().size());
         TestCase.assertSame(filter2, this.httpService.getFilters().get(SERVLET_2_ALIAS));
@@ -588,17 +604,17 @@
         TestCase.assertSame(sampleContext, filter2.getHttpContext());
 
         // unregister filter2
-        em.remove(filter2Reference);
+        em.removeFilter(filter2Reference);
 
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(filter1, ((FilterMapping) em.getMappings().get(filter1Reference)).getFilter());
+        TestCase.assertSame(filter1, getFilterMapping(em, filter1Reference).getFilter());
         TestCase.assertEquals(1, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getFilters().size());
         TestCase.assertSame(filter1, this.httpService.getFilters().get(SERVLET_1_ALIAS));
         TestCase.assertTrue(em.getOrphanMappings().isEmpty());
 
         // unregister filter1
-        em.remove(filter1Reference);
+        em.removeFilter(filter1Reference);
         TestCase.assertTrue(em.getMappings().isEmpty());
         TestCase.assertTrue(em.getHttpContexts().get(id).getMappings().isEmpty());
         TestCase.assertEquals(0, this.httpService.getFilters().size());
@@ -628,7 +644,7 @@
         em.add(filter1, filter1Reference);
 
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(filter1, ((FilterMapping) em.getMappings().get(filter1Reference)).getFilter());
+        TestCase.assertSame(filter1, getFilterMapping(em, filter1Reference).getFilter());
         TestCase.assertEquals(1, em.getHttpContexts().get(id1).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getFilters().size());
         TestCase.assertSame(filter1, this.httpService.getFilters().get(SERVLET_1_ALIAS));
@@ -640,7 +656,7 @@
         em.add(filter2, filter2Reference);
 
         TestCase.assertEquals(2, em.getMappings().size());
-        TestCase.assertSame(filter2, ((FilterMapping) em.getMappings().get(filter2Reference)).getFilter());
+        TestCase.assertSame(filter2, getFilterMapping(em, filter2Reference).getFilter());
         TestCase.assertEquals(1, em.getHttpContexts().get(id2).getMappings().size());
         TestCase.assertEquals(2, this.httpService.getFilters().size());
         TestCase.assertSame(filter2, this.httpService.getFilters().get(SERVLET_2_ALIAS));
@@ -654,7 +670,7 @@
         em.add(filter1_1, filter1_1Reference);
 
         TestCase.assertEquals(3, em.getMappings().size());
-        TestCase.assertSame(filter1_1, ((FilterMapping) em.getMappings().get(filter1_1Reference)).getFilter());
+        TestCase.assertSame(filter1_1, getFilterMapping(em, filter1_1Reference).getFilter());
         TestCase.assertEquals(2, em.getHttpContexts().get(id1).getMappings().size());
         TestCase.assertEquals(3, this.httpService.getFilters().size());
         TestCase.assertSame(filter1_1, this.httpService.getFilters().get(SERVLET_1_1_ALIAS));
@@ -681,14 +697,14 @@
 
         // servlet not registered with HttpService yet
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(filter1, ((FilterMapping) em.getMappings().get(filter1Reference)).getFilter());
+        TestCase.assertSame(filter1, getFilterMapping(em, filter1Reference).getFilter());
         TestCase.assertEquals(0, em.getHttpContexts().size());
         TestCase.assertEquals(0, this.httpService.getFilters().size());
         TestCase.assertNull(this.httpService.getFilters().get(SERVLET_1_ALIAS));
         TestCase.assertEquals(1, em.getOrphanMappings().size());
         TestCase.assertEquals(1, em.getOrphanMappings().get(SAMPLE_CONTEXT_ID).size());
         TestCase.assertTrue(em.getOrphanMappings().get(SAMPLE_CONTEXT_ID)
-            .contains(em.getMappings().get(filter1Reference)));
+            .contains(getFilterMapping(em, filter1Reference)));
 
         // set up a context with context ID and not shared
         when(httpContextReference.getProperty(HttpWhiteboardConstants.CONTEXT_ID)).thenReturn(SAMPLE_CONTEXT_ID);
@@ -697,7 +713,7 @@
 
         // servlet registered with HttpService
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(filter1, ((FilterMapping) em.getMappings().get(filter1Reference)).getFilter());
+        TestCase.assertSame(filter1, getFilterMapping(em, filter1Reference).getFilter());
         TestCase.assertEquals(1, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getFilters().size());
         TestCase.assertSame(filter1, this.httpService.getFilters().get(SERVLET_1_ALIAS));
@@ -707,17 +723,17 @@
         // unregister context
         em.remove(sampleContext);
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(filter1, ((FilterMapping) em.getMappings().get(filter1Reference)).getFilter());
+        TestCase.assertSame(filter1, getFilterMapping(em, filter1Reference).getFilter());
         TestCase.assertEquals(0, em.getHttpContexts().size());
         TestCase.assertEquals(0, this.httpService.getFilters().size());
         TestCase.assertNull(this.httpService.getFilters().get(SERVLET_1_ALIAS));
         TestCase.assertEquals(1, em.getOrphanMappings().size());
         TestCase.assertEquals(1, em.getOrphanMappings().get(SAMPLE_CONTEXT_ID).size());
         TestCase.assertTrue(em.getOrphanMappings().get(SAMPLE_CONTEXT_ID)
-            .contains(em.getMappings().get(filter1Reference)));
+            .contains(getFilterMapping(em, filter1Reference)));
 
         // unregister filter1
-        em.remove(filter1Reference);
+        em.removeFilter(filter1Reference);
         TestCase.assertTrue(em.getMappings().isEmpty());
         TestCase.assertTrue(em.getHttpContexts().isEmpty());
         TestCase.assertEquals(0, this.httpService.getFilters().size());
@@ -740,14 +756,14 @@
 
         // servlet not registered with HttpService yet
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(filter1, ((FilterMapping) em.getMappings().get(filter1Reference)).getFilter());
+        TestCase.assertSame(filter1, getFilterMapping(em, filter1Reference).getFilter());
         TestCase.assertEquals(0, em.getHttpContexts().size());
         TestCase.assertEquals(0, this.httpService.getFilters().size());
         TestCase.assertNull(this.httpService.getFilters().get(SERVLET_1_ALIAS));
         TestCase.assertEquals(1, em.getOrphanMappings().size());
         TestCase.assertEquals(1, em.getOrphanMappings().get(SAMPLE_CONTEXT_ID).size());
         TestCase.assertTrue(em.getOrphanMappings().get(SAMPLE_CONTEXT_ID)
-            .contains(em.getMappings().get(filter1Reference)));
+            .contains(getFilterMapping(em, filter1Reference)));
 
         // set up a context with context ID and not shared
         when(httpContextReference.getProperty(HttpWhiteboardConstants.CONTEXT_ID)).thenReturn(SAMPLE_CONTEXT_ID);
@@ -757,7 +773,7 @@
 
         // servlet registered with HttpService
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(filter1, ((FilterMapping) em.getMappings().get(filter1Reference)).getFilter());
+        TestCase.assertSame(filter1, getFilterMapping(em, filter1Reference).getFilter());
         TestCase.assertEquals(1, em.getHttpContexts().get(id).getMappings().size());
         TestCase.assertEquals(1, this.httpService.getFilters().size());
         TestCase.assertSame(filter1, this.httpService.getFilters().get(SERVLET_1_ALIAS));
@@ -767,23 +783,66 @@
         // unregister context
         em.remove(sampleContext);
         TestCase.assertEquals(1, em.getMappings().size());
-        TestCase.assertSame(filter1, ((FilterMapping) em.getMappings().get(filter1Reference)).getFilter());
+        TestCase.assertSame(filter1, getFilterMapping(em, filter1Reference).getFilter());
         TestCase.assertEquals(0, em.getHttpContexts().size());
         TestCase.assertEquals(0, this.httpService.getFilters().size());
         TestCase.assertNull(this.httpService.getFilters().get(SERVLET_1_ALIAS));
         TestCase.assertEquals(1, em.getOrphanMappings().size());
         TestCase.assertEquals(1, em.getOrphanMappings().get(SAMPLE_CONTEXT_ID).size());
         TestCase.assertTrue(em.getOrphanMappings().get(SAMPLE_CONTEXT_ID)
-            .contains(em.getMappings().get(filter1Reference)));
+            .contains(getFilterMapping(em, filter1Reference)));
 
         // unregister filter1
-        em.remove(filter1Reference);
+        em.removeFilter(filter1Reference);
         TestCase.assertTrue(em.getMappings().isEmpty());
         TestCase.assertTrue(em.getHttpContexts().isEmpty());
         TestCase.assertEquals(0, this.httpService.getFilters().size());
         TestCase.assertTrue(em.getOrphanMappings().isEmpty());
     }
 
+    @Test
+    public void test_filter_and_servlet_in_one_service()
+    {
+        ExtenderManager em = new ExtenderManager();
+
+        // prepare with http service
+        em.setHttpService(this.httpService);
+        TestCase.assertTrue(this.httpService.getServlets().isEmpty());
+        TestCase.assertTrue(this.httpService.getFilters().isEmpty());
+
+        // register filter1 from bundle1
+        em.add(filter1, filterAndServletReference);
+        em.add(servlet1, filterAndServletReference);
+
+        TestCase.assertEquals(2, em.getMappings().size());
+        TestCase.assertEquals(1, this.httpService.getServlets().size());
+        TestCase.assertEquals(1, this.httpService.getFilters().size());
+        TestCase.assertSame(filter1, getFilterMapping(em, filterAndServletReference).getFilter());
+        TestCase.assertSame(servlet1, getServletMapping(em, filterAndServletReference).getServlet());
+
+        em.removeFilter(filterAndServletReference);
+        TestCase.assertEquals(1, em.getMappings().size());
+        TestCase.assertEquals(1, this.httpService.getServlets().size());
+        TestCase.assertEquals(0, this.httpService.getFilters().size());
+        TestCase.assertNull(getFilterMapping(em, filterAndServletReference));
+        TestCase.assertSame(servlet1, getServletMapping(em, filterAndServletReference).getServlet());
+
+        em.removeServlet(filterAndServletReference);
+        TestCase.assertEquals(0, em.getMappings().size());
+        TestCase.assertEquals(0, this.httpService.getServlets().size());
+        TestCase.assertEquals(0, this.httpService.getFilters().size());
+        TestCase.assertNull(getFilterMapping(em, filterAndServletReference));
+        TestCase.assertNull(getServletMapping(em, filterAndServletReference));
+    }
+
+    private FilterMapping getFilterMapping(final ExtenderManager em, final ServiceReference ref) {
+        return (FilterMapping) em.getMappings().get(ref.getProperty(Constants.SERVICE_ID).toString() + ExtenderManager.TYPE_FILTER);
+    }
+
+    private ServletMapping getServletMapping(final ExtenderManager em, final ServiceReference ref) {
+        return (ServletMapping) em.getMappings().get(ref.getProperty(Constants.SERVICE_ID).toString() + ExtenderManager.TYPE_SERVLET);
+    }
+
     static interface ExtFilter extends Filter
     {
         HttpContext getHttpContext();