1 |
30936368
|
Andreas Kohlbecker
|
// $Id$
|
2 |
|
|
/**
|
3 |
|
|
* Copyright (C) 2014 EDIT
|
4 |
|
|
* European Distributed Institute of Taxonomy
|
5 |
|
|
* http://www.e-taxonomy.eu
|
6 |
|
|
*
|
7 |
|
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8 |
|
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9 |
|
|
*/
|
10 |
|
|
package eu.etaxonomy.cdm.model;
|
11 |
|
|
|
12 |
|
|
import java.util.ArrayList;
|
13 |
|
|
import java.util.Collection;
|
14 |
|
|
import java.util.List;
|
15 |
|
|
|
16 |
|
|
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
17 |
|
|
import org.springframework.beans.factory.config.BeanDefinition;
|
18 |
|
|
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
|
19 |
|
|
import org.springframework.util.ClassUtils;
|
20 |
|
|
|
21 |
|
|
/**
|
22 |
|
|
* @author a.kohlbecker
|
23 |
|
|
* @date Jul 31, 2014
|
24 |
|
|
*
|
25 |
|
|
*/
|
26 |
|
|
public class CdmTypeScanner<T> extends ClassPathScanningCandidateComponentProvider {
|
27 |
|
|
|
28 |
|
|
static final String defaultBasePackage = "eu/etaxonomy/cdm/";
|
29 |
|
|
|
30 |
|
|
boolean includeAbstract;
|
31 |
|
|
boolean includeInterfaces;
|
32 |
|
|
|
33 |
45e9e404
|
Andreas Müller
|
public CdmTypeScanner(boolean considerAbstract, boolean considerInterfaces) {
|
34 |
|
|
super(false);
|
35 |
|
|
this.includeAbstract = considerAbstract;
|
36 |
|
|
this.includeInterfaces = considerInterfaces;
|
37 |
|
|
}
|
38 |
30936368
|
Andreas Kohlbecker
|
|
39 |
45e9e404
|
Andreas Müller
|
public final Collection<Class<? extends T>> scanTypesIn(String basePackage) {
|
40 |
|
|
String _basePackage = basePackage == null ? defaultBasePackage : basePackage;
|
41 |
|
|
List<Class<? extends T>> classes = new ArrayList<Class<? extends T>>();
|
42 |
|
|
for (BeanDefinition candidate : findCandidateComponents(_basePackage)) {
|
43 |
|
|
Class cls = ClassUtils.resolveClassName(candidate.getBeanClassName(),
|
44 |
|
|
ClassUtils.getDefaultClassLoader());
|
45 |
|
|
classes.add(cls);
|
46 |
30936368
|
Andreas Kohlbecker
|
}
|
47 |
45e9e404
|
Andreas Müller
|
return classes;
|
48 |
|
|
}
|
49 |
30936368
|
Andreas Kohlbecker
|
|
50 |
45e9e404
|
Andreas Müller
|
/**
|
51 |
|
|
* Determine whether the given bean definition qualifies as candidate.
|
52 |
|
|
* <p>The special implementation checks whether the class is concrete
|
53 |
|
|
* or abstract or an interface. The latter two conditions depend on
|
54 |
|
|
* the state of the two boolean fields includeAnstract, includeInterface.
|
55 |
|
|
*
|
56 |
|
|
* @param beanDefinition the bean definition to check
|
57 |
|
|
* @return whether the bean definition qualifies as a candidate component
|
58 |
|
|
*/
|
59 |
|
|
@Override
|
60 |
|
|
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
|
61 |
|
|
return (beanDefinition.getMetadata().isIndependent()
|
62 |
|
|
&& (beanDefinition.getMetadata().isConcrete()
|
63 |
|
|
|| (includeAbstract && beanDefinition.getMetadata().isAbstract())
|
64 |
|
|
|| (includeInterfaces && beanDefinition.getMetadata().isInterface())
|
65 |
|
|
)
|
66 |
|
|
);
|
67 |
|
|
}
|
68 |
30936368
|
Andreas Kohlbecker
|
|
69 |
|
|
}
|