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

com.iqiny.silly.spring.scan.AutoScanSillyConfigContent Maven / Gradle / Ivy

/*
 *  Copyright  iqiny.com
 *
 *  https://gitee.com/iqiny/silly
 *
 *  project name:silly-spring
 *  project description:top silly project pom.xml file
 */
package com.iqiny.silly.spring.scan;

import com.iqiny.silly.common.util.SillyAssert;
import com.iqiny.silly.common.util.SillyReflectUtil;
import com.iqiny.silly.common.util.StringUtils;
import com.iqiny.silly.core.base.SillyCategory;
import com.iqiny.silly.core.base.SillyContext;
import com.iqiny.silly.core.base.SillyFactory;
import com.iqiny.silly.core.base.SillyProperties;
import com.iqiny.silly.core.base.core.*;
import com.iqiny.silly.core.base.impl.DefaultSillyFactory;
import com.iqiny.silly.core.common.SillyCoreUtil;
import com.iqiny.silly.core.config.property.SillyProcessProperty;
import com.iqiny.silly.core.config.property.SillyPropertyHandle;
import com.iqiny.silly.core.data.SillyCategoryJsonData;
import com.iqiny.silly.core.data.SillyOptionData;
import com.iqiny.silly.core.resume.SillyResume;
import com.iqiny.silly.core.savehandle.SillyOnceSourceData;
import com.iqiny.silly.spring.SpringSillyConfigContent;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReaderFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import static org.springframework.util.StringUtils.tokenizeToStringArray;

@SuppressWarnings("all")
public class AutoScanSillyConfigContent extends SpringSillyConfigContent {

    private final static Log log = LogFactory.getLog(AutoScanSillyConfigContent.class);

    protected static final MetadataReaderFactory METADATA_READER_FACTORY = new CachingMetadataReaderFactory();

    protected String entityScanPackage;

    protected final List> masterClazzList = new ArrayList<>();
    protected final List> nodeClazzList = new ArrayList<>();
    protected final List> variableClazzList = new ArrayList<>();
    protected final List> resumeClazzList = new ArrayList<>();
    protected final List> optionDataClazzList = new ArrayList<>();

    protected final List> processPropertyClazzList = new ArrayList<>();
    protected final List> propertyHandleClazzList = new ArrayList<>();
    protected final List> nodeSourceDataClazzList = new ArrayList<>();
    protected final List> rootDataClazzList = new ArrayList<>();

    public AutoScanSillyConfigContent(SillyProperties sillyProperties, SillyContext sillyContext) {
        super(sillyProperties, sillyContext);
    }


    @Override
    protected void preInit() {
        super.preInit();
        this.entityScanPackage = sillyProperties.getEntityScanPackage();
    }

    @Override
    protected void initFiled() {
        masterClazzList.clear();
        nodeClazzList.clear();
        variableClazzList.clear();
        resumeClazzList.clear();
        optionDataClazzList.clear();

        processPropertyClazzList.clear();
        propertyHandleClazzList.clear();
        nodeSourceDataClazzList.clear();
        rootDataClazzList.clear();

        processPropertyClazzList.add(sillyProperties.getProcessPropertyClazz());
        propertyHandleClazzList.add(sillyProperties.getPropertyHandleClazz());
        nodeSourceDataClazzList.add(sillyProperties.getDefaultNodeSourceDataClazz());
        rootDataClazzList.add(sillyProperties.getSillyRootDataClazz());

        super.initFiled();
    }


    @Override
    protected void initBaseSillyFactoryList() {

        if (StringUtils.isNotEmpty(entityScanPackage)) {
            try {
                scanRefreshClasses(entityScanPackage, (clazz) -> {
                    if (SillyCategory.class.isAssignableFrom(clazz)) {
                        addCategorySet(((SillyCategory) SillyReflectUtil.newInstance(clazz)).usedCategory());
                    }
                    if (SillyEntity.class.isAssignableFrom(clazz)) {
                        addSillyEntityClazzList(clazz);
                    }
                    if (SillyMaster.class.isAssignableFrom(clazz)) {
                        masterClazzList.add(clazz);
                    } else if (SillyNode.class.isAssignableFrom(clazz)) {
                        nodeClazzList.add(clazz);
                    } else if (SillyVariable.class.isAssignableFrom(clazz)) {
                        variableClazzList.add(clazz);
                    } else if (SillyResume.class.isAssignableFrom(clazz)) {
                        resumeClazzList.add(clazz);
                    } else if (SillyOptionData.class.isAssignableFrom(clazz)) {
                        optionDataClazzList.add(clazz);
                    } else if (SillyCategoryJsonData.class.isAssignableFrom(clazz)) {
                        jsonDataClazz = clazz;
                    } else if (SillyProcessProperty.class.isAssignableFrom(clazz)) {
                        processPropertyClazzList.add(clazz);
                    } else if (SillyPropertyHandle.class.isAssignableFrom(clazz)) {
                        propertyHandleClazzList.add(clazz);
                    } else if (SillyOnceSourceData.class.isAssignableFrom(clazz)) {
                        nodeSourceDataClazzList.add(clazz);
                    } else if (SillyRootData.class.isAssignableFrom(clazz)) {
                        rootDataClazzList.add(clazz);
                    }
                });
            } catch (IOException e) {
                log.warn("包扫描异常!" + entityScanPackage + e.getMessage());
            }
        }

        Set categorys = allCategorySet();
        for (String category : categorys) {
            SillyFactory sillyFactory = new DefaultSillyFactory(category);
            Class masterClass = SillyCoreUtil.availableOne(category, masterClazzList);
            sillyFactory.setSillyClazz(masterClass);
            Class nodeClass = SillyCoreUtil.availableOne(category, nodeClazzList);
            sillyFactory.setSillyClazz(nodeClass);
            Class variableClazz = SillyCoreUtil.availableOne(category, variableClazzList);
            sillyFactory.setSillyClazz(variableClazz);
            Class resumeClazz = SillyCoreUtil.availableOne(category, resumeClazzList);
            sillyFactory.setSillyClazz(resumeClazz);
            Class optionDataClazz = SillyCoreUtil.availableOne(category, optionDataClazzList);
            sillyFactory.setSillyClazz(optionDataClazz);

            Class processPropertyClazz = SillyCoreUtil.availableOne(category, processPropertyClazzList);
            sillyFactory.setSillyClazz(processPropertyClazz);
            Class propertyHandleClazz = SillyCoreUtil.availableOne(category, propertyHandleClazzList);
            sillyFactory.setSillyClazz(propertyHandleClazz);
            Class nodeSourceDataClazz = SillyCoreUtil.availableOne(category, nodeSourceDataClazzList);
            sillyFactory.setSillyClazz(nodeSourceDataClazz);
            Class sillyRootDataClazz = SillyCoreUtil.availableOne(category, rootDataClazzList);
            sillyFactory.setSillyClazz(sillyRootDataClazz);
            addSillyFactory(sillyFactory);
        }
    }

    /**
     * 借鉴 MyBatis 扫描包的方法
     *
     * @param packagePatterns 包扫描表达式
     * @param assignableTypes 基本数据类型
     * @throws IOException
     */
    private void scanRefreshClasses(String packagePatterns, Consumer classConsumer)
            throws IOException {
        SillyAssert.notEmpty(packagePatterns, "包扫描表达式不可为空");

        String[] packagePatternArray = tokenizeToStringArray(packagePatterns,
                ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packagePattern : packagePatternArray) {
            Resource[] resources = RESOURCE_PATTERN_RESOLVER.getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
                    org.springframework.util.ClassUtils.convertClassNameToResourcePath(packagePattern) + "/**/*.class");
            for (Resource resource : resources) {
                try {
                    ClassMetadata classMetadata = METADATA_READER_FACTORY.getMetadataReader(resource).getClassMetadata();
                    Class clazz = Class.forName(classMetadata.getClassName());
                    classConsumer.accept(clazz);
                } catch (Throwable e) {
                    log.warn(resource + ",加载失败:" + e.getMessage());
                }
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy