com.ksc.internal.config.InternalConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ksyun-java-sdk-core Show documentation
Show all versions of ksyun-java-sdk-core Show documentation
The KSC SDK for Java - Core module holds the classes that is used
by the individual service clients to interact with KSC Web Services.
Users need to depend on KSC-java-sdk artifact for accessing individual client classes.
The newest version!
/*
* Copyright 2010-2016 ksyun.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://ksyun.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.ksc.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.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;
import com.ksc.annotation.Immutable;
import com.ksc.log.InternalLogApi;
import com.ksc.log.InternalLogFactory;
import com.ksc.util.ClassLoaderHelper;
/**
* Internal configuration for the KSC 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 = "kscsdk_config_default.json";
static final String DEFAULT_CONFIG_RESOURCE_ABSOLUTE_PATH = "/com/ksc/internal/config/"
+ DEFAULT_CONFIG_RESOURCE_RELATIVE_PATH;
static final String CONFIG_OVERRIDE_RESOURCE = "kscsdk_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 KSC 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 KSC Java SDK", ex);
}
SINGELTON = config;
}
/**
* Returns a non-null and immutable instance of the KSC SDK internal configuration.
*/
public static InternalConfig getInternalConfig() {
return SINGELTON;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy