Skip to content

Commit

Permalink
Polish apache#4119 : DynamicConfiguration adds the publish and multip…
Browse files Browse the repository at this point in the history
…le get configuration methods
  • Loading branch information
mercyblitz committed May 23, 2019
1 parent f4a5490 commit a5da695
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
import org.apache.dubbo.common.config.Configuration;
import org.apache.dubbo.common.config.Environment;

import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;

import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;

Expand All @@ -28,12 +33,13 @@
* <br/>
* From the use scenario internally in framework, there're mainly three kinds of methods:
* <ul>
* <li>1. getConfig, get governance rules or single config item from Config Center.</li>
* <li>2. getConfigFile, get configuration file from Config Center at start up.</li>
* <li>3. addListener/removeListener, add or remove listeners for governance rules or config items that need to watch.</li>
* <li>1. getConfig, get governance rules or single config item from Config Center.</li>
* <li>2. getConfigFile, get configuration file from Config Center at start up.</li>
* <li>3. addListener/removeListener, add or remove listeners for governance rules or config items that need to watch.</li>
* </ul>
*/
public interface DynamicConfiguration extends Configuration {

String DEFAULT_GROUP = "dubbo";

/**
Expand Down Expand Up @@ -113,7 +119,7 @@ default String getConfig(String key, String group) {

/**
* {@see #getConfig(String, String, long)}
*
* <p>
* This method are mostly used to get a compound config file, such as a complete dubbo.properties file.
*/
default String getConfigs(String key, String group) throws IllegalStateException {
Expand All @@ -122,11 +128,67 @@ default String getConfigs(String key, String group) throws IllegalStateException

/**
* {@see #getConfig(String, String, long)}
*
* <p>
* This method are mostly used to get a compound config file, such as a complete dubbo.properties file.
*/
String getConfigs(String key, String group, long timeout) throws IllegalStateException;

/**
* Publish Config mapped to the given key and the given group.
*
* @param key the key to represent a configuration
* @param group the group where the key belongs to
* @param content the content of configuration
* @return <code>true</code> if success, or <code>false</code>
* @throws UnsupportedOperationException If the under layer does not support
* @since 2.7.2
*/
default boolean publishConfig(String key, String group, String content) throws UnsupportedOperationException {
throw new UnsupportedOperationException("No support");
}

/**
* Get the config keys by the specified group
*
* @param group the specified group
* @return the read-only non-null sorted {@link Set set} of config keys
* @throws UnsupportedOperationException If the under layer does not support
* @since 2.7.2
*/
default SortedSet<String> getConfigKeys(String group) throws UnsupportedOperationException {
throw new UnsupportedOperationException("No support");
}

/**
* Get the {@link SortedMap} with with config keys and contents value by the specified group
*
* @param group the specified group
* @return the read-only non-null sorted {@link SortedMap map}
* @throws UnsupportedOperationException If the under layer does not support
* @since 2.7.2
*/
default SortedMap<String, String> getConfigs(String group) throws UnsupportedOperationException {
return getConfigs(group, -1);
}

/**
* Get the {@link SortedMap} with with config keys and content value by the specified group
*
* @param group the specified group
* @param timeout the millisecond for timeout
* @return the read-only non-null sorted {@link SortedMap map}
* @throws UnsupportedOperationException If the under layer does not support
* @throws IllegalStateException If timeout exceeds
* @since 2.7.2
*/
default SortedMap<String, String> getConfigs(String group, long timeout) throws UnsupportedOperationException,
IllegalStateException {
SortedMap<String, String> configs = new TreeMap<>();
SortedSet<String> configKeys = getConfigKeys(group);
configKeys.forEach(key -> configs.put(key, getConfig(key, group, timeout)));
return Collections.unmodifiableSortedMap(configs);
}

/**
* Find DynamicConfiguration instance
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import org.apache.dubbo.configcenter.ConfigurationListener;
import org.apache.dubbo.configcenter.DynamicConfiguration;

import java.util.SortedSet;

import static java.util.Collections.emptySortedSet;

/**
* The default extension of {@link DynamicConfiguration}. If user does not specify a config centre, or specifies one
* that is not a valid extension, it will default to this one.
Expand All @@ -30,7 +34,6 @@ public NopDynamicConfiguration(URL url) {
// no-op
}


@Override
public Object getInternalProperty(String key) {
return null;
Expand All @@ -55,4 +58,20 @@ public String getConfig(String key, String group, long timeout) throws IllegalSt
public String getConfigs(String key, String group, long timeout) throws IllegalStateException {
return null;
}

/**
* @since 2.7.2
*/
@Override
public boolean publishConfig(String key, String group, String content) {
return true;
}

/**
* @since 2.7.2
*/
@Override
public SortedSet<String> getConfigKeys(String group) {
return emptySortedSet();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.dubbo.configcenter.support.nop;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* {@link NopDynamicConfiguration} Test
*
* @since 2.7.2
*/
public class NopDynamicConfigurationTest {

private NopDynamicConfiguration configuration = new NopDynamicConfiguration(null);

@Test
public void testGetInternalProperty() {
assertNull(configuration.getInternalProperty(null));
}

@Test
public void testGetConfig() {
assertNull(configuration.getConfig(null, null, -1));
}


@Test
public void testGetConfigs() {
assertNull(configuration.getConfigs(null, null, -1));
}

@Test
public void testAddListener() {
configuration.addListener(null, null, null);
}

@Test
public void testRemoveListener() {
configuration.removeListener(null, null, null);
}

@Test
public void testPublishConfig() {
assertTrue(configuration.publishConfig(null, null, null));
}

@Test
public void testGetConfigKeys() {
assertTrue(configuration.getConfigKeys(null).isEmpty());
}


}

0 comments on commit a5da695

Please sign in to comment.