插件依赖第三方jar多版本问题兼容
This commit is contained in:
15
src/main/java/com/cool/Welcome.java
Normal file
15
src/main/java/com/cool/Welcome.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package com.cool;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class Welcome {
|
||||||
|
|
||||||
|
@RequestMapping("/")
|
||||||
|
public String welcome() {
|
||||||
|
return "welcome";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import com.cool.core.config.PluginJson;
|
|||||||
import com.cool.core.exception.CoolPreconditions;
|
import com.cool.core.exception.CoolPreconditions;
|
||||||
import com.cool.core.plugin.config.DynamicJarClassLoader;
|
import com.cool.core.plugin.config.DynamicJarClassLoader;
|
||||||
import com.cool.core.util.AnnotationUtils;
|
import com.cool.core.util.AnnotationUtils;
|
||||||
|
import com.cool.core.util.CompilerUtils;
|
||||||
import com.cool.modules.plugin.entity.PluginInfoEntity;
|
import com.cool.modules.plugin.entity.PluginInfoEntity;
|
||||||
import com.cool.modules.plugin.service.PluginInfoService;
|
import com.cool.modules.plugin.service.PluginInfoService;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -74,19 +75,7 @@ public class DynamicJarLoaderService {
|
|||||||
try (JarFile jarFile = ((JarURLConnection) jarUrl.openConnection()).getJarFile()) {
|
try (JarFile jarFile = ((JarURLConnection) jarUrl.openConnection()).getJarFile()) {
|
||||||
Enumeration<JarEntry> entries = jarFile.entries();
|
Enumeration<JarEntry> entries = jarFile.entries();
|
||||||
while (entries.hasMoreElements()) {
|
while (entries.hasMoreElements()) {
|
||||||
JarEntry jarEntry = entries.nextElement();
|
loadClass(entries, dynamicJarClassLoader, plugins);
|
||||||
if (jarEntry.getName().endsWith(".class")) {
|
|
||||||
String className = jarEntry.getName().replace('/', '.').substring(0,
|
|
||||||
jarEntry.getName().length() - 6);
|
|
||||||
try {
|
|
||||||
// 加载类
|
|
||||||
Class<?> clazz = dynamicJarClassLoader.loadClass(className);
|
|
||||||
if (AnnotationUtils.hasCoolPluginAnnotation(clazz)) {
|
|
||||||
plugins.add(clazz);
|
|
||||||
}
|
|
||||||
} catch (NoClassDefFoundError ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,6 +88,38 @@ public class DynamicJarLoaderService {
|
|||||||
return pluginJson;
|
return pluginJson;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加载class
|
||||||
|
*/
|
||||||
|
private static void loadClass(Enumeration<JarEntry> entries,
|
||||||
|
DynamicJarClassLoader dynamicJarClassLoader, List<Class<?>> plugins)
|
||||||
|
throws ClassNotFoundException {
|
||||||
|
JarEntry jarEntry = entries.nextElement();
|
||||||
|
|
||||||
|
String entryName = jarEntry.getName();
|
||||||
|
if (!entryName.endsWith(".class")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String className = entryName.replace('/', '.').substring(0, entryName.length() - 6);
|
||||||
|
if (entryName.startsWith(CompilerUtils.META_INF_VERSIONS)) {
|
||||||
|
// 处理多版本类
|
||||||
|
String jdkVersion = CompilerUtils.getJdkVersion();
|
||||||
|
if (!entryName.startsWith(CompilerUtils.META_INF_VERSIONS + jdkVersion)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 替换版本目录
|
||||||
|
className = className.replace((CompilerUtils.META_INF_VERSIONS + jdkVersion).replace("/", ".") + ".", "");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// 加载类
|
||||||
|
Class<?> clazz = dynamicJarClassLoader.loadClass(className);
|
||||||
|
if (AnnotationUtils.hasCoolPluginAnnotation(clazz)) {
|
||||||
|
plugins.add(clazz);
|
||||||
|
}
|
||||||
|
} catch (NoClassDefFoundError | UnsupportedClassVersionError ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注册插件
|
* 注册插件
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import cn.hutool.core.io.FileUtil;
|
|||||||
import com.mybatisflex.processor.MybatisFlexProcessor;
|
import com.mybatisflex.processor.MybatisFlexProcessor;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
import java.lang.management.RuntimeMXBean;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.annotation.processing.Processor;
|
import javax.annotation.processing.Processor;
|
||||||
@@ -17,6 +19,22 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class CompilerUtils {
|
public class CompilerUtils {
|
||||||
|
|
||||||
|
public final static String META_INF_VERSIONS = "META-INF/versions/";
|
||||||
|
|
||||||
|
// jdk版本
|
||||||
|
private static String JVM_VERSION = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取jdk版本
|
||||||
|
*/
|
||||||
|
public static String getJdkVersion() {
|
||||||
|
if (JVM_VERSION == null) {
|
||||||
|
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
|
||||||
|
JVM_VERSION = runtimeMXBean.getSpecVersion();
|
||||||
|
}
|
||||||
|
return JVM_VERSION;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建文件, 先删除在创建
|
* 创建文件, 先删除在创建
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user