Search in sources :

Example 1 with BaseAppException

use of com.littlefisher.core.exception.BaseAppException in project littlefisher-system by littlefishercoder.

the class PackageUtil method scanTypePackage.

/**
 * 扫描获取指定包路径所有类
 *
 * @param typePackage 扫描类包路径
 * @return Set<Class>
 */
public static Set<Class> scanTypePackage(String typePackage) {
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
    String pkg = ClassUtils.convertClassNameToResourcePath(typePackage) + ".class";
    /*
         * 将加载多个绝对匹配的所有Resource
         * 将首先通过ClassLoader.getResource("META-INF")加载非模式路径部分,然后进行遍历模式匹配,排除重复包路径
         */
    try {
        Set<Class> set = Sets.newHashSet();
        Resource[] resources = resolver.getResources(pkg);
        if (ArrayUtils.isNotEmpty(resources)) {
            MetadataReader metadataReader;
            for (Resource resource : resources) {
                if (resource.isReadable()) {
                    metadataReader = metadataReaderFactory.getMetadataReader(resource);
                    set.add(Class.forName(metadataReader.getClassMetadata().getClassName()));
                }
            }
        }
        return set;
    } catch (Exception e) {
        throw new BaseAppException("CORE-000007", null, e);
    }
}
Also used : BaseAppException(com.littlefisher.core.exception.BaseAppException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) Resource(org.springframework.core.io.Resource) MetadataReader(org.springframework.core.type.classreading.MetadataReader) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) BaseAppException(com.littlefisher.core.exception.BaseAppException)

Example 2 with BaseAppException

use of com.littlefisher.core.exception.BaseAppException in project littlefisher-system by littlefishercoder.

the class MyBatisConfig method customize.

@Override
public void customize(org.apache.ibatis.session.Configuration configuration) {
    String typeEnumsPackage = littleFisherProperties.getMybatis().getTypeEnumsPackage();
    final Set<Class> classes = Sets.newHashSet();
    if (StringUtil.isNotBlank(typeEnumsPackage)) {
        String[] typeEnumsPackageArray = StringUtils.tokenizeToStringArray(typeEnumsPackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        Arrays.stream(typeEnumsPackageArray).forEach(typeEnumsPackageStr -> classes.addAll(PackageUtil.scanTypePackage(typeEnumsPackage)));
        // 取得类型转换注册器
        TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
        try {
            for (Class cls : classes) {
                if (cls.isEnum()) {
                    if (IEnum.class.isAssignableFrom(cls)) {
                        typeHandlerRegistry.register(cls.getName(), EnumTypeHandler.class.getName());
                    } else {
                        // 使用原生 EnumOrdinalTypeHandler
                        typeHandlerRegistry.register(cls.getName(), EnumOrdinalTypeHandler.class.getName());
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            throw new BaseAppException("CORE-000007", null, e);
        }
    }
}
Also used : BaseAppException(com.littlefisher.core.exception.BaseAppException) TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) EnumOrdinalTypeHandler(org.apache.ibatis.type.EnumOrdinalTypeHandler) EnumTypeHandler(com.littlefisher.core.mybatis.EnumTypeHandler)

Example 3 with BaseAppException

use of com.littlefisher.core.exception.BaseAppException in project littlefisher-system by littlefishercoder.

the class ExceptionHandler method publish.

public static void publish(String errorCode, String message, Throwable t, String param1, String param2, String param3, String param4, String param5) {
    BaseAppException baseAppException;
    if (t != null && t instanceof BaseAppException) {
        baseAppException = (BaseAppException) t;
    } else if (t != null && t instanceof InvocationTargetException) {
        // 仅仅对此情况进行处理,不能进行深层检查!
        Throwable cause = t.getCause();
        if (cause instanceof BaseAppException) {
            baseAppException = (BaseAppException) cause;
        } else {
            baseAppException = new BaseAppException(errorCode, message, t, param1, param2, param3, param4, param5);
        }
    } else {
        baseAppException = new BaseAppException(errorCode, message, t, param1, param2, param3, param4, param5);
    }
    logErrorInfo(baseAppException);
    throw baseAppException;
}
Also used : BaseAppException(com.littlefisher.core.exception.BaseAppException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

BaseAppException (com.littlefisher.core.exception.BaseAppException)3 EnumTypeHandler (com.littlefisher.core.mybatis.EnumTypeHandler)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 EnumOrdinalTypeHandler (org.apache.ibatis.type.EnumOrdinalTypeHandler)1 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)1 Resource (org.springframework.core.io.Resource)1 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)1 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)1 CachingMetadataReaderFactory (org.springframework.core.type.classreading.CachingMetadataReaderFactory)1 MetadataReader (org.springframework.core.type.classreading.MetadataReader)1 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)1