Project

General

Profile

Download (2.61 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2014 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.model;
10

    
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.List;
14

    
15
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
16
import org.springframework.beans.factory.config.BeanDefinition;
17
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
18
import org.springframework.util.ClassUtils;
19

    
20
/**
21
 * @author a.kohlbecker
22
 * @since Jul 31, 2014
23
 */
24
public class CdmTypeScanner<T> extends ClassPathScanningCandidateComponentProvider {
25

    
26
    static  final String defaultBasePackage = "eu/etaxonomy/cdm/";
27

    
28
    boolean includeAbstract;
29
    boolean includeInterfaces;
30

    
31
    public CdmTypeScanner(boolean considerAbstract, boolean considerInterfaces) {
32
        super(false);
33
        this.includeAbstract = considerAbstract;
34
        this.includeInterfaces = considerInterfaces;
35
    }
36

    
37
    public final Collection<Class<? extends T>> scanTypesIn(String basePackage) {
38
        String _basePackage = basePackage == null ? defaultBasePackage : basePackage;
39
        List<Class<? extends T>> classes = new ArrayList<Class<? extends T>>();
40
        for (BeanDefinition candidate : findCandidateComponents(_basePackage)) {
41
                @SuppressWarnings("unchecked")
42
                Class<? extends T> cls = (Class<? extends T>)ClassUtils.resolveClassName(candidate.getBeanClassName(),
43
                        ClassUtils.getDefaultClassLoader());
44
                classes.add(cls);
45
        }
46
        return classes;
47
    }
48

    
49
    /**
50
     * Determine whether the given bean definition qualifies as candidate.
51
     * <p>The special implementation checks whether the class is concrete
52
     * or abstract or an interface. The latter two conditions depend on
53
     * the state of the two boolean fields includeAnstract, includeInterface.
54
     *
55
     * @param beanDefinition the bean definition to check
56
     * @return whether the bean definition qualifies as a candidate component
57
     */
58
    @Override
59
    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
60
        return (beanDefinition.getMetadata().isIndependent()
61
                && (beanDefinition.getMetadata().isConcrete()
62
                  || (includeAbstract && beanDefinition.getMetadata().isAbstract())
63
                  || (includeInterfaces && beanDefinition.getMetadata().isInterface())
64
                 )
65
                );
66
    }
67

    
68
    }
(4-4/10)