001/** 002 * Copyright 2010-2016 Boxfuse GmbH 003 * <p/> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p/> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p/> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.avaje.classpath.scanner.core; 017 018import org.avaje.classpath.scanner.ClassFilter; 019import org.avaje.classpath.scanner.Resource; 020import org.avaje.classpath.scanner.ResourceFilter; 021import org.avaje.classpath.scanner.internal.EnvironmentDetection; 022import org.avaje.classpath.scanner.internal.ResourceAndClassScanner; 023import org.avaje.classpath.scanner.internal.scanner.classpath.ClassPathScanner; 024import org.avaje.classpath.scanner.internal.scanner.classpath.android.AndroidScanner; 025import org.avaje.classpath.scanner.internal.scanner.filesystem.FileSystemScanner; 026 027import java.util.List; 028 029/** 030 * Scanner for Resources and Classes. 031 */ 032public class Scanner implements org.avaje.classpath.scanner.ClassPathScanner { 033 034 private final ResourceAndClassScanner resourceAndClassScanner; 035 036 private final FileSystemScanner fileSystemScanner = new FileSystemScanner(); 037 038 public Scanner(ClassLoader classLoader) { 039 if (EnvironmentDetection.isAndroid()) { 040 resourceAndClassScanner = new AndroidScanner(classLoader); 041 } else { 042 resourceAndClassScanner = new ClassPathScanner(classLoader); 043 } 044 } 045 046 /** 047 * Scans this location for resources matching the given predicate. 048 * <p> 049 * The location can have a prefix of <code>filesystem:</code> or <code>classpath:</code> to determine 050 * how to scan. If no prefix is used then classpath scan is the default. 051 * </p> 052 * 053 * @param location The location to start searching. Subdirectories are also searched. 054 * @param predicate The predicate used to match resource names. 055 * @return The resources that were found. 056 */ 057 public List<Resource> scanForResources(Location location, ResourceFilter predicate) { 058 059 if (location.isFileSystem()) { 060 return fileSystemScanner.scanForResources(location, predicate); 061 } 062 return resourceAndClassScanner.scanForResources(location, predicate); 063 } 064 065 /** 066 * Scans this location for resources matching the given predicate. 067 * <p> 068 * The location can have a prefix of <code>filesystem:</code> or <code>classpath:</code> to determine 069 * how to scan. If no prefix is used then classpath scan is the default. 070 * </p> 071 * 072 * @param location The location to start searching. Subdirectories are also searched. 073 * @param predicate The predicate used to match resource names. 074 * @return The resources that were found. 075 */ 076 @Override 077 public List<Resource> scanForResources(String location, ResourceFilter predicate) { 078 return scanForResources(new Location(location), predicate); 079 } 080 081 /** 082 * Scans the classpath for classes under the specified package matching the given predicate. 083 * 084 * @param location The package in the classpath to start scanning. Subpackages are also scanned. 085 * @param predicate The predicate used to match scanned classes. 086 * @return The classes found matching the predicate 087 */ 088 public List<Class<?>> scanForClasses(Location location, ClassFilter predicate) { 089 return resourceAndClassScanner.scanForClasses(location, predicate); 090 } 091 092 /** 093 * Scans the classpath for classes under the specified package matching the given predicate. 094 * 095 * @param location The package in the classpath to start scanning. Subpackages are also scanned. 096 * @param predicate The predicate used to match scanned classes. 097 * @return The classes found matching the predicate 098 */ 099 @Override 100 public List<Class<?>> scanForClasses(String location, ClassFilter predicate) { 101 return scanForClasses(new Location(location), predicate); 102 } 103 104}