felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1615050 - in /felix/sandbox/http-rfc189/whiteboard/src: main/java/org/apache/felix/http/whiteboard/internal/ main/java/org/apache/felix/http/whiteboard/internal/manager/ main/java/org/apache/felix/http/whiteboard/internal/tracker/ test/jav...
Date Fri, 01 Aug 2014 08:10:41 GMT
Author: cziegeler
Date: Fri Aug  1 08:10:40 2014
New Revision: 1615050

URL: http://svn.apache.org/r1615050
Log:
FELIX-4545 : Implement Servlet Context Helper. Apply patch from Dominique Pfister

Added:
    felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextBridge.java
  (with props)
    felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/ServletContextHelperTracker.java
  (with props)
Modified:
    felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/WhiteboardActivator.java
    felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManager.java
    felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextManager.java
    felix/sandbox/http-rfc189/whiteboard/src/test/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManagerTest.java
    felix/sandbox/http-rfc189/whiteboard/src/test/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextManagerTest.java

Modified: felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/WhiteboardActivator.java
URL: http://svn.apache.org/viewvc/felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/WhiteboardActivator.java?rev=1615050&r1=1615049&r2=1615050&view=diff
==============================================================================
--- felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/WhiteboardActivator.java
(original)
+++ felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/WhiteboardActivator.java
Fri Aug  1 08:10:40 2014
@@ -26,6 +26,7 @@ import org.apache.felix.http.whiteboard.
 import org.apache.felix.http.whiteboard.internal.tracker.FilterTracker;
 import org.apache.felix.http.whiteboard.internal.tracker.HttpContextTracker;
 import org.apache.felix.http.whiteboard.internal.tracker.HttpServiceTracker;
+import org.apache.felix.http.whiteboard.internal.tracker.ServletContextHelperTracker;
 import org.apache.felix.http.whiteboard.internal.tracker.ServletTracker;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
@@ -49,6 +50,7 @@ public final class WhiteboardActivator e
         addTracker(new FilterTracker(getBundleContext(), this.manager));
         addTracker(new ServletTracker(getBundleContext(), this.manager));
         addTracker(new HttpServiceTracker(getBundleContext(), this.manager));
+        addTracker(new ServletContextHelperTracker(getBundleContext(), this.manager));
 
         HttpWhiteboardWebConsolePlugin plugin = new HttpWhiteboardWebConsolePlugin(this.manager);
         Hashtable<String, Object> props = new Hashtable<String, Object>();

Modified: felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManager.java
URL: http://svn.apache.org/viewvc/felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManager.java?rev=1615050&r1=1615049&r2=1615050&view=diff
==============================================================================
--- felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManager.java
(original)
+++ felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManager.java
Fri Aug  1 08:10:40 2014
@@ -37,6 +37,7 @@ import org.osgi.framework.ServiceReferen
 import org.osgi.service.http.HttpContext;
 import org.osgi.service.http.HttpService;
 import org.osgi.service.http.NamespaceException;
+import org.osgi.service.http.context.ServletContextHelper;
 import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
 
 @SuppressWarnings({ "deprecation" })
@@ -194,9 +195,45 @@ public final class ExtenderManager
         }
     }
 
+    public void add(ServletContextHelper service, ServiceReference ref) throws ServletException,
NamespaceException
+    {
+        String name = getStringProperty(ref, HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME);
+        String path = getStringProperty(ref, HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH);
+
+        if (!isEmpty(name))
+        {
+            Collection<AbstractMapping> mappings = this.contextManager.addContextHelper(ref.getBundle(),
name, path, service);
+            for (AbstractMapping mapping : mappings)
+            {
+                registerMapping(mapping);
+            }
+        }
+        else
+        {
+            SystemLogger.debug("Ignoring ServletContextHelper Service " + ref + ", " + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME
+ " is missing or empty");
+        }
+    }
+
+    public void remove(ServletContextHelper service) throws ServletException
+    {
+        Collection<AbstractMapping> mappings = this.contextManager.removeContextHelper(service);
+        if (mappings != null)
+        {
+            for (AbstractMapping mapping : mappings)
+            {
+                unregisterMapping(mapping);
+            }
+        }
+    }
+
     private HttpContext getHttpContext(AbstractMapping mapping, ServiceReference ref)
     {
         Bundle bundle = ref.getBundle();
+        String contextName = getStringProperty(ref, HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT);
+        if (!isEmpty(contextName))
+        {
+            return this.contextManager.getHttpContext(bundle, contextName, mapping, true);
+        }
         String contextId = getStringProperty(ref, org.apache.felix.http.whiteboard.HttpWhiteboardConstants.CONTEXT_ID);
         return this.contextManager.getHttpContext(bundle, contextId, mapping);
     }
@@ -204,6 +241,12 @@ public final class ExtenderManager
     private void ungetHttpContext(AbstractMapping mapping, ServiceReference ref)
     {
         Bundle bundle = ref.getBundle();
+        String contextName = getStringProperty(ref, HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT);
+        if (!isEmpty(contextName))
+        {
+            this.contextManager.ungetHttpContext(bundle, contextName, mapping, true);
+            return;
+        }
         String contextId = getStringProperty(ref, org.apache.felix.http.whiteboard.HttpWhiteboardConstants.CONTEXT_ID);
         this.contextManager.ungetHttpContext(bundle, contextId, mapping);
     }

Added: felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextBridge.java
URL: http://svn.apache.org/viewvc/felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextBridge.java?rev=1615050&view=auto
==============================================================================
--- felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextBridge.java
(added)
+++ felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextBridge.java
Fri Aug  1 08:10:40 2014
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.http.whiteboard.internal.manager;
+
+import org.osgi.service.http.HttpContext;
+import org.osgi.service.http.context.ServletContextHelper;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Set;
+
+public class HttpContextBridge extends ServletContextHelper implements HttpContext {
+
+    private final ServletContextHelper delegatee;
+
+    HttpContextBridge(final ServletContextHelper delegatee)
+    {
+        this.delegatee = delegatee;
+    }
+
+    @Override
+    public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response)
throws IOException
+    {
+        return delegatee.handleSecurity(request, response);
+    }
+
+    @Override
+    public URL getResource(String name)
+    {
+        return delegatee.getResource(name);
+    }
+
+    @Override
+    public String getMimeType(String name)
+    {
+        return delegatee.getMimeType(name);
+    }
+
+    @Override
+    public Set<String> getResourcePaths(String path)
+    {
+        return delegatee.getResourcePaths(path);
+    }
+
+    @Override
+    public String getRealPath(String path)
+    {
+        return delegatee.getRealPath(path);
+    }
+
+    public ServletContextHelper getDelegatee()
+    {
+        return delegatee;
+    }
+}

Propchange: felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextBridge.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextBridge.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextBridge.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextManager.java
URL: http://svn.apache.org/viewvc/felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextManager.java?rev=1615050&r1=1615049&r2=1615050&view=diff
==============================================================================
--- felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextManager.java
(original)
+++ felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextManager.java
Fri Aug  1 08:10:40 2014
@@ -26,6 +26,7 @@ import java.util.Set;
 import org.apache.felix.http.base.internal.logger.SystemLogger;
 import org.osgi.framework.Bundle;
 import org.osgi.service.http.HttpContext;
+import org.osgi.service.http.context.ServletContextHelper;
 
 public final class HttpContextManager
 {
@@ -41,12 +42,18 @@ public final class HttpContextManager
     private final HashMap<String, HttpContextHolder> idMap;
 
     /**
-     * Reverse mapping of HttpContext services to the context ID with
-     * which they are registered.
+     * Reverse mapping of HttpContext services to the context ID or
+     * name with which they are registered.
      */
     private final HashMap<HttpContext, String> contextMap;
 
     /**
+     * Mapping of registered ServletContextHelper services to the
+     * HttpContext stored in <code>contextMap</code>.
+     */
+    private final HashMap<ServletContextHelper, HttpContext> helperMap;
+
+    /**
      * Map of servlets and filters registered referring to unregistered
      * contexts as of yet.
      */
@@ -56,11 +63,16 @@ public final class HttpContextManager
     {
         this.idMap = new HashMap<String, HttpContextHolder>();
         this.contextMap = new HashMap<HttpContext, String>();
+        this.helperMap = new HashMap<ServletContextHelper, HttpContext>();
         this.orphanMappings = new HashMap<String, Set<AbstractMapping>>();
     }
 
-    private static String createId(Bundle bundle, String contextId)
+    private static String createId(Bundle bundle, String contextId, boolean isContextHelper)
     {
+        if (isContextHelper)
+        {
+            return "servletcontexthelper-" + ((contextId == null) ? "" : contextId);
+        }
         if (bundle != null)
         {
             return bundle.getBundleId() + "-" + ((contextId == null) ? "" : contextId);
@@ -82,8 +94,14 @@ public final class HttpContextManager
 
     public synchronized HttpContext getHttpContext(Bundle bundle, String contextId, AbstractMapping
mapping)
     {
+        return getHttpContext(bundle, contextId, mapping, false);
+    }
+
+    public synchronized HttpContext getHttpContext(Bundle bundle, String contextId, AbstractMapping
mapping,
+                                                   boolean isContextHelper)
+    {
         // per-bundle context
-        String id = createId(bundle, contextId);
+        String id = createId(bundle, contextId, isContextHelper);
         HttpContextHolder holder = this.idMap.get(id);
 
         // shared context
@@ -132,12 +150,18 @@ public final class HttpContextManager
 
     public synchronized void ungetHttpContext(Bundle bundle, String contextId, AbstractMapping
mapping)
     {
+        ungetHttpContext(bundle, contextId, mapping, false);
+    }
+
+    public synchronized void ungetHttpContext(Bundle bundle, String contextId,
+                                              AbstractMapping mapping, boolean isContextHelper)
+    {
         // per-bundle context
-        String id = createId(bundle, contextId);
+        String id = createId(bundle, contextId, isContextHelper);
         HttpContextHolder context = this.idMap.get(id);
 
         // shared context
-        if (context == null)
+        if (context == null && !isContextHelper)
         {
             id = createId(contextId);
             context = this.idMap.get(id);
@@ -167,7 +191,7 @@ public final class HttpContextManager
 
     public synchronized Collection<AbstractMapping> addHttpContext(Bundle bundle, String
contextId, HttpContext context)
     {
-        String id = createId(bundle, contextId);
+        String id = createId(bundle, contextId, false);
         HttpContextHolder holder = new HttpContextHolder(context);
 
         Set<AbstractMapping> orphans = this.orphanMappings.remove(contextId);
@@ -196,6 +220,39 @@ public final class HttpContextManager
         return holder.getMappings();
     }
 
+    public synchronized Collection<AbstractMapping> addContextHelper(Bundle bundle,
String contextName, String contextPath,
+                                                                     ServletContextHelper
contextHelper)
+    {
+        String id = createId(bundle, contextName, true);
+        HttpContextHolder holder = new HttpContextHolder(contextHelper, contextPath);
+
+        Set<AbstractMapping> orphans = this.orphanMappings.remove(contextName);
+        if (orphans != null)
+        {
+            for (Iterator<AbstractMapping> mi = orphans.iterator(); mi.hasNext();)
+            {
+                AbstractMapping mapping = mi.next();
+                if (bundle == null || bundle.equals(mapping.getBundle()))
+                {
+                    holder.addMapping(mapping);
+                    mi.remove();
+                }
+            }
+
+            // put any remaining orphans back
+            if (!orphans.isEmpty())
+            {
+                this.orphanMappings.put(contextName, orphans);
+            }
+        }
+
+        this.idMap.put(id, holder);
+        this.contextMap.put(holder.getContext(), id);
+        this.helperMap.put(contextHelper, holder.getContext());
+
+        return holder.getMappings();
+    }
+
     public synchronized Collection<AbstractMapping> removeHttpContext(HttpContext context)
     {
         String id = this.contextMap.remove(context);
@@ -228,6 +285,16 @@ public final class HttpContextManager
         return null;
     }
 
+    public synchronized Collection<AbstractMapping> removeContextHelper(ServletContextHelper
contextHelper)
+    {
+        HttpContext context = this.helperMap.remove(contextHelper);
+        if (context != null)
+        {
+            return removeHttpContext(context);
+        }
+        return null;
+    }
+
     synchronized Map<String, HttpContextHolder> getHttpContexts()
     {
         return new HashMap<String, HttpContextHolder>(this.idMap);
@@ -242,11 +309,21 @@ public final class HttpContextManager
     {
         private final HttpContext context;
         private final Set<AbstractMapping> mappings;
+        private final String path;
 
         HttpContextHolder(final HttpContext context)
         {
             this.context = context;
             this.mappings = new HashSet<AbstractMapping>();
+            this.path = null;
+        }
+
+        HttpContextHolder(final ServletContextHelper context,
+                          final String path)
+        {
+            this.context = new HttpContextBridge(context);
+            this.mappings = new HashSet<AbstractMapping>();
+            this.path = path;
         }
 
         public HttpContext getContext()
@@ -254,6 +331,11 @@ public final class HttpContextManager
             return context;
         }
 
+        public String getPath()
+        {
+            return path;
+        }
+
         void addMapping(AbstractMapping mapping)
         {
             this.mappings.add(mapping);

Added: felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/ServletContextHelperTracker.java
URL: http://svn.apache.org/viewvc/felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/ServletContextHelperTracker.java?rev=1615050&view=auto
==============================================================================
--- felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/ServletContextHelperTracker.java
(added)
+++ felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/ServletContextHelperTracker.java
Fri Aug  1 08:10:40 2014
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.http.whiteboard.internal.tracker;
+
+import org.apache.felix.http.whiteboard.internal.manager.ExtenderManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.http.NamespaceException;
+import org.osgi.service.http.context.ServletContextHelper;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+
+public final class ServletContextHelperTracker extends AbstractTracker<ServletContextHelper>
+{
+    private final ExtenderManager manager;
+
+    public ServletContextHelperTracker(BundleContext context, ExtenderManager manager)
+    {
+        super(context, ServletContextHelper.class);
+        this.manager = manager;
+    }
+
+    protected void added(ServletContextHelper service, ServiceReference ref) throws ServletException,
NamespaceException
+    {
+        this.manager.add(service, ref);
+    }
+
+    protected void modified(ServletContextHelper service, ServiceReference ref) throws ServletException,
NamespaceException
+    {
+        removed(service, ref);
+        added(service, ref);
+    }
+
+    protected void removed(ServletContextHelper service, ServiceReference ref) throws ServletException
+    {
+        this.manager.remove(service);
+    }
+}

Propchange: felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/ServletContextHelperTracker.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/ServletContextHelperTracker.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: felix/sandbox/http-rfc189/whiteboard/src/main/java/org/apache/felix/http/whiteboard/internal/tracker/ServletContextHelperTracker.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: felix/sandbox/http-rfc189/whiteboard/src/test/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManagerTest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/http-rfc189/whiteboard/src/test/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManagerTest.java?rev=1615050&r1=1615049&r2=1615050&view=diff
==============================================================================
--- felix/sandbox/http-rfc189/whiteboard/src/test/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManagerTest.java
(original)
+++ felix/sandbox/http-rfc189/whiteboard/src/test/java/org/apache/felix/http/whiteboard/internal/manager/ExtenderManagerTest.java
Fri Aug  1 08:10:40 2014
@@ -25,6 +25,9 @@ import static org.junit.Assert.assertNul
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.when;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT;
 
 import java.util.Dictionary;
 
@@ -50,6 +53,7 @@ import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.http.HttpContext;
 import org.osgi.service.http.NamespaceException;
+import org.osgi.service.http.context.ServletContextHelper;
 import org.osgi.service.http.runtime.dto.FilterDTO;
 import org.osgi.service.http.runtime.dto.RequestInfoDTO;
 import org.osgi.service.http.runtime.dto.RuntimeDTO;
@@ -60,17 +64,22 @@ import org.osgi.service.http.runtime.dto
 public class ExtenderManagerTest
 {
     private static final String SAMPLE_CONTEXT_ID = "some.context.id";
+    private static final String SAMPLE_CONTEXT_NAME = "some.context.name";
+    private static final String SAMPLE_CONTEXT_PATH = "/some/context/path";
     private static final long BUNDLE_1_ID = 1L;
     private static final long BUNDLE_2_ID = 2L;
     private static final String SERVLET_1_ALIAS = "/servet1";
     private static final String SERVLET_1_1_ALIAS = "/servet1_1";
     private static final String SERVLET_2_ALIAS = "/servet2";
+    private static final String SERVLET_3_ALIAS = "/servlet3";
 
     private MockExtHttpService httpService;
 
     @Mock
     private HttpContext sampleContext;
     @Mock
+    private ServletContextHelper sampleContextHelper;
+    @Mock
     private Bundle bundle1;
     @Mock
     private Bundle bundle2;
@@ -81,6 +90,8 @@ public class ExtenderManagerTest
     @Mock
     private ExtServlet servlet2;
     @Mock
+    private ExtServlet servlet3;
+    @Mock
     private ExtFilter filter1;
     @Mock
     private ExtFilter filter1_1;
@@ -93,6 +104,8 @@ public class ExtenderManagerTest
     @Mock
     private ServiceReference servlet2Reference;
     @Mock
+    private ServiceReference servlet3Reference;
+    @Mock
     private ServiceReference filter1Reference;
     @Mock
     private ServiceReference filter1_1Reference;
@@ -100,6 +113,8 @@ public class ExtenderManagerTest
     private ServiceReference filter2Reference;
     @Mock
     private ServiceReference httpContextReference;
+    @Mock
+    private ServiceReference contextHelperReference;
 
     @Before
     public void setup()
@@ -107,6 +122,7 @@ public class ExtenderManagerTest
         when(bundle1.getBundleId()).thenReturn(BUNDLE_1_ID);
         when(bundle2.getBundleId()).thenReturn(BUNDLE_2_ID);
         when(httpContextReference.getBundle()).thenReturn(bundle1);
+        when(contextHelperReference.getBundle()).thenReturn(bundle2);
 
         when(servlet1Reference.getBundle()).thenReturn(bundle1);
         when(servlet1Reference.getPropertyKeys()).thenReturn(new String[0]);
@@ -140,6 +156,12 @@ public class ExtenderManagerTest
         when(filter2Reference.getProperty(Constants.SERVICE_ID)).thenReturn(Long.valueOf(6));
         when(filter2Reference.getProperty(HttpWhiteboardConstants.CONTEXT_ID)).thenReturn(SAMPLE_CONTEXT_ID);
 
+        when(servlet3Reference.getBundle()).thenReturn(bundle2);
+        when(servlet3Reference.getPropertyKeys()).thenReturn(new String[0]);
+        when(servlet3Reference.getProperty(Constants.SERVICE_ID)).thenReturn(Long.valueOf(7));
+        when(servlet3Reference.getProperty(HttpWhiteboardConstants.ALIAS)).thenReturn(SERVLET_3_ALIAS);
+        when(servlet3Reference.getProperty(HTTP_WHITEBOARD_CONTEXT_SELECT)).thenReturn(SAMPLE_CONTEXT_NAME);
+
         this.httpService = new MockExtHttpService();
     }
 
@@ -414,6 +436,66 @@ public class ExtenderManagerTest
     }
 
     @Test
+    public void test_servlet_before_context_helper() throws Exception
+    {
+        ExtenderManager em = new ExtenderManager();
+        final String id = HttpContextManagerTest.createId(bundle2, SAMPLE_CONTEXT_NAME, true);
+
+        // prepare with http service
+        em.setHttpService(this.httpService);
+        assertTrue(this.httpService.getServlets().isEmpty());
+        assertTrue(this.httpService.getFilters().isEmpty());
+
+        // register servlet3 from bundle2
+        em.add(servlet3, servlet3Reference);
+
+        // servlet not registered with HttpService yet
+        assertEquals(1, em.getMappings().size());
+        assertSame(servlet3, ((ServletMapping) em.getMappings().get(servlet3Reference)).getServlet());
+        assertEquals(0, em.getHttpContexts().size());
+        assertEquals(0, this.httpService.getServlets().size());
+        assertNull(this.httpService.getServlets().get(SERVLET_3_ALIAS));
+        assertEquals(1, em.getOrphanMappings().size());
+        assertEquals(1, em.getOrphanMappings().get(SAMPLE_CONTEXT_NAME).size());
+        assertTrue(em.getOrphanMappings().get(SAMPLE_CONTEXT_NAME).contains(em.getMappings().get(servlet3Reference)));
+
+        // set up a context helper with name
+        when(contextHelperReference.getProperty(HTTP_WHITEBOARD_CONTEXT_NAME)).thenReturn(SAMPLE_CONTEXT_NAME);
+        em.add(sampleContextHelper, contextHelperReference);
+        assertEquals(1, em.getHttpContexts().size());
+
+        // servlet registered with HttpService
+        assertEquals(1, em.getMappings().size());
+        assertSame(servlet3, ((ServletMapping) em.getMappings().get(servlet3Reference)).getServlet());
+        assertEquals(1, em.getHttpContexts().get(id).getMappings().size());
+        assertEquals(1, this.httpService.getServlets().size());
+        assertSame(servlet3, this.httpService.getServlets().get(SERVLET_3_ALIAS));
+        assertTrue(em.getOrphanMappings().isEmpty());
+
+        final HttpContext context = servlet3.getHttpContext();
+        assertTrue(context instanceof HttpContextBridge);
+        assertSame(sampleContextHelper, ((HttpContextBridge) context).getDelegatee());
+
+        // unregister context helper
+        em.remove(sampleContextHelper);
+        assertEquals(1, em.getMappings().size());
+        assertSame(servlet3, ((ServletMapping) em.getMappings().get(servlet3Reference)).getServlet());
+        assertEquals(0, em.getHttpContexts().size());
+        assertEquals(0, this.httpService.getServlets().size());
+        assertNull(this.httpService.getServlets().get(SERVLET_3_ALIAS));
+        assertEquals(1, em.getOrphanMappings().size());
+        assertEquals(1, em.getOrphanMappings().get(SAMPLE_CONTEXT_NAME).size());
+        assertTrue(em.getOrphanMappings().get(SAMPLE_CONTEXT_NAME).contains(em.getMappings().get(servlet3Reference)));
+
+        // unregister servlet1
+        em.remove(servlet3Reference);
+        assertTrue(em.getMappings().isEmpty());
+        assertTrue(em.getHttpContexts().isEmpty());
+        assertEquals(0, this.httpService.getServlets().size());
+        assertTrue(em.getOrphanMappings().isEmpty());
+    }
+
+    @Test
     public void test_servlet_before_context_shared() throws Exception
     {
         ExtenderManager em = new ExtenderManager();

Modified: felix/sandbox/http-rfc189/whiteboard/src/test/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextManagerTest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/http-rfc189/whiteboard/src/test/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextManagerTest.java?rev=1615050&r1=1615049&r2=1615050&view=diff
==============================================================================
--- felix/sandbox/http-rfc189/whiteboard/src/test/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextManagerTest.java
(original)
+++ felix/sandbox/http-rfc189/whiteboard/src/test/java/org/apache/felix/http/whiteboard/internal/manager/HttpContextManagerTest.java
Fri Aug  1 08:10:40 2014
@@ -325,11 +325,16 @@ public class HttpContextManagerTest
 
     static String createId(Bundle bundle, String contextId)
     {
+        return createId(bundle, contextId, false);
+    }
+
+    static String createId(Bundle bundle, String contextId, boolean isContextHelper)
+    {
         try
         {
-            Method m = HttpContextManager.class.getDeclaredMethod("createId", Bundle.class,
String.class);
+            Method m = HttpContextManager.class.getDeclaredMethod("createId", Bundle.class,
String.class, Boolean.TYPE);
             m.setAccessible(true);
-            return (String) m.invoke(null, bundle, contextId);
+            return (String) m.invoke(null, bundle, contextId, isContextHelper);
         }
         catch (Throwable t)
         {



Mime
View raw message