Skip to content

Commit

Permalink
assigning ConfigFactory a more specialized meaning - a source of fact…
Browse files Browse the repository at this point in the history
…ories
  • Loading branch information
andrus committed Dec 10, 2015
1 parent 36ce1ec commit 0950344
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 43 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/nhl/launcher/BootstrapModule.java
Expand Up @@ -10,9 +10,9 @@
import com.nhl.launcher.command.FailoverHelpCommand; import com.nhl.launcher.command.FailoverHelpCommand;
import com.nhl.launcher.command.HelpCommand; import com.nhl.launcher.command.HelpCommand;
import com.nhl.launcher.config.CliConfigurationSource; import com.nhl.launcher.config.CliConfigurationSource;
import com.nhl.launcher.config.ConfigurationFactory; import com.nhl.launcher.config.FactoryConfigurationService;
import com.nhl.launcher.config.ConfigurationSource; import com.nhl.launcher.config.ConfigurationSource;
import com.nhl.launcher.config.YamlConfigurationFactory; import com.nhl.launcher.config.YamlFactoryConfigurationService;
import com.nhl.launcher.env.DefaultEnvironment; import com.nhl.launcher.env.DefaultEnvironment;
import com.nhl.launcher.env.Environment; import com.nhl.launcher.env.Environment;
import com.nhl.launcher.jackson.DefaultJacksonService; import com.nhl.launcher.jackson.DefaultJacksonService;
Expand All @@ -38,7 +38,7 @@ public void configure(Binder binder) {
binder.bind(Runner.class).to(DefaultRunner.class).in(Singleton.class); binder.bind(Runner.class).to(DefaultRunner.class).in(Singleton.class);
binder.bind(Options.class).toProvider(OptionsProvider.class).in(Singleton.class); binder.bind(Options.class).toProvider(OptionsProvider.class).in(Singleton.class);
binder.bind(ConfigurationSource.class).to(CliConfigurationSource.class).in(Singleton.class); binder.bind(ConfigurationSource.class).to(CliConfigurationSource.class).in(Singleton.class);
binder.bind(ConfigurationFactory.class).to(YamlConfigurationFactory.class); binder.bind(FactoryConfigurationService.class).to(YamlFactoryConfigurationService.class);
binder.bind(Environment.class).to(DefaultEnvironment.class); binder.bind(Environment.class).to(DefaultEnvironment.class);


binder.bind(Command.class).annotatedWith(DefaultCommand.class).to(FailoverHelpCommand.class) binder.bind(Command.class).annotatedWith(DefaultCommand.class).to(FailoverHelpCommand.class)
Expand Down

This file was deleted.

Expand Up @@ -3,6 +3,10 @@
import java.io.InputStream; import java.io.InputStream;
import java.util.function.Function; import java.util.function.Function;


/**
* A facade that presents configuration data as a stream to consumers.
* Configuration can be stored in a file, etc.
*/
public interface ConfigurationSource { public interface ConfigurationSource {


<T> T readConfig(Function<InputStream, T> processor); <T> T readConfig(Function<InputStream, T> processor);
Expand Down
@@ -0,0 +1,25 @@
package com.nhl.launcher.config;

/**
* An object that has access to a tree of factories configuration data, and acts
* as a factory of custom factories that are initialized from this
* configuration.
*/
public interface FactoryConfigurationService {

/**
* Creates and returns a custom factory instance with its state initialized
* based on internal configuration. "prefix" argument defines
* sub-configuration location in the config tree.
*
*
* @param type
* a type of factory to create.
* @param prefix
* defines sub-configuration location in the config tree. Use
* empty string to access root config.
* @return a fully initialized factory of tghe specified type.
*
*/
<T> T factory(Class<T> type, String prefix);
}
Expand Up @@ -12,7 +12,10 @@
import com.nhl.launcher.env.Environment; import com.nhl.launcher.env.Environment;
import com.nhl.launcher.jackson.JacksonService; import com.nhl.launcher.jackson.JacksonService;


public class YamlConfigurationFactory implements ConfigurationFactory { /**
* {@link FactoryConfigurationService} based on YAML configs.
*/
public class YamlFactoryConfigurationService implements FactoryConfigurationService {


private JsonNode rootNode; private JsonNode rootNode;
private ObjectMapper mapper; private ObjectMapper mapper;
Expand All @@ -27,7 +30,7 @@ protected static JsonNode readYaml(InputStream in, ObjectMapper mapper) {
} }


@Inject @Inject
public YamlConfigurationFactory(ConfigurationSource configurationSource, Environment environment, public YamlFactoryConfigurationService(ConfigurationSource configurationSource, Environment environment,
JacksonService jacksonService) { JacksonService jacksonService) {
this.mapper = jacksonService.newObjectMapper(); this.mapper = jacksonService.newObjectMapper();
this.rootNode = configurationSource.readConfig(in -> readYaml(in, mapper)); this.rootNode = configurationSource.readConfig(in -> readYaml(in, mapper));
Expand All @@ -36,12 +39,7 @@ public YamlConfigurationFactory(ConfigurationSource configurationSource, Environ
} }


@Override @Override
public <T> T config(Class<T> type) { public <T> T factory(Class<T> type, String prefix) {
return subconfig("", type);
}

@Override
public <T> T subconfig(String prefix, Class<T> type) {
if (rootNode == null) { if (rootNode == null) {
throw new IllegalStateException("No configuration data available.."); throw new IllegalStateException("No configuration data available..");
} }
Expand Down
Expand Up @@ -11,15 +11,15 @@
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;


public class YamlConfigurationFactoryStaticsTest { public class YamlFactoryConfigurationServiceStaticsTest {


@Test @Test
public void testReadYaml() { public void testReadYaml() {


InputStream in = new ByteArrayInputStream("a: b\nb: c".getBytes()); InputStream in = new ByteArrayInputStream("a: b\nb: c".getBytes());
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();


JsonNode node = YamlConfigurationFactory.readYaml(in, mapper); JsonNode node = YamlFactoryConfigurationService.readYaml(in, mapper);
assertNotNull(node); assertNotNull(node);


assertEquals("b", node.get("a").asText()); assertEquals("b", node.get("a").asText());
Expand Down
Expand Up @@ -18,7 +18,7 @@
import com.nhl.launcher.env.Environment; import com.nhl.launcher.env.Environment;
import com.nhl.launcher.jackson.JacksonService; import com.nhl.launcher.jackson.JacksonService;


public class YamlConfigurationFactoryTest { public class YamlFactoryConfigurationServiceTest {


private ConfigurationSource mockConfigSource; private ConfigurationSource mockConfigSource;
private JacksonService mockJacksonService; private JacksonService mockJacksonService;
Expand All @@ -34,7 +34,7 @@ public void before() {
} }


@Test @Test
public void testConfig() { public void testCreateFactory() {


when(mockConfigSource.readConfig(any())).thenAnswer(i -> { when(mockConfigSource.readConfig(any())).thenAnswer(i -> {


Expand All @@ -45,15 +45,15 @@ public void testConfig() {
return processor.apply(in); return processor.apply(in);
}); });


Bean1 b1 = new YamlConfigurationFactory(mockConfigSource, mockEnvironment, mockJacksonService) Bean1 b1 = new YamlFactoryConfigurationService(mockConfigSource, mockEnvironment, mockJacksonService)
.config(Bean1.class); .factory(Bean1.class, "");
assertNotNull(b1); assertNotNull(b1);
assertEquals("SS", b1.getS()); assertEquals("SS", b1.getS());
assertEquals(55, b1.getI()); assertEquals(55, b1.getI());
} }


@Test @Test
public void testConfig_Nested() { public void testCreateFactory_Nested() {


when(mockConfigSource.readConfig(any())).thenAnswer(i -> { when(mockConfigSource.readConfig(any())).thenAnswer(i -> {


Expand All @@ -64,16 +64,16 @@ public void testConfig_Nested() {
return processor.apply(in); return processor.apply(in);
}); });


Bean2 b2 = new YamlConfigurationFactory(mockConfigSource, mockEnvironment, mockJacksonService) Bean2 b2 = new YamlFactoryConfigurationService(mockConfigSource, mockEnvironment, mockJacksonService)
.config(Bean2.class); .factory(Bean2.class, "");
assertNotNull(b2); assertNotNull(b2);
assertNotNull(b2.getB1()); assertNotNull(b2.getB1());
assertEquals("SS", b2.getB1().getS()); assertEquals("SS", b2.getB1().getS());
assertEquals(55, b2.getB1().getI()); assertEquals(55, b2.getB1().getI());
} }


@Test @Test
public void testSubconfig() { public void testCreateFactory_Subconfig() {


when(mockConfigSource.readConfig(any())).thenAnswer(i -> { when(mockConfigSource.readConfig(any())).thenAnswer(i -> {


Expand All @@ -84,15 +84,15 @@ public void testSubconfig() {
return processor.apply(in); return processor.apply(in);
}); });


Bean1 b1 = new YamlConfigurationFactory(mockConfigSource, mockEnvironment, mockJacksonService).subconfig("b1", Bean1 b1 = new YamlFactoryConfigurationService(mockConfigSource, mockEnvironment, mockJacksonService)
Bean1.class); .factory(Bean1.class, "b1");
assertNotNull(b1); assertNotNull(b1);
assertEquals("SS", b1.getS()); assertEquals("SS", b1.getS());
assertEquals(55, b1.getI()); assertEquals(55, b1.getI());
} }


@Test @Test
public void testSubconfig_MultiLevel() { public void testCreateFactory_Subconfig_MultiLevel() {


when(mockConfigSource.readConfig(any())).thenAnswer(i -> { when(mockConfigSource.readConfig(any())).thenAnswer(i -> {


Expand All @@ -103,15 +103,15 @@ public void testSubconfig_MultiLevel() {
return processor.apply(in); return processor.apply(in);
}); });


Bean1 b1 = new YamlConfigurationFactory(mockConfigSource, mockEnvironment, mockJacksonService) Bean1 b1 = new YamlFactoryConfigurationService(mockConfigSource, mockEnvironment, mockJacksonService)
.subconfig("b0.b1", Bean1.class); .factory(Bean1.class, "b0.b1");
assertNotNull(b1); assertNotNull(b1);
assertEquals("SS", b1.getS()); assertEquals("SS", b1.getS());
assertEquals(55, b1.getI()); assertEquals(55, b1.getI());
} }


@Test @Test
public void testSubconfig_Missing() { public void testCreateFactory_Subconfig_Missing() {


when(mockConfigSource.readConfig(any())).thenAnswer(i -> { when(mockConfigSource.readConfig(any())).thenAnswer(i -> {


Expand All @@ -122,15 +122,15 @@ public void testSubconfig_Missing() {
return processor.apply(in); return processor.apply(in);
}); });


Bean1 b1 = new YamlConfigurationFactory(mockConfigSource, mockEnvironment, mockJacksonService) Bean1 b1 = new YamlFactoryConfigurationService(mockConfigSource, mockEnvironment, mockJacksonService)
.subconfig("no.such.path", Bean1.class); .factory(Bean1.class, "no.such.path");
assertNotNull(b1); assertNotNull(b1);
assertEquals(null, b1.getS()); assertEquals(null, b1.getS());
assertEquals(0, b1.getI()); assertEquals(0, b1.getI());
} }


@Test @Test
public void testConfig_PropSubstitution() { public void testCreateFactory_PropSubstitution() {


when(mockEnvironment.frameworkProperties()).thenReturn(new HashMap<String, String>() { when(mockEnvironment.frameworkProperties()).thenReturn(new HashMap<String, String>() {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
Expand All @@ -150,15 +150,15 @@ public void testConfig_PropSubstitution() {
return processor.apply(in); return processor.apply(in);
}); });


Bean1 b1 = new YamlConfigurationFactory(mockConfigSource, mockEnvironment, mockJacksonService) Bean1 b1 = new YamlFactoryConfigurationService(mockConfigSource, mockEnvironment, mockJacksonService)
.config(Bean1.class); .factory(Bean1.class, "");
assertNotNull(b1); assertNotNull(b1);
assertEquals("SS", b1.getS()); assertEquals("SS", b1.getS());
assertEquals(55, b1.getI()); assertEquals(55, b1.getI());
} }


@Test @Test
public void testConfig_PropSubstitution_Nested() { public void testCreateFactory_PropSubstitution_Nested() {


when(mockEnvironment.frameworkProperties()).thenReturn(new HashMap<String, String>() { when(mockEnvironment.frameworkProperties()).thenReturn(new HashMap<String, String>() {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
Expand All @@ -178,8 +178,8 @@ public void testConfig_PropSubstitution_Nested() {
return processor.apply(in); return processor.apply(in);
}); });


Bean1 b1 = new YamlConfigurationFactory(mockConfigSource, mockEnvironment, mockJacksonService).subconfig("b1", Bean1 b1 = new YamlFactoryConfigurationService(mockConfigSource, mockEnvironment, mockJacksonService)
Bean1.class); .factory(Bean1.class, "b1");
assertNotNull(b1); assertNotNull(b1);
assertEquals("SS", b1.getS()); assertEquals("SS", b1.getS());
assertEquals(55, b1.getI()); assertEquals(55, b1.getI());
Expand Down

0 comments on commit 0950344

Please sign in to comment.