|
@@ -6,15 +6,17 @@ import org.dbsyncer.common.util.CollectionUtils;
|
|
import org.dbsyncer.plugin.config.Plugin;
|
|
import org.dbsyncer.plugin.config.Plugin;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.net.MalformedURLException;
|
|
import java.net.MalformedURLException;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
import java.net.URLClassLoader;
|
|
import java.net.URLClassLoader;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author AE86
|
|
* @author AE86
|
|
@@ -40,17 +42,36 @@ public class PluginFactory {
|
|
|
|
|
|
private final List<Plugin> plugins = new LinkedList<>();
|
|
private final List<Plugin> plugins = new LinkedList<>();
|
|
|
|
|
|
- private final Map<String, ConvertService> service = new ConcurrentHashMap<>();
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private Map<String, ConvertService> service;
|
|
|
|
+
|
|
|
|
+ @PostConstruct
|
|
|
|
+ private void init() {
|
|
|
|
+ Map<String, ConvertService> unmodifiable = new LinkedHashMap<>();
|
|
|
|
+ if (!CollectionUtils.isEmpty(service)) {
|
|
|
|
+ service.forEach((k, s) -> {
|
|
|
|
+ String className = s.getClass().getName();
|
|
|
|
+ unmodifiable.putIfAbsent(className, s);
|
|
|
|
+ plugins.add(new Plugin(s.getName(), className, s.getVersion(), "", true));
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
|
|
- public synchronized void loadPlugins() {
|
|
|
|
- plugins.clear();
|
|
|
|
service.clear();
|
|
service.clear();
|
|
|
|
+ service.putAll(unmodifiable);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public synchronized void loadPlugins() {
|
|
|
|
+ if (!CollectionUtils.isEmpty(plugins)) {
|
|
|
|
+ List<Plugin> unmodifiablePlugin = plugins.stream().filter(p -> p.isUnmodifiable()).collect(Collectors.toList());
|
|
|
|
+ plugins.clear();
|
|
|
|
+ plugins.addAll(unmodifiablePlugin);
|
|
|
|
+ }
|
|
try {
|
|
try {
|
|
FileUtils.forceMkdir(new File(PLUGIN_PATH));
|
|
FileUtils.forceMkdir(new File(PLUGIN_PATH));
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
logger.error(e.getMessage());
|
|
logger.error(e.getMessage());
|
|
}
|
|
}
|
|
- Collection<File> files = FileUtils.listFiles(new File(PLUGIN_PATH), new String[] {"jar"}, true);
|
|
|
|
|
|
+ Collection<File> files = FileUtils.listFiles(new File(PLUGIN_PATH), new String[]{"jar"}, true);
|
|
if (!CollectionUtils.isEmpty(files)) {
|
|
if (!CollectionUtils.isEmpty(files)) {
|
|
files.forEach(f -> loadPlugin(f));
|
|
files.forEach(f -> loadPlugin(f));
|
|
}
|
|
}
|
|
@@ -66,7 +87,7 @@ public class PluginFactory {
|
|
}
|
|
}
|
|
|
|
|
|
public List<Plugin> getPluginAll() {
|
|
public List<Plugin> getPluginAll() {
|
|
- return plugins;
|
|
|
|
|
|
+ return Collections.unmodifiableList(plugins);
|
|
}
|
|
}
|
|
|
|
|
|
public void convert(Plugin plugin, List<Map> source, List<Map> target) {
|
|
public void convert(Plugin plugin, List<Map> source, List<Map> target) {
|
|
@@ -90,11 +111,11 @@ public class PluginFactory {
|
|
try {
|
|
try {
|
|
String fileName = jar.getName();
|
|
String fileName = jar.getName();
|
|
URL url = jar.toURI().toURL();
|
|
URL url = jar.toURI().toURL();
|
|
- URLClassLoader loader = new URLClassLoader(new URL[] {url}, Thread.currentThread().getContextClassLoader());
|
|
|
|
|
|
+ URLClassLoader loader = new URLClassLoader(new URL[]{url}, Thread.currentThread().getContextClassLoader());
|
|
ServiceLoader<ConvertService> services = ServiceLoader.load(ConvertService.class, loader);
|
|
ServiceLoader<ConvertService> services = ServiceLoader.load(ConvertService.class, loader);
|
|
for (ConvertService s : services) {
|
|
for (ConvertService s : services) {
|
|
String className = s.getClass().getName();
|
|
String className = s.getClass().getName();
|
|
- service.put(className, s);
|
|
|
|
|
|
+ service.putIfAbsent(className, s);
|
|
plugins.add(new Plugin(s.getName(), className, s.getVersion(), fileName));
|
|
plugins.add(new Plugin(s.getName(), className, s.getVersion(), fileName));
|
|
}
|
|
}
|
|
} catch (MalformedURLException e) {
|
|
} catch (MalformedURLException e) {
|