From 4d3dfea530d636084506eab58b69f729c5760cfc Mon Sep 17 00:00:00 2001 From: Alfredo Gutierrez Date: Wed, 21 Aug 2024 13:20:23 -0600 Subject: [PATCH] Injecting BlockNodeContext using Dagger Signed-off-by: Alfredo Gutierrez --- .../com/hedera/block/server/BlockNodeApp.java | 9 +++++---- .../server/BlockNodeAppInjectionModule.java | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/com/hedera/block/server/BlockNodeApp.java b/server/src/main/java/com/hedera/block/server/BlockNodeApp.java index 464c48b36..11fdd5b81 100644 --- a/server/src/main/java/com/hedera/block/server/BlockNodeApp.java +++ b/server/src/main/java/com/hedera/block/server/BlockNodeApp.java @@ -17,7 +17,6 @@ package com.hedera.block.server; import com.hedera.block.server.config.BlockNodeContext; -import com.hedera.block.server.config.BlockNodeContextFactory; import com.hedera.block.server.data.ObjectEvent; import com.hedera.block.server.health.HealthService; import com.hedera.block.server.mediator.LiveStreamMediatorBuilder; @@ -46,6 +45,7 @@ public class BlockNodeApp { private static final System.Logger LOGGER = System.getLogger(Server.class.getName()); private final ServiceStatus serviceStatus; private final HealthService healthService; + private final BlockNodeContext blockNodeContext; /** * Has all needed dependencies to start the server and initialize the context. @@ -55,9 +55,12 @@ public class BlockNodeApp { */ @Inject public BlockNodeApp( - @NonNull ServiceStatus serviceStatus, @NonNull HealthService healthService) { + @NonNull ServiceStatus serviceStatus, + @NonNull HealthService healthService, + @NonNull BlockNodeContext blockNodeContext) { this.serviceStatus = serviceStatus; this.healthService = healthService; + this.blockNodeContext = blockNodeContext; } /** @@ -66,8 +69,6 @@ public BlockNodeApp( * @throws IOException if the server cannot be started */ public void startServer() throws IOException { - // init context, metrics, and configuration. - @NonNull final BlockNodeContext blockNodeContext = BlockNodeContextFactory.create(); @NonNull final BlockWriter blockWriter = diff --git a/server/src/main/java/com/hedera/block/server/BlockNodeAppInjectionModule.java b/server/src/main/java/com/hedera/block/server/BlockNodeAppInjectionModule.java index f69f4de5e..6f3ea4053 100644 --- a/server/src/main/java/com/hedera/block/server/BlockNodeAppInjectionModule.java +++ b/server/src/main/java/com/hedera/block/server/BlockNodeAppInjectionModule.java @@ -16,9 +16,14 @@ package com.hedera.block.server; +import com.hedera.block.server.config.BlockNodeContext; +import com.hedera.block.server.config.BlockNodeContextFactory; import dagger.Binds; import dagger.Module; +import dagger.Provides; + import javax.inject.Singleton; +import java.io.IOException; /** * A Dagger Module for interfaces that are at the BlockNodeApp Level, should be temporary and @@ -36,4 +41,18 @@ public interface BlockNodeAppInjectionModule { @Singleton @Binds ServiceStatus bindServiceStatus(ServiceStatusImpl serviceStatus); + + /** + * Provides a block node context singleton using the factory. + * @return a block node context singleton + */ + @Provides + @Singleton + static BlockNodeContext provideBlockNodeContext() { + try { + return BlockNodeContextFactory.create(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } }