Skip to content

Commit

Permalink
Refactor BlockNodeAppInjectionComponent to need an external configura…
Browse files Browse the repository at this point in the history
…tion instead of providing one itself.

Signed-off-by: Alfredo Gutierrez <[email protected]>
  • Loading branch information
AlfredoG87 committed Aug 25, 2024
1 parent 883969c commit 30cda24
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.hedera.block.server.mediator.MediatorInjectionModule;
import com.hedera.block.server.metrics.MetricsInjectionModule;
import com.hedera.block.server.persistence.storage.PersistenceInjectionModule;
import com.swirlds.config.api.Configuration;
import dagger.BindsInstance;
import dagger.Component;
import javax.inject.Singleton;

Expand All @@ -42,4 +44,30 @@ public interface BlockNodeAppInjectionComponent {
* @return the block node app server
*/
BlockNodeApp getBlockNodeApp();

// @Component.Factory
// interface Factory {
// BlockNodeAppInjectionComponent create(@BindsInstance Configuration configuration);
// }

/** Builder for the BlockNodeAppInjectionComponent. */
@Component.Builder
interface Builder {

/**
* Bind the configuration to the BlockNodeAppInjectionComponent.
*
* @param configuration the configuration
* @return the BlockNodeAppInjectionComponentBuilder
*/
@BindsInstance
Builder configuration(Configuration configuration);

/**
* Build the BlockNodeAppInjectionComponent.
*
* @return the BlockNodeAppInjectionComponent
*/
BlockNodeAppInjectionComponent build();
}
}
25 changes: 24 additions & 1 deletion server/src/main/java/com/hedera/block/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@
import static java.lang.System.Logger;
import static java.lang.System.Logger.Level.INFO;

import com.swirlds.config.api.Configuration;
import com.swirlds.config.api.ConfigurationBuilder;
import com.swirlds.config.extensions.sources.ClasspathFileConfigSource;
import com.swirlds.config.extensions.sources.SystemEnvironmentConfigSource;
import com.swirlds.config.extensions.sources.SystemPropertiesConfigSource;
import java.io.IOException;
import java.nio.file.Path;

/** Main class for the block node server */
public class Server {

private static final Logger LOGGER = System.getLogger(Server.class.getName());
private static final String APPLICATION_PROPERTIES = "app.properties";

private Server() {}

Expand All @@ -36,8 +43,24 @@ private Server() {}
*/
public static void main(final String[] args) throws IOException {
LOGGER.log(INFO, "Starting BlockNode Server");

// Init BlockNode Configuration
Configuration configuration =
ConfigurationBuilder.create()
.withSource(SystemEnvironmentConfigSource.getInstance())
.withSource(SystemPropertiesConfigSource.getInstance())
.withSource(new ClasspathFileConfigSource(Path.of(APPLICATION_PROPERTIES)))
.autoDiscoverExtensions()
.build();

// Init Dagger DI Component, passing in the configuration.
// this is where all the dependencies are wired up (magic happens)
// final BlockNodeAppInjectionComponent daggerComponent =
// DaggerBlockNodeAppInjectionComponent.factory().create(configuration);
final BlockNodeAppInjectionComponent daggerComponent =
DaggerBlockNodeAppInjectionComponent.create();
DaggerBlockNodeAppInjectionComponent.builder().configuration(configuration).build();

// Use Dagger DI Component to start the BlockNodeApp with all wired dependencies
final BlockNodeApp blockNodeApp = daggerComponent.getBlockNodeApp();
blockNodeApp.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,9 @@
import com.swirlds.common.metrics.config.MetricsConfig;
import com.swirlds.common.metrics.platform.prometheus.PrometheusConfig;
import com.swirlds.config.api.Configuration;
import com.swirlds.config.api.ConfigurationBuilder;
import com.swirlds.config.extensions.sources.ClasspathFileConfigSource;
import com.swirlds.config.extensions.sources.SystemEnvironmentConfigSource;
import com.swirlds.config.extensions.sources.SystemPropertiesConfigSource;
import dagger.Module;
import dagger.Provides;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import java.nio.file.Path;
import javax.inject.Singleton;

/**
Expand All @@ -40,30 +34,6 @@
@Module
public interface ConfigInjectionModule {

/** The application properties file name within the resources package. */
String APPLICATION_PROPERTIES = "app.properties";

/**
* Provides a configuration singleton using the configuration builder. Injected by the DI
* Framework.
*
* @return a configuration singleton
*/
@Singleton
@Provides
static Configuration provideConfiguration() {
try {
return ConfigurationBuilder.create()
.withSource(SystemEnvironmentConfigSource.getInstance())
.withSource(SystemPropertiesConfigSource.getInstance())
.withSource(new ClasspathFileConfigSource(Path.of(APPLICATION_PROPERTIES)))
.autoDiscoverExtensions()
.build();
} catch (IOException e) {
throw new RuntimeException("Failed to create configuration", e);
}
}

/**
* Provides a persistence storage configuration singleton using the configuration.
*
Expand Down

0 comments on commit 30cda24

Please sign in to comment.