package com.hotent.base.typefilter;

import java.io.IOException;

import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import com.baomidou.mybatisplus.core.toolkit.StringPool;

/**
 * 1. 如果使用分布式定时任务xxl-job 则不注入job模块中的bean
 * @author jason
 *
 */
public class CustomTypeFilter implements TypeFilter,EnvironmentAware {
	
	private Environment environment;
	
	public static final String XXL_JOB_ENABLED = "xxl.job.enabled";
	
	public static final String XXL_JOB_ADMIN_ADDRESS = "xxl.job.admin.addresses";
	
	@Override
	public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
			throws IOException {
		ClassMetadata classMetadata = metadataReader.getClassMetadata();
		String className = classMetadata.getClassName();
		if(StringPool.TRUE.equals(environment.getProperty(XXL_JOB_ENABLED)) && className.startsWith("com.hotent.job")) {
			return true;
		}
		return false;
	}

	@Override
	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}

}
