Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.config.NamespaceHandler;
import com.ctrip.framework.apollo.spring.property.PlaceholderHelper;
import com.ctrip.framework.apollo.spring.property.SpringValue;
import com.ctrip.framework.apollo.spring.property.SpringValueRegistry;
Expand All @@ -32,6 +33,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.Nullable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -128,9 +130,10 @@ private void processApolloConfigChangeListener(final Object bean, final Method m
annotatedInterestedKeyPrefixes.length > 0 ? Sets.newHashSet(annotatedInterestedKeyPrefixes)
: null;

for (String namespace : namespaces) {
final String resolvedNamespace = this.environment.resolveRequiredPlaceholders(namespace);
Config config = ConfigService.getConfig(resolvedNamespace);
Set<String> resolvedNamespaces = processResolveNameSpaceValue(namespaces);
Comment thread
nobodyiam marked this conversation as resolved.
Outdated

for (String namespace : resolvedNamespaces) {
Config config = ConfigService.getConfig(namespace);

if (interestedKeys == null && interestedKeyPrefixes == null) {
config.addChangeListener(configChangeListener);
Expand All @@ -140,6 +143,30 @@ private void processApolloConfigChangeListener(final Object bean, final Method m
}
}

/**
* Evaluate and resolve namespaces from env/properties.
* Split delimited namespaces
* @param namespaces
* @return resolved namespaces
*/
private Set<String> processResolveNameSpaceValue(String[] namespaces) {
Comment thread
nobodyiam marked this conversation as resolved.
Outdated

Set<String> resolvedNamespaces = new HashSet<>();

for (String namespace : namespaces) {
final String resolvedNamespace = this.environment.resolveRequiredPlaceholders(namespace);

if (resolvedNamespace.contains(NamespaceHandler.NAMESPACE_DELIMITER)) {
resolvedNamespaces.addAll(
NamespaceHandler.parseCommaSeparatedNamespaces(resolvedNamespace));
} else {
resolvedNamespaces.add(resolvedNamespace);
}
}

return resolvedNamespaces;
}

private void processApolloJsonValue(Object bean, String beanName, Field field) {
ApolloJsonValue apolloJsonValue = AnnotationUtils.getAnnotation(field, ApolloJsonValue.class);
if (apolloJsonValue == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.ctrip.framework.apollo.spring.config;

import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
Expand All @@ -32,7 +34,9 @@
*/
public class NamespaceHandler extends NamespaceHandlerSupport {

private static final Splitter NAMESPACE_SPLITTER = Splitter.on(",").omitEmptyStrings()

public static final String NAMESPACE_DELIMITER = ",";
private static final Splitter NAMESPACE_SPLITTER = Splitter.on(NAMESPACE_DELIMITER).omitEmptyStrings()
.trimResults();

@Override
Expand Down Expand Up @@ -79,4 +83,17 @@ protected void doParse(Element element, BeanDefinitionBuilder builder) {
PropertySourcesProcessor.addNamespaces(NAMESPACE_SPLITTER.splitToList(namespaces), order);
}
}

/**
* @param namespaces - comma separated string of namespaces
* @return List of namespaces
*/
public static List<String> parseCommaSeparatedNamespaces(String namespaces) {
Comment thread
anandjoshisn marked this conversation as resolved.
Outdated

if (namespaces == null) {
return Collections.emptyList();
}

return NAMESPACE_SPLITTER.splitToList(namespaces);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -94,6 +94,7 @@ public void tearDown() throws Exception {
System.clearProperty(SystemPropertyKeyConstants.FROM_SYSTEM_YAML_NAMESPACE);
System.clearProperty(SystemPropertyKeyConstants.FROM_NAMESPACE_APPLICATION_KEY);
System.clearProperty(SystemPropertyKeyConstants.FROM_NAMESPACE_APPLICATION_KEY_YAML);
System.clearProperty(SystemPropertyKeyConstants.DELIMITED_NAMESPACES);
System.clearProperty(ApolloClientSystemConsts.APOLLO_PROPERTY_NAMES_CACHE_ENABLE);
super.tearDown();
}
Expand Down Expand Up @@ -471,6 +472,57 @@ public void testApolloConfigChangeListenerResolveExpressionSimple() {
verify(applicationConfig, times(3)).addChangeListener(any(ConfigChangeListener.class));
}


/**
* resolve namespace's from comma separated namespaces
*/
@Test
public void testApolloConfigChangeListenerWithCommaSeparatedNameSpaces() {

final String propValue = "app1,app2,app3";
System.setProperty(SystemPropertyKeyConstants.DELIMITED_NAMESPACES, propValue);

Config app1Config = mock(Config.class);
mockConfig("app1", app1Config);

Config app2Config = mock(Config.class);
mockConfig("app2", app2Config);

Config app3Config = mock(Config.class);
mockConfig("app3", app3Config);

getSimpleBean(TestApolloConfigChangeListenerWithCommaSeparatedNameSpaces.class);

verify(app1Config, times(1)).addChangeListener(any(ConfigChangeListener.class));
verify(app2Config, times(1)).addChangeListener(any(ConfigChangeListener.class));
verify(app3Config, times(1)).addChangeListener(any(ConfigChangeListener.class));
}

/**
* resolve namespace's from comma separated namespaces
*/
@Test
public void testApolloConfigChangeListenerWithCommaSeparatedNameSpacesMergedWithOnesInValue() {

final String propValue = "app1,app2";
System.setProperty(SystemPropertyKeyConstants.DELIMITED_NAMESPACES, propValue);

Config app1Config = mock(Config.class);
mockConfig("app1", app1Config);

Config app2Config = mock(Config.class);
mockConfig("app2", app2Config);

Config appConfig = mock(Config.class);
mockConfig("app", appConfig);

getSimpleBean(TestApolloConfigChangeListenerWithCommaSeparatedNameSpacesMergedWithOnesInValue.class);

verify(app1Config, times(1)).addChangeListener(any(ConfigChangeListener.class));
verify(app2Config, times(1)).addChangeListener(any(ConfigChangeListener.class));
verify(appConfig, times(1)).addChangeListener(any(ConfigChangeListener.class));
}

/**
* resolve namespace's name from system property.
*/
Expand Down Expand Up @@ -597,6 +649,7 @@ private static class SystemPropertyKeyConstants {
static final String FROM_SYSTEM_YAML_NAMESPACE = "from.system.yaml.namespace";
static final String FROM_NAMESPACE_APPLICATION_KEY = "from.namespace.application.key";
static final String FROM_NAMESPACE_APPLICATION_KEY_YAML = "from.namespace.application.key.yaml";
static final String DELIMITED_NAMESPACES = "delimited.namespaces";
}

@EnableApolloConfig
Expand Down Expand Up @@ -670,6 +723,24 @@ private void onChange(ConfigChangeEvent event) {
}
}

@Configuration
@EnableApolloConfig
static class TestApolloConfigChangeListenerWithCommaSeparatedNameSpaces {

@ApolloConfigChangeListener("${" + SystemPropertyKeyConstants.DELIMITED_NAMESPACES + "}")
private void onChange(ConfigChangeEvent changeEvent) {
}
}

@Configuration
@EnableApolloConfig
static class TestApolloConfigChangeListenerWithCommaSeparatedNameSpacesMergedWithOnesInValue {

@ApolloConfigChangeListener(value = {"app", "${" + SystemPropertyKeyConstants.DELIMITED_NAMESPACES + "}"})
private void onChange(ConfigChangeEvent changeEvent) {
}
}

@Configuration
@EnableApolloConfig
static class TestApolloConfigChangeListenerResolveExpressionFromSystemPropertyConfiguration {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2022 Apollo 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
*
* 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.ctrip.framework.apollo.spring.config;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class NameSpaceHandlerTest {

@Test
public void testParseCommaSeparatedNamespacesForValidCase() {
final String NAMESPACES = "app1,app2,app3";
List<String> namespaces = NamespaceHandler.parseCommaSeparatedNamespaces(NAMESPACES);
assertEquals(namespaces.size(), 3);
assertEquals(namespaces, Arrays.asList(NAMESPACES.split(",")));
}

@Test
public void testParseCommaSeparatedNamespacesForInValidCase() {
final String NAMESPACES = "";
List<String> namespaces = NamespaceHandler.parseCommaSeparatedNamespaces(NAMESPACES);
assertEquals(namespaces.size(), 0);

namespaces = NamespaceHandler.parseCommaSeparatedNamespaces(null);
assertEquals(namespaces.size(), 0);
}

}