fix (插件管理): 代码合并问题恢复

This commit is contained in:
fit2-zhao 2021-09-10 15:51:27 +08:00 committed by fit2-zhao
parent ab961ed5cb
commit e97de2f6bd
1 changed files with 11 additions and 41 deletions

View File

@ -14,7 +14,6 @@ import io.metersphere.plugin.core.ui.PluginResource;
import io.metersphere.service.utils.CommonUtil;
import io.metersphere.service.utils.MsClassLoader;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -26,8 +25,6 @@ import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
@ -85,7 +82,7 @@ public class PluginService {
private List<PluginResourceDTO> getMethod(String path, String fileName) {
List<PluginResourceDTO> resources = new LinkedList<>();
try {
// classLoader.unloadJarFile(path);
// classLoader.unloadJarFile(path);
this.loadJar(path);
List<Class<?>> classes = CommonUtil.getSubClass(fileName);
for (Class<?> aClass : classes) {
@ -104,28 +101,6 @@ public class PluginService {
return resources;
}
private void closeJar(String jarPath) {
File jarFile = new File(jarPath);
Method method = null;
try {
method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
} catch (NoSuchMethodException | SecurityException e1) {
e1.printStackTrace();
}
// 获取方法的访问权限以便写回
try {
method.setAccessible(true);
// 获取系统类加载器
URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
URL url = jarFile.toURI().toURL();
method.invoke(classLoader, url);
classLoader.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
private void loadJar(String jarPath) {
File jarFile = new File(jarPath);
// 从URLClassLoader类中获取类所在文件夹的方法jar也可以认为是一个文件夹
@ -147,26 +122,21 @@ public class PluginService {
method.invoke(classLoader, url);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
private <T> Predicate<T> distinctByName(Function<? super T, ?> keys) {
Map<Object, Boolean> sen = new ConcurrentHashMap<>();
return t -> sen.putIfAbsent(keys.apply(t), Boolean.TRUE) == null;
}
public void loadPlugins() {
try {
PluginExample example = new PluginExample();
List<Plugin> plugins = pluginMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(plugins)) {
plugins = plugins.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
-> new TreeSet<>(Comparator.comparing(Plugin::getPluginId))), ArrayList::new));
try {
PluginExample example = new PluginExample();
List<Plugin> plugins = pluginMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(plugins)) {
plugins.forEach(item -> {
this.loadJar(item.getSourcePath());
});
plugins = plugins.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
-> new TreeSet<>(Comparator.comparing(Plugin::getPluginId))), ArrayList::new));
if (CollectionUtils.isNotEmpty(plugins)) {
plugins.forEach(item -> {
this.loadJar(item.getSourcePath());
});
}
}
} catch (Exception e) {
LogUtil.error(e);