This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new 80314fb  [SCB-2160] can not resolve list placeholders by 
environment.getProperty (#2133)
80314fb is described below

commit 80314fb39c6ca545a27bd323c495e831f6b5c422
Author: wujimin <[email protected]>
AuthorDate: Tue Dec 15 11:15:04 2020 +0800

    [SCB-2160] can not resolve list placeholders by environment.getProperty 
(#2133)
---
 .../core/ConfigurationSpringInitializer.java       | 64 +++++++++++-----------
 .../org/apache/servicecomb/it/ConsumerMain.java    |  3 +
 .../it/testcase/TestSpringConfiguration.java       | 39 +++++++++++++
 .../src/main/resources/microservice.yaml           |  8 ++-
 4 files changed, 81 insertions(+), 33 deletions(-)

diff --git 
a/core/src/main/java/org/apache/servicecomb/core/ConfigurationSpringInitializer.java
 
b/core/src/main/java/org/apache/servicecomb/core/ConfigurationSpringInitializer.java
index 172afc4..9d11189 100644
--- 
a/core/src/main/java/org/apache/servicecomb/core/ConfigurationSpringInitializer.java
+++ 
b/core/src/main/java/org/apache/servicecomb/core/ConfigurationSpringInitializer.java
@@ -23,6 +23,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.stream.Collectors;
 
 import org.apache.commons.configuration.AbstractConfiguration;
 import org.apache.servicecomb.config.ConfigMapping;
@@ -105,47 +106,46 @@ public class ConfigurationSpringInitializer extends 
PropertyPlaceholderConfigure
   }
 
   private void addMicroserviceYAMLToSpring(Environment environment) {
-    if (environment instanceof ConfigurableEnvironment) {
-      ConfigurableEnvironment ce = (ConfigurableEnvironment) environment;
-      MicroserviceConfigLoader loader = new MicroserviceConfigLoader();
-      loader.loadAndSort();
-
-      ce.getPropertySources()
-          .addLast(new 
EnumerablePropertySource<MicroserviceConfigLoader>("microservice.yaml",
-              loader) {
+    if (!(environment instanceof ConfigurableEnvironment)) {
+      return;
+    }
 
-            private boolean parsed = false;
+    ((ConfigurableEnvironment) environment).getPropertySources()
+        .addLast(new 
EnumerablePropertySource<MicroserviceConfigLoader>("microservice.yaml") {
+          private final Map<String, Object> values = new HashMap<>();
 
-            private Map<String, Object> values = null;
+          private final String[] propertyNames;
 
-            private String[] propertyNames = null;
+          {
+            MicroserviceConfigLoader loader = new MicroserviceConfigLoader();
+            loader.loadAndSort();
 
-            @Override
-            public String[] getPropertyNames() {
-              if (!parsed) {
-                parseData();
-              }
+            loader.getConfigModels()
+                .forEach(configModel -> 
values.putAll(YAMLUtil.retrieveItems("", configModel.getConfig())));
 
-              return propertyNames;
-            }
+            propertyNames = values.keySet().toArray(new String[values.size()]);
+          }
 
-            @Override
-            public Object getProperty(String s) {
-              if (!parsed) {
-                parseData();
-              }
+          @Override
+          public String[] getPropertyNames() {
+            return propertyNames;
+          }
 
-              return this.values.get(s);
-            }
+          @SuppressWarnings("unchecked")
+          @Override
+          public Object getProperty(String name) {
+            Object value = this.values.get(name);
 
-            private void parseData() {
-              values = new HashMap<>();
-              loader.getConfigModels()
-                  .forEach(configModel -> 
values.putAll(YAMLUtil.retrieveItems("", configModel.getConfig())));
-              propertyNames = values.keySet().toArray(new 
String[values.size()]);
+            // spring will not resolve nested placeholder of list, so try to 
fix the problem
+            if (value instanceof List) {
+              value = ((List<Object>) value).stream()
+                  .filter(item -> item instanceof String)
+                  .map(item -> environment.resolvePlaceholders((String) item))
+                  .collect(Collectors.toList());
             }
-          });
-    }
+            return value;
+          }
+        });
   }
 
   private void addMappingToString(Environment environment) {
diff --git 
a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
 
b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
index 9193b13..13cdfe2 100644
--- 
a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
+++ 
b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
@@ -48,6 +48,7 @@ import 
org.apache.servicecomb.it.testcase.TestRequestBodySpringMvcSchema;
 import org.apache.servicecomb.it.testcase.TestRestController;
 import org.apache.servicecomb.it.testcase.TestRestServerConfigEdge;
 import org.apache.servicecomb.it.testcase.TestRestVertxTransportConfig;
+import org.apache.servicecomb.it.testcase.TestSpringConfiguration;
 import org.apache.servicecomb.it.testcase.TestTrace;
 import org.apache.servicecomb.it.testcase.TestTraceEdge;
 import org.apache.servicecomb.it.testcase.TestTransportContext;
@@ -91,6 +92,8 @@ public class ConsumerMain {
   }
 
   protected static void run() throws Throwable {
+    ITJUnitUtils.run(TestSpringConfiguration.class);
+
     // deploy edge/zuul
     // if not ready, will start a new instance and wait for ready
     deploys.getEdge().ensureReady();
diff --git 
a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestSpringConfiguration.java
 
b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestSpringConfiguration.java
new file mode 100644
index 0000000..da74822
--- /dev/null
+++ 
b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestSpringConfiguration.java
@@ -0,0 +1,39 @@
+/*
+ * 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.
+ */
+package org.apache.servicecomb.it.testcase;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.List;
+
+import org.apache.servicecomb.foundation.common.utils.BeanUtils;
+import org.junit.Test;
+import org.springframework.core.env.Environment;
+
+public class TestSpringConfiguration {
+  @Test
+  public void should_resolve_placeholder_in_list_by_spring_environment() {
+    Environment environment = BeanUtils.getContext().getEnvironment();
+
+    String value = environment.getProperty("placeholder");
+    assertThat(value).isEqualTo("actual-value1,actual-value2");
+
+    @SuppressWarnings("unchecked")
+    List<String> list = environment.getProperty("placeholder", List.class);
+    assertThat(list).containsExactly("actual-value1", "actual-value2");
+  }
+}
diff --git a/integration-tests/it-consumer/src/main/resources/microservice.yaml 
b/integration-tests/it-consumer/src/main/resources/microservice.yaml
index 1fe9eea..a132218 100644
--- a/integration-tests/it-consumer/src/main/resources/microservice.yaml
+++ b/integration-tests/it-consumer/src/main/resources/microservice.yaml
@@ -20,4 +20,10 @@ service_description:
 servicecomb:
   rest:
     client:
-      maxHeaderSize: 10000
\ No newline at end of file
+      maxHeaderSize: 10000
+
+placeholder:
+  - ${placeholder-key1}
+  - ${placeholder-key2}
+placeholder-key1: actual-value1
+placeholder-key2: actual-value2
\ No newline at end of file

Reply via email to