forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUE alibaba#8169] add CustomEnvironmentPlugin (alibaba#8169)
* fix issue alibaba#8169 . * reset properties.
- Loading branch information
Showing
11 changed files
with
413 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
.../src/main/java/com/alibaba/nacos/plugin/environment/impl/NacosCustomEnvPluginService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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.impl; | ||
|
||
import com.alibaba.nacos.common.utils.MD5Utils; | ||
import com.alibaba.nacos.config.server.constant.Constants; | ||
import com.alibaba.nacos.plugin.environment.spi.CustomEnvironmentPluginService; | ||
|
||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Nacos default auth plugin service implementation. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
|
||
public class NacosCustomEnvPluginService implements CustomEnvironmentPluginService { | ||
|
||
@Override | ||
public Map<String, Object> customValue(Map<String, Object> property) { | ||
String pwd = (String) property.get("db.password.0"); | ||
property.put("db.password.0", MD5Utils.md5Hex(pwd, Constants.ENCODE)); | ||
return property; | ||
} | ||
|
||
@Override | ||
public Set<String> propertyKey() { | ||
Set<String> propertyKey = new HashSet<>(); | ||
propertyKey.add("db.password.0"); | ||
return propertyKey; | ||
} | ||
|
||
@Override | ||
public Integer order() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public String pluginName() { | ||
return "testCustomEnvironmentPluginService"; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...META-INF/services/com.alibaba.nacos.plugin.environment.spi.CustomEnvironmentPluginService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
com.alibaba.nacos.plugin.environment.impl.NacosCustomEnvPluginService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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>custom-environment-plugin</artifactId> | ||
<name>custom-environment-plugin ${project.version}</name> | ||
<url>http://nacos.io</url> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.alibaba.nacos</groupId> | ||
<artifactId>nacos-common</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
103 changes: 103 additions & 0 deletions
103
...nt/src/main/java/com/alibaba/nacos/plugin/environment/CustomEnvironmentPluginManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
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<>(); | ||
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."); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...rc/main/java/com/alibaba/nacos/plugin/environment/spi/CustomEnvironmentPluginService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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(); | ||
} |
51 changes: 51 additions & 0 deletions
51
...rc/test/java/com/alibaba/nacos/plugin/environment/CustomEnvironmentPluginManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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.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"; | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.