reactive_graph_plugin_service_impl/
plugin_system_impl.rs

1use std::sync::Arc;
2
3use async_trait::async_trait;
4use reactive_graph_di::get_shared_component_factory;
5use springtime_di::Component;
6use springtime_di::component_alias;
7use springtime_di::instance_provider::TypedComponentInstanceProvider;
8
9use reactive_graph_lifecycle::Lifecycle;
10use reactive_graph_plugin_service_api::PluginContainerManager;
11use reactive_graph_plugin_service_api::PluginContextFactory;
12use reactive_graph_plugin_service_api::PluginRepositoryManager;
13use reactive_graph_plugin_service_api::PluginResolver;
14use reactive_graph_plugin_service_api::PluginSystem;
15
16#[derive(Component)]
17pub struct PluginSystemImpl {
18    plugin_container_manager: Arc<dyn PluginContainerManager + Send + Sync>,
19    plugin_context_factory: Arc<dyn PluginContextFactory + Send + Sync>,
20    plugin_repository_manager: Arc<dyn PluginRepositoryManager + Send + Sync>,
21    plugin_resolver: Arc<dyn PluginResolver + Send + Sync>,
22}
23
24#[async_trait]
25#[component_alias]
26impl PluginSystem for PluginSystemImpl {
27    fn get_plugin_context_factory(&self) -> Arc<dyn PluginContextFactory + Send + Sync> {
28        self.plugin_context_factory.clone()
29    }
30
31    fn get_plugin_container_manager(&self) -> Arc<dyn PluginContainerManager + Send + Sync> {
32        self.plugin_container_manager.clone()
33    }
34
35    fn get_plugin_repository_manager(&self) -> Arc<dyn PluginRepositoryManager + Send + Sync> {
36        self.plugin_repository_manager.clone()
37    }
38
39    fn get_plugin_resolver(&self) -> Arc<dyn PluginResolver + Send + Sync> {
40        self.plugin_resolver.clone()
41    }
42}
43
44#[async_trait]
45impl Lifecycle for PluginSystemImpl {
46    async fn init(&self) {
47        self.plugin_context_factory.init().await;
48        self.plugin_repository_manager.init().await;
49        self.plugin_resolver.init().await;
50    }
51
52    async fn post_init(&self) {
53        self.plugin_context_factory.post_init().await;
54        self.plugin_repository_manager.post_init().await;
55        self.plugin_resolver.post_init().await;
56    }
57
58    async fn pre_shutdown(&self) {
59        self.plugin_resolver.pre_shutdown().await;
60        self.plugin_repository_manager.pre_shutdown().await;
61        self.plugin_context_factory.pre_shutdown().await;
62    }
63
64    async fn shutdown(&self) {
65        self.plugin_resolver.shutdown().await;
66        self.plugin_repository_manager.shutdown().await;
67        self.plugin_context_factory.shutdown().await;
68    }
69}
70
71pub fn get_plugin_system() -> Arc<dyn PluginSystem + Send + Sync> {
72    let mut component_factory = get_shared_component_factory();
73    match TypedComponentInstanceProvider::primary_instance_typed::<dyn PluginSystem + Send + Sync>(&mut component_factory) {
74        Ok(runtime) => runtime,
75        Err(e) => {
76            panic!("{}", e);
77        }
78    }
79
80    // match ComponentFactoryBuilder::new() {
81    //     Ok(component_factory) => {
82    //         let mut component_factory = component_factory.build();
83    //         match TypedComponentInstanceProvider::primary_instance_typed::<PluginSystemImpl>(&mut component_factory) {
84    //             Ok(runtime) => runtime,
85    //             Err(e) => {
86    //                 panic!("{}", e);
87    //             }
88    //         }
89    //     }
90    //     Err(e) => {
91    //         panic!("{}", e);
92    //     }
93    // }
94}