|
1 | 1 | package org.lowcoder.api.framework.plugin;
|
2 | 2 |
|
| 3 | +import static org.springframework.web.reactive.function.server.RequestPredicates.DELETE; |
| 4 | +import static org.springframework.web.reactive.function.server.RequestPredicates.GET; |
| 5 | +import static org.springframework.web.reactive.function.server.RequestPredicates.OPTIONS; |
| 6 | +import static org.springframework.web.reactive.function.server.RequestPredicates.PATCH; |
| 7 | +import static org.springframework.web.reactive.function.server.RequestPredicates.POST; |
| 8 | +import static org.springframework.web.reactive.function.server.RequestPredicates.PUT; |
| 9 | +import static org.springframework.web.reactive.function.server.RouterFunctions.route; |
| 10 | + |
| 11 | +import java.lang.reflect.InvocationTargetException; |
| 12 | +import java.lang.reflect.Method; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.Comparator; |
| 15 | +import java.util.LinkedHashMap; |
| 16 | +import java.util.List; |
3 | 17 | import java.util.Map;
|
4 | 18 |
|
| 19 | +import org.apache.commons.collections4.CollectionUtils; |
| 20 | +import org.apache.commons.lang3.StringUtils; |
| 21 | +import org.lowcoder.plugin.EndpointExtension; |
5 | 22 | 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; |
| 23 | +import org.lowcoder.plugin.PluginEndpoint; |
| 24 | +import org.lowcoder.sdk.exception.BaseException; |
| 25 | +import org.springframework.context.ApplicationContext; |
| 26 | +import org.springframework.core.ResolvableType; |
9 | 27 | import org.springframework.stereotype.Component;
|
| 28 | +import org.springframework.web.reactive.function.server.RequestPredicate; |
| 29 | +import org.springframework.web.reactive.function.server.RouterFunction; |
| 30 | +import org.springframework.web.reactive.function.server.ServerRequest; |
| 31 | +import org.springframework.web.reactive.function.server.ServerResponse; |
10 | 32 |
|
11 | 33 | import jakarta.annotation.PostConstruct;
|
| 34 | +import jakarta.annotation.PreDestroy; |
12 | 35 | import lombok.RequiredArgsConstructor;
|
13 | 36 | import lombok.extern.slf4j.Slf4j;
|
| 37 | +import reactor.core.publisher.Mono; |
14 | 38 |
|
15 | 39 | @RequiredArgsConstructor
|
16 | 40 | @Component
|
17 | 41 | @Slf4j
|
18 | 42 | public class LowcoderPluginManager
|
19 | 43 | {
|
20 |
| - private final ConfigurableApplicationContext applicationContext; |
21 |
| - private final CommonConfig common; |
22 |
| - private final ApplicationHome applicationHome; |
23 |
| - |
24 |
| - private Map<String, LowcoderPlugin> plugins; |
25 |
| - |
| 44 | + private final ApplicationContext applicationContext; |
| 45 | + private final PluginLoader pluginLoader; |
26 | 46 |
|
| 47 | + private Map<String, LowcoderPlugin> plugins = new LinkedHashMap<>(); |
| 48 | + private List<RouterFunction<ServerResponse>> routes = new ArrayList<>(); |
| 49 | + |
27 | 50 | @PostConstruct
|
28 | 51 | private void loadPlugins()
|
29 | 52 | {
|
| 53 | + registerPlugins(); |
| 54 | + List<LowcoderPlugin> sorted = new ArrayList<>(plugins.values()); |
| 55 | + sorted.sort(Comparator.comparing(LowcoderPlugin::loadOrder)); |
| 56 | + |
| 57 | + for (LowcoderPlugin plugin : sorted) |
| 58 | + { |
| 59 | + if (plugin.load(applicationContext)) |
| 60 | + { |
| 61 | + log.info("Plugin [{}] loaded successfully.", plugin.pluginId()); |
| 62 | + registerEndpoints(plugin); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @PreDestroy |
| 68 | + public void unloadPlugins() |
| 69 | + { |
| 70 | + for (LowcoderPlugin plugin : plugins.values()) |
| 71 | + { |
| 72 | + try |
| 73 | + { |
| 74 | + plugin.unload(); |
| 75 | + } |
| 76 | + catch(Throwable cause) |
| 77 | + { |
| 78 | + log.warn("Error unloading plugin: {}!", plugin.pluginId(), cause); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public List<RouterFunction<ServerResponse>> getEndpoints() |
| 84 | + { |
| 85 | + return this.routes; |
| 86 | + } |
| 87 | + |
| 88 | + public List<PluginInfo> getLoadedPluginsInfo() |
| 89 | + { |
| 90 | + List<PluginInfo> infos = new ArrayList<>(); |
| 91 | + for (LowcoderPlugin plugin : plugins.values()) |
| 92 | + { |
| 93 | + infos.add(new PluginInfo(plugin.pluginId(), plugin.description(), plugin.pluginInfo())); |
| 94 | + } |
| 95 | + return infos; |
| 96 | + } |
| 97 | + |
| 98 | + private void registerPlugins() |
| 99 | + { |
| 100 | + List<LowcoderPlugin> loaded = pluginLoader.loadPlugins(); |
| 101 | + if (CollectionUtils.isNotEmpty(loaded)) |
| 102 | + { |
| 103 | + for (LowcoderPlugin plugin : loaded) |
| 104 | + { |
| 105 | + if (!plugins.containsKey(plugin.pluginId())) |
| 106 | + { |
| 107 | + log.info("Registered plugin: {} ({})", plugin.pluginId(), plugin.getClass().getName()); |
| 108 | + plugins.put(plugin.pluginId(), plugin); |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + log.warn("Plugin {} already registered (from: {}), skipping {}.", plugin.pluginId(), |
| 113 | + plugins.get(plugin.pluginId()).getClass().getName(), |
| 114 | + plugin.getClass().getName()); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + private void registerEndpoints(LowcoderPlugin plugin) |
| 122 | + { |
| 123 | + if (CollectionUtils.isNotEmpty(plugin.endpoints())) |
| 124 | + { |
| 125 | + for (PluginEndpoint endpoint : plugin.endpoints()) |
| 126 | + { |
| 127 | + Method[] handlers = endpoint.getClass().getDeclaredMethods(); |
| 128 | + if (handlers != null && handlers.length > 0) |
| 129 | + { |
| 130 | + for (Method handler : handlers) |
| 131 | + { |
| 132 | + registerEndpointHandler(plugin, endpoint, handler); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @SuppressWarnings("unchecked") |
| 140 | + private void registerEndpointHandler(LowcoderPlugin plugin, PluginEndpoint endpoint, Method handler) |
| 141 | + { |
| 142 | + if (handler.isAnnotationPresent(EndpointExtension.class)) |
| 143 | + { |
| 144 | + if (checkHandlerMethod(handler)) |
| 145 | + { |
| 146 | + |
| 147 | + EndpointExtension endpointMeta = handler.getAnnotation(EndpointExtension.class); |
| 148 | + routes.add(route(createRequestPredicate(plugin, endpointMeta), req -> { |
| 149 | + Mono<ServerResponse> result = null; |
| 150 | + try |
| 151 | + { |
| 152 | + result = (Mono<ServerResponse>)handler.invoke(endpoint, req); |
| 153 | + } |
| 154 | + catch (IllegalAccessException | InvocationTargetException cause) |
| 155 | + { |
| 156 | + throw new BaseException("Error running handler for [ " + endpointMeta.method() + ": " + endpointMeta.uri() + "] !"); |
| 157 | + } |
| 158 | + return result; |
| 159 | + }) |
| 160 | + ); |
| 161 | + log.info("Registered plugin endpoint: {} -> {} -> {}: {}", plugin.pluginId(), endpoint.getClass().getSimpleName(), endpointMeta.method(), endpointMeta.uri()); |
| 162 | + } |
| 163 | + else |
| 164 | + { |
| 165 | + log.error("Cannot register plugin endpoint: {} -> {} -> {}! Handler method must be defined as: public Mono<ServerResponse> {}(ServerRequest request)", plugin.pluginId(), endpoint.getClass().getSimpleName(), handler.getName(), handler.getName()); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + private boolean checkHandlerMethod(Method method) |
| 172 | + { |
| 173 | + ResolvableType returnType = ResolvableType.forMethodReturnType(method); |
30 | 174 |
|
| 175 | + return (returnType.isAssignableFrom(Mono.class) |
| 176 | + && returnType.getGenerics().length == 1 |
| 177 | + && returnType.getGeneric(0).isAssignableFrom(ServerResponse.class) |
| 178 | + && method.getParameterCount() == 1 |
| 179 | + && method.getParameterTypes()[0].isAssignableFrom(ServerRequest.class) |
| 180 | + ); |
31 | 181 | }
|
32 | 182 |
|
| 183 | + private RequestPredicate createRequestPredicate(LowcoderPlugin plugin, EndpointExtension endpoint) |
| 184 | + { |
| 185 | + String basePath = "/plugins/" + plugin.pluginId(); |
| 186 | + |
| 187 | + switch(endpoint.method()) |
| 188 | + { |
| 189 | + case GET: |
| 190 | + return GET(pluginEndpointUri(basePath, endpoint.uri())); |
| 191 | + case POST: |
| 192 | + return POST(pluginEndpointUri(basePath, endpoint.uri())); |
| 193 | + case PUT: |
| 194 | + return PUT(pluginEndpointUri(basePath, endpoint.uri())); |
| 195 | + case PATCH: |
| 196 | + return PATCH(pluginEndpointUri(basePath, endpoint.uri())); |
| 197 | + case DELETE: |
| 198 | + return DELETE(pluginEndpointUri(basePath, endpoint.uri())); |
| 199 | + case OPTIONS: |
| 200 | + return OPTIONS(pluginEndpointUri(basePath, endpoint.uri())); |
| 201 | + } |
| 202 | + return null; |
| 203 | + } |
| 204 | + |
| 205 | + private String pluginEndpointUri(String basePath, String uri) |
| 206 | + { |
| 207 | + return StringUtils.join(basePath, StringUtils.prependIfMissing(uri, "/")); |
| 208 | + } |
| 209 | + |
| 210 | + |
| 211 | + private record PluginInfo( |
| 212 | + String id, |
| 213 | + String description, |
| 214 | + Object info |
| 215 | + ) {} |
| 216 | + |
33 | 217 | }
|
0 commit comments