10000 feat(autoconfig): use shared config in autoconfig · googleapis/sdk-platform-java@f5de117 · GitHub
[go: up one dir, main page]

Skip to content

Commit f5de117

Browse files
committed
feat(autoconfig): use shared config in autoconfig
1 parent 753db69 commit f5de117

File tree

4 files changed

+201
-28
lines changed

4 files changed

+201
-28
lines changed

src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java

Lines changed: 117 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.api.gax.retrying.RetrySettings;
2020
import com.google.api.gax.rpc.TransportChannelProvider;
2121
import com.google.api.generator.engine.ast.AnnotationNode;
22+
import com.google.api.generator.engine.ast.AnonymousArrayAnnotationExpr;
2223
import com.google.api.generator.engine.ast.ArithmeticOperationExpr;
2324
import com.google.api.generator.engine.ast.AssignmentExpr;
2425
import com.google.api.generator.engine.ast.CastExpr;
@@ -32,6 +33,7 @@
3233
import com.google.api.generator.engine.ast.NewObjectExpr;
3334
import com.google.api.generator.engine.ast.PrimitiveValue;
3435
import com.google.api.generator.engine.ast.RelationalOperationExpr;
36+
import com.google.api.generator.engine.ast.ReturnExpr;
3537
import com.google.api.generator.engine.ast.ScopeNode;
3638
import com.google.api.generator.engine.ast.Statement;
3739
import com.google.api.generator.engine.ast.StringObjectValue;
@@ -49,9 +51,11 @@
4951
import com.google.api.generator.gapic.model.GapicServiceConfig;
5052
import com.google.api.generator.gapic.model.Service;
5153
import com.google.api.generator.spring.utils.LoggerUtils;
54+
import com.google.api.generator.spring.utils.SharedPropertiesUtils;
5255
import com.google.api.generator.spring.utils.Utils;
5356
import com.google.common.base.CaseFormat;
5457
import com.google.common.base.Joiner;
58+
import com.google.common.collect.ImmutableList;
5559
import com.google.common.collect.ImmutableMap;
5660
import java.io.IOException;
5761
import java.util.ArrayList;
@@ -60,6 +64,7 @@
6064
import java.util.Map;
6165
import java.util.stream.Collectors;
6266
import javax.annotation.Generated;
67+
import javax.management.StringValueExp;
6368

6469
public class SpringAutoConfigClassComposer implements ClassComposer {
6570
private static final String CLASS_NAME_PATTERN = "%sSpringAutoConfiguration";
@@ -162,7 +167,8 @@ private static List<Statement> createMemberVariables(
162167

163168
Statement loggerStatement =
164169
LoggerUtils.getLoggerDeclarationExpr(serviceName + "AutoConfig", types);
165-
return Arrays.asList(clientPropertiesStatement, loggerStatement);
170+
Statement sharedPropertiesStatement = SharedPropertiesUtils.getSharedPropertiesDeclaration(types);
171+
return Arrays.asList(clientPropertiesStatement, sharedPropertiesStatement, loggerStatement);
166172
}
167173

168174
private static MethodDefinition createConstructor(
@@ -182,12 +188,18 @@ private static MethodDefinition createConstructor(
182188
// .setType(types.get("GcpProjectIdProvider"))
183189
// .build());
184190

185-
VariableExpr propertiesVarExpr =
191+
VariableExpr clientPropertiesVarExpr =
186192
VariableExpr.withVariable(
187193
Variable.builder()
188194
.setName("clientProperties")
189195
.setType(types.get(serviceName + "Properties"))
190196
.build());
197+
VariableExpr sharedPropertiesVarExpr =
198+
VariableExpr.withVariable(
199+
Variable.builder()
200+
.setName("sharedProperties")
201+
.setType(types.get("SharedProperties"))
202+
.build());
191203
// Variable projectIdProviderVar =
192204
// Variable.builder()
193205
// .setName("projectIdProvider")
@@ -198,27 +210,50 @@ private static MethodDefinition createConstructor(
198210
.setName("clientProperties")
199211
.setType(types.get(serviceName + "Properties"))
200212
.build();
213+
Variable sharedPropertiesVar =
214+
Variable.builder()
215+
.setName("sharedProperties")
216+
.setType(types.get("SharedProperties"))
217+
.build();
201218

202219
Expr thisExpr = ValueExpr.withValue(ThisObjectValue.withType(types.get(className)));
203220

204221
// this.clientProperties = clientProperties;
205-
AssignmentExpr thisPropertiesAssignmentExpr =
222+
AssignmentExpr thisClientPropertiesAssignmentExpr =
206223
AssignmentExpr.builder()
207224
.setVariableExpr(
208225
VariableExpr.withVariable(clientPropertiesVar)
209226
.toBuilder()
210227
.setExprReferenceExpr(thisExpr)
211228
.build())
212-
.setValueExpr(propertiesVarExpr)
229+
.setValueExpr(clientPropertiesVarExpr)
213230
.build();
214-
ExprStatement thisPropertiesAssignmentStatement =
215-
ExprStatement.withExpr(thisPropertiesAssignmentExpr);
231+
ExprStatement thisClientPropertiesAssignmentStatement =
232+
ExprStatement.withExpr(thisClientPropertiesAssignmentExpr);
233+
234+
AssignmentExpr thisSharedPropertiesAssignmentExpr =
235+
AssignmentExpr.builder()
236+
.setVariableExpr(
237+
VariableExpr.withVariable(sharedPropertiesVar)
238+
.toBuilder()
239+
.setExprReferenceExpr(thisExpr)
240+
.build())
241+
.setValueExpr(sharedPropertiesVarExpr)
242+
.build();
243+
ExprStatement thisSharedPropertiesAssignmentStatement =
244+
ExprStatement.withExpr(thisSharedPropertiesAssignmentExpr);
216245

217246
return MethodDefinition.constructorBuilder()
218247
.setScope(ScopeNode.PROTECTED)
219248
.setReturnType(types.get(className))
220-
.setArguments(Arrays.asList(propertiesVarExpr.toBuilder().setIsDecl(true).build()))
221-
.setBody(Arrays.asList(thisPropertiesAssignmentStatement))
249+
.setArguments(Arrays.asList(
250+
clientPropertiesVarExpr.toBuilder().setIsDecl(true).build(),
251+
sharedPropertiesVarExpr.toBuilder().setIsDecl(true).build()
252+
))
253+
.setBody(Arrays.asList(
254+
thisClientPropertiesAssignmentStatement,
255+
thisSharedPropertiesAssignmentStatement
256+
))
222257
.build();
223258
}
224259

@@ -286,12 +321,21 @@ private static List<AnnotationNode> createClassAnnotations(
286321
AnnotationNode enableConfigurationPropertiesNode =
287322
AnnotationNode.builder()
288323
.setType(types.get("EnableConfigurationProperties"))
289-
.setDescription(
290-
VariableExpr.builder()
291-
.setVariable(
292-
Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
293-
.setStaticReferenceType(types.get(service.name() + "Properties"))
294-
.build())
324+
.addDescription(
325+
AnonymousArrayAnnotationExpr.builder()
326+
.addExpr(VariableExpr.builder()
327+
.setVariable(
328+
Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
329+
.setStaticReferenceType(types.get(service.name() + "Properties"))
330+
.build())
331+
.addExpr(VariableExpr.builder()
332+
.setVariable(
333+
Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
334+
.setStaticReferenceType(types.get("SharedProperties"))
335+
.build())
336+
.build()
337+
338+
)
295339
.build();
296340

297341
return Arrays.asList(
@@ -308,11 +352,19 @@ private static List<AnnotationNode> createClassAnnotations(
308352
private static MethodDefinition createCredentialsProviderBeanMethod(
309353
Service service, String className, String methodName, Map<String, TypeNode> types) {
310354
// @Bean
311-
// @ConditionalOnMissingBean
355+
// @ConditionalOnMissingBean(name = "[serviceName]ServiceCredentials")
312356
// public CredentialsProvider languageServiceCredentials() throws IOException {
313-
// return new DefaultCredentialsProvider(this.clientProperties);
357+
// if (this.clientProperties.getCredentials().hasKey()) {
358+
// return new DefaultCredentialsProvider(this.clientProperties);
359+
// }
360+
// return new DefaultCredentialsProvider(this.sharedProperties);
314361
// }
315-
362+
List<Statement> bodyStatements = new ArrayList<>();
363+
Variable sharedPropertiesVar =
364+
Variable.builder()
365+
.setName("sharedProperties")
366+
.setType(types.get("SharedProperties"))
367+
.build();
316368
Variable clientPropertiesVar =
317369
Variable.builder()
318370
.setName("clientProperties")
@@ -325,7 +377,12 @@ private static MethodDefinition createCredentialsProviderBeanMethod(
325377
.toBuilder()
326378
.setExprReferenceExpr(thisExpr)
327379
.build();
328-
CastExpr castExpr =
380+
VariableExpr thisSharedProperties =
381+
VariableExpr.withVariable(sharedPropertiesVar)
382+
.toBuilder()
383+
.setExprReferenceExpr(thisExpr)
384+
.build();
385+
CastExpr clientCastExpr =
329386
CastExpr.builder()
330387
.setExpr(
331388
NewObjectExpr.builder()
@@ -334,6 +391,42 @@ private static MethodDefinition createCredentialsProviderBeanMethod(
334391
.build())
335392
.setType(types.get("CredentialsProvider"))
336393
.build();
394+
CastExpr sharedCredentialsCastExpr =
395+
CastExpr.builder()
396+
.setExpr(
397+
NewObjectExpr.builder()
398+
.setType(types.get("DefaultCredentialsProvider"))
399+
.setArguments(thisSharedProperties)
400+
.build())
401+
.setType(types.get("CredentialsProvider"))
402+
.build();
403+
ExprStatement clientCredentialsReturnExpr = ExprStatement.withExpr(ReturnExpr.withExpr(clientCastExpr));
404+
Expr clientPropertiesGetCredentials = MethodInvocationExpr.builder()
405+
.setExprReferenceExpr(thisClientProperties)
406+
.setMethodName("getCredentials")
407+
.setReturnType(types.get("Credentials"))
408+
.build();
409+
Expr clientPropertiesCredentialsHasKey = MethodInvocationExpr.builder()
410+
.setExprReferenceExpr(clientPropertiesGetCredentials)
411+
.setMethodName("hasKey")
412+
.setReturnType(TypeNode.BOOLEAN)
413+
.build();
414+
IfStatement clientCredentialsIfStatement = createIfStatement(clientPropertiesCredentialsHasKey,
415+
Arrays.asList(clientCredentialsReturnExpr), null);
416+
bodyStatements.add(clientCredentialsIfStatement);
417+
418+
// @ConditionalOnMissingBean(name = "[serviceName]ServiceCredentials")
419+
AnnotationNode conditionalOnMissingBeanExpr = AnnotationNode.builder()
420+
.setType(types.get("ConditionalOnMissingBean"))
421+
.addDescription(AssignmentExpr.builder()
422+
.setVariableExpr(VariableExpr.withVariable(
423+
Variable.builder()
424+
.setName("name")
425+
.setType(TypeNode.STRING)
426+
.build()))
427+
.setValueExpr(ValueExpr.withValue(StringObjectValue.withValue(methodName)))
428+
.build())
429+
.build();
337430

338431
return MethodDefinition.builder()
339432
.setName(methodName)
@@ -342,9 +435,12 @@ private static MethodDefinition createCredentialsProviderBeanMethod(
342435
.setAnnotations(
343436
Arrays.asList(
344437
AnnotationNode.withType(types.get("Bean")),
345-
AnnotationNode.withType(types.get("ConditionalOnMissingBean"))))
438+
conditionalOnMissingBeanExpr
439+
)
440+
)
346441
.setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(IOException.class)))
347-
.setReturnExpr(castExpr)
442+
.setBody(bodyStatements)
443+
.setReturnExpr(sharedCredentialsCastExpr)
348444
.build();
349445
}
350446

@@ -985,6 +1081,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
9851081
typeMap.put("Qualifier", qualifier);
9861082
typeMap.put("Log", LoggerUtils.getLoggerType());
9871083
typeMap.put("LogFactory", LoggerUtils.getLoggerFactoryType());
1084+
typeMap.put("SharedProperties", SharedPropertiesUtils.getSharedPropertiesType());
9881085

9891086
return typeMap;
9901087
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.api.generator.spring.utils;
16+
17+
import com.google.api.generator.engine.ast.ArithmeticOperationExpr;
18+
import com.google.api.generator.engine.ast.AssignmentExpr;
19+
import com.google.api.generator.engine.ast.Expr;
20+
import com.google.api.generator.engine.ast.ExprStatement;
21+
import com.google.api.generator.engine.ast.MethodInvocationExpr;
22+
import com.google.api.generator.engine.ast.ScopeNode;
23+
import com.google.api.generator.engine.ast.Statement;
24+
import com.google.api.generator.engine.ast.TypeNode;
25+
import com.google.api.generator.engine.ast.VaporReference;
26+
import com.google.api.generator.engine.ast.Variable;
27+
import com.google.api.generator.engine.ast.VariableExpr;
28+
import java.util.Arrays;
29+
import java.util.List;
30+
import java.util.Map;
31+
import java.util.Optional;
32+
33+
public class SharedPropertiesUtils {
34+
public static final String SHARED_PROPERTIES_CLAZZ_NAME = "SharedProperties";
35+
public static final String SHARED_PROPERTIES_PAKKAGE_NAME = "com.google.cloud.spring.shared";
36+
37+
public static TypeNode getSharedPropertiesType() {
38+
TypeNode loggerType =
39+
TypeNode.withReference(
40+
VaporReference.builder()
41+
.setName(SHARED_PROPERTIES_CLAZZ_NAME)
42+
.setPakkage(SHARED_PROPERTIES_PAKKAGE_NAME)
43+
.build());
44+
return loggerType;
45+
}
46+
47+
public static Statement getSharedPropertiesDeclaration(Map<String, TypeNode> types) {
48+
Variable sharedPropertiesVar = Variable.builder()
49+
.setName("sharedProperties")
50+
.setType(types.get("SharedProperties"))
51+
.build();
52+
return ExprStatement.withExpr(VariableExpr.builder()
53+
.setVariable(sharedPropertiesVar)
54+
.setScope(ScopeNode.PRIVATE)
55+
.setIsStatic(false)
56+
.setIsFinal(true)
57+
.setIsDecl(true)
58+
.build());
59+
}
60+
}

src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfiguration.golden

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import com.google.api.gax.core.ExecutorProvider;
55
import com.google.api.gax.httpjson.InstantiatingHttpJsonChannelProvider;
66
import com.google.api.gax.retrying.RetrySettings;
77
import com.google.api.gax.rpc.TransportChannelProvider;
8+
import com.google.cloud.spring.core.Credentials;
89
import com.google.cloud.spring.core.DefaultCredentialsProvider;
10+
import com.google.cloud.spring.shared.SharedProperties;
911
import com.google.showcase.v1beta1.EchoClient;
1012
import com.google.showcase.v1beta1.EchoSettings;
1113
import java.io.IOException;
@@ -26,19 +28,25 @@ import org.threeten.bp.Duration;
2628
@ConditionalOnProperty(
2729
value = "com.google.showcase.v1beta1.spring.auto.echo.enabled",
2830
matchIfMissing = false)
29-
@EnableConfigurationProperties(EchoSpringProperties.class)
31+
@EnableConfigurationProperties({EchoSpringProperties.class, SharedProperties.class})
3032
public class EchoSpringAutoConfiguration {
3133
private final EchoSpringProperties clientProperties;
34+
private final SharedProperties sharedProperties;
3235
private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfig.class);
3336

34-
protected EchoSpringAutoConfiguration(EchoSpringProperties clientProperties) {
37+
protected EchoSpringAutoConfiguration(
38+
EchoSpringProperties clientProperties, SharedProperties sharedProperties) {
3539
this.clientProperties = clientProperties;
40+
this.sharedProperties = sharedProperties;
3641
}
3742

3843
@Bean
39-
@ConditionalOnMissingBean
44+
@ConditionalOnMissingBean(name = "echoCredentials")
4045
public CredentialsProvider echoCredentials() throws IOException {
41-
return ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties));
46+
if (this.clientProperties.getCredentials().hasKey()) {
47+
return ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties));
48+
}
49+
return ((CredentialsProvider) new DefaultCredentialsProvider(this.sharedProperties));
4250
}
4351

4452
@Bean

src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import com.google.api.gax.core.ExecutorProvider;
2121
import com.google.api.gax.httpjson.InstantiatingHttpJsonChannelProvider;
2222
import com.google.api.gax.retrying.RetrySettings;
2323
import com.google.api.gax.rpc.TransportChannelProvider;
24+
import com.google.cloud.spring.core.Credentials;
2425
import com.google.cloud.spring.core.DefaultCredentialsProvider;
26+
import com.google.cloud.spring.shared.SharedProperties;
2527
import com.google.showcase.v1beta1.EchoClient;
2628
import com.google.showcase.v1beta1.EchoSettings;
2729
import java.io.IOException;
@@ -42,19 +44,25 @@ import org.threeten.bp.Duration;
4244
@ConditionalOnProperty(
4345
value = "com.google.showcase.v1beta1.spring.auto.echo.enabled",
4446
matchIfMissing = false)
45-
@EnableConfigurationProperties(EchoSpringProperties.class)
47+
@EnableConfigurationProperties({EchoSpringProperties.class, SharedProperties.class})
4648
public class EchoSpringAutoConfiguration {
4749
private final EchoSpringProperties clientProperties;
50+
private final SharedProperties sharedProperties;
4851
private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfig.class);
4952

50-
protected EchoSpringAutoConfiguration(EchoSpringProperties clientProperties) {
53+
protected EchoSpringAutoConfiguration(
54+
EchoSpringProperties clientProperties, SharedProperties sharedProperties) {
5155
this.clientProperties = clientProperties;
56+
this.sharedProperties = sharedProperties;
5257
}
5358

5459
@Bean
55-
@ConditionalOnMissingBean
60+
@ConditionalOnMissingBean(name = "echoCredentials")
5661
public CredentialsProvider echoCredentials() throws IOException {
57-
return ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties));
62+
if (this.clientProperties.getCredentials().hasKey()) {
63+
return ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties));
64+
}
65+
return ((CredentialsProvider) new DefaultCredentialsProvider(this.sharedProperties));
5866
}
5967

6068
@Bean

0 commit comments

Comments
 (0)
0