mirror of https://github.com/halo-dev/halo
refactor: Unstructured loading opportunity for plugin (#2191)
* refactor: Unstructured loading opportunity for plugin * refactor: gvkpull/2204/head
parent
61d2169a51
commit
d7cfe4c4a5
|
@ -1,16 +1,10 @@
|
|||
package run.halo.app.plugin;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.pf4j.PluginWrapper;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.stereotype.Component;
|
||||
import run.halo.app.core.extension.Plugin;
|
||||
import run.halo.app.extension.ExtensionClient;
|
||||
import run.halo.app.infra.utils.YamlUnstructuredLoader;
|
||||
import run.halo.app.plugin.event.HaloPluginLoadedEvent;
|
||||
|
||||
/**
|
||||
|
@ -32,26 +26,10 @@ public class PluginLoadedListener implements ApplicationListener<HaloPluginLoade
|
|||
// load plugin.yaml
|
||||
YamlPluginFinder yamlPluginFinder = new YamlPluginFinder();
|
||||
Plugin plugin = yamlPluginFinder.find(pluginWrapper.getPluginPath());
|
||||
extensionClient.create(plugin);
|
||||
|
||||
// load unstructured
|
||||
DefaultResourceLoader resourceLoader =
|
||||
new DefaultResourceLoader(pluginWrapper.getPluginClassLoader());
|
||||
plugin.getSpec().getExtensionLocations()
|
||||
.stream()
|
||||
.map(resourceLoader::getResource)
|
||||
.filter(Resource::exists)
|
||||
.map(resource -> new YamlUnstructuredLoader(resource).load())
|
||||
.flatMap(List::stream)
|
||||
.forEach(unstructured -> {
|
||||
Map<String, String> labels = unstructured.getMetadata().getLabels();
|
||||
if (labels == null) {
|
||||
unstructured.getMetadata().setLabels(new HashMap<>());
|
||||
}
|
||||
unstructured.getMetadata()
|
||||
.getLabels()
|
||||
.put(PluginConst.PLUGIN_NAME_LABEL_NAME, plugin.getMetadata().getName());
|
||||
extensionClient.create(unstructured);
|
||||
});
|
||||
extensionClient.fetch(Plugin.class, plugin.getMetadata().getName())
|
||||
.ifPresentOrElse(persisted -> {
|
||||
plugin.getMetadata().setVersion(persisted.getMetadata().getVersion());
|
||||
extensionClient.update(plugin);
|
||||
}, () -> extensionClient.create(plugin));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
package run.halo.app.plugin;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.pf4j.PluginWrapper;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.stereotype.Component;
|
||||
import run.halo.app.core.extension.Plugin;
|
||||
import run.halo.app.extension.ExtensionClient;
|
||||
import run.halo.app.extension.MetadataOperator;
|
||||
import run.halo.app.infra.utils.YamlUnstructuredLoader;
|
||||
import run.halo.app.plugin.event.HaloPluginStartedEvent;
|
||||
|
||||
/**
|
||||
* TODO Optimized Unstructured loading.
|
||||
*
|
||||
* @author guqing
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@Component
|
||||
public class PluginStartedListener implements ApplicationListener<HaloPluginStartedEvent> {
|
||||
|
||||
private final ExtensionClient extensionClient;
|
||||
|
||||
public PluginStartedListener(ExtensionClient extensionClient) {
|
||||
this.extensionClient = extensionClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(HaloPluginStartedEvent event) {
|
||||
PluginWrapper pluginWrapper = event.getPlugin();
|
||||
Plugin plugin =
|
||||
extensionClient.fetch(Plugin.class, pluginWrapper.getPluginId()).orElseThrow();
|
||||
// load unstructured
|
||||
DefaultResourceLoader resourceLoader =
|
||||
new DefaultResourceLoader(pluginWrapper.getPluginClassLoader());
|
||||
plugin.getSpec().getExtensionLocations()
|
||||
.stream()
|
||||
.map(resourceLoader::getResource)
|
||||
.filter(Resource::exists)
|
||||
.map(resource -> new YamlUnstructuredLoader(resource).load())
|
||||
.flatMap(List::stream)
|
||||
.forEach(unstructured -> {
|
||||
MetadataOperator metadata = unstructured.getMetadata();
|
||||
Map<String, String> labels = metadata.getLabels();
|
||||
if (labels == null) {
|
||||
labels = new HashMap<>();
|
||||
metadata.setLabels(labels);
|
||||
}
|
||||
labels.put(PluginConst.PLUGIN_NAME_LABEL_NAME, plugin.getMetadata().getName());
|
||||
extensionClient.fetch(unstructured.groupVersionKind(), metadata.getName())
|
||||
.ifPresentOrElse(persisted -> {
|
||||
unstructured.getMetadata().setVersion(persisted.getMetadata().getVersion());
|
||||
extensionClient.update(unstructured);
|
||||
}, () -> extensionClient.create(unstructured));
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue