mirror of https://github.com/grpc/grpc-java.git
okhttp: Add OkHttpServerProvider
This allows okhttp to service the Grpc.newServerBuilderForPort() API.
Note that, unlike Netty, it will throw if you try to use
ServerBuilder.forPort().
This fixes android-interop-testing which was broken by c9864a119.
This commit is contained in:
parent
a0d8f2eb31
commit
3b61799f73
|
|
@ -23,6 +23,7 @@ import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import javax.annotation.concurrent.GuardedBy;
|
import javax.annotation.concurrent.GuardedBy;
|
||||||
import javax.annotation.concurrent.ThreadSafe;
|
import javax.annotation.concurrent.ThreadSafe;
|
||||||
|
|
@ -92,7 +93,7 @@ public final class ServerRegistry {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
List<ServerProvider> providerList = ServiceProviders.loadAll(
|
List<ServerProvider> providerList = ServiceProviders.loadAll(
|
||||||
ServerProvider.class,
|
ServerProvider.class,
|
||||||
Collections.<Class<?>>emptyList(),
|
getHardCodedClasses(),
|
||||||
ServerProvider.class.getClassLoader(),
|
ServerProvider.class.getClassLoader(),
|
||||||
new ServerPriorityAccessor());
|
new ServerPriorityAccessor());
|
||||||
instance = new ServerRegistry();
|
instance = new ServerRegistry();
|
||||||
|
|
@ -119,6 +120,20 @@ public final class ServerRegistry {
|
||||||
return providers.isEmpty() ? null : providers.get(0);
|
return providers.isEmpty() ? null : providers.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
static List<Class<?>> getHardCodedClasses() {
|
||||||
|
// 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<Class<?>> list = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
list.add(Class.forName("io.grpc.okhttp.OkHttpServerProvider"));
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
logger.log(Level.FINE, "Unable to find OkHttpServerProvider", e);
|
||||||
|
}
|
||||||
|
return Collections.unmodifiableList(list);
|
||||||
|
}
|
||||||
|
|
||||||
ServerBuilder<?> newServerBuilderForPort(int port, ServerCredentials creds) {
|
ServerBuilder<?> newServerBuilderForPort(int port, ServerCredentials creds) {
|
||||||
List<ServerProvider> providers = providers();
|
List<ServerProvider> providers = providers();
|
||||||
if (providers.isEmpty()) {
|
if (providers.isEmpty()) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2023 The gRPC Authors
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
/** Accesses test-only methods of {@link ServerRegistry}. */
|
||||||
|
public final class ServerRegistryAccessor {
|
||||||
|
private ServerRegistryAccessor() {}
|
||||||
|
|
||||||
|
public static Iterable<Class<?>> getHardCodedClasses() {
|
||||||
|
return ServerRegistry.getHardCodedClasses();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -50,6 +50,7 @@ tasks.named("checkstyleMain").configure {
|
||||||
tasks.named("javadoc").configure {
|
tasks.named("javadoc").configure {
|
||||||
options.links 'http://square.github.io/okhttp/2.x/okhttp/'
|
options.links 'http://square.github.io/okhttp/2.x/okhttp/'
|
||||||
exclude 'io/grpc/okhttp/Internal*'
|
exclude 'io/grpc/okhttp/Internal*'
|
||||||
|
exclude 'io/grpc/okhttp/*Provider.java'
|
||||||
exclude 'io/grpc/okhttp/internal/**'
|
exclude 'io/grpc/okhttp/internal/**'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ package io.grpc.okhttp;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkArgument;
|
import static com.google.common.base.Preconditions.checkArgument;
|
||||||
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import com.google.errorprone.annotations.CanIgnoreReturnValue;
|
import com.google.errorprone.annotations.CanIgnoreReturnValue;
|
||||||
import com.google.errorprone.annotations.DoNotCall;
|
import com.google.errorprone.annotations.DoNotCall;
|
||||||
|
|
@ -89,7 +88,7 @@ public final class OkHttpServerBuilder extends ForwardingServerBuilder<OkHttpSer
|
||||||
@DoNotCall("Always throws. Use forPort(int, ServerCredentials) instead")
|
@DoNotCall("Always throws. Use forPort(int, ServerCredentials) instead")
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public static OkHttpServerBuilder forPort(int port) {
|
public static OkHttpServerBuilder forPort(int port) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException("Use forPort(int, ServerCredentials) instead");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -131,7 +130,6 @@ public final class OkHttpServerBuilder extends ForwardingServerBuilder<OkHttpSer
|
||||||
long maxConnectionAgeInNanos = MAX_CONNECTION_AGE_NANOS_DISABLED;
|
long maxConnectionAgeInNanos = MAX_CONNECTION_AGE_NANOS_DISABLED;
|
||||||
long maxConnectionAgeGraceInNanos = MAX_CONNECTION_AGE_GRACE_NANOS_INFINITE;
|
long maxConnectionAgeGraceInNanos = MAX_CONNECTION_AGE_GRACE_NANOS_INFINITE;
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
OkHttpServerBuilder(
|
OkHttpServerBuilder(
|
||||||
SocketAddress address, HandshakerSocketFactory handshakerSocketFactory) {
|
SocketAddress address, HandshakerSocketFactory handshakerSocketFactory) {
|
||||||
this.listenAddress = Preconditions.checkNotNull(address, "address");
|
this.listenAddress = Preconditions.checkNotNull(address, "address");
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2023 The gRPC Authors
|
||||||
|
*
|
||||||
|
* 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.okhttp;
|
||||||
|
|
||||||
|
import io.grpc.Internal;
|
||||||
|
import io.grpc.ServerCredentials;
|
||||||
|
import io.grpc.ServerProvider;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
|
/** Provider for {@link OkHttpServerBuilder} instances. */
|
||||||
|
@Internal
|
||||||
|
public final class OkHttpServerProvider extends ServerProvider {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean isAvailable() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int priority() {
|
||||||
|
// Use a priority less than Netty since builderForPort() always throws.
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected OkHttpServerBuilder builderForPort(int port) {
|
||||||
|
throw new UnsupportedOperationException("Use Grpc.newServerBuilderForPort() instead");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NewServerBuilderResult newServerBuilderForPort(int port, ServerCredentials creds) {
|
||||||
|
OkHttpServerBuilder.HandshakerSocketFactoryResult result =
|
||||||
|
OkHttpServerBuilder.handshakerSocketFactoryFrom(creds);
|
||||||
|
if (result.error != null) {
|
||||||
|
return NewServerBuilderResult.error(result.error);
|
||||||
|
}
|
||||||
|
return NewServerBuilderResult.serverBuilder(
|
||||||
|
new OkHttpServerBuilder(new InetSocketAddress(port), result.factory));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
io.grpc.okhttp.OkHttpServerProvider
|
||||||
|
|
@ -0,0 +1,71 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2023 The gRPC Authors
|
||||||
|
*
|
||||||
|
* 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.okhttp;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
import static org.junit.Assert.assertThrows;
|
||||||
|
|
||||||
|
import io.grpc.InsecureServerCredentials;
|
||||||
|
import io.grpc.ServerCredentials;
|
||||||
|
import io.grpc.ServerProvider;
|
||||||
|
import io.grpc.ServerRegistryAccessor;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.JUnit4;
|
||||||
|
|
||||||
|
/** Unit tests for {@link OkHttpServerProvider}. */
|
||||||
|
@RunWith(JUnit4.class)
|
||||||
|
public class OkHttpServerProviderTest {
|
||||||
|
private OkHttpServerProvider provider = new OkHttpServerProvider();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void provided() {
|
||||||
|
assertThat(ServerProvider.provider()).isInstanceOf(OkHttpServerProvider.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void providedHardCoded() {
|
||||||
|
assertThat(ServerRegistryAccessor.getHardCodedClasses()).contains(OkHttpServerProvider.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void basicMethods() {
|
||||||
|
assertThat(provider.isAvailable()).isTrue();
|
||||||
|
assertThat(provider.priority()).isEqualTo(4);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void builderIsAOkHttpBuilder() {
|
||||||
|
assertThrows(UnsupportedOperationException.class, () -> provider.builderForPort(80));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void newServerBuilderForPort_success() {
|
||||||
|
ServerProvider.NewServerBuilderResult result =
|
||||||
|
provider.newServerBuilderForPort(80, InsecureServerCredentials.create());
|
||||||
|
assertThat(result.getServerBuilder()).isInstanceOf(OkHttpServerBuilder.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void newServerBuilderForPort_fail() {
|
||||||
|
ServerProvider.NewServerBuilderResult result = provider.newServerBuilderForPort(
|
||||||
|
80, new FakeServerCredentials());
|
||||||
|
assertThat(result.getError()).contains("FakeServerCredentials");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class FakeServerCredentials extends ServerCredentials {}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue