|
1 | 1 | packageorg.lowcoder.api.framework.plugin;
|
2 | 2 |
|
3 |
| -importstaticorg.springframework.web.reactive.function.server.RequestPredicates.DELETE; |
4 |
| -importstaticorg.springframework.web.reactive.function.server.RequestPredicates.GET; |
5 |
| -importstaticorg.springframework.web.reactive.function.server.RequestPredicates.OPTIONS; |
6 |
| -importstaticorg.springframework.web.reactive.function.server.RequestPredicates.PATCH; |
7 |
| -importstaticorg.springframework.web.reactive.function.server.RequestPredicates.POST; |
8 |
| -importstaticorg.springframework.web.reactive.function.server.RequestPredicates.PUT; |
9 |
| -importstaticorg.springframework.web.reactive.function.server.RouterFunctions.route; |
10 |
| - |
11 |
| -importjava.lang.reflect.InvocationTargetException; |
12 |
| -importjava.lang.reflect.Method; |
13 |
| -importjava.util.ArrayList; |
14 |
| -importjava.util.Comparator; |
15 |
| -importjava.util.LinkedHashMap; |
16 |
| -importjava.util.List; |
17 | 3 | importjava.util.Map;
|
18 | 4 |
|
19 |
| -importorg.apache.commons.collections4.CollectionUtils; |
20 |
| -importorg.apache.commons.lang3.StringUtils; |
21 |
| -importorg.lowcoder.api.framework.plugin.data.PluginServerRequest; |
22 |
| -importorg.lowcoder.plugin.api.EndpointExtension; |
23 |
| -importorg.lowcoder.plugin.api.LowcoderPlugin; |
24 |
| -importorg.lowcoder.plugin.api.LowcoderServices; |
25 |
| -importorg.lowcoder.plugin.api.PluginEndpoint; |
26 |
| -importorg.lowcoder.plugin.api.data.EndpointRequest; |
27 |
| -importorg.lowcoder.plugin.api.data.EndpointResponse; |
28 |
| -importorg.lowcoder.sdk.exception.BaseException; |
29 |
| -importorg.springframework.core.ResolvableType; |
30 |
| -importorg.springframework.http.ResponseCookie; |
| 5 | +importorg.lowcoder.plugin.LowcoderPlugin; |
| 6 | +importorg.lowcoder.sdk.config.CommonConfig; |
| 7 | +importorg.springframework.boot.system.ApplicationHome; |
| 8 | +importorg.springframework.context.ConfigurableApplicationContext; |
31 | 9 | importorg.springframework.stereotype.Component;
|
32 |
| -importorg.springframework.web.reactive.function.server.RequestPredicate; |
33 |
| -importorg.springframework.web.reactive.function.server.RouterFunction; |
34 |
| -importorg.springframework.web.reactive.function.server.ServerResponse; |
35 |
| -importorg.springframework.web.reactive.function.server.ServerResponse.BodyBuilder; |
36 | 10 |
|
37 | 11 | importjakarta.annotation.PostConstruct;
|
38 |
| -importjakarta.annotation.PreDestroy; |
39 | 12 | importlombok.RequiredArgsConstructor;
|
40 | 13 | importlombok.extern.slf4j.Slf4j;
|
41 |
| -importreactor.core.publisher.Mono; |
42 | 14 |
|
43 | 15 | @RequiredArgsConstructor
|
44 | 16 | @Component
|
45 | 17 | @Slf4j
|
46 | 18 | publicclassLowcoderPluginManager
|
47 | 19 | {
|
48 |
| -privatefinalLowcoderServiceslowcoderServices; |
49 |
| -privatefinalPluginLoaderpluginLoader; |
50 |
| - |
51 |
| -privateMap<String,LowcoderPlugin>plugins =newLinkedHashMap<>(); |
52 |
| -privateList<RouterFunction<ServerResponse>>routes =newArrayList<>(); |
53 |
| - |
54 |
| -@PostConstruct |
55 |
| -privatevoidloadPlugins() |
56 |
| -{ |
57 |
| -registerPlugins(); |
58 |
| -List<LowcoderPlugin>sorted =newArrayList<>(plugins.values()); |
59 |
| -sorted.sort(Comparator.comparing(LowcoderPlugin::loadOrder)); |
60 |
| - |
61 |
| -for (LowcoderPluginplugin :sorted) |
62 |
| -{ |
63 |
| -if (plugin.load(lowcoderServices)) |
64 |
| -{ |
65 |
| -log.info("Plugin [{}] loaded successfully.",plugin.pluginId()); |
66 |
| -registerEndpoints(plugin); |
67 |
| -} |
68 |
| -} |
69 |
| -} |
70 |
| - |
71 |
| -@PreDestroy |
72 |
| -publicvoidunloadPlugins() |
73 |
| -{ |
74 |
| -for (LowcoderPluginplugin :plugins.values()) |
75 |
| -{ |
76 |
| -try |
77 |
| -{ |
78 |
| -plugin.unload(); |
79 |
| -} |
80 |
| -catch(Throwablecause) |
81 |
| -{ |
82 |
| -log.warn("Error unloading plugin: {}!",plugin.pluginId(),cause); |
83 |
| -} |
84 |
| -} |
85 |
| -} |
86 |
| - |
87 |
| -publicList<RouterFunction<ServerResponse>>getEndpoints() |
88 |
| -{ |
89 |
| -returnthis.routes; |
90 |
| -} |
91 |
| - |
92 |
| -publicList<PluginInfo>getLoadedPluginsInfo() |
93 |
| -{ |
94 |
| -List<PluginInfo>infos =newArrayList<>(); |
95 |
| -for (LowcoderPluginplugin :plugins.values()) |
96 |
| -{ |
97 |
| -infos.add(newPluginInfo(plugin.pluginId(),plugin.description(),plugin.pluginInfo())); |
98 |
| -} |
99 |
| -returninfos; |
100 |
| -} |
101 |
| - |
102 |
| -privatevoidregisterPlugins() |
103 |
| -{ |
104 |
| -List<LowcoderPlugin>loaded =pluginLoader.loadPlugins(); |
105 |
| -if (CollectionUtils.isNotEmpty(loaded)) |
106 |
| -{ |
107 |
| -for (LowcoderPluginplugin :loaded) |
108 |
| -{ |
109 |
| -if (!plugins.containsKey(plugin.pluginId())) |
110 |
| -{ |
111 |
| -log.info("Registered plugin: {} ({})",plugin.pluginId(),plugin.getClass().getName()); |
112 |
| -plugins.put(plugin.pluginId(),plugin); |
113 |
| -} |
114 |
| -else |
115 |
| -{ |
116 |
| -log.warn("Plugin {} already registered (from: {}), skipping {}.",plugin.pluginId(), |
117 |
| -plugins.get(plugin.pluginId()).getClass().getName(), |
118 |
| -plugin.getClass().getName()); |
119 |
| -} |
120 |
| -} |
121 |
| -} |
122 |
| -} |
123 |
| - |
| 20 | +privatefinalConfigurableApplicationContextapplicationContext; |
| 21 | +privatefinalCommonConfigcommon; |
| 22 | +privatefinalApplicationHomeapplicationHome; |
124 | 23 |
|
125 |
| -privatevoidregisterEndpoints(LowcoderPluginplugin) |
126 |
| -{ |
127 |
| -if (CollectionUtils.isNotEmpty(plugin.endpoints())) |
128 |
| -{ |
129 |
| -for (PluginEndpointendpoint :plugin.endpoints()) |
130 |
| -{ |
131 |
| -Method[]handlers =endpoint.getClass().getDeclaredMethods(); |
132 |
| -if (handlers !=null &&handlers.length >0) |
133 |
| -{ |
134 |
| -for (Methodhandler :handlers) |
135 |
| -{ |
136 |
| -registerEndpointHandler(plugin,endpoint,handler); |
137 |
| -} |
138 |
| -} |
139 |
| -} |
140 |
| -} |
141 |
| -} |
142 |
| - |
143 |
| -privatevoidregisterEndpointHandler(LowcoderPluginplugin,PluginEndpointendpoint,Methodhandler) |
144 |
| -{ |
145 |
| -if (handler.isAnnotationPresent(EndpointExtension.class)) |
146 |
| -{ |
147 |
| -if (checkHandlerMethod(handler)) |
148 |
| -{ |
149 |
| - |
150 |
| -EndpointExtensionendpointMeta =handler.getAnnotation(EndpointExtension.class); |
151 |
| -routes.add(route(createRequestPredicate(plugin,endpointMeta),req -> { |
152 |
| -Mono<ServerResponse>result =null; |
153 |
| -try |
154 |
| -{ |
155 |
| -EndpointResponseresponse = (EndpointResponse)handler.invoke(endpoint,PluginServerRequest.fromServerRequest(req)); |
156 |
| -result =createServerResponse(response); |
157 |
| -} |
158 |
| -catch (IllegalAccessException |InvocationTargetExceptioncause) |
159 |
| -{ |
160 |
| -thrownewBaseException("Error running handler for [ " +endpointMeta.method() +": " +endpointMeta.uri() +"] !"); |
161 |
| -} |
162 |
| -returnresult; |
163 |
| -}) |
164 |
| -); |
165 |
| -log.info("Registered plugin endpoint: {} -> {} -> {}: {}",plugin.pluginId(),endpoint.getClass().getSimpleName(),endpointMeta.method(),endpointMeta.uri()); |
166 |
| -} |
167 |
| -else |
168 |
| -{ |
169 |
| -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()); |
170 |
| -} |
171 |
| -} |
172 |
| -} |
| 24 | +privateMap<String,LowcoderPlugin>plugins; |
173 | 25 |
|
174 |
| -privateMono<ServerResponse>createServerResponse(EndpointResponsepluginResponse) |
175 |
| -{ |
176 |
| -/** Create response with given status **/ |
177 |
| -BodyBuilderbuilder =ServerResponse.status(pluginResponse.statusCode()); |
178 |
| - |
179 |
| -/** Set response headers **/ |
180 |
| -if (pluginResponse.headers() !=null && !pluginResponse.headers().isEmpty()) |
181 |
| -{ |
182 |
| -pluginResponse.headers().entrySet() |
183 |
| -.forEach(entry -> { |
184 |
| -builder.header(entry.getKey(),entry.getValue().toArray(newString[] {})); |
185 |
| -}); |
186 |
| - |
187 |
| -} |
188 |
| - |
189 |
| -/** Set cookies if available **/ |
190 |
| -if (pluginResponse.cookies() !=null && !pluginResponse.cookies().isEmpty()) |
191 |
| -{ |
192 |
| -pluginResponse.cookies().values() |
193 |
| -.forEach(cookies -> { |
194 |
| -cookies.forEach(cookie -> { |
195 |
| -builder.cookie(ResponseCookie.from(cookie.getKey(),cookie.getValue()).build()); |
196 |
| -}); |
197 |
| - |
198 |
| -}); |
199 |
| -} |
200 |
| - |
201 |
| -/** Set response body if available **/ |
202 |
| -if (pluginResponse.body() !=null) |
203 |
| -{ |
204 |
| -returnbuilder.bodyValue(pluginResponse.body()); |
205 |
| -} |
206 |
| - |
207 |
| -returnbuilder.build(); |
208 |
| -} |
209 | 26 |
|
210 |
| -privatebooleancheckHandlerMethod(Methodmethod) |
211 |
| -{ |
212 |
| -ResolvableTypereturnType =ResolvableType.forMethodReturnType(method); |
213 |
| - |
214 |
| -return (returnType.getRawClass().isAssignableFrom(EndpointResponse.class) |
215 |
| -&&method.getParameterCount() ==1 |
216 |
| -&&method.getParameterTypes()[0].isAssignableFrom(EndpointRequest.class) |
217 |
| -); |
218 |
| -} |
219 |
| - |
220 |
| -privateRequestPredicatecreateRequestPredicate(LowcoderPluginplugin,EndpointExtensionendpoint) |
| 27 | +@PostConstruct |
| 28 | +privatevoidloadPlugins() |
221 | 29 | {
|
222 |
| -StringbasePath ="/plugins/" +plugin.pluginId(); |
223 | 30 |
|
224 |
| -switch(endpoint.method()) |
225 |
| -{ |
226 |
| -caseGET: |
227 |
| -returnGET(pluginEndpointUri(basePath,endpoint.uri())); |
228 |
| -casePOST: |
229 |
| -returnPOST(pluginEndpointUri(basePath,endpoint.uri())); |
230 |
| -casePUT: |
231 |
| -returnPUT(pluginEndpointUri(basePath,endpoint.uri())); |
232 |
| -casePATCH: |
233 |
| -returnPATCH(pluginEndpointUri(basePath,endpoint.uri())); |
234 |
| -caseDELETE: |
235 |
| -returnDELETE(pluginEndpointUri(basePath,endpoint.uri())); |
236 |
| -caseOPTIONS: |
237 |
| -returnOPTIONS(pluginEndpointUri(basePath,endpoint.uri())); |
238 |
| -} |
239 |
| -returnnull; |
240 | 31 | }
|
241 |
| - |
242 |
| -privateStringpluginEndpointUri(StringbasePath,Stringuri) |
243 |
| -{ |
244 |
| -returnStringUtils.join(basePath,StringUtils.prependIfMissing(uri,"/")); |
245 |
| -} |
246 |
| - |
247 |
| - |
248 |
| -privaterecordPluginInfo( |
249 |
| -Stringid, |
250 |
| -Stringdescription, |
251 |
| -Objectinfo |
252 |
| -) {} |
253 | 32 |
|
254 | 33 | }
|