Skip to content

Commit

Permalink
Merge pull request googleapis#11 from pongad/connsettings
Browse files Browse the repository at this point in the history
Add ConnectionSettings
  • Loading branch information
garrettjonesgoogle committed Feb 23, 2016
2 parents 403d344 + a4c582b commit 4864425
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 149 deletions.
122 changes: 122 additions & 0 deletions src/main/java/com/google/api/gax/core/ConnectionSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.api.gax.core;

import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auto.value.AutoValue;

import java.io.IOException;
import java.util.List;

@AutoValue
public abstract class ConnectionSettings {

interface CredentialsProvider {
Credentials getCredentials() throws IOException;
}

/**
* Credentials to use in order to call the service.
*/
public Credentials getCredentials() throws IOException {
return getCredentialsProvider().getCredentials();
}

abstract CredentialsProvider getCredentialsProvider();

/**
* The path used to reach the service.
*/
public abstract String getServiceAddress();

/**
* The port used to reach the service.
*/
public abstract int getPort();

public static Builder builder() {
return new AutoValue_ConnectionSettings.Builder();
}

public Builder toBuilder() {
return new AutoValue_ConnectionSettings.Builder(this);
}

@AutoValue.Builder
public abstract static class Builder {

abstract Builder setCredentialsProvider(CredentialsProvider provider);

/**
* Sets the credentials to use in order to call the service.
*/
public Builder provideCredentialsWith(Credentials credentials) {
return setCredentialsProvider(new CredentialsProvider() {
@Override
public Credentials getCredentials() {
return credentials;
}
});
}

/**
* Sets the credentials using application default, applying the given scopes if needed.
*/
public Builder provideCredentialsWith(List<String> scopes) {
return setCredentialsProvider(new CredentialsProvider() {
@Override
public Credentials getCredentials() throws IOException {
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
if (credentials.createScopedRequired()) {
credentials = credentials.createScoped(scopes);
}
return credentials;
}
});
}

/**
* Sets the path used to reach the service.
*/
public abstract Builder setServiceAddress(String serviceAddress);

/**
* Sets the port used to reach the service.
*/
public abstract Builder setPort(int port);

/**
* Builds the ConnectionSettings.
*/
public abstract ConnectionSettings build();
}
}
80 changes: 47 additions & 33 deletions src/main/java/com/google/api/gax/grpc/ServiceApiSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,26 @@

package com.google.api.gax.grpc;

import com.google.api.gax.core.ConnectionSettings;
import com.google.api.gax.core.RetryParams;
import com.google.auth.Credentials;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.MoreExecutors;

import io.grpc.auth.ClientAuthInterceptor;
import io.grpc.ClientInterceptor;
import io.grpc.ManagedChannel;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.Status;

import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

import javax.annotation.Nullable;
import java.util.List;

// TODO(pongad): Don't close the channel if the user gives one to us
/**
Expand All @@ -57,6 +63,11 @@
*/
@AutoValue
public abstract class ServiceApiSettings<MethodId> {

interface ChannelProvider {
ManagedChannel getChannel(Executor executor) throws IOException;
}

public static final int DEFAULT_EXECUTOR_THREADS = 4;
private static final ScheduledExecutorService DEFAULT_EXECUTOR =
MoreExecutors.getExitingScheduledExecutorService(
Expand All @@ -72,25 +83,6 @@ public abstract class ServiceApiSettings<MethodId> {
*/
public abstract ImmutableMap<MethodId, RetryParams> getRetryParams();

/**
* Credentials to use in order to call the service.
* The default is to acquire credentials using GoogleCredentials.getApplicationDefault().
* These credentials are not used if the channel is set.
*/
@Nullable
public abstract Credentials getCredentials();

/**
* The path used to reach the service. This value will not be used if the channel is set.
*/
@Nullable
public abstract String getServiceAddress();

/**
* The port used to reach the service. This value will not be used if the channel is set.
*/
public abstract int getPort();

/**
* The executor to be used by the client.
*
Expand All @@ -111,15 +103,17 @@ public abstract class ServiceApiSettings<MethodId> {
* The channel used to send requests to the service.
* See class documentation on channels.
*/
@Nullable
public abstract ManagedChannel getChannel();
public ManagedChannel getChannel() throws IOException {
return getChannelProvider().getChannel(getExecutor());
}

abstract ChannelProvider getChannelProvider();

public static <MethodId> Builder<MethodId> builder() {
return new AutoValue_ServiceApiSettings.Builder<MethodId>()
.setRetryableCodes(ImmutableMap.<MethodId, ImmutableSet<Status.Code>>of())
.setRetryParams(ImmutableMap.<MethodId, RetryParams>of())
.setExecutor(DEFAULT_EXECUTOR)
.setPort(0);
.setExecutor(DEFAULT_EXECUTOR);
}

public Builder<MethodId> toBuilder() {
Expand All @@ -134,13 +128,33 @@ public abstract Builder<MethodId> setRetryableCodes(
public abstract Builder<MethodId> setRetryParams(
ImmutableMap<MethodId, RetryParams> retryParams);

public abstract Builder<MethodId> setCredentials(Credentials credentials);

public abstract Builder<MethodId> setServiceAddress(String serviceAddress);

public abstract Builder<MethodId> setPort(int port);

public abstract Builder<MethodId> setChannel(ManagedChannel channel);
public Builder<MethodId> provideChannelWith(ManagedChannel channel) {
ChannelProvider provider = new ChannelProvider() {
@Override
public ManagedChannel getChannel(Executor executor) {
return channel;
}
};
return setChannelProvider(provider);
}

public Builder<MethodId> provideChannelWith(ConnectionSettings settings) {
ChannelProvider provider = new ChannelProvider() {
@Override
public ManagedChannel getChannel(Executor executor) throws IOException {
List<ClientInterceptor> interceptors = Lists.newArrayList();
interceptors.add(new ClientAuthInterceptor(settings.getCredentials(), executor));

return NettyChannelBuilder.forAddress(settings.getServiceAddress(), settings.getPort())
.negotiationType(NegotiationType.TLS)
.intercept(interceptors)
.build();
}
};
return setChannelProvider(provider);
}

abstract Builder<MethodId> setChannelProvider(ChannelProvider provider);

public abstract Builder<MethodId> setExecutor(ScheduledExecutorService executor);

Expand Down
116 changes: 0 additions & 116 deletions src/main/java/com/google/api/gax/internal/ApiUtils.java

This file was deleted.

0 comments on commit 4864425

Please sign in to comment.