All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onetwo.common.file.FileScanner Maven / Gradle / Ivy

There is a newer version: 4.7.2
Show newest version
package org.onetwo.common.file;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.onetwo.common.exception.BaseException;
import org.onetwo.common.log.MyLoggerFactory;
import org.onetwo.common.utils.Assert;
import org.onetwo.common.utils.LangUtils;
import org.slf4j.Logger;

public class FileScanner {
	protected final Logger logger = MyLoggerFactory.getLogger(this.getClass());

//	private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
//	private MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory();
	private String resourcePattern;
	
	public FileScanner(){}
	
	public FileScanner(String resourcePattern) {
		super();
		this.resourcePattern = resourcePattern;
	}
	
	protected List scanFiles(String path){
		List files = FileUtils.listFile(path, resourcePattern);
		return files;
	}

	public  List scan(FileCallback filter, String... pathsToScan) {
		Assert.notNull(filter);
		List classesToBound = new ArrayList();
		try {
			int count = 0;
			for (String path : pathsToScan) {
				if(!path.endsWith("/"))
					path += "/";
				List resources = this.scanFiles(path);
				if (LangUtils.isEmpty(resources))
					continue;
				for (File resource : resources) {
//					System.out.println("fond file: " + resource.getPath());
					T obj = filter.doWithCandidate(resource, count++);
					if(obj!=null){
						classesToBound.add(obj);
					}
				}
			}
		} catch (Exception e) {
			throw new BaseException("scan resource in[" + LangUtils.toString(pathsToScan) + "] error : " + e.getMessage(), e);
		}
		return classesToBound;
	}

	public void setResourcePattern(String resourcePattern) {
		this.resourcePattern = resourcePattern;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy