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

Commit

Permalink
Revert "java: move CredentialProvider up to ClientSettings (#1251)" (#…
Browse files Browse the repository at this point in the history
…1317)

This reverts commit 2da784d.
  • Loading branch information
pongad authored Jun 1, 2017
1 parent bb472d3 commit 7ed21da
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -439,20 +439,15 @@ 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.concurrent.ScheduledExecutorService");
typeTable.saveNicknameFor("java.util.List");
typeTable.saveNicknameFor("java.util.concurrent.ScheduledExecutorService");
typeTable.saveNicknameFor("javax.annotation.Generated");

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

@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 @@ -212,8 +216,9 @@
*/
protected {@xapiClass.name}({@xapiClass.settingsClassName} settings) throws IOException {
this.settings = settings;

ClientContext clientContext = ClientContext.create(settings);
ChannelAndExecutor channelAndExecutor = settings.getChannelAndExecutor();
this.executor = channelAndExecutor.getExecutor();
this.channel = channelAndExecutor.getChannel();

@if xapiClass.hasLongRunningOperations
FixedExecutorProvider executorProvider = FixedExecutorProvider.create(this.executor);
Expand All @@ -222,33 +227,49 @@
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}(), clientContext);
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}(), this.channel, this.executor);
@case "PagedApiCallable"
this.{@apiCallable.name} =
UnaryCallable.createPagedVariant(settings.{@apiCallable.settingsFunctionName}(), clientContext);
UnaryCallable.createPagedVariant(settings.{@apiCallable.settingsFunctionName}(), this.channel, this.executor);
@case "BatchingApiCallable"
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}(), clientContext);
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}(), this.channel, this.executor);
@case "StreamingApiCallable"
this.{@apiCallable.name} = StreamingCallable.create(settings.{@apiCallable.settingsFunctionName}(), this.channel);
@case "InitialOperationApiCallable"
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}().getInitialCallSettings(), clientContext);
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}().getInitialCallSettings(), this.channel, this.executor);
@case "OperationApiCallable"
this.{@apiCallable.name} = OperationCallable.create(settings.{@apiCallable.settingsFunctionName}(),\
clientContext, this.operationsClient);
this.channel, this.executor, this.operationsClient);
@default
$unhandledCase: {@apiCallable.type}$
@end
@end

closeables.addAll(clientContext.getCloseables());
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();
}
});
}
}
{@""}
@end
Expand Down
13 changes: 3 additions & 10 deletions src/main/resources/com/google/api/codegen/java/settings.snip
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@
@if xsettingsClass.hasDefaultServiceAddress
.setEndpoint(getDefaultEndpoint())
@end
.setGeneratorHeader(DEFAULT_GAPIC_NAME, getGapicVersion());
.setGeneratorHeader(DEFAULT_GAPIC_NAME, getGapicVersion())
.setCredentialsProvider(defaultCredentialsProviderBuilder().build());
}

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

@join settings : xsettingsClass.callSettings
{@settings.memberName} = settingsBuilder.{@settings.settingsGetFunction}().build();
Expand Down Expand Up @@ -508,7 +508,6 @@
private Builder() {
@if xsettingsClass.hasDefaultInstance
super(defaultChannelProviderBuilder().build());
setCredentialsProvider(defaultCredentialsProviderBuilder().build());
@else
super((InstantiatingChannelProvider) null);
@end
Expand Down Expand Up @@ -613,12 +612,6 @@
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,9 +17,7 @@
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 @@ -28,7 +26,6 @@ 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 @@ -85,9 +82,7 @@ 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 @@ -156,10 +151,12 @@ import javax.annotation.Generated;
*
* <pre>
* <code>
* LibrarySettings librarySettings =
* LibrarySettings.defaultBuilder()
* InstantiatingChannelProvider channelProvider =
* LibrarySettings.defaultChannelProviderBuilder()
* .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
* .build();
* LibrarySettings librarySettings =
* LibrarySettings.defaultBuilder().setChannelProvider(channelProvider).build();
* LibraryClient libraryClient =
* LibraryClient.create(librarySettings);
* </code>
Expand All @@ -168,6 +165,8 @@ 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 @@ -230,58 +229,75 @@ public class LibraryClient implements AutoCloseable {
*/
protected LibraryClient(LibrarySettings settings) throws IOException {
this.settings = settings;

ClientContext clientContext = ClientContext.create(settings);
ChannelAndExecutor channelAndExecutor = settings.getChannelAndExecutor();
this.executor = channelAndExecutor.getExecutor();
this.channel = channelAndExecutor.getChannel();

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(), clientContext);
this.getShelfCallable = UnaryCallable.create(settings.getShelfSettings(), clientContext);
this.listShelvesCallable = UnaryCallable.create(settings.listShelvesSettings(), clientContext);
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.listShelvesPagedCallable =
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);
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);
this.listBooksPagedCallable =
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);
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);
this.listStringsPagedCallable =
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);
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);
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(), clientContext);
this.findRelatedBooksCallable = UnaryCallable.create(settings.findRelatedBooksSettings(), this.channel, this.executor);
this.findRelatedBooksPagedCallable =
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());
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();
}
});
}
}

public final LibrarySettings getSettings() {
Expand Down
Loading

0 comments on commit 7ed21da

Please sign in to comment.