AE86 3 سال پیش
والد
کامیت
0273357af4

+ 29 - 8
dbsyncer-plugin/src/main/java/org/dbsyncer/plugin/PluginFactory.java

@@ -6,15 +6,17 @@ import org.dbsyncer.common.util.CollectionUtils;
 import org.dbsyncer.plugin.config.Plugin;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import javax.annotation.PostConstruct;
 import java.io.File;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.*;
-import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
 
 /**
  * @author AE86
@@ -40,17 +42,36 @@ public class PluginFactory {
 
     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.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 {
             FileUtils.forceMkdir(new File(PLUGIN_PATH));
         } catch (IOException e) {
             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)) {
             files.forEach(f -> loadPlugin(f));
         }
@@ -66,7 +87,7 @@ public class PluginFactory {
     }
 
     public List<Plugin> getPluginAll() {
-        return plugins;
+        return Collections.unmodifiableList(plugins);
     }
 
     public void convert(Plugin plugin, List<Map> source, List<Map> target) {
@@ -90,11 +111,11 @@ public class PluginFactory {
         try {
             String fileName = jar.getName();
             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);
             for (ConvertService s : services) {
                 String className = s.getClass().getName();
-                service.put(className, s);
+                service.putIfAbsent(className, s);
                 plugins.add(new Plugin(s.getName(), className, s.getVersion(), fileName));
             }
         } catch (MalformedURLException e) {

+ 16 - 0
dbsyncer-plugin/src/main/java/org/dbsyncer/plugin/config/Plugin.java

@@ -19,14 +19,22 @@ public class Plugin {
     // Jar名称
     private String fileName;
 
+    // 是否系统预置
+    private boolean unmodifiable;
+
     public Plugin() {
     }
 
     public Plugin(String name, String className, String version, String fileName) {
+        this(name, className, version, fileName, false);
+    }
+
+    public Plugin(String name, String className, String version, String fileName, boolean unmodifiable) {
         this.name = name;
         this.className = className;
         this.version = version;
         this.fileName = fileName;
+        this.unmodifiable = unmodifiable;
     }
 
     public String getName() {
@@ -63,4 +71,12 @@ public class Plugin {
         this.fileName = fileName;
         return this;
     }
+
+    public boolean isUnmodifiable() {
+        return unmodifiable;
+    }
+
+    public void setUnmodifiable(boolean unmodifiable) {
+        this.unmodifiable = unmodifiable;
+    }
 }

+ 40 - 0
dbsyncer-plugin/src/main/java/org/dbsyncer/plugin/service/DemoConvertServiceImpl.java

@@ -0,0 +1,40 @@
+package org.dbsyncer.plugin.service;
+
+import org.dbsyncer.common.spi.ConvertService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+import java.util.Map;
+
+@Component
+public class DemoConvertServiceImpl implements ConvertService {
+
+    private final Logger logger = LoggerFactory.getLogger(getClass());
+
+    /**
+     * 版本号
+     */
+    @Value(value = "${info.app.version}")
+    private String version;
+
+    @Override
+    public void convert(List<Map> source, List<Map> target) {
+    }
+
+    @Override
+    public void convert(String event, Map source, Map target) {
+        logger.info(String.format("插件正在处理同步数据,事件:%s,数据:%s", event, source));
+    }
+
+    @Override
+    public String getVersion() {
+        return version;
+    }
+
+    public String getName() {
+        return "Demo";
+    }
+}

+ 3 - 1
dbsyncer-web/src/main/resources/public/upload/upload.html

@@ -27,6 +27,8 @@
                         <simple>新建一个类,比如MyPlugin,实现接口ConvertService方法</simple>
 <pre>package org.test;
 
+import org.dbsyncer.common.spi.ConvertService;
+
 import java.util.List;
 import java.util.Map;
 
@@ -118,7 +120,7 @@ public class MyPlugin implements ConvertService{
                                 <td th:text="${p?.className}"/>
                                 <td th:text="${p?.version}"/>
                                 <td th:text="${p?.fileName}"/>
-                                <td><a th:id="${p?.name}" class='fa fa-remove fa-2x pluginDelete dbsyncer_pointer' title='暂不支持' aria-disabled="true"></a></td>
+                                <td><a th:if="${not p?.unmodifiable}" th:id="${p?.name}" class='fa fa-remove fa-2x pluginDelete dbsyncer_pointer' title='暂不支持' aria-disabled="true"></a></td>
                             </tr>
                             </tbody>
                         </table>

BIN
dbsyncer-web/src/main/resources/static/img/plugin/jar.png