Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8d0c483

Browse files
committed
new: reworked Plugin classloader system
1 parentdda057d commit8d0c483

File tree

5 files changed

+77
-72
lines changed

5 files changed

+77
-72
lines changed

‎server/api-service/lowcoder-infra/src/main/java/org/lowcoder/infra/serverlog/ServerLogService.java‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
importorg.apache.commons.collections4.CollectionUtils;
1010
importorg.lowcoder.infra.event.SystemCommonEvent;
1111
importorg.lowcoder.infra.perf.PerfHelper;
12-
importorg.lowcoder.plugin.events.ServerLogEvent;
1312
importorg.springframework.beans.factory.annotation.Autowired;
1413
importorg.springframework.context.ApplicationEventPublisher;
1514
importorg.springframework.scheduling.annotation.Scheduled;
@@ -47,13 +46,11 @@ private void scheduledInsert() {
4746
.subscribe(result -> {
4847
intcount =result.size();
4948
perfHelper.count(SERVER_LOG_BATCH_INSERT,Tags.of("size",String.valueOf(result.size())));
50-
publishServerLogEvent(count);
49+
applicationEventPublisher.publishEvent(SystemCommonEvent.builder()
50+
.apiCalls(count)
51+
.detail("apiCalls",Integer.toString(count))
52+
.build()
53+
);
5154
});
5255
}
53-
54-
privatevoidpublishServerLogEvent(intcount) {
55-
ServerLogEventevent =newServerLogEvent();
56-
event.setApiCallsCount(count);
57-
applicationEventPublisher.publishEvent(event);
58-
}
5956
}

‎server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/framework/configuration/PluginConfiguration.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
@Configuration
2121
publicclassPluginConfiguration
2222
{
23-
privatefinalApplicationContextapplicationContext;
24-
privatefinalPluginLoaderpluginLoader;
25-
26-
publicLowcoderPluginManagerlowcoderPluginManager()
27-
{
28-
returnnewLowcoderPluginManager(applicationContext,pluginLoader);
29-
}
23+
//private final ApplicationContext applicationContext;
24+
//private final PluginLoader pluginLoader;
25+
//
26+
//public LowcoderPluginManager lowcoderPluginManager()
27+
//{
28+
//return new LowcoderPluginManager(applicationContext, pluginLoader);
29+
//}
3030

3131
@SuppressWarnings("unchecked")
3232
@Bean

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

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@
1818

1919
importorg.apache.commons.collections4.CollectionUtils;
2020
importorg.apache.commons.lang3.StringUtils;
21-
importorg.lowcoder.plugin.EndpointExtension;
22-
importorg.lowcoder.plugin.LowcoderPlugin;
23-
importorg.lowcoder.plugin.PluginEndpoint;
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;
2428
importorg.lowcoder.sdk.exception.BaseException;
25-
importorg.springframework.context.ApplicationContext;
2629
importorg.springframework.core.ResolvableType;
30+
importorg.springframework.http.ResponseCookie;
2731
importorg.springframework.stereotype.Component;
2832
importorg.springframework.web.reactive.function.server.RequestPredicate;
2933
importorg.springframework.web.reactive.function.server.RouterFunction;
30-
importorg.springframework.web.reactive.function.server.ServerRequest;
3134
importorg.springframework.web.reactive.function.server.ServerResponse;
35+
importorg.springframework.web.reactive.function.server.ServerResponse.BodyBuilder;
3236

3337
importjakarta.annotation.PostConstruct;
3438
importjakarta.annotation.PreDestroy;
@@ -41,7 +45,7 @@
4145
@Slf4j
4246
publicclassLowcoderPluginManager
4347
{
44-
privatefinalApplicationContextapplicationContext;
48+
privatefinalLowcoderServiceslowcoderServices;
4549
privatefinalPluginLoaderpluginLoader;
4650

4751
privateMap<String,LowcoderPlugin>plugins =newLinkedHashMap<>();
@@ -56,7 +60,7 @@ private void loadPlugins()
5660

5761
for (LowcoderPluginplugin :sorted)
5862
{
59-
if (plugin.load(applicationContext))
63+
if (plugin.load(lowcoderServices))
6064
{
6165
log.info("Plugin [{}] loaded successfully.",plugin.pluginId());
6266
registerEndpoints(plugin);
@@ -136,7 +140,6 @@ private void registerEndpoints(LowcoderPlugin plugin)
136140
}
137141
}
138142

139-
@SuppressWarnings("unchecked")
140143
privatevoidregisterEndpointHandler(LowcoderPluginplugin,PluginEndpointendpoint,Methodhandler)
141144
{
142145
if (handler.isAnnotationPresent(EndpointExtension.class))
@@ -149,7 +152,8 @@ private void registerEndpointHandler(LowcoderPlugin plugin, PluginEndpoint endpo
149152
Mono<ServerResponse>result =null;
150153
try
151154
{
152-
result = (Mono<ServerResponse>)handler.invoke(endpoint,req);
155+
EndpointResponseresponse = (EndpointResponse)handler.invoke(endpoint,PluginServerRequest.fromServerRequest(req));
156+
result =createServerResponse(response);
153157
}
154158
catch (IllegalAccessException |InvocationTargetExceptioncause)
155159
{
@@ -167,16 +171,49 @@ private void registerEndpointHandler(LowcoderPlugin plugin, PluginEndpoint endpo
167171
}
168172
}
169173

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+
}
170209

171210
privatebooleancheckHandlerMethod(Methodmethod)
172211
{
173212
ResolvableTypereturnType =ResolvableType.forMethodReturnType(method);
174-
175-
return (returnType.getRawClass().isAssignableFrom(Mono.class)
176-
&&returnType.getGenerics().length ==1
177-
&&returnType.getGeneric(0).isAssignableFrom(ServerResponse.class)
213+
214+
return (returnType.getRawClass().isAssignableFrom(EndpointResponse.class)
178215
&&method.getParameterCount() ==1
179-
&&method.getParameterTypes()[0].isAssignableFrom(ServerRequest.class)
216+
&&method.getParameterTypes()[0].isAssignableFrom(EndpointRequest.class)
180217
);
181218
}
182219

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

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
packageorg.lowcoder.api.framework.plugin;
22

33
importjava.io.IOException;
4-
importjava.net.URL;
5-
importjava.net.URLClassLoader;
64
importjava.nio.file.Files;
75
importjava.nio.file.Path;
86
importjava.util.ArrayList;
7+
importjava.util.Iterator;
98
importjava.util.List;
10-
importjava.util.Set;
9+
importjava.util.ServiceLoader;
1110

1211
importorg.apache.commons.collections4.CollectionUtils;
1312
importorg.apache.commons.lang3.StringUtils;
14-
importorg.lowcoder.plugin.LowcoderPlugin;
13+
importorg.lowcoder.plugin.api.LowcoderPlugin;
1514
importorg.lowcoder.sdk.config.CommonConfig;
16-
importorg.reflections.Reflections;
17-
importorg.reflections.scanners.SubTypesScanner;
18-
importorg.reflections.scanners.TypeAnnotationsScanner;
19-
importorg.reflections.util.ClasspathHelper;
20-
importorg.reflections.util.ConfigurationBuilder;
21-
importorg.springframework.beans.factory.BeanClassLoaderAware;
2215
importorg.springframework.boot.system.ApplicationHome;
2316
importorg.springframework.stereotype.Component;
2417

@@ -28,12 +21,10 @@
2821
@Slf4j
2922
@RequiredArgsConstructor
3023
@Component
31-
publicclassPathBasedPluginLoaderimplementsPluginLoader,BeanClassLoaderAware
24+
publicclassPathBasedPluginLoaderimplementsPluginLoader
3225
{
3326
privatefinalCommonConfigcommon;
3427
privatefinalApplicationHomeapplicationHome;
35-
36-
privateClassLoaderbeanClassLoader;
3728

3829
@Override
3930
publicList<LowcoderPlugin>loadPlugins()
@@ -58,7 +49,6 @@ public List<LowcoderPlugin> loadPlugins()
5849
{
5950
for (LowcoderPluginplugin :loadedPlugins)
6051
{
61-
log.debug(" - loaded plugin: {} :: {}",plugin.pluginId(),plugin.description());
6252
plugins.add(plugin);
6353
}
6454
}
@@ -104,38 +94,26 @@ protected List<String> findPluginCandidates(Path pluginsDir)
10494
returnpluginCandidates;
10595
}
10696

107-
protectedList<LowcoderPlugin>loadPluginCandidates(StringpluginsDir)
97+
protectedList<LowcoderPlugin>loadPluginCandidates(StringpluginJar)
10898
{
10999
List<LowcoderPlugin>pluginCandidates =newArrayList<>();
110100

111-
URLClassLoadertestClassLoader =null;
112-
101+
PluginJarClassLoaderpluginClassLoader =null;
113102
try
114103
{
115-
testClassLoader =URLClassLoader.newInstance(newURL[] {
116-
Path.of(pluginsDir).toUri().toURL()
117-
},beanClassLoader);
118-
119-
Reflectionsreflections =newReflections(newConfigurationBuilder()
120-
.addClassLoader(testClassLoader)
121-
.addUrls(ClasspathHelper.forClassLoader(testClassLoader))
122-
.setScanners(newSubTypesScanner(false),newTypeAnnotationsScanner())
123-
);
104+
pluginClassLoader =newPluginJarClassLoader(getClass().getClassLoader(),Path.of(pluginJar));
124105

125-
Set<Class<?extendsLowcoderPlugin>>found =reflections.getSubTypesOf(LowcoderPlugin.class);
126-
for (Class<?extendsLowcoderPlugin>pluginClass :found)
106+
107+
ServiceLoader<LowcoderPlugin>pluginServices =ServiceLoader.load(LowcoderPlugin.class,pluginClassLoader);
108+
if (pluginServices !=null )
127109
{
128-
log.debug(" - found plugin: {}",pluginClass.getName());
129-
try
110+
Iterator<LowcoderPlugin>pluginIterator =pluginServices.iterator();
111+
while(pluginIterator.hasNext())
130112
{
131-
LowcoderPluginplugin =pluginClass.getConstructor().newInstance();
113+
LowcoderPluginplugin =pluginIterator.next();
132114
log.debug(" - loaded plugin: {} - {}",plugin.pluginId(),plugin.description());
133115
pluginCandidates.add(plugin);
134116
}
135-
catch(ThrowableloadFail)
136-
{
137-
log.error(" - error loading plugin: {}!",pluginClass.getName(),loadFail);
138-
}
139117
}
140118
}
141119
catch(Throwablecause)
@@ -160,11 +138,4 @@ private Path getAbsoluteNormalizedPath(String path)
160138

161139
returnnull;
162140
}
163-
164-
165-
@Override
166-
publicvoidsetBeanClassLoader(ClassLoaderclassLoader)
167-
{
168-
this.beanClassLoader =classLoader;
169-
}
170141
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
importjava.util.List;
44

5-
importorg.lowcoder.plugin.LowcoderPlugin;
5+
importorg.lowcoder.plugin.api.LowcoderPlugin;
66

77
publicinterfacePluginLoader
88
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp