mirror of https://github.com/grpc/grpc-java.git
android: add AndroidChannelBuilder#fromBuilder (#4723)
This commit is contained in:
parent
6fa2822923
commit
f329d120d2
|
|
@ -27,6 +27,7 @@ import android.net.NetworkInfo;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
import io.grpc.CallOptions;
|
import io.grpc.CallOptions;
|
||||||
import io.grpc.ClientCall;
|
import io.grpc.ClientCall;
|
||||||
import io.grpc.ConnectivityState;
|
import io.grpc.ConnectivityState;
|
||||||
|
|
@ -41,7 +42,6 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import javax.annotation.concurrent.GuardedBy;
|
import javax.annotation.concurrent.GuardedBy;
|
||||||
import javax.net.ssl.HostnameVerifier;
|
|
||||||
import javax.net.ssl.SSLSocketFactory;
|
import javax.net.ssl.SSLSocketFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -81,6 +81,10 @@ public final class AndroidChannelBuilder extends ForwardingChannelBuilder<Androi
|
||||||
return forTarget(GrpcUtil.authorityFromHostAndPort(name, port));
|
return forTarget(GrpcUtil.authorityFromHostAndPort(name, port));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static AndroidChannelBuilder fromBuilder(ManagedChannelBuilder builder) {
|
||||||
|
return new AndroidChannelBuilder(builder);
|
||||||
|
}
|
||||||
|
|
||||||
private AndroidChannelBuilder(String target) {
|
private AndroidChannelBuilder(String target) {
|
||||||
if (OKHTTP_CHANNEL_BUILDER_CLASS == null) {
|
if (OKHTTP_CHANNEL_BUILDER_CLASS == null) {
|
||||||
throw new UnsupportedOperationException("No ManagedChannelBuilder found on the classpath");
|
throw new UnsupportedOperationException("No ManagedChannelBuilder found on the classpath");
|
||||||
|
|
@ -96,13 +100,23 @@ public final class AndroidChannelBuilder extends ForwardingChannelBuilder<Androi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private AndroidChannelBuilder(ManagedChannelBuilder delegateBuilder) {
|
||||||
|
this.delegateBuilder = Preconditions.checkNotNull(delegateBuilder, "delegateBuilder");
|
||||||
|
}
|
||||||
|
|
||||||
/** Enables automatic monitoring of the device's network state. */
|
/** Enables automatic monitoring of the device's network state. */
|
||||||
public AndroidChannelBuilder context(Context context) {
|
public AndroidChannelBuilder context(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Set the delegate channel builder's transportExecutor. */
|
/**
|
||||||
|
* Set the delegate channel builder's transportExecutor.
|
||||||
|
*
|
||||||
|
* @deprecated Use {@link #fromBuilder(ManagedChannelBuilder)} with a pre-configured
|
||||||
|
* ManagedChannelBuilder instead.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public AndroidChannelBuilder transportExecutor(@Nullable Executor transportExecutor) {
|
public AndroidChannelBuilder transportExecutor(@Nullable Executor transportExecutor) {
|
||||||
try {
|
try {
|
||||||
OKHTTP_CHANNEL_BUILDER_CLASS
|
OKHTTP_CHANNEL_BUILDER_CLASS
|
||||||
|
|
@ -114,7 +128,13 @@ public final class AndroidChannelBuilder extends ForwardingChannelBuilder<Androi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Set the delegate channel builder's sslSocketFactory. */
|
/**
|
||||||
|
* Set the delegate channel builder's sslSocketFactory.
|
||||||
|
*
|
||||||
|
* @deprecated Use {@link #fromBuilder(ManagedChannelBuilder)} with a pre-configured
|
||||||
|
* ManagedChannelBuilder instead.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public AndroidChannelBuilder sslSocketFactory(SSLSocketFactory factory) {
|
public AndroidChannelBuilder sslSocketFactory(SSLSocketFactory factory) {
|
||||||
try {
|
try {
|
||||||
OKHTTP_CHANNEL_BUILDER_CLASS
|
OKHTTP_CHANNEL_BUILDER_CLASS
|
||||||
|
|
@ -126,7 +146,13 @@ public final class AndroidChannelBuilder extends ForwardingChannelBuilder<Androi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Set the delegate channel builder's scheduledExecutorService. */
|
/**
|
||||||
|
* Set the delegate channel builder's scheduledExecutorService.
|
||||||
|
*
|
||||||
|
* @deprecated Use {@link #fromBuilder(ManagedChannelBuilder)} with a pre-configured
|
||||||
|
* ManagedChannelBuilder instead.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public AndroidChannelBuilder scheduledExecutorService(
|
public AndroidChannelBuilder scheduledExecutorService(
|
||||||
ScheduledExecutorService scheduledExecutorService) {
|
ScheduledExecutorService scheduledExecutorService) {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ import io.grpc.CallOptions;
|
||||||
import io.grpc.ClientCall;
|
import io.grpc.ClientCall;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.MethodDescriptor;
|
import io.grpc.MethodDescriptor;
|
||||||
|
import io.grpc.okhttp.OkHttpChannelBuilder;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -40,8 +41,6 @@ import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import javax.net.ssl.HostnameVerifier;
|
|
||||||
import javax.net.ssl.SSLSession;
|
|
||||||
import javax.net.ssl.SSLSocketFactory;
|
import javax.net.ssl.SSLSocketFactory;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
@ -96,6 +95,13 @@ public final class AndroidChannelBuilderTest {
|
||||||
AndroidChannelBuilder.forTarget("target");
|
AndroidChannelBuilder.forTarget("target");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fromBuilderConstructor() {
|
||||||
|
OkHttpChannelBuilder wrappedBuilder = OkHttpChannelBuilder.forTarget("target");
|
||||||
|
AndroidChannelBuilder androidBuilder = AndroidChannelBuilder.fromBuilder(wrappedBuilder);
|
||||||
|
assertThat(androidBuilder.delegate()).isSameAs(wrappedBuilder);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void transportExecutor() {
|
public void transportExecutor() {
|
||||||
AndroidChannelBuilder.forTarget("target")
|
AndroidChannelBuilder.forTarget("target")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue