Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple configuration parsing #109

Merged
merged 24 commits into from
Mar 16, 2018
Merged
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
16 changes: 13 additions & 3 deletions components/proxy/src/main/java/com/hotels/styx/StyxConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2013-2017 Expedia Inc.
* Copyright (C) 2013-2018 Expedia Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,9 @@
import com.hotels.styx.api.configuration.Configuration;
import com.hotels.styx.api.io.ResourceFactory;
import com.hotels.styx.client.StyxHeaderConfig;
import com.hotels.styx.infrastructure.configuration.yaml.YamlConfig;
import com.hotels.styx.infrastructure.configuration.ConfigurationParser;
import com.hotels.styx.infrastructure.configuration.ConfigurationSource;
import com.hotels.styx.infrastructure.configuration.yaml.YamlConfiguration;
import com.hotels.styx.proxy.ProxyServerConfig;

import java.nio.file.Path;
Expand All @@ -30,6 +32,7 @@

import static com.google.common.base.Preconditions.checkNotNull;
import static com.hotels.styx.StartupConfig.defaultStartupConfig;
import static com.hotels.styx.infrastructure.configuration.yaml.YamlConfigurationFormat.YAML;
import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;

Expand All @@ -55,7 +58,7 @@ public StyxConfig(Configuration configuration) {
}

public StyxConfig(String yaml) {
this(new YamlConfig(yaml));
this(loadYamlConfiguration(yaml));
}

public StyxConfig(StartupConfig startupConfig, Configuration configuration) {
Expand All @@ -67,6 +70,13 @@ public StyxConfig(StartupConfig startupConfig, Configuration configuration) {
this.styxHeaderConfig = get("styxHeaders", StyxHeaderConfig.class).orElseGet(StyxHeaderConfig::new);
}

private static Configuration loadYamlConfiguration(String yaml) {
return new ConfigurationParser.Builder<YamlConfiguration>()
.format(YAML)
.build()
.parse(ConfigurationSource.from(yaml));
}

@Override
public <T> Optional<T> get(String key, Class<T> tClass) {
return configuration.get(key, tClass);
Expand Down
15 changes: 12 additions & 3 deletions components/proxy/src/main/java/com/hotels/styx/StyxServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
import com.hotels.styx.admin.AdminServerBuilder;
import com.hotels.styx.api.HttpHandler2;
import com.hotels.styx.api.HttpInterceptor;
import com.hotels.styx.api.configuration.Configuration;
import com.hotels.styx.api.metrics.MetricRegistry;
import com.hotels.styx.api.service.spi.StyxService;
import com.hotels.styx.client.applications.BackendService;
import com.hotels.styx.infrastructure.Registry;
import com.hotels.styx.infrastructure.configuration.yaml.YamlConfig;
import com.hotels.styx.infrastructure.configuration.ConfigurationParser;
import com.hotels.styx.infrastructure.configuration.ConfigurationSource;
import com.hotels.styx.infrastructure.configuration.yaml.YamlConfiguration;
import com.hotels.styx.metrics.reporting.sets.NettyAllocatorMetricSet;
import com.hotels.styx.proxy.ProxyServerBuilder;
import com.hotels.styx.proxy.StyxBackendServiceClientFactory;
Expand Down Expand Up @@ -77,6 +80,7 @@

import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor;
import static com.hotels.styx.api.configuration.ConfigurationContextResolver.EMPTY_CONFIGURATION_CONTEXT_RESOLVER;
import static com.hotels.styx.infrastructure.configuration.yaml.YamlConfigurationFormat.YAML;
import static com.hotels.styx.infrastructure.logging.LOGBackConfigurer.shutdownLogging;
import static com.hotels.styx.serviceproviders.ServiceProvision.loadServices;
import static io.netty.util.ResourceLeakDetector.Level.DISABLED;
Expand Down Expand Up @@ -126,9 +130,14 @@ private static StyxServer createStyxServer(String[] args) {
LOG.info("Styx configFileLocation={}", startupConfig.configFileLocation());
LOG.info("Styx logConfigLocation={}", startupConfig.logConfigLocation());

YamlConfig yamlConfig = new YamlConfig(startupConfig.configFileLocation(), System.getProperties());
Configuration configFromFile =
new ConfigurationParser.Builder<YamlConfiguration>()
.format(YAML)
.overrides(System.getProperties())
.build()
.parse(ConfigurationSource.from(startupConfig.configFileLocation()));

StyxConfig styxConfig = new StyxConfig(startupConfig, yamlConfig);
StyxConfig styxConfig = new StyxConfig(startupConfig, configFromFile);

return new StyxServerBuilder(styxConfig)
.logConfigLocationFromEnvironment()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2013-2017 Expedia Inc.
* Copyright (C) 2013-2018 Expedia Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.hotels.styx.api.Resource;
import com.hotels.styx.infrastructure.configuration.UnresolvedPlaceholder;
import com.hotels.styx.infrastructure.configuration.yaml.NodePath;
import com.hotels.styx.infrastructure.configuration.yaml.PlaceholderResolver;

Expand Down Expand Up @@ -71,7 +72,7 @@ public T read(byte[] content, TypeReference<T> typeReference) throws Exception {
JsonNode rootNode = MAPPER.readTree(content);
List<String> includePaths = includedPaths(rootNode);

Collection<PlaceholderResolver.UnresolvedPlaceholder> unresolvedPlaceholders = resolvePlaceholders(rootNode, overrides);
Collection<UnresolvedPlaceholder> unresolvedPlaceholders = resolvePlaceholders(rootNode, overrides);

checkState(unresolvedPlaceholders.isEmpty(), "Unresolved placeholders: %s", unresolvedPlaceholders);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (C) 2013-2018 Expedia Inc.
*
* 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 com.hotels.styx.infrastructure.configuration;

import com.hotels.styx.api.Resource;

import java.util.Map;

/**
* Format for configuration, e.g. JSON, YAML.
*
* @param <C> configuration type
*/
public interface ConfigurationFormat<C extends ExtensibleConfiguration<C>> {
/**
* Deserialise configuration from a literal string. This is primarily intended for tests,
* but could be used whenever the configuration has already been loaded.
*
* @param string string
* @return configuration
*/
C deserialise(String string);

/**
* Deserialise configuration from a resource.
*
* @param resource resource
* @return configuration
*/
C deserialise(Resource resource);

/**
* Resolves placeholders in a piece of text, using only overrides, not loaded configuration.
*
* @param text text
* @param overrides overrides
* @return text with resolved placeholders
*/
String resolvePlaceholdersInText(String text, Map<String, String> overrides);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* Copyright (C) 2013-2018 Expedia Inc.
*
* 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 com.hotels.styx.infrastructure.configuration;

import com.google.common.annotations.VisibleForTesting;
import com.hotels.styx.infrastructure.configuration.ExtensibleConfiguration.PlaceholderResolutionResult;
import org.slf4j.Logger;

import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.function.Function;

import static com.google.common.base.Preconditions.checkState;
import static com.hotels.styx.api.io.ResourceFactory.newResource;
import static com.hotels.styx.common.Logging.sanitise;
import static java.util.Collections.emptyMap;
import static java.util.Objects.requireNonNull;
import static org.slf4j.LoggerFactory.getLogger;

/**
* Configuration parser.
*
* @param <C> configuration type
*/
public final class ConfigurationParser<C extends ExtensibleConfiguration<C>> {
private static final Logger LOGGER = getLogger(ConfigurationParser.class);

private final ConfigurationFormat<C> format;
private final Map<String, String> overrides;
private final Function<String, ConfigurationSource> sourceFromIncludeFunction;

private ConfigurationParser(Builder<C> builder) {
this.format = requireNonNull(builder.format);
this.overrides = requireNonNull(builder.overrides);
this.sourceFromIncludeFunction = builder.sourceFromIncludeFunction != null
? builder.sourceFromIncludeFunction
: ConfigurationParser::sourceFromInclude;
}

public C parse(ConfigurationSource provider) {
LOGGER.debug("Parsing configuration in format={} from source={}", format, provider);

C configuration = doParse(provider);

PlaceholderResolutionResult<C> resolved = configuration.resolvePlaceholders(overrides);

checkState(resolved.unresolvedPlaceholders().isEmpty(), "Unresolved placeholders: %s", resolved.unresolvedPlaceholders());

return resolved.resolvedConfiguration();
}

private C doParse(ConfigurationSource provider) {
C main = provider.deserialise(format);

C withInclude = includedParentConfig(main)
.map(main::withParent)
.orElse(main);

return withInclude.withOverrides(overrides);
}

private Optional<C> includedParentConfig(C main) {
return main.get("include")
.map(include -> format.resolvePlaceholdersInText(include, overrides))
.map(sourceFromIncludeFunction)
.map(this::doParse);
}

private static ConfigurationSource sourceFromInclude(String includePath) {
LOGGER.info("Including config file: path={}", sanitise(includePath));
return ConfigurationSource.from(newResource(includePath));
}

/**
* Builder.
*
* @param <C> configuration type
*/
public static final class Builder<C extends ExtensibleConfiguration<C>> {
private ConfigurationFormat<C> format;
private Map<String, String> overrides = emptyMap();
private Function<String, ConfigurationSource> sourceFromIncludeFunction;

public Builder<C> format(ConfigurationFormat<C> format) {
this.format = requireNonNull(format);
return this;
}

public Builder<C> overrides(Map<String, String> overrides) {
this.overrides = requireNonNull(overrides);
return this;
}

public Builder<C> overrides(Properties properties) {
return overrides((Map) properties);
}

@VisibleForTesting
Builder<C> sourceFromIncludeFunction(Function<String, ConfigurationSource> sourceFromIncludeFunction) {
this.sourceFromIncludeFunction = requireNonNull(sourceFromIncludeFunction);
return this;
}

public ConfigurationParser<C> build() {
return new ConfigurationParser<>(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright (C) 2013-2018 Expedia Inc.
*
* 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 com.hotels.styx.infrastructure.configuration;

import com.hotels.styx.api.Resource;

/**
* Provider of configuration, e.g. file, in-memory string.
*/
public interface ConfigurationSource {
/**
* Deserialise configuration that is in a given format.
*
* @param format format
* @param <C> configuration type
* @return configuration
*/
<C extends ExtensibleConfiguration<C>> C deserialise(ConfigurationFormat<C> format);

/**
* Configuration taken from a string in memory.
*
* @param string a string
* @return configuration provider
*/
static ConfigurationSource from(String string) {
return new ConfigurationSource() {
@Override
public <C extends ExtensibleConfiguration<C>> C deserialise(ConfigurationFormat<C> format) {
return format.deserialise(string);
}

@Override
public String toString() {
return "\"" + string + "\"";
}
};
}

/**
* Configuration from a resource.
*
* @param resource resource
* @return configuration provider
*/
static ConfigurationSource from(Resource resource) {
return new ConfigurationSource() {
@Override
public <C extends ExtensibleConfiguration<C>> C deserialise(ConfigurationFormat<C> format) {
return format.deserialise(resource);
}

@Override
public String toString() {
return resource.toString();
}
};
}
}
Loading