Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/main/java/com/microsoft/dhalion/HealthManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,27 @@ protected void configure() {

cb.loadConfig(conf).loadPolicyConf();
config = cb.build();
injector = injector.createChildInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Config.class).toInstance(config);
}
});

//Register additional bindings
String bootstrapModuleClassName = (String) conf.get(Key.BOOTSTRAP_MODULE_CLASS.value());
Class<IBootstrapModule> bootstrapModuleClass = loadClass(bootstrapModuleClassName);
IBootstrapModule bootstrapModule = injector.getInstance(bootstrapModuleClass);

injector = injector.createChildInjector(bootstrapModule.get());

//Read the MetricsProvider class
String metricsProviderClass = (String) conf.get(Key.METRICS_PROVIDER_CLASS.value());
Class<MetricsProvider> mpClass
= (Class<MetricsProvider>) this.getClass().getClassLoader().loadClass(metricsProviderClass);
Class<MetricsProvider> mpClass = loadClass(metricsProviderClass);

injector = injector.createChildInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Config.class).toInstance(config);
bind(mpClass).in(Singleton.class);
}
});
Expand All @@ -131,8 +143,7 @@ private void initializePolicies() throws ClassNotFoundException {
for (PolicyConfig policyConf : config.policies()) {
String policyClassName = policyConf.policyClass();
LOG.info(String.format("Initializing %s with class %s", policyConf.id(), policyClassName));
Class<IHealthPolicy> policyClass
= (Class<IHealthPolicy>) this.getClass().getClassLoader().loadClass(policyClassName);
Class<IHealthPolicy> policyClass = loadClass(policyClassName);

AbstractModule module = constructPolicySpecificModule(policyConf);
IHealthPolicy policy = injector.createChildInjector(module).getInstance(policyClass);
Expand All @@ -141,6 +152,11 @@ private void initializePolicies() throws ClassNotFoundException {
}
}

@SuppressWarnings("unchecked")
private <T> Class<T> loadClass(String className) throws ClassNotFoundException {
return (Class<T>) this.getClass().getClassLoader().loadClass(className);
}

private AbstractModule constructPolicySpecificModule(final PolicyConfig policyConfig) {
return new AbstractModule() {
@Override
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/microsoft/dhalion/IBootstrapModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This program is made available under the terms of the MIT License.
* See the LICENSE file in the project root for more information.
*/
package com.microsoft.dhalion;

import com.google.inject.AbstractModule;
import com.google.inject.Module;

/**
* {@link IBootstrapModule} should be used to customize the injector with additional bindings required for injecting
* the metrics provider and the health policies.
*/
public interface IBootstrapModule {

/**
* Dhalion will create a new injector that will inherit all its state from the existing injector and add this module
* (and binding definitions) to it.
*
* @return {@link Module} with additional binding definitions
*/
default Module get() {
return new AbstractModule() {
@Override
protected void configure() {
}
};
}

public static class DefaultModule implements IBootstrapModule{}
}
4 changes: 2 additions & 2 deletions src/main/java/com/microsoft/dhalion/conf/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public enum Key {
POLICY_CONF_SENSOR_DURATION_SUFFIX(".duration"),
CONF_COMPONENT_NAMES("component.names"),


BOOTSTRAP_MODULE_CLASS("bootstrap.module.class", "com.microsoft.dhalion.IBootstrapModule$DefaultModule"),
METRICS_PROVIDER_CLASS("metrics.provider.class");

private final String value;
private final Object defaultValue;

Key(String value) {
this(value, null);
}
Expand Down