----初始化项目
This commit is contained in:
39
plugin/environment/pom.xml
Normal file
39
plugin/environment/pom.xml
Normal file
@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright 1999-2021 Alibaba Group Holding Ltd.
|
||||
~
|
||||
~ 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
|
||||
~
|
||||
~ 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.
|
||||
-->
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>nacos-plugin</artifactId>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>nacos-custom-environment-plugin</artifactId>
|
||||
<name>nacos-custom-environment-plugin ${project.version}</name>
|
||||
<url>https://nacos.io</url>
|
||||
<description>Nacos custom environment plugin pom.xml file</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<artifactId>nacos-common</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright 1999-2021 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* 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 com.alibaba.nacos.plugin.environment;
|
||||
|
||||
import com.alibaba.nacos.common.spi.NacosServiceLoader;
|
||||
import com.alibaba.nacos.common.utils.StringUtils;
|
||||
import com.alibaba.nacos.plugin.environment.spi.CustomEnvironmentPluginService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.HashSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* CustomEnvironment Plugin Management.
|
||||
*
|
||||
* @author : huangtianhui
|
||||
*/
|
||||
public class CustomEnvironmentPluginManager {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CustomEnvironmentPluginManager.class);
|
||||
|
||||
private static final List<CustomEnvironmentPluginService> SERVICE_LIST = new LinkedList<>();
|
||||
|
||||
private static final CustomEnvironmentPluginManager INSTANCE = new CustomEnvironmentPluginManager();
|
||||
|
||||
public CustomEnvironmentPluginManager() {
|
||||
loadInitial();
|
||||
}
|
||||
|
||||
private void loadInitial() {
|
||||
Collection<CustomEnvironmentPluginService> customEnvironmentPluginServices = NacosServiceLoader.load(
|
||||
CustomEnvironmentPluginService.class);
|
||||
for (CustomEnvironmentPluginService customEnvironmentPluginService : customEnvironmentPluginServices) {
|
||||
if (StringUtils.isBlank(customEnvironmentPluginService.pluginName())) {
|
||||
LOGGER.warn("[customEnvironmentPluginService] Load customEnvironmentPluginService({}) customEnvironmentPluginName(null/empty) fail."
|
||||
+ " Please Add customEnvironmentPluginName to resolve.", customEnvironmentPluginService.getClass());
|
||||
continue;
|
||||
}
|
||||
LOGGER.info("[CustomEnvironmentPluginManager] Load customEnvironmentPluginService({}) customEnvironmentPluginName({}) successfully.",
|
||||
customEnvironmentPluginService.getClass(), customEnvironmentPluginService.pluginName());
|
||||
}
|
||||
SERVICE_LIST.addAll(customEnvironmentPluginServices.stream()
|
||||
.sorted(Comparator.comparingInt(CustomEnvironmentPluginService::order))
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public static CustomEnvironmentPluginManager getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public Set<String> getPropertyKeys() {
|
||||
Set<String> keys = new HashSet<>();
|
||||
for (CustomEnvironmentPluginService customEnvironmentPluginService : SERVICE_LIST) {
|
||||
keys.addAll(customEnvironmentPluginService.propertyKey());
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
public Map<String, Object> getCustomValues(Map<String, Object> sourceProperty) {
|
||||
Map<String, Object> customValuesMap = new HashMap<>(1);
|
||||
for (CustomEnvironmentPluginService customEnvironmentPluginService : SERVICE_LIST) {
|
||||
Set<String> keys = customEnvironmentPluginService.propertyKey();
|
||||
Map<String, Object> propertyMap = new HashMap<>(keys.size());
|
||||
for (String key : keys) {
|
||||
propertyMap.put(key, sourceProperty.get(key));
|
||||
}
|
||||
Map<String, Object> targetPropertyMap = customEnvironmentPluginService.customValue(propertyMap);
|
||||
//Only the current plugin key is allowed
|
||||
Set<String> targetKeys = new HashSet<>(targetPropertyMap.keySet());
|
||||
targetKeys.removeAll(keys);
|
||||
for (String key : targetKeys) {
|
||||
targetPropertyMap.remove(key);
|
||||
}
|
||||
customValuesMap.putAll(targetPropertyMap);
|
||||
}
|
||||
for (Map.Entry<String, Object> entry : customValuesMap.entrySet()) {
|
||||
if (Objects.isNull(entry.getValue())) {
|
||||
customValuesMap.remove(entry.getKey());
|
||||
}
|
||||
}
|
||||
return customValuesMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Injection realization.
|
||||
*
|
||||
* @param customEnvironmentPluginService customEnvironmentPluginService implementation
|
||||
*/
|
||||
public static synchronized void join(CustomEnvironmentPluginService customEnvironmentPluginService) {
|
||||
if (Objects.isNull(customEnvironmentPluginService)) {
|
||||
return;
|
||||
}
|
||||
SERVICE_LIST.add(customEnvironmentPluginService);
|
||||
LOGGER.info("[CustomEnvironmentPluginService] join successfully.");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 1999-2021 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* 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 com.alibaba.nacos.plugin.environment.spi;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* CustomEnvironment Plugin Service.
|
||||
*
|
||||
* @author : huangtianhui
|
||||
*/
|
||||
public interface CustomEnvironmentPluginService {
|
||||
/**
|
||||
* customValue interface.
|
||||
*
|
||||
* @param property property key value
|
||||
* @return custom key value
|
||||
*/
|
||||
Map<String, Object> customValue(Map<String, Object> property);
|
||||
|
||||
/**
|
||||
* propertyKey interface.
|
||||
*
|
||||
* @return propertyKey property Key
|
||||
*/
|
||||
Set<String> propertyKey();
|
||||
|
||||
/**
|
||||
* order The larger the priority, the higher the priority.
|
||||
*
|
||||
* @return order
|
||||
*/
|
||||
Integer order();
|
||||
|
||||
/**
|
||||
* pluginName.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String pluginName();
|
||||
}
|
||||
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 1999-2021 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* 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 com.alibaba.nacos.plugin.environment;
|
||||
|
||||
import com.alibaba.nacos.plugin.environment.spi.CustomEnvironmentPluginService;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* CustomEnvironment Plugin Test.
|
||||
*
|
||||
* @author : huangtianhui
|
||||
*/
|
||||
public class CustomEnvironmentPluginManagerTest {
|
||||
@Test
|
||||
public void testInstance() {
|
||||
CustomEnvironmentPluginManager instance = CustomEnvironmentPluginManager.getInstance();
|
||||
Assert.assertNotNull(instance);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoin() {
|
||||
CustomEnvironmentPluginManager.join(new CustomEnvironmentPluginService() {
|
||||
@Override
|
||||
public Map<String, Object> customValue(Map<String, Object> property) {
|
||||
String pwd = (String) property.get("db.password.0");
|
||||
property.put("db.password.0", "test" + pwd);
|
||||
return property;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> propertyKey() {
|
||||
Set<String> propertyKey = new HashSet<>();
|
||||
propertyKey.add("db.password.0");
|
||||
return propertyKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer order() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String pluginName() {
|
||||
return "test";
|
||||
}
|
||||
});
|
||||
Assert.assertNotNull(CustomEnvironmentPluginManager.getInstance().getPropertyKeys());
|
||||
Map<String, Object> sourcePropertyMap = new HashMap<>();
|
||||
sourcePropertyMap.put("db.password.0", "nacos");
|
||||
Assert.assertNotNull(CustomEnvironmentPluginManager.getInstance().getCustomValues(sourcePropertyMap));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user