|
...
|
...
|
@@ -4,6 +4,8 @@ import com.ruoyi.common.utils.StringUtils; |
|
|
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
|
|
|
import com.zhonglai.luhui.device.protocol.factory.analysis.ProtocolParserFactory;
|
|
|
|
import com.zhonglai.luhui.device.protocol.factory.plugins.InitPlugins;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.util.FileCopyUtils;
|
|
...
|
...
|
@@ -15,10 +17,11 @@ import java.lang.reflect.Modifier; |
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLClassLoader;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.jar.JarEntry;
|
|
|
|
import java.util.jar.JarFile;
|
|
|
|
|
|
...
|
...
|
@@ -26,12 +29,13 @@ import java.util.jar.JarFile; |
|
|
|
* 自定义的类加载器
|
|
|
|
*/
|
|
|
|
public class PluginsClassLoader extends URLClassLoader {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(PluginsClassLoader.class);
|
|
|
|
/**
|
|
|
|
* 存放类
|
|
|
|
*/
|
|
|
|
private static Map<String,PluginsClassLoader> jarMap = new HashMap<>();
|
|
|
|
private static ConcurrentHashMap<String,PluginsClassLoader> jarMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
private static Map<String,Class<?>> classMap = new HashMap<>();
|
|
|
|
private static ConcurrentHashMap<String,Class<?>> classMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
private PluginsClassLoader(URL[] urls) {
|
|
|
|
super(urls,PluginsClassLoader.class.getClassLoader());
|
|
...
|
...
|
@@ -41,16 +45,30 @@ public class PluginsClassLoader extends URLClassLoader { |
|
|
|
* 卸载jar
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
private static void unloadJar(String... filePaths) throws IOException {
|
|
|
|
public static void unloadJar(String... filePaths) throws IOException {
|
|
|
|
for (String filePath:filePaths)
|
|
|
|
{
|
|
|
|
String key = InitPlugins.toJarPath(filePath);
|
|
|
|
if(jarMap.containsKey(key))
|
|
|
|
{
|
|
|
|
PluginsClassLoader pluginsClassLoader = jarMap.get(key);
|
|
|
|
jarMap.remove(key);
|
|
|
|
pluginsClassLoader.close();
|
|
|
|
|
|
|
|
// 移除并关闭 ClassLoader
|
|
|
|
PluginsClassLoader pluginsClassLoader = jarMap.remove(key);
|
|
|
|
if (pluginsClassLoader != null) {
|
|
|
|
try {
|
|
|
|
pluginsClassLoader.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
log.error("Failed to close ClassLoader for JAR: " + key, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 移除 classMap 中的类
|
|
|
|
Iterator<Map.Entry<String, Class<?>>> it = classMap.entrySet().iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Map.Entry<String, Class<?>> entry = it.next();
|
|
|
|
if (entry.getValue().getClassLoader() == pluginsClassLoader) {
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
File file = new File(key);
|
|
|
|
if(file.exists())
|
|
|
|
{
|
|
...
|
...
|
@@ -61,6 +79,7 @@ public class PluginsClassLoader extends URLClassLoader { |
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 更新jar
|
|
|
|
* @throws IOException
|
|
...
|
...
|
@@ -70,11 +89,13 @@ public class PluginsClassLoader extends URLClassLoader { |
|
|
|
for (File file:filePaths)
|
|
|
|
{
|
|
|
|
String filePath = file.getAbsolutePath();
|
|
|
|
System.out.println("绝对路径:"+filePath);
|
|
|
|
|
|
|
|
unloadJar(filePath);
|
|
|
|
|
|
|
|
String key = InitPlugins.toJarPath(filePath);
|
|
|
|
|
|
|
|
FileCopyUtils.copy(new File(filePath),new File(key));
|
|
|
|
copyFile(filePath,key);
|
|
|
|
|
|
|
|
PluginsClassLoader pluginsClassLoader = new PluginsClassLoader(new URL[]{new URL("file:"+key)});
|
|
|
|
jarMap.put(key,pluginsClassLoader);
|
|
...
|
...
|
@@ -82,40 +103,31 @@ public class PluginsClassLoader extends URLClassLoader { |
|
|
|
laodJar(new File(key),pluginsClassLoader);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
log.error("更新jar异常",e);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void laodJar(File jarfile,ClassLoader classLoader)
|
|
|
|
{
|
|
|
|
JarFile jarFile = null;
|
|
|
|
try {
|
|
|
|
jarFile = new JarFile(jarfile);
|
|
|
|
private static void laodJar(File jarfile, ClassLoader classLoader) {
|
|
|
|
try (JarFile jarFile = new JarFile(jarfile)) {
|
|
|
|
Enumeration<JarEntry> entries = jarFile.entries();
|
|
|
|
while (entries.hasMoreElements())
|
|
|
|
{
|
|
|
|
while (entries.hasMoreElements()) {
|
|
|
|
JarEntry jarEntry = entries.nextElement();
|
|
|
|
String entryName = jarEntry.getName();
|
|
|
|
if (entryName != null && entryName.endsWith(".class")) {
|
|
|
|
entryName = entryName.replace("/", ".").substring(0, entryName.lastIndexOf("."));
|
|
|
|
Class<?> clas = classLoader.loadClass(entryName);
|
|
|
|
System.out.println(ProtocolParserFactory.class.isAssignableFrom(clas));;
|
|
|
|
if(SpringUtils.isSpringBean())
|
|
|
|
{
|
|
|
|
if (SpringUtils.isSpringBean()) {
|
|
|
|
Object object = loadBean(clas);
|
|
|
|
}
|
|
|
|
classMap.put(entryName,clas);
|
|
|
|
classMap.put(entryName, clas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
} catch (IOException | ClassNotFoundException e) {
|
|
|
|
log.error("加载jar:"+jarfile.getAbsolutePath()+" 失败",e);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private static <T> T loadBean(Class<T> clas)
|
|
...
|
...
|
@@ -181,4 +193,34 @@ public class PluginsClassLoader extends URLClassLoader { |
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void copyFile(String sourcePathStr, String targetPathStr) {
|
|
|
|
Path sourcePath = Paths.get(sourcePathStr);
|
|
|
|
Path targetPath = Paths.get(targetPathStr);
|
|
|
|
|
|
|
|
// 检查源文件是否存在
|
|
|
|
if (!Files.exists(sourcePath)) {
|
|
|
|
log.error("Source file does not exist: " + sourcePath.toString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 检查目标文件所在目录是否存在,如果不存在则创建
|
|
|
|
Path targetDir = targetPath.getParent();
|
|
|
|
if (!Files.exists(targetDir)) {
|
|
|
|
try {
|
|
|
|
Files.createDirectories(targetDir);
|
|
|
|
} catch (IOException e) {
|
|
|
|
log.error("Failed to create target directory: " + targetDir.toString(),e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 执行文件复制
|
|
|
|
try {
|
|
|
|
Files.copy(sourcePath, targetPath);
|
|
|
|
System.out.println("File copied successfully from " + sourcePath.toString() + " to " + targetPath.toString());
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException("Failed to copy file from " + sourcePath.toString() + " to " + targetPath.toString(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|