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 2022/11/22 06:14:33 UTC

[felix-dev] branch master updated: FELIX-6584 : Avoid registering default servlet context as service

This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git


The following commit(s) were added to refs/heads/master by this push:
     new 32c294487f FELIX-6584 : Avoid registering default servlet context as service
32c294487f is described below

commit 32c294487f083f7482fc35605209b80dfa1a636b
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Tue Nov 22 07:14:03 2022 +0100

    FELIX-6584 : Avoid registering default servlet context as service
---
 .../runtime/DefaultServletContextHelperInfo.java   |  45 +++++++++
 .../internal/runtime/ServletContextHelperInfo.java |  14 +++
 .../internal/whiteboard/WhiteboardManager.java     | 109 ++-------------------
 .../internal/runtime/AbstractInfoOrderingTest.java |   4 +-
 .../felix/http/itest/BaseIntegrationTest.java      |   2 +-
 5 files changed, 73 insertions(+), 101 deletions(-)

diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/DefaultServletContextHelperInfo.java b/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/DefaultServletContextHelperInfo.java
new file mode 100644
index 0000000000..756b20db5e
--- /dev/null
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/DefaultServletContextHelperInfo.java
@@ -0,0 +1,45 @@
+/*
+ * 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.base.internal.runtime;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.service.servlet.whiteboard.HttpWhiteboardConstants;
+import org.osgi.service.servlet.context.ServletContextHelper;
+
+/**
+ * Info for the default context
+ */
+public class DefaultServletContextHelperInfo extends ServletContextHelperInfo {
+
+    public DefaultServletContextHelperInfo() {
+        super(Integer.MIN_VALUE, Integer.MIN_VALUE, HttpWhiteboardConstants.HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME, "/", null);
+    }
+
+    @Override
+    public ServletContextHelper getService(final BundleContext bundleContext) {
+        return new ServletContextHelper(bundleContext.getBundle()) {
+            // nothing to override
+        };
+}
+
+    @Override
+    public void ungetService(final BundleContext bundleContext, final ServletContextHelper service) {
+        // nothing to do
+    }
+}
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/ServletContextHelperInfo.java b/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/ServletContextHelperInfo.java
index 6b75817312..c04c505948 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/ServletContextHelperInfo.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/ServletContextHelperInfo.java
@@ -19,6 +19,7 @@
 package org.apache.felix.http.base.internal.runtime;
 
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.felix.http.base.internal.service.HttpServiceFactory;
@@ -119,4 +120,17 @@ public class ServletContextHelperInfo extends AbstractInfo<ServletContextHelper>
     public @NotNull String getServiceType() {
         return ServletContextHelper.class.getName();
     }
+
+    public boolean match(final WhiteboardServiceInfo<?> info) {
+        if ( this.getServiceReference() != null ) {
+            return info.getContextSelectionFilter().match(this.getServiceReference());
+        }
+        final Map<String, String> props = new HashMap<>();
+        props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, this.getName());
+        props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, this.getPath());
+        if ( HttpServiceFactory.HTTP_SERVICE_CONTEXT_NAME.equals(this.getName()) ) {
+            props.put(org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_SERVICE_CONTEXT_PROPERTY, this.getName());
+        }
+        return info.getContextSelectionFilter().matches(props);
+    }
 }
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java
index 5b2ddad1b9..dd47c50168 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java
@@ -27,7 +27,6 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Dictionary;
 import java.util.HashMap;
-import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -45,6 +44,7 @@ import org.apache.felix.http.base.internal.logger.SystemLogger;
 import org.apache.felix.http.base.internal.registry.EventListenerRegistry;
 import org.apache.felix.http.base.internal.registry.HandlerRegistry;
 import org.apache.felix.http.base.internal.runtime.AbstractInfo;
+import org.apache.felix.http.base.internal.runtime.DefaultServletContextHelperInfo;
 import org.apache.felix.http.base.internal.runtime.FilterInfo;
 import org.apache.felix.http.base.internal.runtime.ListenerInfo;
 import org.apache.felix.http.base.internal.runtime.PreprocessorInfo;
@@ -70,19 +70,14 @@ import org.apache.felix.http.base.internal.whiteboard.tracker.ResourceTracker;
 import org.apache.felix.http.base.internal.whiteboard.tracker.ServletContextHelperTracker;
 import org.apache.felix.http.base.internal.whiteboard.tracker.ServletTracker;
 import org.jetbrains.annotations.NotNull;
-import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.Filter;
 import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceFactory;
 import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.servlet.context.ServletContextHelper;
 import org.osgi.service.servlet.runtime.dto.DTOConstants;
 import org.osgi.service.servlet.runtime.dto.PreprocessorDTO;
 import org.osgi.service.servlet.runtime.dto.ServletContextDTO;
-import org.osgi.service.servlet.whiteboard.HttpWhiteboardConstants;
 import org.osgi.service.servlet.whiteboard.Preprocessor;
 import org.osgi.util.tracker.ServiceTracker;
 
@@ -124,10 +119,6 @@ public final class WhiteboardManager
 
     private volatile ServletContext webContext;
 
-    private volatile ServiceRegistration<ServletContextHelper> defaultContextRegistration;
-
-    private volatile ServiceRegistration<org.osgi.service.http.context.ServletContextHelper> defaultJavaxContextRegistration;
-
     /**
      * Create a new whiteboard http manager
      *
@@ -160,72 +151,19 @@ public final class WhiteboardManager
 
         this.webContext = containerContext;
 
-
         // add context for http service
-        final List<WhiteboardContextHandler> list = new ArrayList<>();
-        final ServletContextHelperInfo info = new ServletContextHelperInfo(Integer.MAX_VALUE,
+        final List<WhiteboardContextHandler> httpContextList = new ArrayList<>();
+        final ServletContextHelperInfo httpInfo = new ServletContextHelperInfo(Integer.MAX_VALUE,
                 HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID,
                 HttpServiceFactory.HTTP_SERVICE_CONTEXT_NAME, "/", null);
-        list.add(new HttpServiceContextHandler(info, registry.getRegistry(HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID),
+        httpContextList.add(new HttpServiceContextHandler(httpInfo, registry.getRegistry(HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID),
                 httpServiceFactory, webContext, this.httpBundleContext.getBundle()));
-        this.contextMap.put(HttpServiceFactory.HTTP_SERVICE_CONTEXT_NAME, list);
-
-        // register default context
-        final Dictionary<String, Object> props = new Hashtable<>();
-        props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, HttpWhiteboardConstants.HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME);
-        props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/");
-        props.put(Constants.SERVICE_RANKING, Integer.MIN_VALUE);
-        this.defaultContextRegistration = httpBundleContext.registerService(
-                ServletContextHelper.class,
-                new ServiceFactory<ServletContextHelper>()
-                {
-
-                    @Override
-                    public ServletContextHelper getService(
-                            final Bundle bundle,
-                            final ServiceRegistration<ServletContextHelper> registration)
-                    {
-                        return new ServletContextHelper(bundle)
-                        {
-                            // nothing to override
-                        };
-                    }
+        this.contextMap.put(HttpServiceFactory.HTTP_SERVICE_CONTEXT_NAME, httpContextList);
 
-                    @Override
-                    public void ungetService(
-                            final Bundle bundle,
-                            final ServiceRegistration<ServletContextHelper> registration,
-                            final ServletContextHelper service)
-                    {
-                        // nothing to do
-                    }
-                }, props);
-        // register default context for javax whiteboard
-        this.defaultJavaxContextRegistration = httpBundleContext.registerService(
-                org.osgi.service.http.context.ServletContextHelper.class,
-                new ServiceFactory<org.osgi.service.http.context.ServletContextHelper>()
-                {
-
-                    @Override
-                    public org.osgi.service.http.context.ServletContextHelper getService(
-                            final Bundle bundle,
-                            final ServiceRegistration<org.osgi.service.http.context.ServletContextHelper> registration)
-                    {
-                        return new org.osgi.service.http.context.ServletContextHelper(bundle)
-                        {
-                            // nothing to override
-                        };
-                    }
+        // Add default context
+        this.addContextHelper(new DefaultServletContextHelperInfo());
 
-                    @Override
-                    public void ungetService(
-                            final Bundle bundle,
-                            final ServiceRegistration<org.osgi.service.http.context.ServletContextHelper> registration,
-                            final org.osgi.service.http.context.ServletContextHelper service)
-                    {
-                        // nothing to do
-                    }
-                }, props);
+        // Start tracker
         addTracker(new FilterTracker(this.httpBundleContext, this));
         addTracker(new ListenersTracker(this.httpBundleContext, this));
         addTracker(new PreprocessorTracker(this.httpBundleContext, this));
@@ -268,15 +206,6 @@ public final class WhiteboardManager
         this.failureStateHandler.clear();
         this.registry.reset();
 
-        if ( this.defaultJavaxContextRegistration != null ) {
-            this.defaultJavaxContextRegistration.unregister();
-            this.defaultJavaxContextRegistration = null;
-        }
-        if (this.defaultContextRegistration != null)
-        {
-            this.defaultContextRegistration.unregister();
-            this.defaultContextRegistration = null;
-        }
         this.webContext = null;
     }
 
@@ -569,27 +498,9 @@ public final class WhiteboardManager
                     // we ignore this and treat it as an invisible service
                 }
             }
-            if ( visible )
+            if ( visible && h.getContextInfo().match(info) )
             {
-                if ( h.getContextInfo().getServiceReference() != null )
-                {
-                    if ( info.getContextSelectionFilter().match(h.getContextInfo().getServiceReference()) )
-                    {
-                        result.add(h);
-                    }
-                }
-                else
-                {
-                    final Map<String, String> props = new HashMap<>();
-                    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, h.getContextInfo().getName());
-                    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, h.getContextInfo().getPath());
-                    props.put(org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_SERVICE_CONTEXT_PROPERTY, h.getContextInfo().getName());
-
-                    if ( info.getContextSelectionFilter().matches(props) )
-                    {
-                        result.add(h);
-                    }
-                }
+                result.add(h);
             }
         }
         return result;
diff --git a/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/AbstractInfoOrderingTest.java b/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/AbstractInfoOrderingTest.java
index 6395e38093..c579c81d35 100644
--- a/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/AbstractInfoOrderingTest.java
+++ b/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/AbstractInfoOrderingTest.java
@@ -63,7 +63,9 @@ public class AbstractInfoOrderingTest
                 { 1, 0, 0, -2, 1 },
                 { 1, 0, 0, -1, 2 },
                 { 1, 0, 0, -1, 1 },
-                { 1, 0, 0, -2, -1 }
+                { 1, 0, 0, -2, -1 },
+                { 1, Integer.MIN_VALUE, 0, Integer.MIN_VALUE, 4},
+                { 1, Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE, 5}
            });
     }
 
diff --git a/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java b/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java
index d53943f755..24cdb850fd 100644
--- a/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java
+++ b/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java
@@ -274,7 +274,7 @@ public abstract class BaseIntegrationTest {
 
     protected Bundle getHttpJettyBundle() {
         Bundle b = findBundle(ORG_APACHE_FELIX_HTTP_JETTY);
-        assertNotNull("Filestore bundle not found?!", b);
+        assertNotNull("Apache Felix Jetty bundle not found?!", b);
         return b;
     }