From b6f36dadb2d86ee92a87fd66859f1f8e1b450dda Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Fri, 7 Mar 2014 17:10:51 +1100 Subject: [PATCH] Add appropriate headers to allow ServiceLoader mechanism to work with OSGi. --- jetty-annotations/pom.xml | 1 + .../annotations/AnnotationConfiguration.java | 22 +++++++++++++------ .../javax-websocket-server-impl/pom.xml | 2 ++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/jetty-annotations/pom.xml b/jetty-annotations/pom.xml index efaba6abb4b..34aefc907e7 100644 --- a/jetty-annotations/pom.xml +++ b/jetty-annotations/pom.xml @@ -44,6 +44,7 @@ javax.servlet.*;version="[2.6.0,3.2)",* + osgi.serviceloader; filter:="(osgi.serviceloader=javax.servlet.ServletContainerInitializer)";cardinality:=multiple, osgi.extender; filter:="(osgi.extender=osgi.serviceloader.processor)" diff --git a/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationConfiguration.java b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationConfiguration.java index 541ac475ba2..9eb87351e49 100644 --- a/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationConfiguration.java +++ b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationConfiguration.java @@ -793,15 +793,23 @@ public class AnnotationConfiguration extends AbstractConfiguration //We use the ServiceLoader mechanism to find the ServletContainerInitializer classes to inspect long start = 0; - if (LOG.isDebugEnabled()) - start = System.nanoTime(); - ServiceLoader loadedInitializers = ServiceLoader.load(ServletContainerInitializer.class, context.getClassLoader()); + + ClassLoader old = Thread.currentThread().getContextClassLoader(); + ServiceLoader loadedInitializers = null; + try + { + if (LOG.isDebugEnabled()) + start = System.nanoTime(); + Thread.currentThread().setContextClassLoader(context.getClassLoader()); + loadedInitializers = ServiceLoader.load(ServletContainerInitializer.class); + } + finally + { + Thread.currentThread().setContextClassLoader(old); + } + if (LOG.isDebugEnabled()) LOG.debug("Service loaders found in {}ms", (TimeUnit.MILLISECONDS.convert((System.nanoTime()-start), TimeUnit.NANOSECONDS))); - - //no ServletContainerInitializers found - if (loadedInitializers == null) - return Collections.emptyList(); ServletContainerInitializerOrdering initializerOrdering = getInitializerOrdering(context); diff --git a/jetty-websocket/javax-websocket-server-impl/pom.xml b/jetty-websocket/javax-websocket-server-impl/pom.xml index 6844a9f42c6..c3136a315d5 100644 --- a/jetty-websocket/javax-websocket-server-impl/pom.xml +++ b/jetty-websocket/javax-websocket-server-impl/pom.xml @@ -63,6 +63,8 @@ javax.websocket.server Implementation org.eclipse.jetty.websocket.jsr356.server.*;version="${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}" + osgi.extender; filter:="(osgi.extender=osgi.serviceloader.registrar)" + osgi.serviceloader; osgi.serviceloader=javax.servlet.ServletContainerInitializer