mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
[enhance issue #1492 ]Improved nacos configuration parsing, based on PropertySourceLoader---check
This commit is contained in:
@@ -24,7 +24,6 @@ import com.alibaba.cloud.nacos.NacosPropertySourceRepository;
|
||||
import com.alibaba.cloud.nacos.parser.NacosDataParserHandler;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -99,10 +98,10 @@ public class NacosPropertySourceBuilder {
|
||||
fileExtension);
|
||||
}
|
||||
catch (NacosException e) {
|
||||
log.error("get data from Nacos error,dataId:{}, ", dataId, e);
|
||||
log.error("get data from Nacos error,dataId:{} ", dataId, e);
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("parse data from Nacos error,dataId:{},data:{},", dataId, data, e);
|
||||
log.error("parse data from Nacos error,dataId:{},data:{}", dataId, data, e);
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
@@ -50,9 +50,10 @@ public abstract class AbstractPropertySourceLoader implements PropertySourceLoad
|
||||
/**
|
||||
* Prevent interference with other loaders.Nacos-specific loader, unless the reload
|
||||
* changes it.
|
||||
* @param name
|
||||
* @param resource
|
||||
* @return
|
||||
* @param name the root name of the property source. If multiple documents are loaded
|
||||
* an additional suffix should be added to the name for each source loaded.
|
||||
* @param resource the resource to load
|
||||
* @return if the resource can be loaded
|
||||
*/
|
||||
protected boolean canLoad(String name, Resource resource) {
|
||||
return resource instanceof NacosByteArrayResource;
|
||||
|
@@ -19,7 +19,7 @@ package com.alibaba.cloud.nacos.parser;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
|
||||
/**
|
||||
* Nacos-specific resource
|
||||
* Nacos-specific resource.
|
||||
*
|
||||
* @author zkz
|
||||
*/
|
||||
|
@@ -28,11 +28,9 @@ import java.util.stream.Collectors;
|
||||
import org.springframework.boot.env.OriginTrackedMapPropertySource;
|
||||
import org.springframework.boot.env.PropertySourceLoader;
|
||||
import org.springframework.core.env.EnumerablePropertySource;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -40,10 +38,19 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public final class NacosDataParserHandler {
|
||||
|
||||
/**
|
||||
* symbol: dot.
|
||||
*/
|
||||
public static final String DOT = ".";
|
||||
|
||||
/**
|
||||
* constant.
|
||||
*/
|
||||
public static final String VALUE = "value";
|
||||
|
||||
/**
|
||||
* default extension.
|
||||
*/
|
||||
public static final String DEFAULT_EXTENSION = "properties";
|
||||
|
||||
private static List<PropertySourceLoader> propertySourceLoaders;
|
||||
@@ -101,6 +108,12 @@ public final class NacosDataParserHandler {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* check the current extension can be processed.
|
||||
* @param loader the propertySourceLoader
|
||||
* @param extension file extension
|
||||
* @return if can match extension
|
||||
*/
|
||||
private boolean canLoadFileExtension(PropertySourceLoader loader, String extension) {
|
||||
return Arrays.stream(loader.getFileExtensions())
|
||||
.anyMatch((fileExtension) -> StringUtils.endsWithIgnoreCase(extension,
|
||||
|
@@ -34,7 +34,7 @@ import static com.alibaba.cloud.nacos.parser.NacosDataParserHandler.VALUE;
|
||||
/**
|
||||
* @author zkz
|
||||
*/
|
||||
public class JsonPropertySourceLoader extends AbstractPropertySourceLoader {
|
||||
public class NacosJsonPropertySourceLoader extends AbstractPropertySourceLoader {
|
||||
|
||||
/**
|
||||
* Returns the file extensions that the loader supports (excluding the '.').
|
||||
@@ -46,9 +46,14 @@ public class JsonPropertySourceLoader extends AbstractPropertySourceLoader {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name
|
||||
* @param resource
|
||||
* @return
|
||||
* Load the resource into one or more property sources. Implementations may either
|
||||
* return a list containing a single source, or in the case of a multi-document format
|
||||
* such as yaml a source for each document in the resource.
|
||||
* @param name the root name of the property source. If multiple documents are loaded
|
||||
* an additional suffix should be added to the name for each source loaded.
|
||||
* @param resource the resource to load
|
||||
* @return a list property sources
|
||||
* @throws IOException if the source cannot be loaded
|
||||
*/
|
||||
@Override
|
||||
protected List<PropertySource<?>> doLoad(String name, Resource resource)
|
||||
@@ -57,9 +62,9 @@ public class JsonPropertySourceLoader extends AbstractPropertySourceLoader {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
Map<String, Object> nacosDataMap = mapper.readValue(resource.getInputStream(),
|
||||
LinkedHashMap.class);
|
||||
flattenedMap(result, this.reloadMap(nacosDataMap), null);
|
||||
flattenedMap(result, nacosDataMap, null);
|
||||
return Collections.singletonList(
|
||||
new OriginTrackedMapPropertySource(name, nacosDataMap, true));
|
||||
new OriginTrackedMapPropertySource(name, this.reloadMap(result), true));
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,168 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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.alibaba.cloud.nacos.parser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import org.springframework.boot.env.OriginTrackedMapPropertySource;
|
||||
import org.springframework.boot.env.PropertiesPropertySourceLoader;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Parsing for XML requires overwriting the default
|
||||
* {@link PropertiesPropertySourceLoader}, because it internally rigorously validates
|
||||
* ({@conde DOCTYPE}) THE XML in a way that makes it difficult to customize the
|
||||
* configuration; at finally, make sure it's in the first place.
|
||||
*
|
||||
* @author zkz
|
||||
*/
|
||||
public class NacosXmlPropertySourceLoader extends AbstractPropertySourceLoader
|
||||
implements Ordered {
|
||||
|
||||
/**
|
||||
* Get the order value of this object.
|
||||
* <p>
|
||||
* Higher values are interpreted as lower priority. As a consequence, the object with
|
||||
* the lowest value has the highest priority (somewhat analogous to Servlet
|
||||
* {@code load-on-startup} values).
|
||||
* <p>
|
||||
* Same order values will result in arbitrary sort positions for the affected objects.
|
||||
* @return the order value
|
||||
* @see #HIGHEST_PRECEDENCE
|
||||
* @see #LOWEST_PRECEDENCE
|
||||
*/
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file extensions that the loader supports (excluding the '.').
|
||||
* @return the file extensions
|
||||
*/
|
||||
@Override
|
||||
public String[] getFileExtensions() {
|
||||
return new String[] { "xml" };
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the resource into one or more property sources. Implementations may either
|
||||
* return a list containing a single source, or in the case of a multi-document format
|
||||
* such as yaml a source for each document in the resource.
|
||||
* @param name the root name of the property source. If multiple documents are loaded
|
||||
* an additional suffix should be added to the name for each source loaded.
|
||||
* @param resource the resource to load
|
||||
* @return a list property sources
|
||||
* @throws IOException if the source cannot be loaded
|
||||
*/
|
||||
@Override
|
||||
protected List<PropertySource<?>> doLoad(String name, Resource resource)
|
||||
throws IOException {
|
||||
Map<String, Object> nacosDataMap = parseXml2Map(resource);
|
||||
return Collections.singletonList(
|
||||
new OriginTrackedMapPropertySource(name, nacosDataMap, true));
|
||||
|
||||
}
|
||||
|
||||
private Map<String, Object> parseXml2Map(Resource resource) throws IOException {
|
||||
Map<String, Object> map = new LinkedHashMap<>(32);
|
||||
try {
|
||||
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance()
|
||||
.newDocumentBuilder();
|
||||
Document document = documentBuilder.parse(resource.getInputStream());
|
||||
if (null == document) {
|
||||
return null;
|
||||
}
|
||||
parseNodeList(document.getChildNodes(), map, "");
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IOException("The xml content parse error.", e.getCause());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private void parseNodeList(NodeList nodeList, Map<String, Object> map,
|
||||
String parentKey) {
|
||||
if (nodeList == null || nodeList.getLength() < 1) {
|
||||
return;
|
||||
}
|
||||
parentKey = parentKey == null ? "" : parentKey;
|
||||
for (int i = 0; i < nodeList.getLength(); i++) {
|
||||
Node node = nodeList.item(i);
|
||||
String value = node.getNodeValue();
|
||||
value = value == null ? "" : value.trim();
|
||||
String name = node.getNodeName();
|
||||
name = name == null ? "" : name.trim();
|
||||
|
||||
if (StringUtils.isEmpty(name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String key = StringUtils.isEmpty(parentKey) ? name
|
||||
: parentKey + NacosDataParserHandler.DOT + name;
|
||||
NamedNodeMap nodeMap = node.getAttributes();
|
||||
parseNodeAttr(nodeMap, map, key);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE && node.hasChildNodes()) {
|
||||
parseNodeList(node.getChildNodes(), map, key);
|
||||
continue;
|
||||
}
|
||||
if (value.length() < 1) {
|
||||
continue;
|
||||
}
|
||||
map.put(parentKey, value);
|
||||
}
|
||||
}
|
||||
|
||||
private void parseNodeAttr(NamedNodeMap nodeMap, Map<String, Object> map,
|
||||
String parentKey) {
|
||||
if (null == nodeMap || nodeMap.getLength() < 1) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < nodeMap.getLength(); i++) {
|
||||
Node node = nodeMap.item(i);
|
||||
if (null == node) {
|
||||
continue;
|
||||
}
|
||||
if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
|
||||
if (StringUtils.isEmpty(node.getNodeName())) {
|
||||
continue;
|
||||
}
|
||||
if (StringUtils.isEmpty(node.getNodeValue())) {
|
||||
continue;
|
||||
}
|
||||
map.put(String.join(NacosDataParserHandler.DOT, parentKey,
|
||||
node.getNodeName()), node.getNodeValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -6,4 +6,5 @@ com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration
|
||||
org.springframework.boot.diagnostics.FailureAnalyzer=\
|
||||
com.alibaba.cloud.nacos.diagnostics.analyzer.NacosConnectionFailureAnalyzer
|
||||
org.springframework.boot.env.PropertySourceLoader=\
|
||||
com.alibaba.cloud.nacos.parser.JsonPropertySourceLoader
|
||||
com.alibaba.cloud.nacos.parser.NacosJsonPropertySourceLoader,\
|
||||
com.alibaba.cloud.nacos.parser.NacosXmlPropertySourceLoader
|
@@ -50,7 +50,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
@PowerMockIgnore({ "javax.management.*", "javax.xml.parsers.*",
|
||||
"com.sun.org.apache.xerces.internal.jaxp.*" })
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ NacosConfigService.class })
|
||||
@SpringBootTest(classes = NacosConfigurationNoSuffixTest.TestConfig.class, properties = {
|
||||
|
@@ -49,7 +49,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
||||
* @author zkz
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
@PowerMockIgnore({ "javax.management.*", "javax.xml.parsers.*",
|
||||
"com.sun.org.apache.xerces.internal.jaxp.*", "org.w3c.dom.*" })
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ NacosConfigService.class })
|
||||
@SpringBootTest(classes = NacosConfigurationXmlJsonTest.TestConfig.class, properties = {
|
||||
@@ -83,13 +84,15 @@ public class NacosConfigurationXmlJsonTest {
|
||||
throws Throwable {
|
||||
|
||||
if ("xmlApp.xml".equals(args[0]) && "test-group".equals(args[1])) {
|
||||
return "<top>\n" + " <first>one</first>\n"
|
||||
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<top>\n"
|
||||
+ " <first>one</first>\n"
|
||||
+ " <sencond value=\"two\">\n"
|
||||
+ " <third>three</third>\n" + " </sencond>\n"
|
||||
+ "</top>";
|
||||
}
|
||||
if ("test-name.xml".equals(args[0]) && "test-group".equals(args[1])) {
|
||||
return "<Server port=\"8005\" shutdown=\"SHUTDOWN\"> \n"
|
||||
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
|
||||
+ "<Server port=\"8005\" shutdown=\"SHUTDOWN\"> \n"
|
||||
+ " <Service name=\"Catalina\"> \n"
|
||||
+ " <Connector value=\"第二个连接器\"> \n"
|
||||
+ " <open>开启服务</open> \n"
|
||||
@@ -108,7 +111,8 @@ public class NacosConfigurationXmlJsonTest {
|
||||
|
||||
if ("test-name-dev.xml".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "<application android:label=\"@string/app_name\" android:icon=\"@drawable/osg\">\n"
|
||||
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
|
||||
+ "<application android:label=\"@string/app_name\" android:icon=\"@drawable/osg\">\n"
|
||||
+ " <activity android:name=\".osgViewer\"\n"
|
||||
+ " android:label=\"@string/app_name\" android:screenOrientation=\"landscape\">\n"
|
||||
+ " <intent-filter>\n"
|
||||
|
Reference in New Issue
Block a user