Skip to content

Commit

Permalink
Merge pull request #52 from nort/multi-env
Browse files Browse the repository at this point in the history
support multiple environments
  • Loading branch information
norbertpotocki committed Jun 7, 2015
2 parents 17a8149 + 04b7384 commit 1617d80
Show file tree
Hide file tree
Showing 28 changed files with 1,149 additions and 191 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ buildscript {
ext {
artifactGroup = "pl.nort"
artifactName = "config"
artifactVersion = "1.2.0-SNAPSHOT"
artifactVersion = "2.0.0-SNAPSHOT"
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.slf4j.LoggerFactory;
import pl.nort.config.source.ConfigurationSource;
import pl.nort.config.source.EmptyConfigurationSource;
import pl.nort.config.source.context.DefaultEnvironment;
import pl.nort.config.source.context.Environment;
import pl.nort.config.source.refresh.RefreshStrategy;
import pl.nort.config.source.refresh.strategy.OnInitRefreshStrategy;

Expand All @@ -33,6 +35,7 @@ public class ConfigurationProviderBuilder {

private ConfigurationSource configurationSource;
private RefreshStrategy refreshStrategy;
private Environment environment;

/**
* Construct {@link ConfigurationProvider}s builder
Expand All @@ -41,11 +44,13 @@ public class ConfigurationProviderBuilder {
* <ul>
* <li>ConfigurationSource: {@link EmptyConfigurationSource}</li>
* <li>RefreshStrategy: {@link OnInitRefreshStrategy}</li>
* <li>Environment: {@link DefaultEnvironment}</li>
* </ul>
*/
public ConfigurationProviderBuilder() {
configurationSource = new EmptyConfigurationSource();
refreshStrategy = new OnInitRefreshStrategy();
environment = new DefaultEnvironment();
}

/**
Expand All @@ -70,18 +75,30 @@ public ConfigurationProviderBuilder withRefreshStrategy(RefreshStrategy refreshS
return this;
}

/**
* Set {@link Environment} for {@link ConfigurationProviders}s built by this builder
*
* @param environment {@link Environment} to use
* @return this builder with {@link Environment} set to {@code environment}
*/
public ConfigurationProviderBuilder withEnvironment(Environment environment) {
this.environment = environment;
return this;
}

/**
* Build a {@link ConfigurationProvider} using this builder's configuration
*
* @return new {@link ConfigurationProvider}
*/
public ConfigurationProvider build() {
LOG.info("Initializing ConfigurationProvider with " + configurationSource.getClass() + " source and " +
refreshStrategy.getClass() + " refresh strategy.");
LOG.info("Initializing ConfigurationProvider with " + configurationSource.getClass() + " source, " +
refreshStrategy.getClass() + " refresh strategy and " + environment.getClass() + " environment" +
"selection strategy.");

refreshStrategy.init(configurationSource);

return new SimpleConfigurationProvider(configurationSource);
return new SimpleConfigurationProvider(configurationSource, environment);
}

}
38 changes: 36 additions & 2 deletions src/main/java/pl/nort/config/provider/ConfigurationProviders.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package pl.nort.config.provider;

import pl.nort.config.source.ConfigurationSource;
import pl.nort.config.source.GitConfigurationSource;
import pl.nort.config.source.context.Environment;
import pl.nort.config.source.git.GitConfigurationSource;
import pl.nort.config.source.git.GitConfigurationSourceBuilder;

/**
* A factory producing {@link ConfigurationProvider}s.
Expand All @@ -31,7 +33,25 @@ public class ConfigurationProviders {
*/
public static ConfigurationProvider backedByGit(String repositoryURI) {
return new ConfigurationProviderBuilder()
.withConfigurationSource(new GitConfigurationSource(repositoryURI))
.withConfigurationSource(new GitConfigurationSourceBuilder()
.withRepositoryURI(repositoryURI)
.build())
.build();
}

/**
* A {@link ConfigurationProvider} backed by {@link GitConfigurationSource} and using {@code environment}.
*
* @param repositoryURI a git repository URI (can be remote)
* @param environment {@link Environment} to use
* @return {@link ConfigurationProvider} using provided git repository as a {@link ConfigurationSource}
*/
public static ConfigurationProvider backedByGit(String repositoryURI, Environment environment) {
return new ConfigurationProviderBuilder()
.withConfigurationSource(new GitConfigurationSourceBuilder()
.withRepositoryURI(repositoryURI)
.build())
.withEnvironment(environment)
.build();
}

Expand All @@ -47,4 +67,18 @@ public static ConfigurationProvider withSource(ConfigurationSource source) {
.build();
}

/**
* A {@link ConfigurationProvider} backed by a provided {@link ConfigurationSource} and using {@code environment}.
*
* @param source {@link ConfigurationSource} used to supply provider with configuration
* @param environment {@link Environment} to use
* @return {@link ConfigurationProvider} backed by a {@code source}
*/
public static ConfigurationProvider withSource(ConfigurationSource source, Environment environment) {
return new ConfigurationProviderBuilder()
.withConfigurationSource(source)
.withEnvironment(environment)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import com.github.drapostolos.typeparser.NoSuchRegisteredParserException;
import com.github.drapostolos.typeparser.TypeParser;
import com.github.drapostolos.typeparser.TypeParserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.nort.config.source.ConfigurationSource;
import pl.nort.config.source.context.Environment;
import pl.nort.config.source.context.MissingEnvironmentException;
import pl.nort.config.validator.BindingValidator;

import java.lang.reflect.InvocationHandler;
Expand All @@ -38,21 +38,24 @@
public class SimpleConfigurationProvider implements ConfigurationProvider {

private final ConfigurationSource configurationSource;
private final Environment environment;

/**
* {@link ConfigurationProvider} backed by provided {@link ConfigurationSource}
*
* {@link ConfigurationProvider} backed by provided {@link ConfigurationSource} and using {@code environment}
* to select environment.
* @param configurationSource source for configuration
* @param environment {@link Environment} to use
*/
public SimpleConfigurationProvider(ConfigurationSource configurationSource) {
public SimpleConfigurationProvider(ConfigurationSource configurationSource, Environment environment) {
this.configurationSource = checkNotNull(configurationSource);
this.environment = checkNotNull(environment);
}

@Override
public Properties allConfigurationAsProperties() {
try {
return configurationSource.getConfiguration();
} catch (IllegalStateException e) {
return configurationSource.getConfiguration(environment);
} catch (IllegalStateException | MissingEnvironmentException e) {
throw new IllegalStateException("Couldn't fetch configuration from configuration source", e);
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/pl/nort/config/source/ConfigurationSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package pl.nort.config.source;

import pl.nort.config.source.context.Environment;
import pl.nort.config.source.context.MissingEnvironmentException;
import pl.nort.config.source.refresh.Refreshable;

import java.util.Properties;
Expand All @@ -25,11 +27,20 @@
public interface ConfigurationSource extends Refreshable {

/**
* Get full configuration set from this source in a form of {@link Properties}.
* Get configuration set from this source in a form of {@link Properties}. Uses default environment.
*
* @return full configuration set
* @return configuration set for default environment
* @throws IllegalStateException when unable to fetch configuration
*/
Properties getConfiguration();

/**
* Get configuration set for a given environment from this source in a form of {@link Properties}.
* Provided {@link Environment} will be used to determine which environment to use.
*
* @return full configuration set
* @throws MissingEnvironmentException when requested environment couldn't be found
* @throws IllegalStateException when unable to fetch configuration
*/
Properties getConfiguration(Environment environment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,27 @@
*/
package pl.nort.config.source;

import pl.nort.config.source.context.Environment;

import java.util.Properties;

/**
* Empty {@link ConfigurationSource}
*/
public class EmptyConfigurationSource implements ConfigurationSource {

private final Properties properties = new Properties();
private static final Properties properties = new Properties();

@Override
public Properties getConfiguration() {
return properties;
}

@Override
public Properties getConfiguration(Environment environment) {
return properties;
}

@Override
public void refresh() {
// NOP
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2015 Norbert Potocki (norbert.potocki@nort.pl)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pl.nort.config.source.context;

/**
* Environment named "" (empty string).
*/
public class DefaultEnvironment extends ImmutableEnvironment {
/**
* Constructs environment named "" (empty string).
*/
public DefaultEnvironment() {
super("");
}
}
30 changes: 30 additions & 0 deletions src/main/java/pl/nort/config/source/context/Environment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2015 Norbert Potocki (norbert.potocki@nort.pl)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pl.nort.config.source.context;

/**
* Configuration environment.
*/
public interface Environment {

/**
* Name of the environment.
*
* @return environment name
*/
String getName();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2015 Norbert Potocki (norbert.potocki@nort.pl)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pl.nort.config.source.context;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* {@link Environment} that never changes.
*/
public class ImmutableEnvironment implements Environment {

private final String envName;

/**
* Construct environment named {@code envName}. This name never changes.
*
* @param envName environment name to use
*/
public ImmutableEnvironment(String envName) {
this.envName = checkNotNull(envName);
}

@Override
public String getName() {
return envName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2015 Norbert Potocki (norbert.potocki@nort.pl)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pl.nort.config.source.context;

/**
* Indicates missing configuration environment
*/
public class MissingEnvironmentException extends RuntimeException {

private static final String MISSING_ENV_MSG = "Missing environment: ";

/**
* Environment named {@code envName} is missing.
*
* @param envName environment name
* @param cause root cause
*/
public MissingEnvironmentException(String envName, Throwable cause) {
super(MISSING_ENV_MSG + envName, cause);
}
}

0 comments on commit 1617d80

Please sign in to comment.