mirror of https://github.com/grpc/grpc-java.git
core: minor cleanup of NameResolverProvider
* Make the list of providers an immutable List * Make obvious that the list is statically initialized * Add documentation for when methods were added. * Use RuntimeException, rather than IllegalStateException.
This commit is contained in:
parent
c318b4e9d6
commit
3f05a6e331
|
|
@ -17,11 +17,13 @@
|
||||||
package io.grpc;
|
package io.grpc;
|
||||||
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provider of name resolvers for name agnostic consumption.
|
* Provider of name resolvers for name agnostic consumption.
|
||||||
|
|
@ -32,40 +34,42 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4159")
|
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4159")
|
||||||
public abstract class NameResolverProvider extends NameResolver.Factory {
|
public abstract class NameResolverProvider extends NameResolver.Factory {
|
||||||
|
|
||||||
|
private static final Logger logger = Logger.getLogger(NameResolverProvider.class.getName());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The port number used in case the target or the underlying naming system doesn't provide a
|
* The port number used in case the target or the underlying naming system doesn't provide a
|
||||||
* port number.
|
* port number.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unused") // Avoids outside callers accidentally depending on the super class.
|
||||||
public static final Attributes.Key<Integer> PARAMS_DEFAULT_PORT =
|
public static final Attributes.Key<Integer> PARAMS_DEFAULT_PORT =
|
||||||
NameResolver.Factory.PARAMS_DEFAULT_PORT;
|
NameResolver.Factory.PARAMS_DEFAULT_PORT;
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final Iterable<Class<?>> HARDCODED_CLASSES = new HardcodedClasses();
|
static final Iterable<Class<?>> HARDCODED_CLASSES = getHardCodedClasses();
|
||||||
|
|
||||||
private static final List<NameResolverProvider> providers = ServiceProviders.loadAll(
|
private static final List<NameResolverProvider> providers = ServiceProviders.loadAll(
|
||||||
NameResolverProvider.class,
|
NameResolverProvider.class,
|
||||||
HARDCODED_CLASSES,
|
HARDCODED_CLASSES,
|
||||||
NameResolverProvider.class.getClassLoader(),
|
NameResolverProvider.class.getClassLoader(),
|
||||||
new ServiceProviders.PriorityAccessor<NameResolverProvider>() {
|
new NameResolverPriorityAccessor());
|
||||||
@Override
|
|
||||||
public boolean isAvailable(NameResolverProvider provider) {
|
|
||||||
return provider.isAvailable();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getPriority(NameResolverProvider provider) {
|
|
||||||
return provider.priority();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
private static final NameResolver.Factory factory = new NameResolverFactory(providers);
|
private static final NameResolver.Factory factory = new NameResolverFactory(providers);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns non-{@code null} ClassLoader-wide providers, in preference order.
|
* Returns non-{@code null} ClassLoader-wide providers, in preference order.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
public static List<NameResolverProvider> providers() {
|
public static List<NameResolverProvider> providers() {
|
||||||
return providers;
|
return providers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
public static NameResolver.Factory asFactory() {
|
public static NameResolver.Factory asFactory() {
|
||||||
return factory;
|
return factory;
|
||||||
}
|
}
|
||||||
|
|
@ -78,6 +82,8 @@ public abstract class NameResolverProvider extends NameResolver.Factory {
|
||||||
/**
|
/**
|
||||||
* Whether this provider is available for use, taking the current environment into consideration.
|
* Whether this provider is available for use, taking the current environment into consideration.
|
||||||
* If {@code false}, no other methods are safe to be called.
|
* If {@code false}, no other methods are safe to be called.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
protected abstract boolean isAvailable();
|
protected abstract boolean isAvailable();
|
||||||
|
|
||||||
|
|
@ -86,17 +92,20 @@ public abstract class NameResolverProvider extends NameResolver.Factory {
|
||||||
* consideration. 5 should be considered the default, and then tweaked based on environment
|
* consideration. 5 should be considered the default, and then tweaked based on environment
|
||||||
* detection. A priority of 0 does not imply that the provider wouldn't work; just that it should
|
* detection. A priority of 0 does not imply that the provider wouldn't work; just that it should
|
||||||
* be last in line.
|
* be last in line.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
protected abstract int priority();
|
protected abstract int priority();
|
||||||
|
|
||||||
private static class NameResolverFactory extends NameResolver.Factory {
|
private static final class NameResolverFactory extends NameResolver.Factory {
|
||||||
private final List<NameResolverProvider> providers;
|
private final List<NameResolverProvider> providers;
|
||||||
|
|
||||||
public NameResolverFactory(List<NameResolverProvider> providers) {
|
NameResolverFactory(List<NameResolverProvider> providers) {
|
||||||
this.providers = providers;
|
this.providers = Collections.unmodifiableList(new ArrayList<NameResolverProvider>(providers));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Nullable
|
||||||
public NameResolver newNameResolver(URI targetUri, Attributes params) {
|
public NameResolver newNameResolver(URI targetUri, Attributes params) {
|
||||||
checkForProviders();
|
checkForProviders();
|
||||||
for (NameResolverProvider provider : providers) {
|
for (NameResolverProvider provider : providers) {
|
||||||
|
|
@ -115,26 +124,41 @@ public abstract class NameResolverProvider extends NameResolver.Factory {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkForProviders() {
|
private void checkForProviders() {
|
||||||
Preconditions.checkState(!providers.isEmpty(),
|
if (providers.isEmpty()) {
|
||||||
"No NameResolverProviders found via ServiceLoader, including for DNS. "
|
String msg = "No NameResolverProviders found via ServiceLoader, including for DNS. "
|
||||||
+ "This is probably due to a broken build. If using ProGuard, check your configuration");
|
+ "This is probably due to a broken build. If using ProGuard, check your configuration";
|
||||||
|
throw new RuntimeException(msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final class HardcodedClasses implements Iterable<Class<?>> {
|
static final List<Class<?>> getHardCodedClasses() {
|
||||||
@Override
|
|
||||||
public Iterator<Class<?>> iterator() {
|
|
||||||
List<Class<?>> list = new ArrayList<Class<?>>();
|
|
||||||
// Class.forName(String) is used to remove the need for ProGuard configuration. Note that
|
// Class.forName(String) is used to remove the need for ProGuard configuration. Note that
|
||||||
// ProGuard does not detect usages of Class.forName(String, boolean, ClassLoader):
|
// ProGuard does not detect usages of Class.forName(String, boolean, ClassLoader):
|
||||||
// https://sourceforge.net/p/proguard/bugs/418/
|
// https://sourceforge.net/p/proguard/bugs/418/
|
||||||
try {
|
try {
|
||||||
list.add(Class.forName("io.grpc.internal.DnsNameResolverProvider"));
|
return Collections.<Class<?>>singletonList(
|
||||||
|
Class.forName("io.grpc.internal.DnsNameResolverProvider"));
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
// ignore
|
logger.log(Level.FINE, "Unable to find DNS NameResolver", e);
|
||||||
}
|
}
|
||||||
return list.iterator();
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class NameResolverPriorityAccessor
|
||||||
|
implements ServiceProviders.PriorityAccessor<NameResolverProvider> {
|
||||||
|
|
||||||
|
NameResolverPriorityAccessor() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAvailable(NameResolverProvider provider) {
|
||||||
|
return provider.isAvailable();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getPriority(NameResolverProvider provider) {
|
||||||
|
return provider.priority();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,14 +16,13 @@
|
||||||
|
|
||||||
package io.grpc;
|
package io.grpc;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertSame;
|
import static org.junit.Assert.assertSame;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
|
||||||
import io.grpc.NameResolverProvider.HardcodedClasses;
|
|
||||||
import io.grpc.internal.DnsNameResolverProvider;
|
import io.grpc.internal.DnsNameResolverProvider;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
@ -47,7 +46,7 @@ public class NameResolverProviderTest {
|
||||||
try {
|
try {
|
||||||
factory.getDefaultScheme();
|
factory.getDefaultScheme();
|
||||||
fail("Expected exception");
|
fail("Expected exception");
|
||||||
} catch (IllegalStateException ex) {
|
} catch (RuntimeException ex) {
|
||||||
assertTrue(ex.toString(), ex.getMessage().contains("No NameResolverProviders found"));
|
assertTrue(ex.toString(), ex.getMessage().contains("No NameResolverProviders found"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -73,7 +72,7 @@ public class NameResolverProviderTest {
|
||||||
try {
|
try {
|
||||||
factory.newNameResolver(uri, attributes);
|
factory.newNameResolver(uri, attributes);
|
||||||
fail("Expected exception");
|
fail("Expected exception");
|
||||||
} catch (IllegalStateException ex) {
|
} catch (RuntimeException ex) {
|
||||||
assertTrue(ex.toString(), ex.getMessage().contains("No NameResolverProviders found"));
|
assertTrue(ex.toString(), ex.getMessage().contains("No NameResolverProviders found"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -87,11 +86,10 @@ public class NameResolverProviderTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getClassesViaHardcoded_triesToLoadClasses() throws Exception {
|
public void getClassesViaHardcoded_classesPresent() throws Exception {
|
||||||
ServiceProvidersTestUtil.testHardcodedClasses(
|
List<Class<?>> classes = NameResolverProvider.getHardCodedClasses();
|
||||||
HardcodedClassesCallable.class.getName(),
|
assertThat(classes).hasSize(1);
|
||||||
getClass().getClassLoader(),
|
assertThat(classes.get(0).getName()).isEqualTo("io.grpc.internal.DnsNameResolverProvider");
|
||||||
ImmutableSet.of("io.grpc.internal.DnsNameResolverProvider"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -119,8 +117,8 @@ public class NameResolverProviderTest {
|
||||||
|
|
||||||
public static final class HardcodedClassesCallable implements Callable<Iterator<Class<?>>> {
|
public static final class HardcodedClassesCallable implements Callable<Iterator<Class<?>>> {
|
||||||
@Override
|
@Override
|
||||||
public Iterator<Class<?>> call() throws Exception {
|
public Iterator<Class<?>> call() {
|
||||||
return new HardcodedClasses().iterator();
|
return NameResolverProvider.getHardCodedClasses().iterator();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue