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

org.jbatis.dds.solon.starter.config.MongoPlusAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package org.jbatis.dds.solon.starter.config;

import com.mongodb.MongoException;

import org.jbatis.dds.kernel.cache.global.HandlerCache;
import org.jbatis.dds.kernel.cache.global.InterceptorCache;
import org.jbatis.dds.kernel.execute.SqlExecute;
import org.jbatis.dds.kernel.handlers.DocumentHandler;
import org.jbatis.dds.kernel.handlers.MetaObjectHandler;
import org.jbatis.dds.kernel.interceptor.Interceptor;
import org.jbatis.dds.kernel.interceptor.business.BlockAttackInnerInterceptor;
import org.jbatis.dds.kernel.interceptor.business.LogInterceptor;
import org.jbatis.dds.kernel.service.IService;
import org.jbatis.dds.kernel.service.impl.ServiceImpl;
import org.jbatis.dds.kernel.strategy.convert.ConversionService;
import org.jbatis.dds.kernel.strategy.convert.ConversionStrategy;
import org.jbatis.dds.kernel.toolkit.CollUtil;
import org.jbatis.dds.solon.starter.property.MongoDBCollectionProperty;
import org.jbatis.dds.solon.starter.property.MongoDBLogProperty;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.AppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * MongoPlus自动注入配置
 *
 **/
public class MongoPlusAutoConfiguration {

    private final SqlExecute sqlExecute;

    private final MongoDBLogProperty mongoDBLogProperty;

    private final MongoDBCollectionProperty mongoDBCollectionProperty;

    Logger logger = LoggerFactory.getLogger(MongoPlusAutoConfiguration.class);

    public MongoPlusAutoConfiguration(@Inject SqlExecute sqlExecute, MongoDBLogProperty mongoDBLogProperty, MongoDBCollectionProperty mongoDBCollectionProperty){
        this.sqlExecute = sqlExecute;
        this.mongoDBLogProperty = mongoDBLogProperty;
        this.mongoDBCollectionProperty = mongoDBCollectionProperty;
        AppContext context = Solon.context();
        context.subBeansOfType(IService.class, bean -> {
            if (bean instanceof ServiceImpl){
                setSqlExecute((ServiceImpl) bean,bean.getGenericityClazz());
            }
        });
        //拿到转换器
        setConversion(context);
        //拿到自动填充处理器
        setMetaObjectHandler(context);
        //拿到Document处理器
        setDocumentHandler(context);
        //拿到拦截器
        setInterceptor(context);
    }

    /**
     * 从Bean中拿到Document的处理器
    */
    private void setSqlExecute(ServiceImpl serviceImpl,Class clazz) {
        sqlExecute.init(clazz);
        serviceImpl.setClazz(clazz);
        serviceImpl.setSqlOperation(sqlExecute);
    }

    /**
     * 从Bean中拿到转换器
     */
    @SuppressWarnings("unchecked")
    private void setConversion(AppContext context){
        context.getBeansOfType(ConversionStrategy.class).forEach(conversionStrategy -> {
            try {
                Type[] genericInterfaces = conversionStrategy.getClass().getGenericInterfaces();
                for (Type anInterface : genericInterfaces) {
                    ParameterizedType parameterizedType = (ParameterizedType) anInterface;
                    if (parameterizedType.getRawType().equals(ConversionStrategy.class)){
                        Class clazz = (Class) parameterizedType.getActualTypeArguments()[0];
                        ConversionService.appendConversion(clazz,conversionStrategy);
                        break;
                    }
                }
            }catch (Exception e){
                logger.error("Unknown converter type");
                throw new MongoException("Unknown converter type");
            }
        });
    }

    /**
     * 从Bean中拿到自动填充策略
     */
    private void setMetaObjectHandler(AppContext context){
        context.getBeansOfType(MetaObjectHandler.class).forEach(metaObjectHandler -> HandlerCache.metaObjectHandler = metaObjectHandler);
    }

    private void setDocumentHandler(AppContext appContext){
        appContext.getBeansOfType(DocumentHandler.class).forEach(documentHandler -> HandlerCache.documentHandler = documentHandler);
    }

    /**
     * 从Bean中拿到拦截器
     */
    private void setInterceptor(AppContext context){
        List interceptors = new ArrayList<>();
        if (mongoDBLogProperty.getLog()){
            interceptors.add(new LogInterceptor());
        }
        if (mongoDBCollectionProperty.getBlockAttackInner()){
            interceptors.add(new BlockAttackInnerInterceptor());
        }
        List interceptorCollection = context.getBeansOfType(Interceptor.class);
        if (CollUtil.isNotEmpty(interceptorCollection)){
            interceptors.addAll(interceptorCollection);
        }
        InterceptorCache.interceptors = interceptors.stream().sorted(Comparator.comparingInt(Interceptor::getOrder)).collect(Collectors.toList());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy