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 2020/09/23 04:55:29 UTC

[felix-dev] branch master updated: FELIX-6324 : Protect code against IllegalStateException and IllegalArgumentException

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 08d2eac  FELIX-6324 : Protect code against IllegalStateException and IllegalArgumentException
08d2eac is described below

commit 08d2eac4fc7575719f241b8c8e618392e2e6ae4c
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Sep 23 06:55:04 2020 +0200

    FELIX-6324 : Protect code against IllegalStateException and IllegalArgumentException
---
 .../base/internal/handler/PreprocessorHandler.java |  7 +-
 .../internal/handler/WhiteboardFilterHandler.java  | 22 ++----
 .../handler/WhiteboardListenerHandler.java         | 22 ++----
 .../internal/handler/WhiteboardServletHandler.java | 18 ++---
 .../internal/logger/LogServiceEnabledLogger.java   |  5 +-
 .../internal/runtime/dto/RuntimeDTOBuilder.java    |  6 --
 .../internal/service/PerBundleHttpServiceImpl.java |  3 +-
 .../http/base/internal/util/ServiceUtils.java      | 89 ++++++++++++++++++++++
 .../whiteboard/SharedServletContextImpl.java       |  1 -
 .../whiteboard/WhiteboardContextHandler.java       | 32 +++-----
 .../internal/context/ServletContextImplTest.java   |  6 +-
 .../internal/handler/FilterConfigImplTest.java     | 14 ++--
 .../internal/handler/ServletConfigImplTest.java    | 12 +--
 .../internal/registry/ErrorPageRegistryTest.java   | 11 ++-
 .../registry/EventListenerRegistryTest.java        |  7 +-
 .../base/internal/registry/FilterRegistryTest.java |  6 +-
 .../internal/registry/ServletRegistryTest.java     | 13 ++--
 .../internal/runtime/AbstractInfoOrderingTest.java |  6 +-
 .../base/internal/runtime/ListenerInfoTest.java    |  1 +
 .../jetty/internal/ConnectorFactoryTracker.java    |  7 +-
 .../LoadBalancerCustomizerFactoryTracker.java      | 11 +--
 .../http/jetty/internal/RequestLogTracker.java     | 24 ++++--
 22 files changed, 197 insertions(+), 126 deletions(-)

diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/PreprocessorHandler.java b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/PreprocessorHandler.java
index 22439b4..b2efa0a 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/PreprocessorHandler.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/PreprocessorHandler.java
@@ -26,6 +26,7 @@ import javax.servlet.ServletResponse;
 
 import org.apache.felix.http.base.internal.logger.SystemLogger;
 import org.apache.felix.http.base.internal.runtime.PreprocessorInfo;
+import org.apache.felix.http.base.internal.util.ServiceUtils;
 import org.jetbrains.annotations.NotNull;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
@@ -74,7 +75,7 @@ public class PreprocessorHandler implements Comparable<PreprocessorHandler>
     public int init()
     {
         final ServiceReference<Preprocessor> serviceReference = this.info.getServiceReference();
-        this.preprocessor = this.bundleContext.getService(serviceReference);
+        this.preprocessor = ServiceUtils.safeGetService(this.bundleContext, serviceReference);
 
         if (this.preprocessor == null)
         {
@@ -94,7 +95,7 @@ public class PreprocessorHandler implements Comparable<PreprocessorHandler>
                     e);
 
             this.preprocessor = null;
-            this.bundleContext.ungetService(serviceReference);
+            ServiceUtils.safeUngetService(this.bundleContext, serviceReference);
 
             return DTOConstants.FAILURE_REASON_EXCEPTION_ON_INIT;
         }
@@ -121,7 +122,7 @@ public class PreprocessorHandler implements Comparable<PreprocessorHandler>
                     ignore);
         }
         this.preprocessor = null;
-        this.bundleContext.ungetService(this.info.getServiceReference());
+        ServiceUtils.safeUngetService(this.bundleContext, this.info.getServiceReference());
 
         return true;
     }
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardFilterHandler.java b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardFilterHandler.java
index dc33ff7..8cb7751 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardFilterHandler.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardFilterHandler.java
@@ -20,8 +20,8 @@ import javax.servlet.Filter;
 
 import org.apache.felix.http.base.internal.context.ExtServletContext;
 import org.apache.felix.http.base.internal.runtime.FilterInfo;
+import org.apache.felix.http.base.internal.util.ServiceUtils;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceObjects;
 import org.osgi.framework.ServiceReference;
 
 /**
@@ -50,17 +50,12 @@ public final class WhiteboardFilterHandler extends FilterHandler
         }
 
         final ServiceReference<Filter> serviceReference = getFilterInfo().getServiceReference();
-        final ServiceObjects<Filter> so = this.bundleContext.getServiceObjects(serviceReference);
-
-        this.setFilter((so == null ? null : so.getService()));
+        this.setFilter(ServiceUtils.safeGetServiceObjects(this.bundleContext, serviceReference));
 
         final int reason = super.init();
         if ( reason != -1 )
         {
-            if ( so != null )
-            {
-                so.ungetService(this.getFilter());
-            }
+            ServiceUtils.safeUngetServiceObjects(this.bundleContext, serviceReference, this.getFilter());
             this.setFilter(null);
         }
         return reason;
@@ -69,17 +64,14 @@ public final class WhiteboardFilterHandler extends FilterHandler
     @Override
     public boolean destroy()
     {
-        final Filter s = this.getFilter();
-        if ( s != null )
+        final Filter f = this.getFilter();
+        if ( f != null )
         {
             if ( super.destroy() )
             {
+                ServiceUtils.safeUngetServiceObjects(this.bundleContext,
+                        getFilterInfo().getServiceReference(), f);
 
-                final ServiceObjects<Filter> so = this.bundleContext.getServiceObjects(getFilterInfo().getServiceReference());
-                if (so != null)
-                {
-                    so.ungetService(s);
-                }
                 return true;
             }
         }
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardListenerHandler.java b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardListenerHandler.java
index a9edbe6..3a4ce9f 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardListenerHandler.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardListenerHandler.java
@@ -20,8 +20,8 @@ import java.util.EventListener;
 
 import org.apache.felix.http.base.internal.context.ExtServletContext;
 import org.apache.felix.http.base.internal.runtime.ListenerInfo;
+import org.apache.felix.http.base.internal.util.ServiceUtils;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceObjects;
 import org.osgi.framework.ServiceReference;
 
 /**
@@ -50,17 +50,12 @@ public final class WhiteboardListenerHandler extends ListenerHandler
         }
 
         final ServiceReference<EventListener> serviceReference = getListenerInfo().getServiceReference();
-        final ServiceObjects<EventListener> so = this.bundleContext.getServiceObjects(serviceReference);
-
-        this.setListener((so == null ? null : so.getService()));
+        this.setListener(ServiceUtils.safeGetServiceObjects(this.bundleContext, serviceReference));
 
         final int reason = super.init();
         if ( reason != -1 )
         {
-            if ( so != null )
-            {
-                so.ungetService(this.getListener());
-            }
+            ServiceUtils.safeUngetServiceObjects(this.bundleContext, serviceReference, this.getListener());
             this.setListener(null);
         }
         return reason;
@@ -69,17 +64,14 @@ public final class WhiteboardListenerHandler extends ListenerHandler
     @Override
     public boolean destroy()
     {
-        final EventListener s = this.getListener();
-        if ( s != null )
+        final EventListener l = this.getListener();
+        if ( l != null )
         {
             if ( super.destroy() )
             {
+                ServiceUtils.safeUngetServiceObjects(this.bundleContext,
+                        getListenerInfo().getServiceReference(), l);
 
-                final ServiceObjects<EventListener> so = this.bundleContext.getServiceObjects(getListenerInfo().getServiceReference());
-                if (so != null)
-                {
-                    so.ungetService(s);
-                }
                 return true;
             }
         }
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardServletHandler.java b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardServletHandler.java
index b942f5d..03b5a1a 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardServletHandler.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/WhiteboardServletHandler.java
@@ -22,9 +22,9 @@ import javax.servlet.Servlet;
 
 import org.apache.felix.http.base.internal.context.ExtServletContext;
 import org.apache.felix.http.base.internal.runtime.ServletInfo;
+import org.apache.felix.http.base.internal.util.ServiceUtils;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceObjects;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.http.runtime.dto.DTOConstants;
 
@@ -106,17 +106,12 @@ public final class WhiteboardServletHandler extends ServletHandler
         }
 
         final ServiceReference<Servlet> serviceReference = getServletInfo().getServiceReference();
-        final ServiceObjects<Servlet> so = this.bundleContext.getServiceObjects(serviceReference);
-
-        this.setServlet((so == null ? null : so.getService()));
+        this.setServlet(ServiceUtils.safeGetServiceObjects(this.bundleContext, serviceReference));
 
         final int reason = super.init();
         if ( reason != -1 )
         {
-            if ( so != null )
-            {
-                so.ungetService(this.getServlet());
-            }
+            ServiceUtils.safeUngetServiceObjects(this.bundleContext, serviceReference, this.getServlet());
             this.setServlet(null);
         }
         return reason;
@@ -130,12 +125,9 @@ public final class WhiteboardServletHandler extends ServletHandler
         {
             if ( super.destroy() )
             {
+                ServiceUtils.safeUngetServiceObjects(this.bundleContext,
+                        getServletInfo().getServiceReference(), s);
 
-                final ServiceObjects<Servlet> so = this.bundleContext.getServiceObjects(getServletInfo().getServiceReference());
-                if (so != null)
-                {
-                    so.ungetService(s);
-                }
                 return true;
             }
         }
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/logger/LogServiceEnabledLogger.java b/http/base/src/main/java/org/apache/felix/http/base/internal/logger/LogServiceEnabledLogger.java
index e2d9322..dc74510 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/logger/LogServiceEnabledLogger.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/logger/LogServiceEnabledLogger.java
@@ -18,6 +18,7 @@
  */
 package org.apache.felix.http.base.internal.logger;
 
+import org.apache.felix.http.base.internal.util.ServiceUtils;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.util.tracker.ServiceTracker;
@@ -66,7 +67,7 @@ public class LogServiceEnabledLogger
             {
                 if ( !hasService )
                 {
-                    final Object logService = bundleContext.getService(reference);
+                    final Object logService = ServiceUtils.safeGetService(bundleContext, reference);
                     if ( logService != null )
                     {
                         hasService = true;
@@ -87,7 +88,7 @@ public class LogServiceEnabledLogger
             public void removedService(final ServiceReference<Object> reference, final Object service)
             {
                 hasService = false;
-                bundleContext.ungetService(reference);
+                ServiceUtils.safeUngetService(bundleContext, reference);
             }
         } );
         logServiceTracker.open();
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilder.java b/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilder.java
index 5a14735..d99e3ac 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilder.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilder.java
@@ -19,14 +19,8 @@
 package org.apache.felix.http.base.internal.runtime.dto;
 
 import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
 
-import org.osgi.framework.Bundle;
-import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceReference;
 import org.osgi.framework.dto.ServiceReferenceDTO;
-import org.osgi.service.http.runtime.HttpServiceRuntime;
 import org.osgi.service.http.runtime.dto.FailedErrorPageDTO;
 import org.osgi.service.http.runtime.dto.FailedFilterDTO;
 import org.osgi.service.http.runtime.dto.FailedListenerDTO;
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/service/PerBundleHttpServiceImpl.java b/http/base/src/main/java/org/apache/felix/http/base/internal/service/PerBundleHttpServiceImpl.java
index 89fe92d..a3edacd 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/service/PerBundleHttpServiceImpl.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/service/PerBundleHttpServiceImpl.java
@@ -107,7 +107,7 @@ public final class PerBundleHttpServiceImpl implements HttpService
      * @see org.osgi.service.http.HttpService#registerServlet(java.lang.String, javax.servlet.Servlet, java.util.Dictionary, org.osgi.service.http.HttpContext)
      */
     @Override
-    public void registerServlet(String alias, Servlet servlet, Dictionary initParams, HttpContext context) throws ServletException, NamespaceException
+    public void registerServlet(String alias, Servlet servlet, @SuppressWarnings("rawtypes") Dictionary initParams, HttpContext context) throws ServletException, NamespaceException
     {
         if (servlet == null)
         {
@@ -121,6 +121,7 @@ public final class PerBundleHttpServiceImpl implements HttpService
         final Map<String, String> paramMap = new HashMap<>();
         if (initParams != null && initParams.size() > 0)
         {
+            @SuppressWarnings("rawtypes")
             Enumeration e = initParams.keys();
             while (e.hasMoreElements())
             {
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/util/ServiceUtils.java b/http/base/src/main/java/org/apache/felix/http/base/internal/util/ServiceUtils.java
new file mode 100644
index 0000000..3cb8b24
--- /dev/null
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/util/ServiceUtils.java
@@ -0,0 +1,89 @@
+/*
+ * 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.util;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceException;
+import org.osgi.framework.ServiceObjects;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * Utility methods to get/unget services, ignoring exceptions that might occur
+ * on bundle stop/update
+ */
+public abstract class ServiceUtils {
+
+    /**
+     * Get the service
+     * @return The service or {@code null}
+     */
+    public static <T> T safeGetService(final BundleContext ctx, final ServiceReference<T> ref) {
+        try {
+            return ctx.getService(ref);
+        } catch ( final IllegalStateException | IllegalArgumentException | ServiceException ignore ) {
+            // ignore this
+        }
+        return null;
+    }
+
+    /**
+     * Unget the service
+     */
+    public static <T> void safeUngetService(final BundleContext ctx, final ServiceReference<T> ref) {
+        try {
+            ctx.ungetService(ref);
+        } catch ( final IllegalStateException | IllegalArgumentException | ServiceException ignore ) {
+            // ignore this
+        }
+    }
+
+    /**
+     * Get the service using {@code ServiceObjects}
+     * @return The service or {@code null}
+     */
+    public static <T> T safeGetServiceObjects(final BundleContext ctx, final ServiceReference<T> ref) {
+        if ( ctx != null ) {
+            try {
+                final ServiceObjects<T> so = ctx.getServiceObjects(ref);
+
+                return so == null ? null : so.getService();
+            } catch ( final IllegalStateException | IllegalArgumentException | ServiceException ignore ) {
+                // ignore this
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Unget the service using {@code ServiceObjects}
+     */
+    public static <T> void safeUngetServiceObjects(final BundleContext ctx, final ServiceReference<T> ref, final T service) {
+        if ( ctx != null && service != null ) {
+            try {
+                final ServiceObjects<T> so = ctx.getServiceObjects(ref);
+
+                if ( so != null ) {
+                    so.ungetService(service);
+                }
+            } catch ( final IllegalStateException | IllegalArgumentException | ServiceException ignore ) {
+                // ignore this
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/SharedServletContextImpl.java b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/SharedServletContextImpl.java
index e497573..f858bdf 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/SharedServletContextImpl.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/SharedServletContextImpl.java
@@ -425,7 +425,6 @@ public class SharedServletContextImpl implements ServletContext
         return this.context.getSessionCookieConfig();
     }
 
-    @SuppressWarnings("deprecation")
     @Override
     public void log(final Exception cause, final String message)
     {
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardContextHandler.java b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardContextHandler.java
index 25400a2..3cb96d7 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardContextHandler.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardContextHandler.java
@@ -25,11 +25,11 @@ import org.apache.felix.http.base.internal.context.ExtServletContext;
 import org.apache.felix.http.base.internal.registry.HandlerRegistry;
 import org.apache.felix.http.base.internal.registry.PerContextHandlerRegistry;
 import org.apache.felix.http.base.internal.runtime.ServletContextHelperInfo;
+import org.apache.felix.http.base.internal.util.ServiceUtils;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceObjects;
 import org.osgi.service.http.context.ServletContextHelper;
 
 /**
@@ -135,21 +135,17 @@ public class WhiteboardContextHandler implements Comparable<WhiteboardContextHan
             ContextHolder holder = this.perBundleContextMap.get(key);
             if ( holder == null )
             {
-                final BundleContext ctx = bundle.getBundleContext();
-                final ServiceObjects<ServletContextHelper> so = (ctx == null ? null : ctx.getServiceObjects(this.info.getServiceReference()));
-                if ( so != null )
+
+                final ServletContextHelper service = ServiceUtils.safeGetServiceObjects(bundle.getBundleContext(), this.info.getServiceReference());
+                if ( service != null )
                 {
-                    final ServletContextHelper service = so.getService();
-                    if ( service != null )
-                    {
-                        holder = new ContextHolder();
-                        holder.servletContextHelper = service;
-                        holder.servletContext = new PerBundleServletContextImpl(bundle,
-                                this.sharedContext,
-                                service,
-                                this.registry);
-                        this.perBundleContextMap.put(key, holder);
-                    }
+                    holder = new ContextHolder();
+                    holder.servletContextHelper = service;
+                    holder.servletContext = new PerBundleServletContextImpl(bundle,
+                            this.sharedContext,
+                            service,
+                            this.registry);
+                    this.perBundleContextMap.put(key, holder);
                 }
             }
             if ( holder != null )
@@ -177,11 +173,7 @@ public class WhiteboardContextHandler implements Comparable<WhiteboardContextHan
                     if ( holder.servletContextHelper != null )
                     {
                         final BundleContext ctx = bundle.getBundleContext();
-                        final ServiceObjects<ServletContextHelper> so = (ctx == null ? null : ctx.getServiceObjects(this.info.getServiceReference()));
-                        if ( so != null )
-                        {
-                            so.ungetService(holder.servletContextHelper);
-                        }
+                        ServiceUtils.safeUngetServiceObjects(ctx, this.info.getServiceReference(), holder.servletContextHelper);
                     }
                 }
             }
diff --git a/http/base/src/test/java/org/apache/felix/http/base/internal/context/ServletContextImplTest.java b/http/base/src/test/java/org/apache/felix/http/base/internal/context/ServletContextImplTest.java
index 7894566..24b6523 100644
--- a/http/base/src/test/java/org/apache/felix/http/base/internal/context/ServletContextImplTest.java
+++ b/http/base/src/test/java/org/apache/felix/http/base/internal/context/ServletContextImplTest.java
@@ -288,6 +288,7 @@ public class ServletContextImplTest
             return null;
         }
 
+        @SuppressWarnings({ "unchecked", "rawtypes" })
         @Override
         public Enumeration getInitParameterNames()
         {
@@ -373,7 +374,7 @@ public class ServletContextImplTest
             return null;
         }
 
-        @SuppressWarnings("deprecation")
+        @SuppressWarnings({ "rawtypes", "unchecked" })
         @Deprecated
         @Override
         public Enumeration getServletNames()
@@ -393,7 +394,7 @@ public class ServletContextImplTest
             return null;
         }
 
-        @SuppressWarnings("deprecation")
+        @SuppressWarnings({ "rawtypes", "unchecked" })
         @Deprecated
         @Override
         public Enumeration getServlets()
@@ -407,7 +408,6 @@ public class ServletContextImplTest
             return null;
         }
 
-        @SuppressWarnings("deprecation")
         @Deprecated
         @Override
         public void log(Exception exception, String msg)
diff --git a/http/base/src/test/java/org/apache/felix/http/base/internal/handler/FilterConfigImplTest.java b/http/base/src/test/java/org/apache/felix/http/base/internal/handler/FilterConfigImplTest.java
index f5cc655..c261b5f 100644
--- a/http/base/src/test/java/org/apache/felix/http/base/internal/handler/FilterConfigImplTest.java
+++ b/http/base/src/test/java/org/apache/felix/http/base/internal/handler/FilterConfigImplTest.java
@@ -16,13 +16,15 @@
  */
 package org.apache.felix.http.base.internal.handler;
 
-import org.junit.Before;
+import java.util.Enumeration;
+import java.util.HashMap;
+
+import javax.servlet.ServletContext;
+
 import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
-import javax.servlet.ServletContext;
-import java.util.HashMap;
-import java.util.Enumeration;
 
 public class FilterConfigImplTest
 {
@@ -34,7 +36,7 @@ public class FilterConfigImplTest
     {
         HashMap<String, String> params = new HashMap<String, String>();
         params.put("key1", "value1");
-        
+
         this.context = Mockito.mock(ServletContext.class);
         this.config = new FilterConfigImpl("myfilter", this.context, params);
     }
@@ -61,7 +63,7 @@ public class FilterConfigImplTest
     @Test
     public void testGetInitParameterNames()
     {
-        Enumeration e = this.config.getInitParameterNames();
+        Enumeration<String> e = this.config.getInitParameterNames();
         Assert.assertNotNull(e);
         Assert.assertTrue(e.hasMoreElements());
         Assert.assertEquals("key1", e.nextElement());
diff --git a/http/base/src/test/java/org/apache/felix/http/base/internal/handler/ServletConfigImplTest.java b/http/base/src/test/java/org/apache/felix/http/base/internal/handler/ServletConfigImplTest.java
index f245c74..205394d 100644
--- a/http/base/src/test/java/org/apache/felix/http/base/internal/handler/ServletConfigImplTest.java
+++ b/http/base/src/test/java/org/apache/felix/http/base/internal/handler/ServletConfigImplTest.java
@@ -16,13 +16,15 @@
  */
 package org.apache.felix.http.base.internal.handler;
 
-import org.junit.Before;
+import java.util.Enumeration;
+import java.util.HashMap;
+
+import javax.servlet.ServletContext;
+
 import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
-import javax.servlet.ServletContext;
-import java.util.HashMap;
-import java.util.Enumeration;
 
 public class ServletConfigImplTest
 {
@@ -61,7 +63,7 @@ public class ServletConfigImplTest
     @Test
     public void testGetInitParameterNames()
     {
-        Enumeration e = this.config.getInitParameterNames();
+        Enumeration<String> e = this.config.getInitParameterNames();
         Assert.assertNotNull(e);
         Assert.assertTrue(e.hasMoreElements());
         Assert.assertEquals("key1", e.nextElement());
diff --git a/http/base/src/test/java/org/apache/felix/http/base/internal/registry/ErrorPageRegistryTest.java b/http/base/src/test/java/org/apache/felix/http/base/internal/registry/ErrorPageRegistryTest.java
index bb548da..accc721 100644
--- a/http/base/src/test/java/org/apache/felix/http/base/internal/registry/ErrorPageRegistryTest.java
+++ b/http/base/src/test/java/org/apache/felix/http/base/internal/registry/ErrorPageRegistryTest.java
@@ -40,14 +40,13 @@ import org.apache.felix.http.base.internal.handler.ServletHandler;
 import org.apache.felix.http.base.internal.runtime.ServletInfo;
 import org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder;
 import org.junit.Test;
-import org.mockito.Matchers;
+import org.mockito.ArgumentMatchers;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.http.runtime.dto.DTOConstants;
-import org.osgi.service.http.runtime.dto.FailedErrorPageDTO;
 import org.osgi.service.http.runtime.dto.ServletContextDTO;
 import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
 
@@ -84,7 +83,7 @@ public class ErrorPageRegistryTest
         final ServletHandler h1 = createServletHandler(1L, 0, "404", "java.io.IOException");
         reg.addServlet(h1);
 
-        verify(h1.getServlet()).init(Matchers.any(ServletConfig.class));
+        verify(h1.getServlet()).init(ArgumentMatchers.any(ServletConfig.class));
 
         // one entry in reg
         clear(dto, holder);
@@ -135,8 +134,8 @@ public class ErrorPageRegistryTest
         final ServletHandler h2 = createServletHandler(2L, 10, "404", "some.other.Exception");
         reg.addServlet(h2);
 
-        verify(h1.getServlet()).init(Matchers.any(ServletConfig.class));
-        verify(h2.getServlet()).init(Matchers.any(ServletConfig.class));
+        verify(h1.getServlet()).init(ArgumentMatchers.any(ServletConfig.class));
+        verify(h2.getServlet()).init(ArgumentMatchers.any(ServletConfig.class));
 
         // two entries in DTO
         clear(dto, holder);
@@ -271,7 +270,7 @@ public class ErrorPageRegistryTest
     private static ServletInfo createServletInfo(final long id, final int ranking, final String... codes) throws InvalidSyntaxException
     {
         final BundleContext bCtx = mock(BundleContext.class);
-        when(bCtx.createFilter(Matchers.anyString())).thenReturn(null);
+        when(bCtx.createFilter(ArgumentMatchers.anyString())).thenReturn(null);
         final Bundle bundle = mock(Bundle.class);
         when(bundle.getBundleContext()).thenReturn(bCtx);
 
diff --git a/http/base/src/test/java/org/apache/felix/http/base/internal/registry/EventListenerRegistryTest.java b/http/base/src/test/java/org/apache/felix/http/base/internal/registry/EventListenerRegistryTest.java
index 3a00baf..e01d072 100644
--- a/http/base/src/test/java/org/apache/felix/http/base/internal/registry/EventListenerRegistryTest.java
+++ b/http/base/src/test/java/org/apache/felix/http/base/internal/registry/EventListenerRegistryTest.java
@@ -35,7 +35,7 @@ import org.apache.felix.http.base.internal.handler.HttpServiceFilterHandler;
 import org.apache.felix.http.base.internal.runtime.FilterInfo;
 import org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder;
 import org.junit.Test;
-import org.mockito.Matchers;
+import org.mockito.ArgumentMatchers;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
@@ -73,7 +73,7 @@ public class EventListenerRegistryTest {
         final FilterHandler h1 = createFilterHandler(1L, 0, "/foo");
         reg.addFilter(h1);
 
-        verify(h1.getFilter()).init(Matchers.any(FilterConfig.class));
+        verify(h1.getFilter()).init(ArgumentMatchers.any(FilterConfig.class));
 
         // one entry in DTO
         clear(dto, holder);
@@ -126,10 +126,11 @@ public class EventListenerRegistryTest {
     private static FilterInfo createFilterInfo(final long id, final int ranking, final String... paths) throws InvalidSyntaxException
     {
         final BundleContext bCtx = mock(BundleContext.class);
-        when(bCtx.createFilter(Matchers.anyString())).thenReturn(null);
+        when(bCtx.createFilter(ArgumentMatchers.anyString())).thenReturn(null);
         final Bundle bundle = mock(Bundle.class);
         when(bundle.getBundleContext()).thenReturn(bCtx);
 
+        @SuppressWarnings("unchecked")
         final ServiceReference<Filter> ref = mock(ServiceReference.class);
         when(ref.getBundle()).thenReturn(bundle);
         when(ref.getProperty(Constants.SERVICE_ID)).thenReturn(id);
diff --git a/http/base/src/test/java/org/apache/felix/http/base/internal/registry/FilterRegistryTest.java b/http/base/src/test/java/org/apache/felix/http/base/internal/registry/FilterRegistryTest.java
index 4c23a94..da93f92 100644
--- a/http/base/src/test/java/org/apache/felix/http/base/internal/registry/FilterRegistryTest.java
+++ b/http/base/src/test/java/org/apache/felix/http/base/internal/registry/FilterRegistryTest.java
@@ -34,7 +34,7 @@ import org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler;
 import org.apache.felix.http.base.internal.runtime.ListenerInfo;
 import org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder;
 import org.junit.Test;
-import org.mockito.Matchers;
+import org.mockito.ArgumentMatchers;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
@@ -96,10 +96,11 @@ public class FilterRegistryTest {
         typeNames[index++] = type.getName();
 
         final BundleContext bCtx = mock(BundleContext.class);
-        when(bCtx.createFilter(Matchers.anyString())).thenReturn(null);
+        when(bCtx.createFilter(ArgumentMatchers.anyString())).thenReturn(null);
         final Bundle bundle = mock(Bundle.class);
         when(bundle.getBundleContext()).thenReturn(bCtx);
 
+        @SuppressWarnings("unchecked")
         final ServiceReference<EventListener> ref = mock(ServiceReference.class);
         when(ref.getBundle()).thenReturn(bundle);
         when(ref.getProperty(Constants.SERVICE_ID)).thenReturn(id);
@@ -108,6 +109,7 @@ public class FilterRegistryTest {
         when(ref.getPropertyKeys()).thenReturn(new String[0]);
 
         final EventListener listener = mock(type);
+        @SuppressWarnings("unchecked")
         final ServiceObjects<EventListener> so = mock(ServiceObjects.class);
         when(bCtx.getServiceObjects(ref)).thenReturn(so);
         when(so.getService()).thenReturn(listener);
diff --git a/http/base/src/test/java/org/apache/felix/http/base/internal/registry/ServletRegistryTest.java b/http/base/src/test/java/org/apache/felix/http/base/internal/registry/ServletRegistryTest.java
index 3791fe3..09d2145 100644
--- a/http/base/src/test/java/org/apache/felix/http/base/internal/registry/ServletRegistryTest.java
+++ b/http/base/src/test/java/org/apache/felix/http/base/internal/registry/ServletRegistryTest.java
@@ -40,7 +40,7 @@ import org.apache.felix.http.base.internal.handler.ServletHandler;
 import org.apache.felix.http.base.internal.runtime.ServletInfo;
 import org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder;
 import org.junit.Test;
-import org.mockito.Matchers;
+import org.mockito.ArgumentMatchers;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
@@ -83,7 +83,7 @@ public class ServletRegistryTest {
         final ServletHandler h1 = createServletHandler(1L, 0, "/foo");
         reg.addServlet(h1);
 
-        verify(h1.getServlet()).init(Matchers.any(ServletConfig.class));
+        verify(h1.getServlet()).init(ArgumentMatchers.any(ServletConfig.class));
 
         // one entry in reg
         // check DTO
@@ -120,11 +120,11 @@ public class ServletRegistryTest {
         // register servlets
         final ServletHandler h1 = createServletHandler(1L, 10, "/foo");
         reg.addServlet(h1);
-        verify(h1.getServlet()).init(Matchers.any(ServletConfig.class));
+        verify(h1.getServlet()).init(ArgumentMatchers.any(ServletConfig.class));
 
         final ServletHandler h2 = createServletHandler(2L, 0, "/foo");
         reg.addServlet(h2);
-        verify(h2.getServlet(), never()).init(Matchers.any(ServletConfig.class));
+        verify(h2.getServlet(), never()).init(ArgumentMatchers.any(ServletConfig.class));
         verify(h1.getServlet(), never()).destroy();
 
         // two entries in reg
@@ -150,7 +150,7 @@ public class ServletRegistryTest {
         final Servlet s1 = h1.getServlet();
         reg.removeServlet(h1.getServletInfo(), true);
         verify(s1).destroy();
-        verify(h2.getServlet()).init(Matchers.any(ServletConfig.class));
+        verify(h2.getServlet()).init(ArgumentMatchers.any(ServletConfig.class));
 
         // h2 is active
         clear(dto, holder);
@@ -243,10 +243,11 @@ public class ServletRegistryTest {
     private static ServletInfo createServletInfo(final long id, final int ranking, final String... paths) throws InvalidSyntaxException
     {
         final BundleContext bCtx = mock(BundleContext.class);
-        when(bCtx.createFilter(Matchers.anyString())).thenReturn(null);
+        when(bCtx.createFilter(ArgumentMatchers.anyString())).thenReturn(null);
         final Bundle bundle = mock(Bundle.class);
         when(bundle.getBundleContext()).thenReturn(bCtx);
 
+        @SuppressWarnings("unchecked")
         final ServiceReference<Servlet> ref = mock(ServiceReference.class);
         when(ref.getBundle()).thenReturn(bundle);
         when(ref.getProperty(Constants.SERVICE_ID)).thenReturn(id);
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 1b91578..2846d83 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
@@ -19,9 +19,9 @@
 package org.apache.felix.http.base.internal.runtime;
 
 import static java.lang.Integer.signum;
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.fail;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -88,7 +88,7 @@ public class AbstractInfoOrderingTest
         assertEquals(expected, signum(testInfo.compareTo(other)));
         if ( expected != 0 )
         {
-            final List<AbstractInfo> list = new ArrayList<AbstractInfo>();
+            final List<AbstractInfo<TestInfo>> list = new ArrayList<>();
             list.add(testInfo);
             list.add(other);
             Collections.sort(list);
diff --git a/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/ListenerInfoTest.java b/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/ListenerInfoTest.java
index 08faaf3..51edb74 100755
--- a/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/ListenerInfoTest.java
+++ b/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/ListenerInfoTest.java
@@ -44,6 +44,7 @@ public class ListenerInfoTest
         Bundle b = mock(Bundle.class);
         BundleContext bc = mock(BundleContext.class);
 
+        @SuppressWarnings("unchecked")
         ServiceReference<EventListener> ref = mock(ServiceReference.class);
         when(ref.getProperty(Constants.SERVICE_ID)).thenReturn(1L);
         when(ref.getProperty(Constants.OBJECTCLASS))
diff --git a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/ConnectorFactoryTracker.java b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/ConnectorFactoryTracker.java
index 559b298..78e4700 100644
--- a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/ConnectorFactoryTracker.java
+++ b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/ConnectorFactoryTracker.java
@@ -19,6 +19,7 @@
 package org.apache.felix.http.jetty.internal;
 
 import org.apache.felix.http.base.internal.logger.SystemLogger;
+import org.apache.felix.http.base.internal.util.ServiceUtils;
 import org.apache.felix.http.jetty.ConnectorFactory;
 import org.eclipse.jetty.server.Connector;
 import org.eclipse.jetty.server.Server;
@@ -50,7 +51,7 @@ public class ConnectorFactoryTracker extends ServiceTracker<ConnectorFactory, Co
     @Override
     public Connector addingService(ServiceReference<ConnectorFactory> reference)
     {
-        ConnectorFactory factory = context.getService(reference);
+        ConnectorFactory factory = ServiceUtils.safeGetService(context, reference);
         if (factory != null) {
             Connector connector = null;
             try {
@@ -62,7 +63,7 @@ public class ConnectorFactoryTracker extends ServiceTracker<ConnectorFactory, Co
                 SystemLogger.error("Failed starting connector '" + connector + "' provided by " + reference, e);
             }
             // connector failed to start, don't continue tracking
-            context.ungetService(reference);
+            ServiceUtils.safeUngetService(context, reference);
         }
         return null;
     }
@@ -83,6 +84,6 @@ public class ConnectorFactoryTracker extends ServiceTracker<ConnectorFactory, Co
             }
         }
         this.server.removeConnector(connector);
-        context.ungetService(reference);
+        ServiceUtils.safeUngetService(context, reference);
     }
 }
diff --git a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/LoadBalancerCustomizerFactoryTracker.java b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/LoadBalancerCustomizerFactoryTracker.java
index 8ec3a09..5c955d9 100644
--- a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/LoadBalancerCustomizerFactoryTracker.java
+++ b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/LoadBalancerCustomizerFactoryTracker.java
@@ -21,6 +21,7 @@ package org.apache.felix.http.jetty.internal;
 import java.util.SortedSet;
 import java.util.TreeSet;
 
+import org.apache.felix.http.base.internal.util.ServiceUtils;
 import org.apache.felix.http.jetty.LoadBalancerCustomizerFactory;
 import org.eclipse.jetty.server.HttpConfiguration.Customizer;
 import org.osgi.framework.BundleContext;
@@ -55,7 +56,7 @@ public class LoadBalancerCustomizerFactoryTracker extends ServiceTracker<LoadBal
         if ( highestReference.compareTo(reference) == 0 )
         {
             boolean updated = false;
-            final LoadBalancerCustomizerFactory factory = bundleContext.getService(reference);
+            final LoadBalancerCustomizerFactory factory = ServiceUtils.safeGetService(bundleContext, reference);
             if (factory != null)
             {
                 final Customizer customizer = factory.createCustomizer();
@@ -66,7 +67,7 @@ public class LoadBalancerCustomizerFactoryTracker extends ServiceTracker<LoadBal
                 }
                 else
                 {
-                    bundleContext.ungetService(reference);
+                    ServiceUtils.safeUngetService(bundleContext, reference);
                 }
             }
             if ( !updated)
@@ -115,7 +116,7 @@ public class LoadBalancerCustomizerFactoryTracker extends ServiceTracker<LoadBal
                 }
                 else
                 {
-                    final LoadBalancerCustomizerFactory factory = bundleContext.getService(highestReference);
+                    final LoadBalancerCustomizerFactory factory = ServiceUtils.safeGetService(bundleContext, highestReference);
                     if (factory != null)
                     {
                         final Customizer customizer = factory.createCustomizer();
@@ -126,7 +127,7 @@ public class LoadBalancerCustomizerFactoryTracker extends ServiceTracker<LoadBal
                         }
                         else
                         {
-                            bundleContext.ungetService(highestReference);
+                            ServiceUtils.safeUngetService(bundleContext, highestReference);
                         }
                     }
 
@@ -140,7 +141,7 @@ public class LoadBalancerCustomizerFactoryTracker extends ServiceTracker<LoadBal
                     }
                 }
             } while ( !done);
-            bundleContext.ungetService(reference);
+            ServiceUtils.safeUngetService(bundleContext, reference);
         }
     }
 }
diff --git a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/RequestLogTracker.java b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/RequestLogTracker.java
index 62f4d90..6b15881 100644
--- a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/RequestLogTracker.java
+++ b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/RequestLogTracker.java
@@ -16,17 +16,23 @@
  */
 package org.apache.felix.http.jetty.internal;
 
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
 import org.apache.felix.http.base.internal.logger.SystemLogger;
+import org.apache.felix.http.base.internal.util.ServiceUtils;
 import org.eclipse.jetty.server.Request;
 import org.eclipse.jetty.server.RequestLog;
 import org.eclipse.jetty.server.Response;
-import org.osgi.framework.*;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.util.tracker.ServiceTracker;
 
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-
 /**
  * An instance of Jetty's RequestLog that dispatches to registered RequestLog services in the service registry. A filter
  * can be provided so that it only dispatches to selected services.
@@ -62,8 +68,10 @@ class RequestLogTracker extends ServiceTracker<RequestLog, RequestLog>  implemen
 
     @Override
     public RequestLog addingService(ServiceReference<RequestLog> reference) {
-        RequestLog logSvc = context.getService(reference);
-        logSvcs.put(reference, logSvc);
+        RequestLog logSvc = ServiceUtils.safeGetService(context, reference);
+        if ( logSvc != null ) {
+            logSvcs.put(reference, logSvc);
+        }
         return logSvc;
     }
 
@@ -71,7 +79,7 @@ class RequestLogTracker extends ServiceTracker<RequestLog, RequestLog>  implemen
     public void removedService(ServiceReference<RequestLog> reference, RequestLog logSvc) {
         logSvcs.remove(reference);
         naughtyStep.remove(reference);
-        context.ungetService(reference);
+        ServiceUtils.safeUngetService(context, reference);
     }
 
     @Override