
com.amazonaws.internal.config.InternalConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws-java-sdk-osgi Show documentation
Show all versions of aws-java-sdk-osgi Show documentation
The AWS SDK for Java with support for OSGi. The AWS SDK for Java provides Java APIs for building software on AWS' cost-effective, scalable, and reliable infrastructure products. The AWS Java SDK allows developers to code against APIs for all of Amazon's infrastructure web services (Amazon S3, Amazon EC2, Amazon SQS, Amazon Relational Database Service, Amazon AutoScaling, etc).
/*
* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.amazonaws.internal.config;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.amazonaws.annotation.Immutable;
import com.amazonaws.log.InternalLogApi;
import com.amazonaws.log.InternalLogFactory;
import com.amazonaws.util.ClassLoaderHelper;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Internal configuration for the AWS Java SDK.
*/
@Immutable
public class InternalConfig {
//@formatter:off
private static final ObjectMapper MAPPER = new ObjectMapper()
.disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
//@formatter:on
private static final InternalLogApi log = InternalLogFactory.getLog(InternalConfig.class);
static final String DEFAULT_CONFIG_RESOURCE_RELATIVE_PATH = "awssdk_config_default.json";
static final String DEFAULT_CONFIG_RESOURCE_ABSOLUTE_PATH = "/com/amazonaws/internal/config/"
+ DEFAULT_CONFIG_RESOURCE_RELATIVE_PATH;
static final String CONFIG_OVERRIDE_RESOURCE = "awssdk_config_override.json";
private static final String SERVICE_REGION_DELIMITOR = "/";
private final SignerConfig defaultSignerConfig;
private final Map serviceRegionSigners;
private final Map regionSigners;
private final Map serviceSigners;
private final Map httpClients;
private final List hostRegexToRegionMappings;
private final String userAgentTemplate;
/**
* @param defaults
* default configuration
* @param override
* override configuration
*/
InternalConfig(InternalConfigJsonHelper defaults, InternalConfigJsonHelper override) {
SignerConfigJsonHelper scb = defaults.getDefaultSigner();
this.defaultSignerConfig = scb == null ? null : scb.build();
regionSigners = mergeSignerMap(defaults.getRegionSigners(), override.getRegionSigners(), "region");
serviceSigners = mergeSignerMap(defaults.getServiceSigners(), override.getServiceSigners(), "service");
serviceRegionSigners = mergeSignerMap(defaults.getServiceRegionSigners(), override.getServiceRegionSigners(),
"service" + SERVICE_REGION_DELIMITOR + "region");
httpClients = merge(defaults.getHttpClients(), override.getHttpClients());
hostRegexToRegionMappings = append(override.getHostRegexToRegionMappings(),
defaults.getHostRegexToRegionMappings());
if (override.getUserAgentTemplate() != null) {
userAgentTemplate = override.getUserAgentTemplate();
} else {
userAgentTemplate = defaults.getUserAgentTemplate();
}
}
/**
* Returns an immutable map by merging the override signer configuration into the default signer
* configuration for the given theme.
*
* @param defaults
* default signer configuration
* @param override
* signer configurations overrides
* @param theme
* used for message logging. eg region, service, region+service
*/
private Map mergeSignerMap(JsonIndex[] defaults,
JsonIndex[] overrides,
String theme) {
Map map = buildSignerMap(defaults, theme);
Map mapOverride = buildSignerMap(overrides, theme);
map.putAll(mapOverride);
return Collections.unmodifiableMap(map);
}
private , T> Map merge(JsonIndex[] defaults, JsonIndex[] overrides) {
Map map = buildMap(defaults);
Map mapOverride = buildMap(overrides);
map.putAll(mapOverride);
return Collections.unmodifiableMap(map);
}
private , T> Map buildMap(JsonIndex[] signerIndexes) {
Map map = new HashMap();
if (signerIndexes != null) {
for (JsonIndex index : signerIndexes) {
String region = index.getKey();
T prev = map.put(region, index.newReadOnlyConfig());
if (prev != null) {
log.warn("Duplicate definition of signer for " + index.getKey());
}
}
}
return map;
}
private , T> List append(C[] defaults, C[] overrides) {
List list = new LinkedList();
if (defaults != null) {
for (C builder : defaults) {
list.add(builder.build());
}
}
if (overrides != null) {
for (C builder : overrides) {
list.add(builder.build());
}
}
return list;
}
/**
* Builds and returns a signer configuration map.
*
* @param signerIndexes
* signer configuration entries loaded from JSON
* @param theme
* used for message logging. eg region, service, region+service
*/
private Map buildSignerMap(JsonIndex[] signerIndexes,
String theme) {
Map map = new HashMap();
if (signerIndexes != null) {
for (JsonIndex index : signerIndexes) {
String region = index.getKey();
SignerConfig prev = map.put(region, index.newReadOnlyConfig());
if (prev != null) {
log.warn("Duplicate definition of signer for " + theme + " " + index.getKey());
}
}
}
return map;
}
/**
* Returns the signer configuration for the specified service, not specific to any region.
*/
public SignerConfig getSignerConfig(String serviceName) {
return getSignerConfig(serviceName, null);
}
/**
* Returns the http client configuration for the http client name.
*/
public HttpClientConfig getHttpClientConfig(String httpClientName) {
return httpClients.get(httpClientName);
}
/**
* Returns the signer configuration for the specified service name and an optional region name.
*
* @param serviceName
* must not be null
* @param regionName
* similar to the region name in Regions
; can be null.
* @return the signer
*/
public SignerConfig getSignerConfig(String serviceName, String regionName) {
if (serviceName == null)
throw new IllegalArgumentException();
SignerConfig signerConfig = null;
if (regionName != null) {
// Service+Region signer config has the highest precedence
String key = serviceName + SERVICE_REGION_DELIMITOR + regionName;
signerConfig = serviceRegionSigners.get(key);
if (signerConfig != null) {
return signerConfig;
}
// Region signer config has the 2nd highest precedence
signerConfig = regionSigners.get(regionName);
if (signerConfig != null) {
return signerConfig;
}
}
// Service signer config has the 3rd highest precedence
signerConfig = serviceSigners.get(serviceName);
// Fall back to the default
return signerConfig == null ? defaultSignerConfig : signerConfig;
}
/**
* @return all the host-name-regex to region-name mappings.
*/
public List getHostRegexToRegionMappings() {
return Collections.unmodifiableList(hostRegexToRegionMappings);
}
/**
* @return the custom user agent template, if configured
*/
public String getUserAgentTemplate() {
return userAgentTemplate;
}
static InternalConfigJsonHelper loadfrom(URL url) throws JsonParseException, JsonMappingException, IOException {
if (url == null)
throw new IllegalArgumentException();
InternalConfigJsonHelper target = MAPPER.readValue(url, InternalConfigJsonHelper.class);
return target;
}
/**
* Loads and returns the AWS Java SDK internal configuration from the classpath.
*/
static InternalConfig load() throws JsonParseException, JsonMappingException, IOException {
// First try loading via the class by using a relative path
URL url = ClassLoaderHelper.getResource(DEFAULT_CONFIG_RESOURCE_RELATIVE_PATH, true, InternalConfig.class); // classesFirst=true
if (url == null) { // Then try with the absolute path
url = ClassLoaderHelper.getResource(DEFAULT_CONFIG_RESOURCE_ABSOLUTE_PATH, InternalConfig.class);
}
InternalConfigJsonHelper config = loadfrom(url);
InternalConfigJsonHelper configOverride;
URL overrideUrl = ClassLoaderHelper.getResource("/" + CONFIG_OVERRIDE_RESOURCE, InternalConfig.class);
if (overrideUrl == null) { // Try without a leading "/"
overrideUrl = ClassLoaderHelper.getResource(CONFIG_OVERRIDE_RESOURCE, InternalConfig.class);
}
if (overrideUrl == null) {
log.debug("Configuration override " + CONFIG_OVERRIDE_RESOURCE + " not found.");
configOverride = new InternalConfigJsonHelper();
} else {
configOverride = loadfrom(overrideUrl);
}
InternalConfig merged = new InternalConfig(config, configOverride);
merged.setDefaultConfigFileLocation(url);
merged.setOverrideConfigFileLocation(overrideUrl);
return merged;
}
/*
* For debugging purposes
*/
private URL defaultConfigFileLocation;
private URL overrideConfigFileLocation;
public URL getDefaultConfigFileLocation() {
return defaultConfigFileLocation;
}
public URL getOverrideConfigFileLocation() {
return overrideConfigFileLocation;
}
void setDefaultConfigFileLocation(URL url) {
this.defaultConfigFileLocation = url;
}
void setOverrideConfigFileLocation(URL url) {
this.overrideConfigFileLocation = url;
}
void dump() {
StringBuilder sb = new StringBuilder().append("defaultSignerConfig: ").append(defaultSignerConfig).append("\n")
.append("serviceRegionSigners: ").append(serviceRegionSigners).append("\n").append("regionSigners: ")
.append(regionSigners).append("\n").append("serviceSigners: ").append(serviceSigners).append("\n")
.append("userAgentTemplate: ").append(userAgentTemplate);
log.debug(sb.toString());
}
public static class Factory {
private static final InternalConfig SINGELTON;
static {
InternalConfig config = null;
try {
config = InternalConfig.load();
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new IllegalStateException("Fatal: Failed to load the internal config for AWS Java SDK", ex);
}
SINGELTON = config;
}
/**
* Returns a non-null and immutable instance of the AWS SDK internal configuration.
*/
public static InternalConfig getInternalConfig() {
return SINGELTON;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy