package com.matrix.core.enums;
|
|
import com.matrix.core.tools.LogUtil;
|
import com.matrix.system.hive.hievEnum.SmsPlatformEnum;
|
import com.matrix.system.hive.hievEnum.SmsTypeEnum;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.PostConstruct;
|
import java.io.File;
|
import java.io.IOException;
|
import java.net.JarURLConnection;
|
import java.net.URL;
|
import java.net.URLConnection;
|
import java.util.*;
|
import java.util.jar.JarEntry;
|
import java.util.jar.JarFile;
|
|
/**
|
* TODO 这里后期改成自动扫描包下面的特定注解,而不是硬编码注册
|
*/
|
public class EnumsManager {
|
private static final List<String> CLASS_NAME = new ArrayList<>();
|
private static ClassLoader classLoader;
|
private static final String SUFFIX = ".class";
|
|
static Map<String,List<EnumsShowVo>> showAbleMap =new HashMap<>();
|
|
static {
|
// showAbleMap.put(SmsPlatformEnum.values()[0].getEnumCode(),SmsPlatformEnum.values()[0].getEnumsShowVos());
|
// showAbleMap.put(SmsTypeEnum.values()[0].getEnumCode(),SmsTypeEnum.values()[0].getEnumsShowVos());
|
|
initEnum("com.matrix");
|
}
|
|
public static List<EnumsShowVo> getShowEnum(String emumCode){
|
return showAbleMap.get(emumCode);
|
}
|
|
|
private static void initEnum(String... packages) {
|
classLoader = Thread.currentThread().getContextClassLoader();
|
|
for (String basePackage : packages) {
|
Enumeration<URL> resources = null;
|
try {
|
resources = classLoader.getResources(basePackage.replaceAll("\\.", "/"));
|
} catch (IOException e) {
|
return;
|
}
|
|
// 扫描当前工程和jar包
|
while (resources.hasMoreElements()) {
|
URL url = resources.nextElement();
|
if ("file".equals(url.getProtocol())) {
|
doFileScan(url.getPath());
|
} else if ("jar".equals(url.getProtocol())) {
|
doJarScan(basePackage, url);
|
}
|
}
|
}
|
|
// 初始化枚举数据
|
enumValue();
|
}
|
|
/**
|
* 扫描当前工程对应包下的所有类
|
*
|
* @param path
|
*/
|
private static void doFileScan(String path) {
|
String rootPath = classLoader.getResource("").getPath();
|
File pathFile = new File(path);
|
|
File[] files = pathFile.listFiles();
|
if (files == null || files.length == 0) {
|
return;
|
}
|
|
for (File file : files) {
|
if (file.isDirectory()) {
|
String nextPath = path + "/" + file.getName();
|
doFileScan(nextPath);
|
} else if (file.getName().endsWith(SUFFIX)) {
|
if (!path.contains(rootPath)) {
|
return;
|
}
|
String subStr = path.substring(rootPath.length());
|
String className = (subStr + "/" + file.getName().replaceAll(SUFFIX, "")).replaceAll("/", "\\.");
|
CLASS_NAME.add(className);
|
}
|
}
|
}
|
|
/**
|
* 扫描jar包下对应包下所有类
|
*
|
* @param basePackage
|
* @param baseURL
|
*/
|
public static void doJarScan(String basePackage, URL baseURL) {
|
basePackage = basePackage.replaceAll("\\.", "/");
|
JarFile jarFile;
|
try {
|
URLConnection urlConnection = baseURL.openConnection();
|
JarURLConnection jarUrl = (JarURLConnection) urlConnection;
|
jarFile = jarUrl.getJarFile();
|
} catch (IOException e) {
|
throw new RuntimeException("未找到资源");
|
}
|
|
Enumeration<JarEntry> entries = jarFile.entries();
|
while(entries.hasMoreElements()) {
|
JarEntry entry = entries.nextElement();
|
|
String name = entry.getName();
|
if (name.startsWith(basePackage)) {
|
if (name.endsWith(SUFFIX)) {
|
String className = name.replaceAll(SUFFIX, "").replaceAll("/", "\\.");
|
CLASS_NAME.add(className);
|
}
|
}
|
}
|
}
|
|
public static void enumValue() {
|
for (String className : CLASS_NAME) {
|
Class<?> clazz = null;
|
try {
|
clazz = classLoader.loadClass(className);
|
} catch (ClassNotFoundException e) {
|
continue;
|
}
|
|
// 判断类是否为枚举类型
|
if (!clazz.isEnum()){
|
continue;
|
}
|
|
// 判断ApiShowAble是否为类的父类
|
if (!ApiShowAble.class.isAssignableFrom(clazz)) {
|
continue;
|
}
|
|
Object[] constants = clazz.getEnumConstants();
|
ApiShowAble apiShowAble = (ApiShowAble) constants[0];
|
|
showAbleMap.put(apiShowAble.getEnumCode(), apiShowAble.getEnumsShowVos());
|
}
|
}
|
|
}
|