Skip to content

Commit

Permalink
Merge c7b9a88 into b6ec548
Browse files Browse the repository at this point in the history
  • Loading branch information
jeho0815 committed Jun 5, 2018
2 parents b6ec548 + c7b9a88 commit 4fa4d00
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public void tearDown() {
}

@Test
@SuppressWarnings(value = "unchecked")
public void testConfigurations(@Mocked RoutingContext context
, @Mocked HttpServerRequest requst
, @Mocked EdgeInvocation invocation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public interface ServiceCombConstants {

String CONFIG_DEFAULT_REGISTER_BY = "SDK";

String SERVICECOMB_ENV = "SERVICECOMB_ENV";
String SERVICECOMB_ENV = "service_description.environment";

String DEFAULT_SERVICECOMB_ENV = "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,43 @@

package org.apache.servicecomb.config;

import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Created by on 2017/1/5.
*/
public final class ConfigMapping {
private static Map<String, Object> configMap = null;

private static final Logger LOGGER = LoggerFactory.getLogger(ConfigMapping.class);

static {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream is = loader.getResourceAsStream("mapping.yaml");
configMap = YAMLUtil.yaml2Properties(is);
List<URL> urlList = new ArrayList<>();
configMap = new HashMap<String, Object>();
Enumeration<URL> urls;
try {
urls = loader.getResources("mapping.yaml");
while (urls.hasMoreElements()) {
urlList.add(urls.nextElement());
}
for (URL url : urlList) {
configMap.putAll(YAMLUtil.yaml2Properties(url.openStream()));
}
} catch (IOException e) {
LOGGER.error("get config mapping file error!", e);
}
}

private ConfigMapping() {
Expand All @@ -54,9 +77,40 @@ public static Map<String, Object> getConvertedMap(Map<String, Object> oldMap) {
String key = entry.getKey();
Object configValue = oldMap.get(key);
if (configValue != null) {
String newKey = (String) entry.getValue();
retMap.put(newKey, configValue);
retMap.remove(key);
if (entry.getValue() instanceof List) {
@SuppressWarnings("unchecked")
List<String> newKeys = (List<String>) entry.getValue();
for (String newKey : newKeys) {
retMap.put(newKey, configValue);
}
} else {
String newKey = (String) entry.getValue();
retMap.put(newKey, configValue);
}
}
}
return retMap;
}

public static Map<String, Object> getConvertedMap(Configuration config) {
if (configMap == null) {
return new LinkedHashMap<>();
}
Map<String, Object> retMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : configMap.entrySet()) {
String key = entry.getKey();
Object configValue = config.getProperty(key);
if (configValue != null) {
if (entry.getValue() instanceof List) {
@SuppressWarnings("unchecked")
List<String> newKeys = (List<String>) entry.getValue();
for (String newKey : newKeys) {
retMap.put(newKey, configValue);
}
} else {
String newKey = (String) entry.getValue();
retMap.put(newKey, configValue);
}
}
}
return retMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,19 @@ public static ConcurrentCompositeConfiguration createLocalConfig(List<ConfigMode
convertEnvVariable(new ConcurrentMapConfiguration(new EnvironmentConfiguration())),
"configFromEnvironment");
// If there is extra configurations, add it into config.
EXTRA_CONFIG_MAP.entrySet().stream()
EXTRA_CONFIG_MAP.entrySet()
.stream()
.filter(mapEntry -> !mapEntry.getValue().isEmpty())
.forEachOrdered(configMapEntry ->
duplicateServiceCombConfigToCse(config,
new ConcurrentMapConfiguration(configMapEntry.getValue()),
configMapEntry.getKey()));
.forEachOrdered(configMapEntry -> duplicateServiceCombConfigToCse(config,
new ConcurrentMapConfiguration(configMapEntry.getValue()),
configMapEntry.getKey()));
duplicateServiceCombConfigToCse(config,
new DynamicConfiguration(
new MicroserviceConfigurationSource(configModelList), new NeverStartPollingScheduler()),
"configFromYamlFile");

duplicateServiceCombConfigToCseAtFront(config,
new ConcurrentMapConfiguration(ConfigMapping.getConvertedMap(config)),
"configFromMapping");
return config;
}

Expand Down Expand Up @@ -181,6 +183,14 @@ private static void duplicateServiceCombConfigToCse(ConcurrentCompositeConfigura
compositeConfiguration.addConfiguration(source, sourceName);
}

private static void duplicateServiceCombConfigToCseAtFront(ConcurrentCompositeConfiguration compositeConfiguration,
AbstractConfiguration source,
String sourceName) {
duplicateServiceCombConfigToCse(source);

compositeConfiguration.addConfigurationAtFront(source, sourceName);
}

private static ConfigCenterConfigurationSource createConfigCenterConfigurationSource(
Configuration localConfiguration) {
ConfigCenterConfigurationSource configCenterConfigurationSource =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.List;
import java.util.Map;

import org.apache.servicecomb.config.ConfigMapping;
import org.apache.servicecomb.config.YAMLUtil;

import com.netflix.config.PollResult;
Expand All @@ -45,7 +44,6 @@ public PollResult poll(boolean b, Object o) throws Exception {
configurations.putAll(YAMLUtil.retrieveItems("", configModel.getConfig()));
}

configurations = ConfigMapping.getConvertedMap(configurations);
return PollResult.createFull(configurations);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,4 @@
## limitations under the License.
## ---------------------------------------------------------------------------

eureka:
client:
serviceUrl:
defaultZone: registry.client.serviceUrl.defaultZone

service_description:
environment: SERVICECOMB_ENV
SERVICECOMB_ENV: service_description.environment
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@
public class TestConfigMapping {
@Test
public void testMapping() {
String value = ConfigMapping.map("eureka.client.serviceUrl.defaultZone");
String value = ConfigMapping.map("SERVICECOMB_ENV");
Map<String, Object> m = ConfigMapping.getMapping();
assertEquals(value, "registry.client.serviceUrl.defaultZone");
assertEquals("service_description.environment", value);
assertNotNull(m);
}

@Test
public void testConvertedMap() {
String value = ConfigMapping.map("eureka.client.serviceUrl.defaultZone");
String value = ConfigMapping.map("CSE_ENV_MAPPING");
Map<String, Object> m = ConfigMapping.getMapping();
Map<String, Object> m1 = ConfigMapping.getConvertedMap(m);
assertEquals(value, "registry.client.serviceUrl.defaultZone");
assertEquals("servicecomb.testmapping.key", value);
assertNotNull(m1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public static void beforeTest() {
System.setProperty(systemPropertyName, systemExpected);
try {
setEnv(environmentPropertyName, environmentExpected);
setEnv("MY_SERVICES_ENDPOINT", "https://myhost:8888");
} catch (Exception e) {
throw new IllegalStateException(e);
}
Expand Down Expand Up @@ -133,45 +134,45 @@ public void propertiesFromFileIsDuplicatedToCse() {
String expected = "value";

assertThat(DynamicPropertyFactory
.getInstance()
.getStringProperty("servicecomb.cse.servicecomb.file", null)
.get(),
.getInstance()
.getStringProperty("servicecomb.cse.servicecomb.file", null)
.get(),
equalTo(expected));

assertThat(DynamicPropertyFactory
.getInstance()
.getStringProperty("cse.cse.servicecomb.file", null)
.get(),
.getInstance()
.getStringProperty("cse.cse.servicecomb.file", null)
.get(),
equalTo(expected));
}

@Test
public void propertiesFromSystemIsDuplicatedToCse() {
assertThat(DynamicPropertyFactory
.getInstance()
.getStringProperty(systemPropertyName, null)
.get(),
.getInstance()
.getStringProperty(systemPropertyName, null)
.get(),
equalTo(systemExpected));

assertThat(DynamicPropertyFactory
.getInstance()
.getStringProperty("servicecomb.cse.servicecomb.system.setting", null)
.get(),
.getInstance()
.getStringProperty("servicecomb.cse.servicecomb.system.setting", null)
.get(),
equalTo(systemExpected));
}

@Test
public void propertiesFromEnvironmentIsDuplicatedToCse() {
assertThat(DynamicPropertyFactory
.getInstance()
.getStringProperty(environmentPropertyName, null)
.get(),
.getInstance()
.getStringProperty(environmentPropertyName, null)
.get(),
equalTo(environmentExpected));

assertThat(DynamicPropertyFactory
.getInstance()
.getStringProperty("servicecomb.cse.servicecomb.environment.setting", null)
.get(),
.getInstance()
.getStringProperty("servicecomb.cse.servicecomb.environment.setting", null)
.get(),
equalTo(environmentExpected));
}

Expand Down Expand Up @@ -322,6 +323,8 @@ public void testCreateLocalConfigWithExtraConfig() {
String overriddenConfigKey = "servicecomb.cse.servicecomb.file";
extraConfig.put(extraConfigKey, extraConfigValue);
final String propertyHigherPriority = "higher_priority";
String mapedKey1 = "servicecomb.service.mapping.address";
String mapedKey2 = "servicecomb.service1.mapping.address";
extraConfig.put(overriddenConfigKey, propertyHigherPriority);

ConfigUtil.addExtraConfig("testExtraConfig", extraConfig);
Expand All @@ -330,6 +333,9 @@ public void testCreateLocalConfigWithExtraConfig() {

Assert.assertEquals(extraConfigValue, localConfiguration.getProperty(extraConfigKey));
Assert.assertEquals(propertyHigherPriority, localConfiguration.getString(overriddenConfigKey));
// Test mapping key/value from self mappfing.xml
Assert.assertEquals("https://myhost:8888", localConfiguration.getString(mapedKey1));
Assert.assertEquals("https://myhost:8888", localConfiguration.getString(mapedKey2));
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.apache.servicecomb.config.archaius.sources.ConfigSourceMaker.yamlConfigSource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.net.URL;
import java.util.List;
Expand Down Expand Up @@ -50,8 +49,6 @@ public void testPullFromClassPath() throws Exception {
assertEquals(20, configMap.size());
assertNotNull(configMap.get("trace.handler.sampler.percent"));
assertEquals(0.5, configMap.get("trace.handler.sampler.percent"));
assertEquals("http://10.120.169.202:9980/", configMap.get("registry.client.serviceUrl.defaultZone"));
assertNull(configMap.get("eureka.client.serviceUrl.defaultZone"));
}

@Test
Expand Down
21 changes: 21 additions & 0 deletions foundations/foundation-config/src/test/resources/mapping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements. See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You 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.
## ---------------------------------------------------------------------------

CSE_ENV_MAPPING: servicecomb.testmapping.key
MY_SERVICES_ENDPOINT:
- servicecomb.service.mapping.address
- servicecomb.service1.mapping.address
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,13 @@ public void correctResponsesHavePaths() {
@Test(expected = ServiceCombException.class)
public void testInvalidate() throws Exception {
URL resource = TestSwaggerUtils.class.getResource("/swagger1.yaml");
byte[] buffer = new byte[2048];
Swagger swagger = SwaggerUtils.parseSwagger(resource);
SwaggerUtils.validateSwagger(swagger);
}

@Test
public void testInvalidateValid() throws Exception {
URL resource = TestSwaggerUtils.class.getResource("/swagger2.yaml");
byte[] buffer = new byte[2048];
Swagger swagger = SwaggerUtils.parseSwagger(resource);
SwaggerUtils.validateSwagger(swagger);
}
Expand Down

0 comments on commit 4fa4d00

Please sign in to comment.