Skip to content

Commit

Permalink
Polish apache#6389 : [Issue] Resolving the issues with ConsulServiceD…
Browse files Browse the repository at this point in the history
…iscovery
  • Loading branch information
mercyblitz committed Jul 1, 2020
1 parent d6409ac commit 7791051
Show file tree
Hide file tree
Showing 29 changed files with 404 additions and 83 deletions.
39 changes: 38 additions & 1 deletion dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.USERNAME_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.common.convert.Converter.convertIfPossible;
import static org.apache.dubbo.common.utils.StringUtils.isBlank;

/**
* URL - Uniform Resource Locator (Immutable, ThreadSafe)
Expand Down Expand Up @@ -621,6 +623,41 @@ public List<String> getParameter(String key, List<String> defaultValue) {
return Arrays.asList(strArray);
}

/**
* Get parameter
*
* @param key the key of parameter
* @param valueType the type of parameter value
* @param <T> the type of parameter value
* @return get the parameter if present, or <code>null</code>
* @since 2.7.8
*/
public <T> T getParameter(String key, Class<T> valueType) {
return getParameter(key, valueType, null);
}

/**
* Get parameter
*
* @param key the key of parameter
* @param valueType the type of parameter value
* @param defaultValue the default value if parameter is absent
* @param <T> the type of parameter value
* @return get the parameter if present, or <code>defaultValue</code> will be used.
* @since 2.7.8
*/
public <T> T getParameter(String key, Class<T> valueType, T defaultValue) {
String value = getParameter(key);
T result = null;
if (!isBlank(value)) {
result = convertIfPossible(value, valueType);
}
if (result == null) {
result = defaultValue;
}
return result;
}

private Map<String, Number> getNumbers() {
// concurrent initialization is tolerant
if (numbers == null) {
Expand Down Expand Up @@ -1435,7 +1472,7 @@ public String getColonSeparatedKey() {

private void append(StringBuilder target, String parameterName, boolean first) {
String parameterValue = this.getParameter(parameterName);
if (!StringUtils.isBlank(parameterValue)) {
if (!isBlank(parameterValue)) {
if (!first) {
target.append(":");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,21 @@ default Class<T> getTargetType() {
.findFirst()
.orElse(null);
}

/**
* Convert the value of source to target-type value if possible
*
* @param source the value of source
* @param targetType the target type
* @param <T> the target type
* @return <code>null</code> if can't be converted
* @since 2.7.8
*/
static <T> T convertIfPossible(Object source, Class<T> targetType) {
Converter converter = getConverter(source.getClass(), targetType);
if (converter != null) {
return (T) converter.convert(source);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
*/
package org.apache.dubbo.common.convert.multiple;

import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.extension.SPI;
import org.apache.dubbo.common.lang.Prioritized;

import java.util.Collection;

import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;
import static org.apache.dubbo.common.utils.TypeUtils.findActualTypeArgument;

/**
Expand Down Expand Up @@ -61,4 +63,30 @@ default Class<S> getSourceType() {
return findActualTypeArgument(getClass(), MultiValueConverter.class, 0);
}

/**
* Find the {@link MultiValueConverter} instance from {@link ExtensionLoader} with the specified source and target type
*
* @param sourceType the source type
* @param targetType the target type
* @return <code>null</code> if not found
* @see ExtensionLoader#getSupportedExtensionInstances()
* @since 2.7.8
*/
static MultiValueConverter<?> find(Class<?> sourceType, Class<?> targetType) {
return getExtensionLoader(MultiValueConverter.class)
.getSupportedExtensionInstances()
.stream()
.filter(converter -> converter.accept(sourceType, targetType))
.findFirst()
.orElse(null);
}

static <T> T convertIfPossible(Object source, Class<?> multiValueType, Class<?> elementType) {
Class<?> sourceType = source.getClass();
MultiValueConverter converter = find(sourceType, multiValueType);
if (converter != null) {
return (T) converter.convert(source, multiValueType, elementType);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -890,4 +890,11 @@ public void testGetParameters() {
assertEquals(1, parameters.size());
assertEquals("1.0.0", version);
}

@Test
public void testGetParameter() {
URL url = URL.valueOf("http://127.0.0.1:8080/path?i=1&b=false");
assertEquals(Integer.valueOf(1), url.getParameter("i", Integer.class));
assertEquals(Boolean.FALSE, url.getParameter("b", Boolean.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.common.convert;

import org.junit.jupiter.api.Test;

import static org.apache.dubbo.common.convert.Converter.convertIfPossible;
import static org.apache.dubbo.common.convert.Converter.getConverter;
import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;

/**
* {@link Converter} Test-Cases
*
* @since 2.7.8
*/
public class ConverterTest {

@Test
public void testGetConverter() {
getExtensionLoader(Converter.class)
.getSupportedExtensionInstances()
.forEach(converter -> {
assertSame(converter, getConverter(converter.getSourceType(), converter.getTargetType()));
});
}

@Test
public void testConvertIfPossible() {
assertEquals(Integer.valueOf(2), convertIfPossible("2", Integer.class));
assertEquals(Boolean.FALSE, convertIfPossible("false", Boolean.class));
assertEquals(Double.valueOf(1), convertIfPossible("1", Double.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.convert;

import org.apache.dubbo.common.convert.Converter;
import org.apache.dubbo.common.convert.StringToBooleanConverter;
package org.apache.dubbo.common.convert;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.convert;

import org.apache.dubbo.common.convert.Converter;
import org.apache.dubbo.common.convert.StringToCharArrayConverter;
package org.apache.dubbo.common.convert;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.convert;

import org.apache.dubbo.common.convert.Converter;
import org.apache.dubbo.common.convert.StringToCharacterConverter;
package org.apache.dubbo.common.convert;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.convert;

import org.apache.dubbo.common.convert.Converter;
import org.apache.dubbo.common.convert.StringToDoubleConverter;
package org.apache.dubbo.common.convert;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.convert;

import org.apache.dubbo.common.convert.Converter;
import org.apache.dubbo.common.convert.StringToFloatConverter;
package org.apache.dubbo.common.convert;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.convert;

import org.apache.dubbo.common.convert.Converter;
import org.apache.dubbo.common.convert.StringToIntegerConverter;
package org.apache.dubbo.common.convert;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.convert;

import org.apache.dubbo.common.convert.Converter;
import org.apache.dubbo.common.convert.StringToLongConverter;
package org.apache.dubbo.common.convert;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.convert;

import org.apache.dubbo.common.convert.Converter;
import org.apache.dubbo.common.convert.StringToOptionalConverter;
package org.apache.dubbo.common.convert;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.convert;

import org.apache.dubbo.common.convert.Converter;
import org.apache.dubbo.common.convert.StringToShortConverter;
package org.apache.dubbo.common.convert;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.convert;

import org.apache.dubbo.common.convert.Converter;
import org.apache.dubbo.common.convert.StringToStringConverter;
package org.apache.dubbo.common.convert;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.common.convert.multiple;

import org.junit.jupiter.api.Test;

import java.util.Collection;
import java.util.Deque;
import java.util.List;
import java.util.NavigableSet;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TransferQueue;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* {@link MultiValueConverter} Test
*
* @since 2.7.8
*/
public class MultiValueConverterTest {

@Test
public void testFind() {
MultiValueConverter converter = MultiValueConverter.find(String.class, String[].class);
assertEquals(StringToArrayConverter.class, converter.getClass());

converter = MultiValueConverter.find(String.class, BlockingDeque.class);
assertEquals(StringToBlockingDequeConverter.class, converter.getClass());

converter = MultiValueConverter.find(String.class, BlockingQueue.class);
assertEquals(StringToBlockingQueueConverter.class, converter.getClass());

converter = MultiValueConverter.find(String.class, Collection.class);
assertEquals(StringToCollectionConverter.class, converter.getClass());

converter = MultiValueConverter.find(String.class, Deque.class);
assertEquals(StringToDequeConverter.class, converter.getClass());

converter = MultiValueConverter.find(String.class, List.class);
assertEquals(StringToListConverter.class, converter.getClass());

converter = MultiValueConverter.find(String.class, NavigableSet.class);
assertEquals(StringToNavigableSetConverter.class, converter.getClass());

converter = MultiValueConverter.find(String.class, Queue.class);
assertEquals(StringToQueueConverter.class, converter.getClass());

converter = MultiValueConverter.find(String.class, Set.class);
assertEquals(StringToSetConverter.class, converter.getClass());

converter = MultiValueConverter.find(String.class, TransferQueue.class);
assertEquals(StringToTransferQueueConverter.class, converter.getClass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.convert.multiple;

import org.apache.dubbo.common.convert.multiple.StringToArrayConverter;
package org.apache.dubbo.common.convert.multiple;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Loading

0 comments on commit 7791051

Please sign in to comment.