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

com.zusmart.basic.scanner.support.AbstractScanner Maven / Gradle / Ivy

Go to download

基础模块,提供配置,日志,SPI,图排序,路径匹配,资源扫描,包扫描,常用工具类

There is a newer version: 0.0.3
Show newest version
package com.zusmart.basic.scanner.support;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.zusmart.basic.config.Configuration;
import com.zusmart.basic.extension.ExtensionLoader;
import com.zusmart.basic.logging.Logger;
import com.zusmart.basic.logging.LoggerFactory;
import com.zusmart.basic.matcher.PathMatcher;
import com.zusmart.basic.scanner.Scanner;
import com.zusmart.basic.scanner.ScannerClassProcessor;
import com.zusmart.basic.scanner.ScannerClassResult;
import com.zusmart.basic.scanner.ScannerFilter;
import com.zusmart.basic.scanner.ScannerResourceProcessor;
import com.zusmart.basic.scanner.ScannerResourceResult;
import com.zusmart.basic.scanner.ScannerResult;
import com.zusmart.basic.scanner.ScannerResultType;
import com.zusmart.basic.toolkit.ClassUtils;
import com.zusmart.basic.toolkit.StringUtils;

public abstract class AbstractScanner implements Scanner {

	private static final Logger logger = LoggerFactory.getLogger(AbstractScanner.class);
	private static final Map> classCaches = new ConcurrentHashMap>();

	public static final String SEPARATOR = ".";
	public static final String PROTOCOL_FILE = "file";
	public static final String PROTOCOL_JARF = "jar";
	public static final String CLASS_SUFFIX = ".class";

	private ClassLoader classLoader;
	private PathMatcher pathMatcher;
	private Set classPatterns;
	private Set resourcePatterns;
	private Set scannerFilters;
	private Set classProcessors;
	private Set resourceProcessors;

	public AbstractScanner(ClassLoader classLoader) {
		this.classLoader = ClassUtils.getClassLoader(AbstractScanner.class);
		this.pathMatcher = ExtensionLoader.getExtension(PathMatcher.class, Configuration.getProperty(KEY, VAL));
		this.classPatterns = new LinkedHashSet();
		this.resourcePatterns = new LinkedHashSet();
		this.scannerFilters = new LinkedHashSet();
		this.classProcessors = new LinkedHashSet();
		this.resourceProcessors = new LinkedHashSet();
	}

	@Override
	public void setPathMatcher(PathMatcher pathMatcher) {
		if (null == pathMatcher) {
			logger.warn("ignore path matcher , path matcher is null");
			return;
		}
		this.pathMatcher = pathMatcher;
	}

	@Override
	public PathMatcher getPathMatcher() {
		return this.pathMatcher;
	}

	@Override
	public void setClassLoader(ClassLoader classLoader) {
		if (null == classLoader) {
			return;
		}
		this.classLoader = classLoader;
	}

	@Override
	public ClassLoader getClassLoader() {
		return this.classLoader;
	}

	@Override
	public void attachClassPattern(String classPattern) {
		if (null == classPattern) {
			logger.warn("ignore attach class pattern , class pattern is null");
			return;
		}
		this.classPatterns.add(this.formatClassPattern(classPattern));
	}

	@Override
	public void attachClassPatterns(String... classPatterns) {
		if (null == classPatterns || classPatterns.length == 0) {
			return;
		}
		for (String pattern : classPatterns) {
			if (StringUtils.isBlank(pattern)) {
				continue;
			}
			this.attachClassPattern(pattern);
		}
	}

	@Override
	public void attachClassPatterns(Set classPatterns) {
		if (null == classPatterns || classPatterns.isEmpty()) {
			return;
		}
		for (String pattern : classPatterns) {
			if (StringUtils.isBlank(pattern)) {
				continue;
			}
			this.attachClassPattern(pattern);
		}
	}

	@Override
	public void detachClassPattern(String classPattern) {
		if (null == classPattern) {
			logger.warn("ignore detach class pattern , class pattern is null");
			return;
		}
		this.classPatterns.remove(this.formatClassPattern(classPattern));
	}

	@Override
	public void attachResourcePattern(String resourcePattern) {
		if (null == resourcePattern) {
			logger.warn("ignore attach resource pattern , resource pattern is null");
			return;
		}
		this.resourcePatterns.add(this.formatResourcePattern(resourcePattern));
	}

	@Override
	public void attachResourcePatterns(String... resourcePatterns) {
		if (null == resourcePatterns || resourcePatterns.length == 0) {
			return;
		}
		for (String pattern : resourcePatterns) {
			if (StringUtils.isBlank(pattern)) {
				continue;
			}
			this.attachResourcePattern(pattern);
		}
	}

	@Override
	public void attachResourcePatterns(Set resourcePatterns) {
		if (null == resourcePatterns || resourcePatterns.isEmpty()) {
			return;
		}
		for (String pattern : resourcePatterns) {
			if (StringUtils.isBlank(pattern)) {
				continue;
			}
			this.attachResourcePattern(pattern);
		}
	}

	@Override
	public void detachResourcePattern(String resourcePattern) {
		if (null == resourcePattern) {
			logger.warn("ignore detach resource pattern , resource pattern is null");
			return;
		}
		this.resourcePatterns.remove(this.formatResourcePattern(resourcePattern));
	}

	@Override
	public void attachScannerFilter(ScannerFilter scannerFilter) {
		if (null == scannerFilter) {
			logger.warn("ignore attach scanner filter , scanner filter is null");
			return;
		}
		this.scannerFilters.add(scannerFilter);
	}

	@Override
	public void detachScannerFilter(ScannerFilter scannerFilter) {
		if (null == scannerFilter) {
			logger.warn("ignore detach scanner filter , scanner filter is null");
			return;
		}
		this.scannerFilters.remove(scannerFilter);
	}

	@Override
	public void attachScannerClassProcessor(ScannerClassProcessor classProcessor) {
		if (null == classProcessor) {
			logger.warn("ignore attach class processor , class processor is null");
			return;
		}
		this.classProcessors.add(classProcessor);
	}

	@Override
	public void detachScannerClassProcessor(ScannerClassProcessor classProcessor) {
		if (null == classProcessor) {
			logger.warn("ignore detach class processor , class processor is null");
			return;
		}
		this.classProcessors.remove(classProcessor);
	}

	@Override
	public void attachScannerResourceProcessor(ScannerResourceProcessor resourceProcessor) {
		if (null == resourceProcessor) {
			logger.warn("ignore attach resource processor , resource processor is null");
			return;
		}
		this.resourceProcessors.add(resourceProcessor);
	}

	@Override
	public void detachScannerResourceProcessor(ScannerResourceProcessor resourceProcessor) {
		if (null == resourceProcessor) {
			logger.warn("ignore detach resource processor , resource processor is null");
			return;
		}
		this.resourceProcessors.remove(resourceProcessor);
	}

	@Override
	public Set scan() {
		Set result = new LinkedHashSet();
		Set scannerClassPackages = this.resolveScannerClassPackages();
		Set scannerResourcePackages = this.resolveScannerResourcePackages();
		Set target = this.doScan(scannerClassPackages, scannerResourcePackages);
		if (null != target) {
			result.addAll(target);
		}
		return result;
	}

	protected String formatResourcePattern(String resourcePattern) {
		if (resourcePattern.startsWith("/")) {
			resourcePattern = resourcePattern.substring(1);
		}
		return resourcePattern;
	}

	protected String formatClassPattern(String classPattern) {
		if (this.pathMatcher.isPattern(classPattern)) {
			return classPattern;
		}
		if (classPattern.endsWith(SEPARATOR)) {
			return classPattern + "**";
		} else {
			return classPattern + SEPARATOR + "**";
		}
	}

	protected boolean isFilterPass(String path, ScannerResultType type) {
		if (null == this.scannerFilters || this.scannerFilters.size() == 0) {
			return true;
		}
		Set scannerPatterns = type == ScannerResultType.Class ? this.classPatterns : this.resourcePatterns;
		for (ScannerFilter filter : this.scannerFilters) {
			if (filter.execute(path, type, scannerPatterns)) {
				return true;
			}
		}
		return false;
	}

	protected boolean isProcessorClassPass(ScannerClassResult scannerClassResult) {
		if (null == this.classProcessors || this.classProcessors.size() == 0) {
			return true;
		}
		for (ScannerClassProcessor processor : this.classProcessors) {
			if (processor.execute(scannerClassResult)) {
				return true;
			}
		}
		return false;
	}

	protected boolean isProcessorResourcePass(ScannerResourceResult scannerResourceResult) {
		if (null == this.resourceProcessors || this.resourceProcessors.size() == 0) {
			return true;
		}
		for (ScannerResourceProcessor processor : this.resourceProcessors) {
			if (processor.execute(scannerResourceResult)) {
				return true;
			}
		}
		return false;
	}

	protected Set resolveScannerClassPackages() {
		Set result = new LinkedHashSet();
		List list = new ArrayList();
		if (null != this.classPatterns && this.classPatterns.size() > 0) {
			for (String pattern : this.classPatterns) {
				String newPattern = pattern;
				if (!this.pathMatcher.isPattern(newPattern)) {
					if (newPattern.endsWith(SEPARATOR)) {
						newPattern += "**";
					} else {
						newPattern += SEPARATOR + "**";
					}
				}
				int index = -1;
				if ((index = newPattern.indexOf("*")) != -1) {
					newPattern = newPattern.substring(0, index);
				}
				if ((index = newPattern.indexOf("?")) != -1) {
					newPattern = newPattern.substring(0, index);
				}
				if ((index = newPattern.indexOf("{")) != -1) {
					newPattern = newPattern.substring(0, index);
				}
				if ((index = newPattern.indexOf("}")) != -1) {
					newPattern = newPattern.substring(0, index);
				}
				if (newPattern.indexOf(SEPARATOR) != -1) {
					newPattern = newPattern.substring(0, newPattern.lastIndexOf(SEPARATOR));
				}
				if (!list.contains(newPattern)) {
					list.add(newPattern);
					logger.debug("found class pattern {}", newPattern);
				}
			}
		}
		Collections.sort(list);
		result.addAll(list);
		return result;
	}

	protected Set resolveScannerResourcePackages() {
		Set result = new LinkedHashSet();
		List list = new ArrayList();
		if (null != this.resourcePatterns && this.resourcePatterns.size() > 0) {
			for (String pattern : this.resourcePatterns) {
				String newPattern = pattern;
				if (newPattern.indexOf("/") != -1) {
					newPattern = newPattern.substring(0, newPattern.lastIndexOf("/"));
				} else {
					newPattern = "";
				}
				if (!list.contains(newPattern)) {
					list.add(newPattern);
					logger.debug("found resource pattern {}", newPattern);
				}
			}
		}
		Collections.sort(list);
		result.addAll(list);
		return result;
	}

	protected Class classForName(String className) {
		try {
			Class targetClass = classCaches.get(className);
			if (null == targetClass) {
				classCaches.put(className, Class.forName(className));
				targetClass = classCaches.get(className);
			}
			return targetClass;
		} catch (ClassNotFoundException e) {
			// IGNORE
		} catch (Throwable t) {
			// IGNORE
		}
		return null;
	}

	protected abstract Set doScan(Set scannerClassPackages, Set scannerResourcePackages);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy