1 /* 2 * Copyright 2022 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.cloud.asset.v1p7beta1.stub; 18 19 import com.google.api.core.BetaApi; 20 import com.google.api.gax.core.BackgroundResource; 21 import com.google.api.gax.core.BackgroundResourceAggregation; 22 import com.google.api.gax.grpc.GrpcCallSettings; 23 import com.google.api.gax.grpc.GrpcStubCallableFactory; 24 import com.google.api.gax.rpc.ClientContext; 25 import com.google.api.gax.rpc.OperationCallable; 26 import com.google.api.gax.rpc.UnaryCallable; 27 import com.google.cloud.asset.v1p7beta1.ExportAssetsRequest; 28 import com.google.cloud.asset.v1p7beta1.ExportAssetsResponse; 29 import com.google.common.collect.ImmutableMap; 30 import com.google.longrunning.Operation; 31 import com.google.longrunning.stub.GrpcOperationsStub; 32 import io.grpc.MethodDescriptor; 33 import io.grpc.protobuf.ProtoUtils; 34 import java.io.IOException; 35 import java.util.concurrent.TimeUnit; 36 import javax.annotation.Generated; 37 38 // AUTO-GENERATED DOCUMENTATION AND CLASS. 39 /** 40 * gRPC stub implementation for the AssetService service API. 41 * 42 * <p>This class is for advanced usage and reflects the underlying API directly. 43 */ 44 @BetaApi 45 @Generated("by gapic-generator-java") 46 public class GrpcAssetServiceStub extends AssetServiceStub { 47 private static final MethodDescriptor<ExportAssetsRequest, Operation> 48 exportAssetsMethodDescriptor = 49 MethodDescriptor.<ExportAssetsRequest, Operation>newBuilder() 50 .setType(MethodDescriptor.MethodType.UNARY) 51 .setFullMethodName("google.cloud.asset.v1p7beta1.AssetService/ExportAssets") 52 .setRequestMarshaller(ProtoUtils.marshaller(ExportAssetsRequest.getDefaultInstance())) 53 .setResponseMarshaller(ProtoUtils.marshaller(Operation.getDefaultInstance())) 54 .build(); 55 56 private final UnaryCallable<ExportAssetsRequest, Operation> exportAssetsCallable; 57 private final OperationCallable<ExportAssetsRequest, ExportAssetsResponse, ExportAssetsRequest> 58 exportAssetsOperationCallable; 59 60 private final BackgroundResource backgroundResources; 61 private final GrpcOperationsStub operationsStub; 62 private final GrpcStubCallableFactory callableFactory; 63 create(AssetServiceStubSettings settings)64 public static final GrpcAssetServiceStub create(AssetServiceStubSettings settings) 65 throws IOException { 66 return new GrpcAssetServiceStub(settings, ClientContext.create(settings)); 67 } 68 create(ClientContext clientContext)69 public static final GrpcAssetServiceStub create(ClientContext clientContext) throws IOException { 70 return new GrpcAssetServiceStub(AssetServiceStubSettings.newBuilder().build(), clientContext); 71 } 72 create( ClientContext clientContext, GrpcStubCallableFactory callableFactory)73 public static final GrpcAssetServiceStub create( 74 ClientContext clientContext, GrpcStubCallableFactory callableFactory) throws IOException { 75 return new GrpcAssetServiceStub( 76 AssetServiceStubSettings.newBuilder().build(), clientContext, callableFactory); 77 } 78 79 /** 80 * Constructs an instance of GrpcAssetServiceStub, using the given settings. This is protected so 81 * that it is easy to make a subclass, but otherwise, the static factory methods should be 82 * preferred. 83 */ GrpcAssetServiceStub(AssetServiceStubSettings settings, ClientContext clientContext)84 protected GrpcAssetServiceStub(AssetServiceStubSettings settings, ClientContext clientContext) 85 throws IOException { 86 this(settings, clientContext, new GrpcAssetServiceCallableFactory()); 87 } 88 89 /** 90 * Constructs an instance of GrpcAssetServiceStub, using the given settings. This is protected so 91 * that it is easy to make a subclass, but otherwise, the static factory methods should be 92 * preferred. 93 */ GrpcAssetServiceStub( AssetServiceStubSettings settings, ClientContext clientContext, GrpcStubCallableFactory callableFactory)94 protected GrpcAssetServiceStub( 95 AssetServiceStubSettings settings, 96 ClientContext clientContext, 97 GrpcStubCallableFactory callableFactory) 98 throws IOException { 99 this.callableFactory = callableFactory; 100 this.operationsStub = GrpcOperationsStub.create(clientContext, callableFactory); 101 102 GrpcCallSettings<ExportAssetsRequest, Operation> exportAssetsTransportSettings = 103 GrpcCallSettings.<ExportAssetsRequest, Operation>newBuilder() 104 .setMethodDescriptor(exportAssetsMethodDescriptor) 105 .setParamsExtractor( 106 request -> { 107 ImmutableMap.Builder<String, String> params = ImmutableMap.builder(); 108 params.put("parent", String.valueOf(request.getParent())); 109 return params.build(); 110 }) 111 .build(); 112 113 this.exportAssetsCallable = 114 callableFactory.createUnaryCallable( 115 exportAssetsTransportSettings, settings.exportAssetsSettings(), clientContext); 116 this.exportAssetsOperationCallable = 117 callableFactory.createOperationCallable( 118 exportAssetsTransportSettings, 119 settings.exportAssetsOperationSettings(), 120 clientContext, 121 operationsStub); 122 123 this.backgroundResources = 124 new BackgroundResourceAggregation(clientContext.getBackgroundResources()); 125 } 126 getOperationsStub()127 public GrpcOperationsStub getOperationsStub() { 128 return operationsStub; 129 } 130 131 @Override exportAssetsCallable()132 public UnaryCallable<ExportAssetsRequest, Operation> exportAssetsCallable() { 133 return exportAssetsCallable; 134 } 135 136 @Override 137 public OperationCallable<ExportAssetsRequest, ExportAssetsResponse, ExportAssetsRequest> exportAssetsOperationCallable()138 exportAssetsOperationCallable() { 139 return exportAssetsOperationCallable; 140 } 141 142 @Override close()143 public final void close() { 144 try { 145 backgroundResources.close(); 146 } catch (RuntimeException e) { 147 throw e; 148 } catch (Exception e) { 149 throw new IllegalStateException("Failed to close resource", e); 150 } 151 } 152 153 @Override shutdown()154 public void shutdown() { 155 backgroundResources.shutdown(); 156 } 157 158 @Override isShutdown()159 public boolean isShutdown() { 160 return backgroundResources.isShutdown(); 161 } 162 163 @Override isTerminated()164 public boolean isTerminated() { 165 return backgroundResources.isTerminated(); 166 } 167 168 @Override shutdownNow()169 public void shutdownNow() { 170 backgroundResources.shutdownNow(); 171 } 172 173 @Override awaitTermination(long duration, TimeUnit unit)174 public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { 175 return backgroundResources.awaitTermination(duration, unit); 176 } 177 } 178