Repository: cxf Updated Branches: refs/heads/master 762719c29 -> 8be9f4b6a
[CXF-5704] Improve Spring auto-discovery code to locate classes implementing matching interfaces Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/8be9f4b6 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/8be9f4b6 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/8be9f4b6 Branch: refs/heads/master Commit: 8be9f4b6a780a36f949c05aaafcfb664cabe5b13 Parents: 762719c Author: Sergey Beryozkin <[email protected]> Authored: Tue Apr 22 11:59:16 2014 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Tue Apr 22 11:59:16 2014 +0100 ---------------------------------------------------------------------- .../cxf/common/util/SpringClasspathScanner.java | 35 ++++++++++++++++---- .../cxf/systest/jaxrs/discovery/BookStore.java | 3 +- .../jaxrs/discovery/BookStoreInterface.java | 26 +++++++++++++++ 3 files changed, 56 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/8be9f4b6/core/src/main/java/org/apache/cxf/common/util/SpringClasspathScanner.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/common/util/SpringClasspathScanner.java b/core/src/main/java/org/apache/cxf/common/util/SpringClasspathScanner.java index 90a8538..a3c0676 100644 --- a/core/src/main/java/org/apache/cxf/common/util/SpringClasspathScanner.java +++ b/core/src/main/java/org/apache/cxf/common/util/SpringClasspathScanner.java @@ -24,6 +24,7 @@ import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; @@ -53,9 +54,12 @@ class SpringClasspathScanner extends ClasspathScanner { final Map< Class< ? extends Annotation >, Collection< Class< ? > > > classes = new HashMap< Class< ? extends Annotation >, Collection< Class< ? > > >(); + final Map< Class< ? extends Annotation >, Collection< String > > matchingInterfaces = + new HashMap< Class< ? extends Annotation >, Collection< String > >(); for (Class< ? extends Annotation > annotation: annotations) { - classes.put(annotation, new ArrayList< Class < ? > >()); + classes.put(annotation, new HashSet< Class < ? > >()); + matchingInterfaces.put(annotation, new HashSet< String >()); } if (basePackages == null || basePackages.isEmpty()) { @@ -69,7 +73,9 @@ class SpringClasspathScanner extends ClasspathScanner { + (scanAllPackages ? "" : ClassUtils.convertClassNameToResourcePath(basePackage)) + ALL_CLASS_FILES; - final Resource[] resources = resolver.getResources(packageSearchPath); + final Resource[] resources = resolver.getResources(packageSearchPath); + final Map<String, String[]> nonMatchingClasses = new HashMap<String, String[]>(); + for (final Resource resource: resources) { final MetadataReader reader = factory.getMetadataReader(resource); final AnnotationMetadata metadata = reader.getAnnotationMetadata(); @@ -79,12 +85,29 @@ class SpringClasspathScanner extends ClasspathScanner { } for (Class< ? extends Annotation > annotation: annotations) { - if (metadata.isAnnotated(annotation.getName())) { - classes.get(annotation).add(ClassLoaderUtils.loadClass(metadata.getClassName(), getClass())); + boolean concreteClass = !metadata.isInterface() && !metadata.isAbstract(); + if (metadata.isAnnotated(annotation.getName())) { + if (concreteClass) { + classes.get(annotation).add( + ClassLoaderUtils.loadClass(metadata.getClassName(), getClass())); + } else { + matchingInterfaces.get(annotation).add(metadata.getClassName()); + } + } else if (concreteClass && metadata.getInterfaceNames().length > 0) { + nonMatchingClasses.put(metadata.getClassName(), metadata.getInterfaceNames()); } } - - } + } + for (Map.Entry<Class<? extends Annotation>, Collection<String>> e1 : matchingInterfaces.entrySet()) { + for (Map.Entry<String, String[]> e2 : nonMatchingClasses.entrySet()) { + for (String intName : e2.getValue()) { + if (e1.getValue().contains(intName)) { + classes.get(e1.getKey()).add(ClassLoaderUtils.loadClass(e2.getKey(), getClass())); + break; + } + } + } + } } return classes; http://git-wip-us.apache.org/repos/asf/cxf/blob/8be9f4b6/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/discovery/BookStore.java ---------------------------------------------------------------------- diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/discovery/BookStore.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/discovery/BookStore.java index 6cc2f2b..a980736 100644 --- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/discovery/BookStore.java +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/discovery/BookStore.java @@ -28,8 +28,7 @@ import javax.ws.rs.PathParam; import org.apache.cxf.systest.jaxrs.validation.BookWithValidation; -@Path("/bookstore/") -public class BookStore { +public class BookStore implements BookStoreInterface { @POST @Path("/books") @Valid http://git-wip-us.apache.org/repos/asf/cxf/blob/8be9f4b6/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/discovery/BookStoreInterface.java ---------------------------------------------------------------------- diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/discovery/BookStoreInterface.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/discovery/BookStoreInterface.java new file mode 100644 index 0000000..298310d --- /dev/null +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/discovery/BookStoreInterface.java @@ -0,0 +1,26 @@ +/** + * 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.cxf.systest.jaxrs.discovery; + +import javax.ws.rs.Path; + +@Path("/bookstore/") +public interface BookStoreInterface { + +}
