Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

java: move CredentialProvider up to ClientSettings #1251

Merged
merged 3 commits into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -439,15 +439,20 @@ private PackageInfoView generatePackageInfo(
private void addApiImports(GapicInterfaceContext context) {
ModelTypeTable typeTable = context.getModelTypeTable();
typeTable.saveNicknameFor("com.google.api.core.BetaApi");
typeTable.saveNicknameFor("com.google.api.gax.core.CredentialsProvider");
typeTable.saveNicknameFor("com.google.api.gax.grpc.ChannelAndExecutor");
typeTable.saveNicknameFor("com.google.api.gax.grpc.ClientContext");
typeTable.saveNicknameFor("com.google.api.gax.grpc.UnaryCallable");
typeTable.saveNicknameFor("com.google.api.pathtemplate.PathTemplate");
typeTable.saveNicknameFor("com.google.auth.Credentials");
typeTable.saveNicknameFor("io.grpc.auth.MoreCallCredentials");
typeTable.saveNicknameFor("io.grpc.CallCredentials");
typeTable.saveNicknameFor("io.grpc.ManagedChannel");
typeTable.saveNicknameFor("java.io.Closeable");
typeTable.saveNicknameFor("java.io.IOException");
typeTable.saveNicknameFor("java.util.ArrayList");
typeTable.saveNicknameFor("java.util.List");
typeTable.saveNicknameFor("java.util.concurrent.ScheduledExecutorService");
typeTable.saveNicknameFor("java.util.List");
typeTable.saveNicknameFor("javax.annotation.Generated");

if (context.getInterfaceConfig().hasLongRunningOperations()) {
Expand Down
43 changes: 11 additions & 32 deletions src/main/resources/com/google/api/codegen/java/main.snip
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,10 @@
*
* <pre>
* <code>
* InstantiatingChannelProvider channelProvider =
* {@xapiClassDoc.settingsClassName}.defaultChannelProviderBuilder()
* {@xapiClassDoc.settingsClassName} {@xapiClassDoc.settingsVarName} =
* {@xapiClassDoc.settingsClassName}.defaultBuilder()
* .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
* .build();
* {@xapiClassDoc.settingsClassName} {@xapiClassDoc.settingsVarName} =
* {@xapiClassDoc.settingsClassName}.defaultBuilder().setChannelProvider(channelProvider).build();
* {@xapiClassDoc.apiClassName} {@xapiClassDoc.apiVarName} =
* {@xapiClassDoc.apiClassName}.create({@xapiClassDoc.settingsVarName});
* </code>
Expand Down Expand Up @@ -171,8 +169,6 @@

@private members(xapiClass)
private final {@xapiClass.settingsClassName} settings;
private final ScheduledExecutorService executor;
private final ManagedChannel channel;
@if xapiClass.hasLongRunningOperations
private final OperationsClient operationsClient;
@end
Expand Down Expand Up @@ -216,9 +212,8 @@
*/
protected {@xapiClass.name}({@xapiClass.settingsClassName} settings) throws IOException {
this.settings = settings;
ChannelAndExecutor channelAndExecutor = settings.getChannelAndExecutor();
this.executor = channelAndExecutor.getExecutor();
this.channel = channelAndExecutor.getChannel();

ClientContext clientContext = ClientContext.create(settings);

@if xapiClass.hasLongRunningOperations
FixedExecutorProvider executorProvider = FixedExecutorProvider.create(this.executor);
Expand All @@ -227,49 +222,33 @@
OperationsSettings.defaultBuilder()
.setExecutorProvider(executorProvider)
.setChannelProvider(channelProvider)
.setCredentialsProvider(credentialsProvider)
.build();
this.operationsClient = OperationsClient.create(operationsSettings);
@end

@join apiCallable : xapiClass.apiCallableMembers
@switch apiCallable.type
@case "SimpleApiCallable"
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}(), this.channel, this.executor);
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}(), clientContext);
@case "PagedApiCallable"
this.{@apiCallable.name} =
UnaryCallable.createPagedVariant(settings.{@apiCallable.settingsFunctionName}(), this.channel, this.executor);
UnaryCallable.createPagedVariant(settings.{@apiCallable.settingsFunctionName}(), clientContext);
@case "BatchingApiCallable"
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}(), this.channel, this.executor);
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}(), clientContext);
@case "StreamingApiCallable"
this.{@apiCallable.name} = StreamingCallable.create(settings.{@apiCallable.settingsFunctionName}(), this.channel);
@case "InitialOperationApiCallable"
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}().getInitialCallSettings(), this.channel, this.executor);
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}().getInitialCallSettings(), clientContext);
@case "OperationApiCallable"
this.{@apiCallable.name} = OperationCallable.create(settings.{@apiCallable.settingsFunctionName}(),\
this.channel, this.executor, this.operationsClient);
clientContext, this.operationsClient);
@default
$unhandledCase: {@apiCallable.type}$
@end
@end

if (settings.getChannelProvider().shouldAutoClose()) {
closeables.add(
new Closeable() {
@@Override
public void close() throws IOException {
channel.shutdown();
}
});
}
if (settings.getExecutorProvider().shouldAutoClose()) {
closeables.add(
new Closeable() {
@@Override
public void close() throws IOException {
executor.shutdown();
}
});
}
closeables.addAll(clientContext.getCloseables());
}
{@""}
@end
Expand Down
13 changes: 10 additions & 3 deletions src/main/resources/com/google/api/codegen/java/settings.snip
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@
@if xsettingsClass.hasDefaultServiceAddress
.setEndpoint(getDefaultEndpoint())
@end
.setGeneratorHeader(DEFAULT_GAPIC_NAME, getGapicVersion())
.setCredentialsProvider(defaultCredentialsProviderBuilder().build());
.setGeneratorHeader(DEFAULT_GAPIC_NAME, getGapicVersion());
}

private static String getGapicVersion() {
Expand Down Expand Up @@ -256,7 +255,8 @@
@private constructors(xsettingsClass)
private {@xsettingsClass.name}(Builder settingsBuilder) throws IOException {
super(settingsBuilder.getExecutorProvider(),
settingsBuilder.getChannelProvider());
settingsBuilder.getChannelProvider(),
settingsBuilder.getCredentialsProvider());

@join settings : xsettingsClass.callSettings
{@settings.memberName} = settingsBuilder.{@settings.settingsGetFunction}().build();
Expand Down Expand Up @@ -508,6 +508,7 @@
private Builder() {
@if xsettingsClass.hasDefaultInstance
super(defaultChannelProviderBuilder().build());
setCredentialsProvider(defaultCredentialsProviderBuilder().build());
@else
super((InstantiatingChannelProvider) null);
@end
Expand Down Expand Up @@ -612,6 +613,12 @@
return this;
}

@@Override
public Builder setCredentialsProvider(CredentialsProvider credentialsProvider) {
super.setCredentialsProvider(credentialsProvider);
return this;
}

/**
* Applies the given settings to all of the unary API methods in this service. Only
* values that are non-null will be applied, so this method is not capable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package com.google.gcloud.pubsub.spi;

import com.google.api.core.BetaApi;
import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.grpc.ChannelAndExecutor;
import com.google.api.gax.grpc.ClientContext;
import com.google.api.gax.grpc.FixedChannelProvider;
import com.google.api.gax.grpc.FixedExecutorProvider;
import com.google.api.gax.grpc.OperationCallable;
Expand All @@ -26,6 +28,7 @@ import com.google.api.gax.grpc.StreamingCallable;
import com.google.api.gax.grpc.UnaryCallable;
import com.google.api.pathtemplate.PathTemplate;
import com.google.api.resourcenames.ResourceName;
import com.google.auth.Credentials;
import com.google.example.library.v1.AddCommentsRequest;
import com.google.example.library.v1.ArchivedBookName;
import com.google.example.library.v1.Book;
Expand Down Expand Up @@ -82,7 +85,9 @@ import com.google.tagger.v1.AddLabelRequest;
import com.google.tagger.v1.AddLabelResponse;
import com.google.tagger.v1.AddTagRequest;
import com.google.tagger.v1.AddTagResponse;
import io.grpc.CallCredentials;
import io.grpc.ManagedChannel;
import io.grpc.auth.MoreCallCredentials;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -151,12 +156,10 @@ import javax.annotation.Generated;
*
* <pre>
* <code>
* InstantiatingChannelProvider channelProvider =
* LibrarySettings.defaultChannelProviderBuilder()
* LibrarySettings librarySettings =
* LibrarySettings.defaultBuilder()
* .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
* .build();
* LibrarySettings librarySettings =
* LibrarySettings.defaultBuilder().setChannelProvider(channelProvider).build();
* LibraryClient libraryClient =
* LibraryClient.create(librarySettings);
* </code>
Expand All @@ -165,8 +168,6 @@ import javax.annotation.Generated;
@Generated("by GAPIC")
public class LibraryClient implements AutoCloseable {
private final LibrarySettings settings;
private final ScheduledExecutorService executor;
private final ManagedChannel channel;
private final OperationsClient operationsClient;
private final List<AutoCloseable> closeables = new ArrayList<>();

Expand Down Expand Up @@ -229,75 +230,58 @@ public class LibraryClient implements AutoCloseable {
*/
protected LibraryClient(LibrarySettings settings) throws IOException {
this.settings = settings;
ChannelAndExecutor channelAndExecutor = settings.getChannelAndExecutor();
this.executor = channelAndExecutor.getExecutor();
this.channel = channelAndExecutor.getChannel();

ClientContext clientContext = ClientContext.create(settings);

FixedExecutorProvider executorProvider = FixedExecutorProvider.create(this.executor);
FixedChannelProvider channelProvider = FixedChannelProvider.create(this.channel);
OperationsSettings operationsSettings =
OperationsSettings.defaultBuilder()
.setExecutorProvider(executorProvider)
.setChannelProvider(channelProvider)
.setCredentialsProvider(credentialsProvider)
.build();
this.operationsClient = OperationsClient.create(operationsSettings);

this.createShelfCallable = UnaryCallable.create(settings.createShelfSettings(), this.channel, this.executor);
this.getShelfCallable = UnaryCallable.create(settings.getShelfSettings(), this.channel, this.executor);
this.listShelvesCallable = UnaryCallable.create(settings.listShelvesSettings(), this.channel, this.executor);
this.createShelfCallable = UnaryCallable.create(settings.createShelfSettings(), clientContext);
this.getShelfCallable = UnaryCallable.create(settings.getShelfSettings(), clientContext);
this.listShelvesCallable = UnaryCallable.create(settings.listShelvesSettings(), clientContext);
this.listShelvesPagedCallable =
UnaryCallable.createPagedVariant(settings.listShelvesSettings(), this.channel, this.executor);
this.deleteShelfCallable = UnaryCallable.create(settings.deleteShelfSettings(), this.channel, this.executor);
this.mergeShelvesCallable = UnaryCallable.create(settings.mergeShelvesSettings(), this.channel, this.executor);
this.createBookCallable = UnaryCallable.create(settings.createBookSettings(), this.channel, this.executor);
this.publishSeriesCallable = UnaryCallable.create(settings.publishSeriesSettings(), this.channel, this.executor);
this.getBookCallable = UnaryCallable.create(settings.getBookSettings(), this.channel, this.executor);
this.listBooksCallable = UnaryCallable.create(settings.listBooksSettings(), this.channel, this.executor);
UnaryCallable.createPagedVariant(settings.listShelvesSettings(), clientContext);
this.deleteShelfCallable = UnaryCallable.create(settings.deleteShelfSettings(), clientContext);
this.mergeShelvesCallable = UnaryCallable.create(settings.mergeShelvesSettings(), clientContext);
this.createBookCallable = UnaryCallable.create(settings.createBookSettings(), clientContext);
this.publishSeriesCallable = UnaryCallable.create(settings.publishSeriesSettings(), clientContext);
this.getBookCallable = UnaryCallable.create(settings.getBookSettings(), clientContext);
this.listBooksCallable = UnaryCallable.create(settings.listBooksSettings(), clientContext);
this.listBooksPagedCallable =
UnaryCallable.createPagedVariant(settings.listBooksSettings(), this.channel, this.executor);
this.deleteBookCallable = UnaryCallable.create(settings.deleteBookSettings(), this.channel, this.executor);
this.updateBookCallable = UnaryCallable.create(settings.updateBookSettings(), this.channel, this.executor);
this.moveBookCallable = UnaryCallable.create(settings.moveBookSettings(), this.channel, this.executor);
this.listStringsCallable = UnaryCallable.create(settings.listStringsSettings(), this.channel, this.executor);
UnaryCallable.createPagedVariant(settings.listBooksSettings(), clientContext);
this.deleteBookCallable = UnaryCallable.create(settings.deleteBookSettings(), clientContext);
this.updateBookCallable = UnaryCallable.create(settings.updateBookSettings(), clientContext);
this.moveBookCallable = UnaryCallable.create(settings.moveBookSettings(), clientContext);
this.listStringsCallable = UnaryCallable.create(settings.listStringsSettings(), clientContext);
this.listStringsPagedCallable =
UnaryCallable.createPagedVariant(settings.listStringsSettings(), this.channel, this.executor);
this.addCommentsCallable = UnaryCallable.create(settings.addCommentsSettings(), this.channel, this.executor);
this.getBookFromArchiveCallable = UnaryCallable.create(settings.getBookFromArchiveSettings(), this.channel, this.executor);
this.getBookFromAnywhereCallable = UnaryCallable.create(settings.getBookFromAnywhereSettings(), this.channel, this.executor);
this.updateBookIndexCallable = UnaryCallable.create(settings.updateBookIndexSettings(), this.channel, this.executor);
UnaryCallable.createPagedVariant(settings.listStringsSettings(), clientContext);
this.addCommentsCallable = UnaryCallable.create(settings.addCommentsSettings(), clientContext);
this.getBookFromArchiveCallable = UnaryCallable.create(settings.getBookFromArchiveSettings(), clientContext);
this.getBookFromAnywhereCallable = UnaryCallable.create(settings.getBookFromAnywhereSettings(), clientContext);
this.updateBookIndexCallable = UnaryCallable.create(settings.updateBookIndexSettings(), clientContext);
this.streamShelvesCallable = StreamingCallable.create(settings.streamShelvesSettings(), this.channel);
this.streamBooksCallable = StreamingCallable.create(settings.streamBooksSettings(), this.channel);
this.discussBookCallable = StreamingCallable.create(settings.discussBookSettings(), this.channel);
this.monologAboutBookCallable = StreamingCallable.create(settings.monologAboutBookSettings(), this.channel);
this.findRelatedBooksCallable = UnaryCallable.create(settings.findRelatedBooksSettings(), this.channel, this.executor);
this.findRelatedBooksCallable = UnaryCallable.create(settings.findRelatedBooksSettings(), clientContext);
this.findRelatedBooksPagedCallable =
UnaryCallable.createPagedVariant(settings.findRelatedBooksSettings(), this.channel, this.executor);
this.addTagCallable = UnaryCallable.create(settings.addTagSettings(), this.channel, this.executor);
this.addLabelCallable = UnaryCallable.create(settings.addLabelSettings(), this.channel, this.executor);
this.getBigBookCallable = UnaryCallable.create(settings.getBigBookSettings().getInitialCallSettings(), this.channel, this.executor);
this.getBigBookOperationCallable = OperationCallable.create(settings.getBigBookSettings(),this.channel, this.executor, this.operationsClient);
this.getBigNothingCallable = UnaryCallable.create(settings.getBigNothingSettings().getInitialCallSettings(), this.channel, this.executor);
this.getBigNothingOperationCallable = OperationCallable.create(settings.getBigNothingSettings(),this.channel, this.executor, this.operationsClient);
this.testOptionalRequiredFlatteningParamsCallable = UnaryCallable.create(settings.testOptionalRequiredFlatteningParamsSettings(), this.channel, this.executor);

if (settings.getChannelProvider().shouldAutoClose()) {
closeables.add(
new Closeable() {
@Override
public void close() throws IOException {
channel.shutdown();
}
});
}
if (settings.getExecutorProvider().shouldAutoClose()) {
closeables.add(
new Closeable() {
@Override
public void close() throws IOException {
executor.shutdown();
}
});
}
UnaryCallable.createPagedVariant(settings.findRelatedBooksSettings(), clientContext);
this.addTagCallable = UnaryCallable.create(settings.addTagSettings(), clientContext);
this.addLabelCallable = UnaryCallable.create(settings.addLabelSettings(), clientContext);
this.getBigBookCallable = UnaryCallable.create(settings.getBigBookSettings().getInitialCallSettings(), clientContext);
this.getBigBookOperationCallable = OperationCallable.create(settings.getBigBookSettings(),clientContext, this.operationsClient);
this.getBigNothingCallable = UnaryCallable.create(settings.getBigNothingSettings().getInitialCallSettings(), clientContext);
this.getBigNothingOperationCallable = OperationCallable.create(settings.getBigNothingSettings(),clientContext, this.operationsClient);
this.testOptionalRequiredFlatteningParamsCallable = UnaryCallable.create(settings.testOptionalRequiredFlatteningParamsSettings(), clientContext);

closeables.addAll(clientContext.getCloseables());
}

public final LibrarySettings getSettings() {
Expand Down
Loading