mirror of https://github.com/grpc/grpc-java.git
core,netty,okhttp: use ServiceProviders for ManagedChannelProvider, ServerProvider (#4047)
This commit is contained in:
parent
c735bb3385
commit
f8c7f963ef
|
|
@ -0,0 +1,22 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2018, gRPC Authors All rights reserved.
|
||||||
|
*
|
||||||
|
* 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 io.grpc;
|
||||||
|
|
||||||
|
public final class InternalManagedChannelProvider {
|
||||||
|
public static final Iterable<Class<?>> HARDCODED_CLASSES =
|
||||||
|
ManagedChannelProvider.HARDCODED_CLASSES;
|
||||||
|
}
|
||||||
|
|
@ -17,12 +17,10 @@
|
||||||
package io.grpc;
|
package io.grpc;
|
||||||
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
import io.grpc.ServiceProviders.PriorityAccessor;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Iterator;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ServiceConfigurationError;
|
|
||||||
import java.util.ServiceLoader;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provider of managed channels for transport agnostic consumption.
|
* Provider of managed channels for transport agnostic consumption.
|
||||||
|
|
@ -33,86 +31,24 @@ import java.util.ServiceLoader;
|
||||||
*/
|
*/
|
||||||
@Internal
|
@Internal
|
||||||
public abstract class ManagedChannelProvider {
|
public abstract class ManagedChannelProvider {
|
||||||
private static final ManagedChannelProvider provider
|
|
||||||
= load(ManagedChannelProvider.class.getClassLoader());
|
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static ManagedChannelProvider load(ClassLoader classLoader) {
|
static final Iterable<Class<?>> HARDCODED_CLASSES = new HardcodedClasses();
|
||||||
Iterable<ManagedChannelProvider> candidates;
|
|
||||||
if (isAndroid()) {
|
private static final ManagedChannelProvider provider = ServiceProviders.load(
|
||||||
candidates = getCandidatesViaHardCoded();
|
ManagedChannelProvider.class,
|
||||||
} else {
|
HARDCODED_CLASSES,
|
||||||
candidates = getCandidatesViaServiceLoader(classLoader);
|
ManagedChannelProvider.class.getClassLoader(),
|
||||||
}
|
new PriorityAccessor<ManagedChannelProvider>() {
|
||||||
List<ManagedChannelProvider> list = new ArrayList<ManagedChannelProvider>();
|
|
||||||
for (ManagedChannelProvider current : candidates) {
|
|
||||||
if (!current.isAvailable()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
list.add(current);
|
|
||||||
}
|
|
||||||
if (list.isEmpty()) {
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
return Collections.max(list, new Comparator<ManagedChannelProvider>() {
|
|
||||||
@Override
|
@Override
|
||||||
public int compare(ManagedChannelProvider f1, ManagedChannelProvider f2) {
|
public boolean isAvailable(ManagedChannelProvider provider) {
|
||||||
return f1.priority() - f2.priority();
|
return provider.isAvailable();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getPriority(ManagedChannelProvider provider) {
|
||||||
|
return provider.priority();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads service providers for the {@link ManagedChannelProvider} service using
|
|
||||||
* {@link ServiceLoader}.
|
|
||||||
*/
|
|
||||||
@VisibleForTesting
|
|
||||||
public static Iterable<ManagedChannelProvider> getCandidatesViaServiceLoader(
|
|
||||||
ClassLoader classLoader) {
|
|
||||||
Iterable<ManagedChannelProvider> i
|
|
||||||
= ServiceLoader.load(ManagedChannelProvider.class, classLoader);
|
|
||||||
// Attempt to load using the context class loader and ServiceLoader.
|
|
||||||
// This allows frameworks like http://aries.apache.org/modules/spi-fly.html to plug in.
|
|
||||||
if (!i.iterator().hasNext()) {
|
|
||||||
i = ServiceLoader.load(ManagedChannelProvider.class);
|
|
||||||
}
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load providers from a hard-coded list. This avoids using getResource(), which has performance
|
|
||||||
* problems on Android (see https://github.com/grpc/grpc-java/issues/2037). Any provider that may
|
|
||||||
* be used on Android is free to be added here.
|
|
||||||
*/
|
|
||||||
@VisibleForTesting
|
|
||||||
public static Iterable<ManagedChannelProvider> getCandidatesViaHardCoded() {
|
|
||||||
// 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):
|
|
||||||
// https://sourceforge.net/p/proguard/bugs/418/
|
|
||||||
List<ManagedChannelProvider> list = new ArrayList<ManagedChannelProvider>();
|
|
||||||
try {
|
|
||||||
list.add(create(Class.forName("io.grpc.okhttp.OkHttpChannelProvider")));
|
|
||||||
} catch (ClassNotFoundException ex) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
list.add(create(Class.forName("io.grpc.netty.NettyChannelProvider")));
|
|
||||||
} catch (ClassNotFoundException ex) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
static ManagedChannelProvider create(Class<?> rawClass) {
|
|
||||||
try {
|
|
||||||
return rawClass.asSubclass(ManagedChannelProvider.class).getConstructor().newInstance();
|
|
||||||
} catch (Throwable t) {
|
|
||||||
throw new ServiceConfigurationError(
|
|
||||||
"Provider " + rawClass.getName() + " could not be instantiated: " + t, t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the ClassLoader-wide default channel.
|
* Returns the ClassLoader-wide default channel.
|
||||||
|
|
@ -127,21 +63,6 @@ public abstract class ManagedChannelProvider {
|
||||||
return provider;
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns whether current platform is Android.
|
|
||||||
*/
|
|
||||||
protected static boolean isAndroid() {
|
|
||||||
try {
|
|
||||||
// Specify a class loader instead of null because we may be running under Robolectric
|
|
||||||
Class.forName("android.app.Application", /*initialize=*/ false,
|
|
||||||
ManagedChannelProvider.class.getClassLoader());
|
|
||||||
return true;
|
|
||||||
} catch (Exception e) {
|
|
||||||
// If Application isn't loaded, it might as well not be Android.
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
|
|
@ -176,4 +97,22 @@ public abstract class ManagedChannelProvider {
|
||||||
super(msg);
|
super(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final class HardcodedClasses implements Iterable<Class<?>> {
|
||||||
|
@Override
|
||||||
|
public Iterator<Class<?>> iterator() {
|
||||||
|
List<Class<?>> list = new ArrayList<Class<?>>();
|
||||||
|
try {
|
||||||
|
list.add(Class.forName("io.grpc.okhttp.OkHttpChannelProvider"));
|
||||||
|
} catch (ClassNotFoundException ex) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
list.add(Class.forName("io.grpc.netty.NettyChannelProvider"));
|
||||||
|
} catch (ClassNotFoundException ex) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
return list.iterator();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,9 @@
|
||||||
|
|
||||||
package io.grpc;
|
package io.grpc;
|
||||||
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
|
||||||
import io.grpc.ManagedChannelProvider.ProviderNotFoundException;
|
import io.grpc.ManagedChannelProvider.ProviderNotFoundException;
|
||||||
import java.util.ServiceLoader;
|
import io.grpc.ServiceProviders.PriorityAccessor;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provider of servers for transport agnostic consumption.
|
* Provider of servers for transport agnostic consumption.
|
||||||
|
|
@ -29,32 +29,21 @@ import java.util.ServiceLoader;
|
||||||
*/
|
*/
|
||||||
@Internal
|
@Internal
|
||||||
public abstract class ServerProvider {
|
public abstract class ServerProvider {
|
||||||
private static final ServerProvider provider =
|
private static final ServerProvider provider = ServiceProviders.load(
|
||||||
load(ServerProvider.class.getClassLoader());
|
ServerProvider.class,
|
||||||
|
Collections.<Class<?>>emptyList(),
|
||||||
@VisibleForTesting
|
ServerProvider.class.getClassLoader(),
|
||||||
static final ServerProvider load(ClassLoader cl) {
|
new PriorityAccessor<ServerProvider>() {
|
||||||
ServiceLoader<ServerProvider> providers = ServiceLoader.load(ServerProvider.class, cl);
|
@Override
|
||||||
|
public boolean isAvailable(ServerProvider provider) {
|
||||||
// Attempt to load using the context class loader and ServiceLoader.
|
return provider.isAvailable();
|
||||||
// This allows frameworks like http://aries.apache.org/modules/spi-fly.html to plug in.
|
|
||||||
if (!providers.iterator().hasNext()) {
|
|
||||||
providers = ServiceLoader.load(ServerProvider.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ServerProvider best = null;
|
@Override
|
||||||
|
public int getPriority(ServerProvider provider) {
|
||||||
for (ServerProvider current : providers) {
|
return provider.priority();
|
||||||
if (!current.isAvailable()) {
|
|
||||||
continue;
|
|
||||||
} else if (best == null) {
|
|
||||||
best = current;
|
|
||||||
} else if (current.priority() > best.priority()) {
|
|
||||||
best = current;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return best;
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the ClassLoader-wide default server.
|
* Returns the ClassLoader-wide default server.
|
||||||
|
|
|
||||||
|
|
@ -16,16 +16,9 @@
|
||||||
|
|
||||||
package io.grpc;
|
package io.grpc;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import static org.junit.Assert.assertNull;
|
import java.util.Iterator;
|
||||||
import static org.junit.Assert.assertSame;
|
import java.util.concurrent.Callable;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.ServiceConfigurationError;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
|
|
@ -33,151 +26,21 @@ import org.junit.runners.JUnit4;
|
||||||
/** Unit tests for {@link ManagedChannelProvider}. */
|
/** Unit tests for {@link ManagedChannelProvider}. */
|
||||||
@RunWith(JUnit4.class)
|
@RunWith(JUnit4.class)
|
||||||
public class ManagedChannelProviderTest {
|
public class ManagedChannelProviderTest {
|
||||||
private final String serviceFile = "META-INF/services/io.grpc.ManagedChannelProvider";
|
|
||||||
|
|
||||||
@Test(expected = ManagedChannelProvider.ProviderNotFoundException.class)
|
|
||||||
public void noProvider() {
|
|
||||||
ManagedChannelProvider.provider();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void multipleProvider() {
|
|
||||||
ClassLoader cl = new ReplacingClassLoader(getClass().getClassLoader(), serviceFile,
|
|
||||||
"io/grpc/ManagedChannelProviderTest-multipleProvider.txt");
|
|
||||||
assertSame(Available7Provider.class, ManagedChannelProvider.load(cl).getClass());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void unavailableProvider() {
|
|
||||||
ClassLoader cl = new ReplacingClassLoader(getClass().getClassLoader(), serviceFile,
|
|
||||||
"io/grpc/ManagedChannelProviderTest-unavailableProvider.txt");
|
|
||||||
assertNull(ManagedChannelProvider.load(cl));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getCandidatesViaHardCoded_triesToLoadClasses() throws Exception {
|
public void getCandidatesViaHardCoded_triesToLoadClasses() throws Exception {
|
||||||
ClassLoader cl = getClass().getClassLoader();
|
ServiceProvidersTestUtil.testHardcodedClasses(
|
||||||
final RuntimeException toThrow = new RuntimeException();
|
HardcodedClassesCallable.class.getName(),
|
||||||
cl = new ClassLoader(cl) {
|
getClass().getClassLoader(),
|
||||||
|
ImmutableSet.of(
|
||||||
|
"io.grpc.okhttp.OkHttpChannelProvider",
|
||||||
|
"io.grpc.netty.NettyChannelProvider"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class HardcodedClassesCallable implements Callable<Iterator<Class<?>>> {
|
||||||
@Override
|
@Override
|
||||||
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
|
public Iterator<Class<?>> call() {
|
||||||
if (name.startsWith("io.grpc.netty.") || name.startsWith("io.grpc.okhttp.")) {
|
return ManagedChannelProvider.HARDCODED_CLASSES.iterator();
|
||||||
throw toThrow;
|
|
||||||
} else {
|
|
||||||
return super.loadClass(name, resolve);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
cl = new StaticTestingClassLoader(cl, Pattern.compile("io\\.grpc\\.[^.]*"));
|
|
||||||
try {
|
|
||||||
invokeGetCandidatesViaHardCoded(cl);
|
|
||||||
fail("Expected exception");
|
|
||||||
} catch (RuntimeException ex) {
|
|
||||||
assertSame(toThrow, ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void getCandidatesViaHardCoded_ignoresMissingClasses() throws Exception {
|
|
||||||
ClassLoader cl = getClass().getClassLoader();
|
|
||||||
cl = new ClassLoader(cl) {
|
|
||||||
@Override
|
|
||||||
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
|
|
||||||
if (name.startsWith("io.grpc.netty.") || name.startsWith("io.grpc.okhttp.")) {
|
|
||||||
throw new ClassNotFoundException();
|
|
||||||
} else {
|
|
||||||
return super.loadClass(name, resolve);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
cl = new StaticTestingClassLoader(cl, Pattern.compile("io\\.grpc\\.[^.]*"));
|
|
||||||
Iterable<?> i = invokeGetCandidatesViaHardCoded(cl);
|
|
||||||
assertFalse("Iterator should be empty", i.iterator().hasNext());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void create_throwsErrorOnMisconfiguration() throws Exception {
|
|
||||||
class PrivateClass {}
|
|
||||||
|
|
||||||
try {
|
|
||||||
ManagedChannelProvider.create(PrivateClass.class);
|
|
||||||
fail("Expected exception");
|
|
||||||
} catch (ServiceConfigurationError e) {
|
|
||||||
assertTrue("Expected ClassCastException cause: " + e.getCause(),
|
|
||||||
e.getCause() instanceof ClassCastException);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Iterable<?> invokeGetCandidatesViaHardCoded(ClassLoader cl) throws Exception {
|
|
||||||
// An error before the invoke likely means there is a bug in the test
|
|
||||||
Class<?> klass = Class.forName(ManagedChannelProvider.class.getName(), true, cl);
|
|
||||||
Method getCandidatesViaHardCoded = klass.getMethod("getCandidatesViaHardCoded");
|
|
||||||
try {
|
|
||||||
return (Iterable<?>) getCandidatesViaHardCoded.invoke(null);
|
|
||||||
} catch (InvocationTargetException ex) {
|
|
||||||
if (ex.getCause() instanceof Exception) {
|
|
||||||
throw (Exception) ex.getCause();
|
|
||||||
}
|
|
||||||
throw ex;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class BaseProvider extends ManagedChannelProvider {
|
|
||||||
private final boolean isAvailable;
|
|
||||||
private final int priority;
|
|
||||||
|
|
||||||
public BaseProvider(boolean isAvailable, int priority) {
|
|
||||||
this.isAvailable = isAvailable;
|
|
||||||
this.priority = priority;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean isAvailable() {
|
|
||||||
return isAvailable;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected int priority() {
|
|
||||||
return priority;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ManagedChannelBuilder<?> builderForAddress(String host, int port) {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ManagedChannelBuilder<?> builderForTarget(String target) {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Available0Provider extends BaseProvider {
|
|
||||||
public Available0Provider() {
|
|
||||||
super(true, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Available5Provider extends BaseProvider {
|
|
||||||
public Available5Provider() {
|
|
||||||
super(true, 5);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Available7Provider extends BaseProvider {
|
|
||||||
public Available7Provider() {
|
|
||||||
super(true, 7);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class UnavailableProvider extends BaseProvider {
|
|
||||||
public UnavailableProvider() {
|
|
||||||
super(false, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected int priority() {
|
|
||||||
throw new RuntimeException("purposefully broken");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,124 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2015, gRPC Authors All rights reserved.
|
|
||||||
*
|
|
||||||
* 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 io.grpc;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.Assert.assertSame;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.junit.runners.JUnit4;
|
|
||||||
|
|
||||||
/** Unit tests for {@link ServerProvider}. */
|
|
||||||
@RunWith(JUnit4.class)
|
|
||||||
public class ServerProviderTest {
|
|
||||||
private final String serviceFile = "META-INF/services/io.grpc.ServerProvider";
|
|
||||||
|
|
||||||
@Test(expected = ManagedChannelProvider.ProviderNotFoundException.class)
|
|
||||||
public void noProvider() {
|
|
||||||
ServerProvider.provider();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void multipleProvider() {
|
|
||||||
ClassLoader cl = new ReplacingClassLoader(
|
|
||||||
getClass().getClassLoader(), serviceFile,
|
|
||||||
"io/grpc/ServerProviderTest-multipleProvider.txt");
|
|
||||||
assertSame(Available7Provider.class, ServerProvider.load(cl).getClass());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void unavailableProvider() {
|
|
||||||
ClassLoader cl = new ReplacingClassLoader(
|
|
||||||
getClass().getClassLoader(), serviceFile,
|
|
||||||
"io/grpc/ServerProviderTest-unavailableProvider.txt");
|
|
||||||
assertNull(ServerProvider.load(cl));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void contextClassLoaderProvider() {
|
|
||||||
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
|
|
||||||
try {
|
|
||||||
ClassLoader cl = new ReplacingClassLoader(
|
|
||||||
getClass().getClassLoader(), serviceFile,
|
|
||||||
"io/grpc/ServerProviderTest-empty.txt");
|
|
||||||
|
|
||||||
// test that the context classloader is used as fallback
|
|
||||||
ClassLoader rcll = new ReplacingClassLoader(
|
|
||||||
getClass().getClassLoader(), serviceFile,
|
|
||||||
"io/grpc/ServerProviderTest-multipleProvider.txt");
|
|
||||||
Thread.currentThread().setContextClassLoader(rcll);
|
|
||||||
assertSame(Available7Provider.class, ServerProvider.load(cl).getClass());
|
|
||||||
} finally {
|
|
||||||
Thread.currentThread().setContextClassLoader(ccl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class BaseProvider extends ServerProvider {
|
|
||||||
private final boolean isAvailable;
|
|
||||||
private final int priority;
|
|
||||||
|
|
||||||
public BaseProvider(boolean isAvailable, int priority) {
|
|
||||||
this.isAvailable = isAvailable;
|
|
||||||
this.priority = priority;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean isAvailable() {
|
|
||||||
return isAvailable;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected int priority() {
|
|
||||||
return priority;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ServerBuilder<?> builderForPort(int port) {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Available0Provider extends BaseProvider {
|
|
||||||
public Available0Provider() {
|
|
||||||
super(true, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Available5Provider extends BaseProvider {
|
|
||||||
public Available5Provider() {
|
|
||||||
super(true, 5);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Available7Provider extends BaseProvider {
|
|
||||||
public Available7Provider() {
|
|
||||||
super(true, 7);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class UnavailableProvider extends BaseProvider {
|
|
||||||
public UnavailableProvider() {
|
|
||||||
super(false, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected int priority() {
|
|
||||||
throw new RuntimeException("purposefully broken");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
io.grpc.ManagedChannelProviderTest$Available5Provider
|
|
||||||
io.grpc.ManagedChannelProviderTest$Available7Provider
|
|
||||||
io.grpc.ManagedChannelProviderTest$Available0Provider
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
io.grpc.ManagedChannelProviderTest$UnavailableProvider
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
io.grpc.ServerProviderTest$Available5Provider
|
|
||||||
io.grpc.ServerProviderTest$Available7Provider
|
|
||||||
io.grpc.ServerProviderTest$Available0Provider
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
io.grpc.ServerProviderTest$UnavailableProvider
|
|
||||||
|
|
@ -21,6 +21,8 @@ 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 io.grpc.InternalManagedChannelProvider;
|
||||||
|
import io.grpc.InternalServiceProviders;
|
||||||
import io.grpc.ManagedChannelProvider;
|
import io.grpc.ManagedChannelProvider;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
@ -34,7 +36,8 @@ public class NettyChannelProviderTest {
|
||||||
@Test
|
@Test
|
||||||
public void provided() {
|
public void provided() {
|
||||||
for (ManagedChannelProvider current
|
for (ManagedChannelProvider current
|
||||||
: ManagedChannelProvider.getCandidatesViaServiceLoader(getClass().getClassLoader())) {
|
: InternalServiceProviders.getCandidatesViaServiceLoader(
|
||||||
|
ManagedChannelProvider.class, getClass().getClassLoader())) {
|
||||||
if (current instanceof NettyChannelProvider) {
|
if (current instanceof NettyChannelProvider) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -44,7 +47,8 @@ public class NettyChannelProviderTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void providedHardCoded() {
|
public void providedHardCoded() {
|
||||||
for (ManagedChannelProvider current : ManagedChannelProvider.getCandidatesViaHardCoded()) {
|
for (ManagedChannelProvider current : InternalServiceProviders.getCandidatesViaHardCoded(
|
||||||
|
ManagedChannelProvider.class, InternalManagedChannelProvider.HARDCODED_CLASSES)) {
|
||||||
if (current instanceof NettyChannelProvider) {
|
if (current instanceof NettyChannelProvider) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
package io.grpc.okhttp;
|
package io.grpc.okhttp;
|
||||||
|
|
||||||
import io.grpc.Internal;
|
import io.grpc.Internal;
|
||||||
|
import io.grpc.InternalServiceProviders;
|
||||||
import io.grpc.ManagedChannelProvider;
|
import io.grpc.ManagedChannelProvider;
|
||||||
import io.grpc.internal.GrpcUtil;
|
import io.grpc.internal.GrpcUtil;
|
||||||
|
|
||||||
|
|
@ -33,7 +34,8 @@ public final class OkHttpChannelProvider extends ManagedChannelProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int priority() {
|
public int priority() {
|
||||||
return (GrpcUtil.IS_RESTRICTED_APPENGINE || isAndroid()) ? 8 : 3;
|
return (GrpcUtil.IS_RESTRICTED_APPENGINE
|
||||||
|
|| InternalServiceProviders.isAndroid(getClass().getClassLoader())) ? 8 : 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@ 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 io.grpc.InternalManagedChannelProvider;
|
||||||
|
import io.grpc.InternalServiceProviders;
|
||||||
import io.grpc.ManagedChannelProvider;
|
import io.grpc.ManagedChannelProvider;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
@ -33,7 +35,8 @@ public class OkHttpChannelProviderTest {
|
||||||
@Test
|
@Test
|
||||||
public void provided() {
|
public void provided() {
|
||||||
for (ManagedChannelProvider current
|
for (ManagedChannelProvider current
|
||||||
: ManagedChannelProvider.getCandidatesViaServiceLoader(getClass().getClassLoader())) {
|
: InternalServiceProviders.getCandidatesViaServiceLoader(
|
||||||
|
ManagedChannelProvider.class, getClass().getClassLoader())) {
|
||||||
if (current instanceof OkHttpChannelProvider) {
|
if (current instanceof OkHttpChannelProvider) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -43,7 +46,8 @@ public class OkHttpChannelProviderTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void providedHardCoded() {
|
public void providedHardCoded() {
|
||||||
for (ManagedChannelProvider current : ManagedChannelProvider.getCandidatesViaHardCoded()) {
|
for (ManagedChannelProvider current : InternalServiceProviders.getCandidatesViaHardCoded(
|
||||||
|
ManagedChannelProvider.class, InternalManagedChannelProvider.HARDCODED_CLASSES)) {
|
||||||
if (current instanceof OkHttpChannelProvider) {
|
if (current instanceof OkHttpChannelProvider) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue