/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed 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.internna.iwebmvc.spring.io;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.internna.iwebmvc.utils.ClassUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.AssignableTypeFilter;
import org.springframework.util.StringUtils;
/**
* Classpath scanner that detects implementations of a given interface or annotated classes.
*
* @author Jose Noheda
* @since 1.0
*/
public class ClasspathScanner {
private static final String baseIWebMvcModelPackage = "org.internna.iwebmvc.model";
protected final Log logger = LogFactory.getLog(getClass());
private String baseDomainPackage;
protected ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
public void setBaseDomainPackage(String baseDomainPackage) {
this.baseDomainPackage = baseDomainPackage;
}
@SuppressWarnings("unchecked")
private <T> void collect(Set<Class<T>>candidates, Class<?> clazz, String pkg) throws ClassNotFoundException {
scanner.resetFilters(false);
scanner.addIncludeFilter(clazz.isAnnotation() ? new AnnotationTypeFilter((Class<? extends Annotation>) clazz) : new AssignableTypeFilter(clazz));
if (logger.isInfoEnabled()) logger.info("Scanning classpath [" + pkg + "] looking for " + (clazz.isAnnotation() ? "@" + clazz.getSimpleName() + " annotated" : clazz.getSimpleName()) + " domain entities");
Set<BeanDefinition> definitions = scanner.findCandidateComponents(pkg);
for (BeanDefinition def : definitions) {
if (logger.isDebugEnabled()) logger.debug("Found matching class [" + def.getBeanClassName() + "]");
candidates.add(ClassUtils.forName(def.getBeanClassName()));
}
}
public <T> Set<Class<T>> collect(Class<?> clazz) throws ClassNotFoundException {
Set<Class<T>> candidates = new HashSet<Class<T>>();
collect(candidates, clazz, baseIWebMvcModelPackage);
if (StringUtils.hasText(baseDomainPackage)) collect(candidates, clazz, baseDomainPackage);
if (candidates.isEmpty()) logger.info("No candidates found for [" + (clazz.isAnnotation() ? "@" : "") + clazz.getSimpleName() + "]. Functionality may be disabled!!!");
return candidates;
}
}