Skip to content
This repository has been archived by the owner on Sep 15, 2021. It is now read-only.

Commit

Permalink
Switch back to factory methods only.
Browse files Browse the repository at this point in the history
Now that we are using ManagedChannel instead of ChannelImpl.
  • Loading branch information
deflaux committed Nov 13, 2015
1 parent cc7ce23 commit 2d7a9b3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.google.cloud.genomics.utils.grpc;

import io.grpc.ManagedChannel;

import java.io.FileNotFoundException;
import java.util.Iterator;

Expand Down Expand Up @@ -34,7 +36,7 @@ public static void main(String[] args) throws Exception {
return;
}

GenomicsChannel channel = GenomicsChannel.fromOfflineAuth(auth);
ManagedChannel channel = GenomicsChannel.fromOfflineAuth(auth);

// Regular RPC example: list all reference set assembly ids.
ReferenceServiceV1BlockingStub refStub =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,9 @@
*/
package com.google.cloud.genomics.utils.grpc;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptors;
import io.grpc.ClientInterceptor;
import io.grpc.ManagedChannel;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.auth.ClientAuthInterceptor;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NegotiationType;
Expand All @@ -30,9 +26,9 @@
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLException;

Expand All @@ -42,19 +38,15 @@
/**
* A convenience class for creating gRPC channels to the Google Genomics API.
*/
public class GenomicsChannel extends ManagedChannel {
public class GenomicsChannel {
private static final String GENOMICS_ENDPOINT = "genomics.googleapis.com";
private static final String GENOMICS_SCOPE = "https://www.googleapis.com/auth/genomics";
private static final String API_KEY_HEADER = "X-Goog-Api-Key";
// TODO https://github.com/googlegenomics/utils-java/issues/48
private static final String PARTIAL_RESPONSE_HEADER = "X-Goog-FieldMask";

// NOTE: Unfortunately we need to keep a handle to both of these since Channel does not expose
// the shutdown method and the ClientInterceptors do not return the ManagedChannel instance.
private final ManagedChannel managedChannel;
private final Channel delegate;

private ManagedChannel getGenomicsManagedChannel() throws SSLException {
private static ManagedChannel getGenomicsManagedChannel(List<ClientInterceptor> interceptors)
throws SSLException {
// Java 8's implementation of GCM ciphers is extremely slow. Therefore we disable
// them here.
List<String> defaultCiphers = GrpcSslContexts.forClient().ciphers(null).build().cipherSuites();
Expand All @@ -67,110 +59,68 @@ private ManagedChannel getGenomicsManagedChannel() throws SSLException {

return NettyChannelBuilder.forAddress(GENOMICS_ENDPOINT, 443)
.negotiationType(NegotiationType.TLS)
.sslContext(GrpcSslContexts.forClient().ciphers(performantCiphers).build()).build();
.sslContext(GrpcSslContexts.forClient().ciphers(performantCiphers).build())
.intercept(interceptors).build();
}

private GenomicsChannel(String apiKey) throws SSLException {
managedChannel = getGenomicsManagedChannel();
/**
* Create a new gRPC channel to the Google Genomics API, using the provided api key for auth.
*
* @param apiKey the api key
* @return the ManagedChannel
* @throws SSLException
*/
public static ManagedChannel fromApiKey(String apiKey) throws SSLException {
Metadata headers = new Metadata();
Metadata.Key<String> apiKeyHeader =
Metadata.Key.of(API_KEY_HEADER, Metadata.ASCII_STRING_MARSHALLER);
headers.put(apiKeyHeader, apiKey);
delegate =
ClientInterceptors.intercept(managedChannel,
MetadataUtils.newAttachHeadersInterceptor(headers));
}

private GenomicsChannel(GoogleCredentials creds) throws SSLException {
managedChannel = getGenomicsManagedChannel();
creds = creds.createScoped(Arrays.asList(GENOMICS_SCOPE));
ClientAuthInterceptor interceptor =
new ClientAuthInterceptor(creds, Executors.newSingleThreadExecutor());
delegate = ClientInterceptors.intercept(managedChannel, interceptor);
}

@Override
public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(
MethodDescriptor<RequestT, ResponseT> arg0, CallOptions arg1) {
return delegate.newCall(arg0, arg1);
// TODO https://github.com/googlegenomics/utils-java/issues/48
return getGenomicsManagedChannel(Collections.singletonList(MetadataUtils
.newAttachHeadersInterceptor(headers)));
}

/**
* @throws InterruptedException
* @see io.grpc.ManagedChannel#shutdown()
* @see io.grpc.ManagedChannel#awaitTermination(long, TimeUnit)
* Create a new gRPC channel to the Google Genomics API, using the provided credentials for auth.
*
* @param creds the credential
* @return the ManagedChannel
* @throws SSLException
*/
public void shutdown(long timeout, TimeUnit unit) throws InterruptedException {
managedChannel.shutdown().awaitTermination(timeout, unit);
}


@Override
public boolean awaitTermination(long time, TimeUnit unit) throws InterruptedException {
return managedChannel.awaitTermination(time, unit);
}

@Override
public boolean isShutdown() {
return managedChannel.isShutdown();
}

@Override
public boolean isTerminated() {
return managedChannel.isTerminated();
}

@Override
public ManagedChannel shutdown() {
return managedChannel.shutdown();
}

@Override
public ManagedChannel shutdownNow() {
return managedChannel.shutdownNow();
}

@Override
public String authority() {
return managedChannel.authority();
public static ManagedChannel fromCreds(GoogleCredentials creds) throws SSLException {
ClientInterceptor interceptor =
new ClientAuthInterceptor(creds.createScoped(Arrays.asList(GENOMICS_SCOPE)),
Executors.newSingleThreadExecutor());
// TODO https://github.com/googlegenomics/utils-java/issues/48
return getGenomicsManagedChannel(Collections.singletonList(interceptor));
}

/**
* Creates a new gRPC channel to the Google Genomics API, using the application default
* credentials for auth.
* Create a new gRPC channel to the Google Genomics API, using the application default credentials
* for auth.
*
* @return the ManagedChannel
* @throws SSLException
* @throws IOException
*/
public static GenomicsChannel fromDefaultCreds() throws IOException {
public static ManagedChannel fromDefaultCreds() throws SSLException, IOException {
return fromCreds(GoogleCredentials.getApplicationDefault());
}

/**
* Creates a new gRPC channel to the Google Genomics API, using the provided api key for auth.
*/
public static GenomicsChannel fromApiKey(String apiKey) throws SSLException {
return new GenomicsChannel(apiKey);
}

/**
* Creates a new gRPC channel to the Google Genomics API, using the provided credentials for auth.
*/
public static GenomicsChannel fromCreds(GoogleCredentials creds) throws IOException {
return new GenomicsChannel(creds);
}

/**
* Initialize auth for a gRPC channel from OfflineAuth or the application default credentials.
* Create a new gRPC channel to the Google Genomics API, using OfflineAuth or the application
* default credentials.
*
* This library works with both the older and newer support for OAuth2 clients.
*
* https://developers.google.com/identity/protocols/application-default-credentials
*
* @param auth An OfflineAuth object.
* @return The gRPC channel authorized using either the information in the OfflineAuth or
* application default credentials.
* @param auth the OfflineAuth object
* @return the ManagedChannel
* @throws IOException
* @throws GeneralSecurityException
*/
public static GenomicsChannel fromOfflineAuth(GenomicsFactory.OfflineAuth auth)
public static ManagedChannel fromOfflineAuth(GenomicsFactory.OfflineAuth auth)
throws IOException, GeneralSecurityException {
if (auth.hasUserCredentials()) {
return fromCreds(auth.getUserCredentials());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class FaultyGenomicsServerITCase {

protected static Server server;
protected static IntegrationTestHelper helper;
protected static GenomicsChannel genomicsChannel;
protected static ManagedChannel genomicsChannel;

// Variable accessed by both the InProcess Server executor threads and the test thread.
protected static volatile double faultPercentage = 0.0;
Expand Down

0 comments on commit 2d7a9b3

Please sign in to comment.