Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 19b0c4d

Browse files
committed
new: propagate plugin specific environment variables to plugins
1 parent c5f6d5d commit 19b0c4d

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/framework/plugin/LowcoderPluginManager.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
package org.lowcoder.api.framework.plugin;
22

33
import java.util.ArrayList;
4+
import java.util.Arrays;
45
import java.util.Comparator;
6+
import java.util.HashMap;
57
import java.util.LinkedHashMap;
68
import java.util.List;
79
import java.util.Map;
10+
import java.util.stream.Collectors;
11+
import java.util.stream.StreamSupport;
812

913
import org.apache.commons.collections4.CollectionUtils;
14+
import org.apache.commons.lang3.StringUtils;
1015
import org.lowcoder.plugin.api.LowcoderPlugin;
1116
import org.lowcoder.plugin.api.LowcoderServices;
17+
import org.springframework.core.env.AbstractEnvironment;
18+
import org.springframework.core.env.EnumerablePropertySource;
19+
import org.springframework.core.env.Environment;
20+
import org.springframework.core.env.MutablePropertySources;
1221
import org.springframework.stereotype.Component;
1322

1423
import jakarta.annotation.PostConstruct;
@@ -22,7 +31,8 @@
2231
public class LowcoderPluginManager
2332
{
2433
private final LowcoderServices lowcoderServices;
25-
private final PluginLoader pluginLoader;
34+
private final PluginLoader pluginLoader;
35+
private final Environment environment;
2636

2737
private Map<String, LowcoderPlugin> plugins = new LinkedHashMap<>();
2838

@@ -35,7 +45,7 @@ private void loadPlugins()
3545

3646
for (LowcoderPlugin plugin : sorted)
3747
{
38-
PluginExecutor executor = new PluginExecutor(plugin, lowcoderServices);
48+
PluginExecutor executor = new PluginExecutor(plugin, getPluginEnvironmentVariables(plugin), lowcoderServices);
3949
executor.start();
4050
}
4151
}
@@ -66,6 +76,29 @@ public List<PluginInfo> getLoadedPluginsInfo()
6676
return infos;
6777
}
6878

79+
private Map<String, Object> getPluginEnvironmentVariables(LowcoderPlugin plugin)
80+
{
81+
Map<String, Object> env = new HashMap<>();
82+
83+
String varPrefix = "PLUGIN_" + plugin.pluginId().toUpperCase().replaceAll("-", "_") + "_";
84+
MutablePropertySources propertySources = ((AbstractEnvironment) environment).getPropertySources();
85+
List<String> properties = StreamSupport.stream(propertySources.spliterator(), false)
86+
.filter(propertySource -> propertySource instanceof EnumerablePropertySource)
87+
.map(propertySource -> ((EnumerablePropertySource<?>) propertySource).getPropertyNames())
88+
.flatMap(Arrays::<String> stream)
89+
.distinct()
90+
.sorted()
91+
.filter(prop -> prop.startsWith(varPrefix))
92+
.collect(Collectors.toList());
93+
94+
for (String prop : properties)
95+
{
96+
env.put(StringUtils.removeStart(prop, varPrefix), environment.getProperty(prop));
97+
}
98+
99+
return env;
100+
}
101+
69102
private void registerPlugins()
70103
{
71104
List<LowcoderPlugin> loaded = pluginLoader.loadPlugins();

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/framework/plugin/PluginExecutor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.lowcoder.api.framework.plugin;
22

3+
import java.util.Map;
4+
35
import org.lowcoder.plugin.api.LowcoderPlugin;
46
import org.lowcoder.plugin.api.LowcoderServices;
57

@@ -8,11 +10,13 @@
810
@Slf4j
911
public class PluginExecutor extends Thread
1012
{
13+
private Map<String, Object> env;
1114
private LowcoderPlugin plugin;
1215
private LowcoderServices services;
1316

14-
public PluginExecutor(LowcoderPlugin plugin, LowcoderServices services)
17+
public PluginExecutor(LowcoderPlugin plugin, Map<String, Object> env, LowcoderServices services)
1518
{
19+
this.env = env;
1620
this.plugin = plugin;
1721
this.services = services;
1822
this.setContextClassLoader(plugin.getClass().getClassLoader());
@@ -22,7 +26,7 @@ public PluginExecutor(LowcoderPlugin plugin, LowcoderServices services)
2226
@Override
2327
public void run()
2428
{
25-
if (plugin.load(services))
29+
if (plugin.load(env, services))
2630
{
2731
log.info("Plugin [{}] loaded and running.", plugin.pluginId());
2832
}

0 commit comments

Comments
 (0)