Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0997B107F1 for ; Wed, 11 Feb 2015 10:26:13 +0000 (UTC) Received: (qmail 96812 invoked by uid 500); 11 Feb 2015 10:26:13 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 96754 invoked by uid 500); 11 Feb 2015 10:26:13 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 96745 invoked by uid 99); 11 Feb 2015 10:26:12 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 11 Feb 2015 10:26:12 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id BE8FBE01CB; Wed, 11 Feb 2015 10:26:12 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sergeyb@apache.org To: commits@cxf.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: cxf git commit: CXF-6132: Provide JAX-RS ServletContextInitializer. Adding more @HandlesTypes classes. Date: Wed, 11 Feb 2015 10:26:12 +0000 (UTC) Repository: cxf Updated Branches: refs/heads/3.0.x-fixes 3832db055 -> 8a06e66a3 CXF-6132: Provide JAX-RS ServletContextInitializer. Adding more @HandlesTypes classes. Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/8a06e66a Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/8a06e66a Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/8a06e66a Branch: refs/heads/3.0.x-fixes Commit: 8a06e66a346c974b73bc07f9eca3c7fcd8ac8df3 Parents: 3832db0 Author: reta Authored: Tue Jan 27 19:58:52 2015 -0500 Committer: Sergey Beryozkin Committed: Wed Feb 11 10:25:57 2015 +0000 ---------------------------------------------------------------------- .../JaxrsServletContainerInitializer.java | 59 ++++++++++++++------ 1 file changed, 43 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/8a06e66a/rt/rs/http-sci/src/main/java/org/apache/cxf/jaxrs/servlet/JaxrsServletContainerInitializer.java ---------------------------------------------------------------------- diff --git a/rt/rs/http-sci/src/main/java/org/apache/cxf/jaxrs/servlet/JaxrsServletContainerInitializer.java b/rt/rs/http-sci/src/main/java/org/apache/cxf/jaxrs/servlet/JaxrsServletContainerInitializer.java index 7be0198..4eb3575 100644 --- a/rt/rs/http-sci/src/main/java/org/apache/cxf/jaxrs/servlet/JaxrsServletContainerInitializer.java +++ b/rt/rs/http-sci/src/main/java/org/apache/cxf/jaxrs/servlet/JaxrsServletContainerInitializer.java @@ -18,10 +18,10 @@ */ package org.apache.cxf.jaxrs.servlet; -import java.io.IOException; import java.lang.annotation.Annotation; -import java.util.Arrays; +import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.logging.Logger; @@ -36,11 +36,16 @@ import javax.ws.rs.core.Application; import javax.ws.rs.ext.Provider; import org.apache.cxf.common.logging.LogUtils; -import org.apache.cxf.common.util.ClasspathScanner; -@HandlesTypes(Application.class) -public class JaxrsServletContainerInitializer implements ServletContainerInitializer { +@HandlesTypes({ Application.class, Provider.class, Path.class }) +public class JaxrsServletContainerInitializer implements ServletContainerInitializer { private static final Logger LOG = LogUtils.getL7dLogger(JaxrsServletContainerInitializer.class); + private static final String IGNORE_PACKAGE = "org.apache.cxf"; + + private static final String IGNORE_APP_PATH_PARAM = "jaxrs.application.address.ignore"; + private static final String SERVICE_CLASSES_PARAM = "jaxrs.serviceClasses"; + private static final String PROVIDERS_PARAM = "jaxrs.providers"; + private static final String JAXRS_APPLICATION_PARAM = "javax.ws.rs.Application"; @Override public void onStartup(final Set< Class< ? > > classes, final ServletContext ctx) throws ServletException { @@ -49,19 +54,41 @@ public class JaxrsServletContainerInitializer implements ServletContainerInitial final Class< ? > application = findCandidate(classes); if (application != null) { - servlet.setInitParameter("javax.ws.rs.Application", application.getName()); + servlet.setInitParameter(JAXRS_APPLICATION_PARAM, application.getName()); + servlet.setInitParameter(IGNORE_APP_PATH_PARAM, "false"); } else { - try { - final Map< Class< ? extends Annotation >, Collection< Class< ? > > > providersAndResources = - ClasspathScanner.findClasses(Arrays.asList(ClasspathScanner.WILDCARD), - Arrays.asList(Provider.class, Path.class)); - - servlet.setInitParameter("jaxrs.providers", getClassNames(providersAndResources.get(Provider.class))); - servlet.setInitParameter("jaxrs.serviceClasses", getClassNames(providersAndResources.get(Path.class))); - } catch (final ClassNotFoundException | IOException ex) { - throw new ServletException("Unabled to perform classpath scan", ex); + final Map< Class< ? extends Annotation >, Collection< Class< ? > > > providersAndResources = + groupByAnnotations(classes); + + servlet.setInitParameter(PROVIDERS_PARAM, getClassNames(providersAndResources.get(Provider.class))); + servlet.setInitParameter(SERVICE_CLASSES_PARAM, getClassNames(providersAndResources.get(Path.class))); + } + } + + private Map< Class< ? extends Annotation >, Collection< Class< ? > > > groupByAnnotations( + final Set< Class< ? > > classes) { + + final Map< Class< ? extends Annotation >, Collection< Class< ? > > > grouped = + new HashMap< Class< ? extends Annotation >, Collection< Class< ? > > >(); + + grouped.put(Provider.class, new ArrayList< Class< ? > >()); + grouped.put(Path.class, new ArrayList< Class< ? > >()); + + for (final Class< ? > clazz: classes) { + if (!classShouldBeIgnored(clazz)) { + for (final Class< ? extends Annotation > annotation: grouped.keySet()) { + if (clazz.isAnnotationPresent(annotation)) { + grouped.get(annotation).add(clazz); + } + } } } + + return grouped; + } + + private static boolean classShouldBeIgnored(final Class clazz) { + return clazz.getPackage().getName().startsWith(IGNORE_PACKAGE); } private static String getClassNames(final Collection< Class< ? > > classes) { @@ -78,7 +105,7 @@ public class JaxrsServletContainerInitializer implements ServletContainerInitial private static Class< ? > findCandidate(final Set< Class< ? > > classes) { for (final Class< ? > clazz: classes) { - if (!clazz.getPackage().equals(JaxrsServletContainerInitializer.class.getPackage())) { + if (Application.class.isAssignableFrom(clazz) && !classShouldBeIgnored(clazz)) { LOG.fine("Found JAX-RS application to initialize: " + clazz.getName()); return clazz; }