|
1 | 1 | package org.lowcoder.api.framework.plugin;
|
2 | 2 |
|
3 |
| -import java.util.ArrayList; |
4 |
| -import java.util.Comparator; |
5 |
| -import java.util.LinkedHashMap; |
6 |
| -import java.util.List; |
7 | 3 | import java.util.Map;
|
8 | 4 |
|
9 |
| -import org.apache.commons.collections4.CollectionUtils; |
10 |
| -import org.lowcoder.plugin.api.LowcoderPlugin; |
11 |
| -import org.lowcoder.plugin.api.LowcoderServices; |
| 5 | +import org.lowcoder.plugin.LowcoderPlugin; |
| 6 | +import org.lowcoder.sdk.config.CommonConfig; |
| 7 | +import org.springframework.boot.system.ApplicationHome; |
| 8 | +import org.springframework.context.ConfigurableApplicationContext; |
12 | 9 | import org.springframework.stereotype.Component;
|
13 | 10 |
|
14 | 11 | import jakarta.annotation.PostConstruct;
|
15 |
| -import jakarta.annotation.PreDestroy; |
16 | 12 | import lombok.RequiredArgsConstructor;
|
17 | 13 | import lombok.extern.slf4j.Slf4j;
|
18 | 14 |
|
|
21 | 17 | @Slf4j
|
22 | 18 | public class LowcoderPluginManager
|
23 | 19 | {
|
24 |
| - private final LowcoderServices lowcoderServices; |
25 |
| - private final PluginLoader pluginLoader; |
| 20 | + private final ConfigurableApplicationContext applicationContext; |
| 21 | + private final CommonConfig common; |
| 22 | + private final ApplicationHome applicationHome; |
| 23 | + |
| 24 | + private Map<String, LowcoderPlugin> plugins; |
| 25 | + |
26 | 26 |
|
27 |
| - private Map<String, LowcoderPlugin> plugins = new LinkedHashMap<>(); |
28 |
| - |
29 | 27 | @PostConstruct
|
30 | 28 | private void loadPlugins()
|
31 | 29 | {
|
32 |
| - registerPlugins(); |
33 |
| - List<LowcoderPlugin> sorted = new ArrayList<>(plugins.values()); |
34 |
| - sorted.sort(Comparator.comparing(LowcoderPlugin::loadOrder)); |
35 | 30 |
|
36 |
| - for (LowcoderPlugin plugin : sorted) |
37 |
| - { |
38 |
| - PluginExecutor executor = new PluginExecutor(plugin, lowcoderServices); |
39 |
| - executor.start(); |
40 |
| - } |
41 | 31 | }
|
42 |
| - |
43 |
| - @PreDestroy |
44 |
| - public void unloadPlugins() |
45 |
| - { |
46 |
| - for (LowcoderPlugin plugin : plugins.values()) |
47 |
| - { |
48 |
| - try |
49 |
| - { |
50 |
| - plugin.unload(); |
51 |
| - } |
52 |
| - catch(Throwable cause) |
53 |
| - { |
54 |
| - log.warn("Error unloading plugin: {}!", plugin.pluginId(), cause); |
55 |
| - } |
56 |
| - } |
57 |
| - } |
58 |
| - |
59 |
| - public List<PluginInfo> getLoadedPluginsInfo() |
60 |
| - { |
61 |
| - List<PluginInfo> infos = new ArrayList<>(); |
62 |
| - for (LowcoderPlugin plugin : plugins.values()) |
63 |
| - { |
64 |
| - infos.add(new PluginInfo(plugin.pluginId(), plugin.description(), plugin.pluginInfo())); |
65 |
| - } |
66 |
| - return infos; |
67 |
| - } |
68 |
| - |
69 |
| - private void registerPlugins() |
70 |
| - { |
71 |
| - List<LowcoderPlugin> loaded = pluginLoader.loadPlugins(); |
72 |
| - if (CollectionUtils.isNotEmpty(loaded)) |
73 |
| - { |
74 |
| - for (LowcoderPlugin plugin : loaded) |
75 |
| - { |
76 |
| - if (!plugins.containsKey(plugin.pluginId())) |
77 |
| - { |
78 |
| - log.info("Registered plugin: {} ({})", plugin.pluginId(), plugin.getClass().getName()); |
79 |
| - plugins.put(plugin.pluginId(), plugin); |
80 |
| - } |
81 |
| - else |
82 |
| - { |
83 |
| - log.warn("Plugin {} already registered (from: {}), skipping {}.", plugin.pluginId(), |
84 |
| - plugins.get(plugin.pluginId()).getClass().getName(), |
85 |
| - plugin.getClass().getName()); |
86 |
| - } |
87 |
| - } |
88 |
| - } |
89 |
| - } |
90 |
| - |
91 |
| - private record PluginInfo( |
92 |
| - String id, |
93 |
| - String description, |
94 |
| - Object info |
95 |
| - ) {} |
96 | 32 |
|
97 | 33 | }
|
0 commit comments