From d7f65f085fbaf01c9fe67246ac4521ee3a933945 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Fri, 9 Dec 2022 19:11:03 +0000 Subject: [PATCH 1/5] replace dependency in pom. --- src/main/java/com/google/api/generator/spring/SpringWriter.java | 2 +- .../google/api/generator/spring/goldens/SpringPackagePom.golden | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/api/generator/spring/SpringWriter.java b/src/main/java/com/google/api/generator/spring/SpringWriter.java index 3949012f27..32f64df80a 100644 --- a/src/main/java/com/google/api/generator/spring/SpringWriter.java +++ b/src/main/java/com/google/api/generator/spring/SpringWriter.java @@ -246,7 +246,7 @@ static String buildPomString(GapicContext context) { + "\n" + " \n" + " com.google.cloud\n" - + " spring-cloud-gcp-core\n" + + " spring-cloud-gcp-autoconfigure\n" + " \n" + "\n" + "\n" diff --git a/src/test/java/com/google/api/generator/spring/goldens/SpringPackagePom.golden b/src/test/java/com/google/api/generator/spring/goldens/SpringPackagePom.golden index 02e3b5b848..06756fce50 100644 --- a/src/test/java/com/google/api/generator/spring/goldens/SpringPackagePom.golden +++ b/src/test/java/com/google/api/generator/spring/goldens/SpringPackagePom.golden @@ -27,7 +27,7 @@ com.google.cloud - spring-cloud-gcp-core + spring-cloud-gcp-autoconfigure From 14d38074b0ad7de6c83f3dc8cbb0249cfa8722b1 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Fri, 9 Dec 2022 20:40:01 +0000 Subject: [PATCH 2/5] implement changes to switch to use CredentialsProvider bean from spring-cloud-gcp-autoconfigure from globalProperties.. --- .../SpringAutoConfigClassComposer.java | 271 ++++++------------ .../spring/utils/GlobalPropertiesUtils.java | 45 +++ .../EchoSpringAutoConfigurationFull.golden | 29 +- .../EchoSpringAutoConfigurationGrpc.golden | 29 +- ...EchoSpringAutoConfigurationGrpcRest.golden | 29 +- 5 files changed, 153 insertions(+), 250 deletions(-) diff --git a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java index 3d32292145..b64c53a2b3 100644 --- a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java @@ -22,7 +22,6 @@ import com.google.api.gax.rpc.TransportChannelProvider; import com.google.api.generator.engine.ast.AnnotationNode; import com.google.api.generator.engine.ast.ArithmeticOperationExpr; -import com.google.api.generator.engine.ast.ArrayExpr; import com.google.api.generator.engine.ast.AssignmentExpr; import com.google.api.generator.engine.ast.CastExpr; import com.google.api.generator.engine.ast.ClassDefinition; @@ -36,7 +35,6 @@ import com.google.api.generator.engine.ast.NewObjectExpr; import com.google.api.generator.engine.ast.PrimitiveValue; import com.google.api.generator.engine.ast.RelationalOperationExpr; -import com.google.api.generator.engine.ast.ReturnExpr; import com.google.api.generator.engine.ast.ScopeNode; import com.google.api.generator.engine.ast.Statement; import com.google.api.generator.engine.ast.StringObjectValue; @@ -121,16 +119,11 @@ public GapicClass generate(GapicContext context, Service service) { .setMethods( Arrays.asList( createConstructor(service, className, dynamicTypes, thisExpr), - createCredentialsProviderBeanMethod( - service, className, credentialsProviderName, dynamicTypes, thisExpr), createTransportChannelProviderBeanMethod( transportChannelProviderName, dynamicTypes), createClientBeanMethod( service, - className, - credentialsProviderName, transportChannelProviderName, - clientName, dynamicTypes, gapicServiceConfig, thisExpr, @@ -148,29 +141,22 @@ private static List createMemberVariables( Map types, GapicServiceConfig serviceConfig) { - String serviceName = service.name(); - // private final LanguageProperties clientProperties; - Variable clientPropertiesVar = - Variable.builder() - .setName("clientProperties") - .setType(types.get(Utils.getServicePropertiesClassName(service))) - .build(); - VariableExpr clientPropertiesVarExpr = - VariableExpr.builder() - .setVariable(clientPropertiesVar) - .setScope(ScopeNode.PRIVATE) - .setIsFinal(true) - .setIsDecl(true) - .build(); - ExprStatement clientPropertiesStatement = ExprStatement.withExpr(clientPropertiesVarExpr); + ExprStatement clientPropertiesStatement = + GlobalPropertiesUtils.createMemberVarStatement( + "clientProperties", + types.get(Utils.getServicePropertiesClassName(service)), + true, + null, + null); + Statement credentialProvider = + GlobalPropertiesUtils.createMemberVarStatement( + "credentialsProvider", STATIC_TYPES.get("CredentialsProvider"), true, null, null); Statement loggerStatement = LoggerUtils.getLoggerDeclarationExpr( Utils.getServiceAutoConfigurationClassName(service), types); - Statement globalPropertiesStatement = - GlobalPropertiesUtils.getGlobalPropertiesDeclaration(types); - return Arrays.asList(clientPropertiesStatement, globalPropertiesStatement, loggerStatement); + return Arrays.asList(clientPropertiesStatement, credentialProvider, loggerStatement); } private static MethodDefinition createConstructor( @@ -181,47 +167,79 @@ private static MethodDefinition createConstructor( .setName("clientProperties") .setType(types.get(Utils.getServicePropertiesClassName(service))) .build()); - VariableExpr globalPropertiesVarExpr = + VariableExpr credentialsProviderVarExpr = VariableExpr.withVariable( Variable.builder() - .setName("globalProperties") - .setType(types.get("GlobalProperties")) + .setName("credentialsProvider") + .setType(STATIC_TYPES.get("CredentialsProvider")) .build()); - Variable clientPropertiesVar = - Variable.builder() - .setName("clientProperties") - .setType(types.get(Utils.getServicePropertiesClassName(service))) - .build(); - Variable globalPropertiesVar = - Variable.builder() - .setName("globalProperties") - .setType(types.get("GlobalProperties")) - .build(); // this.clientProperties = clientProperties; AssignmentExpr thisClientPropertiesAssignmentExpr = AssignmentExpr.builder() .setVariableExpr( - VariableExpr.withVariable(clientPropertiesVar) - .toBuilder() - .setExprReferenceExpr(thisExpr) - .build()) + clientPropertiesVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()) .setValueExpr(clientPropertiesVarExpr) .build(); ExprStatement thisClientPropertiesAssignmentStatement = ExprStatement.withExpr(thisClientPropertiesAssignmentExpr); - AssignmentExpr thisGlobalPropertiesAssignmentExpr = + // if (this.clientProperties.getCredentials().hasKey()) { + // this.credentialsProvider = new DefaultCredentialsProvider(this.clientProperties); + // } else { + // this.credentialsProvider = credentialsProvider; + // } + VariableExpr thisClientProperties = + clientPropertiesVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build(); + AssignmentExpr.Builder thisCredentialsProviderAssignmentExprBuilder = AssignmentExpr.builder() .setVariableExpr( - VariableExpr.withVariable(globalPropertiesVar) - .toBuilder() - .setExprReferenceExpr(thisExpr) + credentialsProviderVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()); + ExprStatement thisCredentialsProviderToGlobalAssignmentStatement = + ExprStatement.withExpr( + thisCredentialsProviderAssignmentExprBuilder + .setValueExpr(credentialsProviderVarExpr) + .build()); + + CastExpr newCredentialsProviderExpr = + CastExpr.builder() + .setExpr( + NewObjectExpr.builder() + .setType(STATIC_TYPES.get("DefaultCredentialsProvider")) + .setArguments(thisClientProperties) .build()) - .setValueExpr(globalPropertiesVarExpr) + .setType(STATIC_TYPES.get("CredentialsProvider")) + .build(); + ExprStatement thisCredentialsProviderAssignmentExprNewStatement = + ExprStatement.withExpr( + thisCredentialsProviderAssignmentExprBuilder + .setValueExpr(newCredentialsProviderExpr) + .build()); + + Expr clientPropertiesGetCredentials = + MethodInvocationExpr.builder() + .setExprReferenceExpr(thisClientProperties) + .setMethodName("getCredentials") + .setReturnType(STATIC_TYPES.get("Credentials")) + .build(); + Expr clientPropertiesCredentialsHasKey = + MethodInvocationExpr.builder() + .setExprReferenceExpr(clientPropertiesGetCredentials) + .setMethodName("hasKey") + .setReturnType(TypeNode.BOOLEAN) .build(); - ExprStatement thisGlobalPropertiesAssignmentStatement = - ExprStatement.withExpr(thisGlobalPropertiesAssignmentExpr); + + Statement logClientCredentials = + LoggerUtils.createLoggerStatement( + ValueExpr.withValue( + StringObjectValue.withValue( + "Using credentials from " + service.name() + "-specific configuration")), + types); + IfStatement thisCredentialsProviderAssignmentStatement = + createIfStatement( + clientPropertiesCredentialsHasKey, + Arrays.asList(logClientCredentials, thisCredentialsProviderAssignmentExprNewStatement), + Arrays.asList(thisCredentialsProviderToGlobalAssignmentStatement)); return MethodDefinition.constructorBuilder() .setScope(ScopeNode.PROTECTED) @@ -229,10 +247,11 @@ private static MethodDefinition createConstructor( .setArguments( Arrays.asList( clientPropertiesVarExpr.toBuilder().setIsDecl(true).build(), - globalPropertiesVarExpr.toBuilder().setIsDecl(true).build())) + credentialsProviderVarExpr.toBuilder().setIsDecl(true).build())) .setBody( Arrays.asList( - thisClientPropertiesAssignmentStatement, thisGlobalPropertiesAssignmentStatement)) + thisClientPropertiesAssignmentStatement, + thisCredentialsProviderAssignmentStatement)) .build(); } @@ -287,27 +306,10 @@ private static List createClassAnnotations( AnnotationNode.builder() .setType(STATIC_TYPES.get("EnableConfigurationProperties")) .setDescription( - ArrayExpr.builder() - .setType(TypeNode.createArrayTypeOf(TypeNode.CLASS_OBJECT)) - .addExpr( - VariableExpr.builder() - .setVariable( - Variable.builder() - .setType(TypeNode.CLASS_OBJECT) - .setName("class") - .build()) - .setStaticReferenceType( - types.get(Utils.getServicePropertiesClassName(service))) - .build()) - .addExpr( - VariableExpr.builder() - .setVariable( - Variable.builder() - .setType(TypeNode.CLASS_OBJECT) - .setName("class") - .build()) - .setStaticReferenceType(types.get("GlobalProperties")) - .build()) + VariableExpr.builder() + .setVariable( + Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build()) + .setStaticReferenceType(types.get(Utils.getServicePropertiesClassName(service))) .build()) .build(); @@ -318,107 +320,6 @@ private static List createClassAnnotations( enableConfigurationPropertiesNode); } - private static MethodDefinition createCredentialsProviderBeanMethod( - Service service, - String className, - String methodName, - Map types, - Expr thisExpr) { - // @Bean - // @ConditionalOnMissingBean(name = "[serviceName]ServiceCredentials") - // public CredentialsProvider languageServiceCredentials() throws IOException { - // if (this.clientProperties.getCredentials().hasKey()) { - // return new DefaultCredentialsProvider(this.clientProperties); - // } - // return new DefaultCredentialsProvider(this.globalProperties); - // } - List bodyStatements = new ArrayList<>(); - Variable globalPropertiesVar = - Variable.builder() - .setName("globalProperties") - .setType(types.get("GlobalProperties")) - .build(); - Variable clientPropertiesVar = - Variable.builder() - .setName("clientProperties") - .setType(types.get(Utils.getServicePropertiesClassName(service))) - .build(); - - VariableExpr thisClientProperties = - VariableExpr.withVariable(clientPropertiesVar) - .toBuilder() - .setExprReferenceExpr(thisExpr) - .build(); - VariableExpr thisGlobalProperties = - VariableExpr.withVariable(globalPropertiesVar) - .toBuilder() - .setExprReferenceExpr(thisExpr) - .build(); - CastExpr clientCastExpr = - CastExpr.builder() - .setExpr( - NewObjectExpr.builder() - .setType(STATIC_TYPES.get("DefaultCredentialsProvider")) - .setArguments(thisClientProperties) - .build()) - .setType(STATIC_TYPES.get("CredentialsProvider")) - .build(); - CastExpr globalCredentialsCastExpr = - CastExpr.builder() - .setExpr( - NewObjectExpr.builder() - .setType(STATIC_TYPES.get("DefaultCredentialsProvider")) - .setArguments(thisGlobalProperties) - .build()) - .setType(STATIC_TYPES.get("CredentialsProvider")) - .build(); - ExprStatement clientCredentialsReturnExpr = - ExprStatement.withExpr(ReturnExpr.withExpr(clientCastExpr)); - Expr clientPropertiesGetCredentials = - MethodInvocationExpr.builder() - .setExprReferenceExpr(thisClientProperties) - .setMethodName("getCredentials") - .setReturnType(STATIC_TYPES.get("Credentials")) - .build(); - Expr clientPropertiesCredentialsHasKey = - MethodInvocationExpr.builder() - .setExprReferenceExpr(clientPropertiesGetCredentials) - .setMethodName("hasKey") - .setReturnType(TypeNode.BOOLEAN) - .build(); - Statement logClientCredentials = - LoggerUtils.createLoggerStatement( - ValueExpr.withValue( - StringObjectValue.withValue( - "Using credentials from " + service.name() + "-specific configuration")), - types); - IfStatement clientCredentialsIfStatement = - createIfStatement( - clientPropertiesCredentialsHasKey, - Arrays.asList(logClientCredentials, clientCredentialsReturnExpr), - null); - bodyStatements.add(clientCredentialsIfStatement); - bodyStatements.add( - LoggerUtils.createLoggerStatement( - ValueExpr.withValue( - StringObjectValue.withValue("Using credentials from global configuration")), - types)); - return MethodDefinition.builder() - .setName(methodName) - .setHeaderCommentStatements( - SpringAutoconfigCommentComposer.createCredentialsProviderBeanComment()) - .setScope(ScopeNode.PUBLIC) - .setReturnType(STATIC_TYPES.get("CredentialsProvider")) - .setAnnotations( - Arrays.asList( - AnnotationNode.withType(STATIC_TYPES.get("Bean")), - AnnotationNode.withType(STATIC_TYPES.get("ConditionalOnMissingBean")))) - .setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(IOException.class))) - .setBody(bodyStatements) - .setReturnExpr(globalCredentialsCastExpr) - .build(); - } - private static MethodDefinition createTransportChannelProviderBeanMethod( String methodName, Map types) { @@ -532,10 +433,7 @@ private static Statement setRetrySettingsForMethod( private static MethodDefinition createClientBeanMethod( Service service, - String className, - String credentialsProviderName, String transportChannelProviderName, - String clientName, Map types, GapicServiceConfig gapicServiceConfig, Expr thisExpr, @@ -574,11 +472,20 @@ private static MethodDefinition createClientBeanMethod( .setMethodName("newBuilder") .build(); + VariableExpr thisCredentialsProvider = + VariableExpr.withVariable( + Variable.builder() + .setName("credentialsProvider") + .setType(STATIC_TYPES.get("CredentialsProvider")) + .build()) + .toBuilder() + .setExprReferenceExpr(thisExpr) + .build(); settingsBuilderExpr = MethodInvocationExpr.builder() .setExprReferenceExpr(settingsBuilderExpr) .setMethodName("setCredentialsProvider") - .setArguments(credentialsProviderVariableExpr) + .setArguments(thisCredentialsProvider) .build(); // .setTransportChannelProvider(defaultTransportChannelProvider) settingsBuilderExpr = @@ -866,16 +773,6 @@ private static MethodDefinition createClientBeanMethod( .build(); List argumentsVariableExprs = Arrays.asList( - credentialsProviderVariableExpr - .toBuilder() - .setIsDecl(true) - .setAnnotations( - Arrays.asList( - AnnotationNode.builder() - .setType(STATIC_TYPES.get("Qualifier")) - .setDescription(credentialsProviderName) - .build())) - .build(), transportChannelProviderVariableExpr .toBuilder() .setIsDecl(true) diff --git a/src/main/java/com/google/api/generator/spring/utils/GlobalPropertiesUtils.java b/src/main/java/com/google/api/generator/spring/utils/GlobalPropertiesUtils.java index 7ddde96bed..44e9ae045d 100644 --- a/src/main/java/com/google/api/generator/spring/utils/GlobalPropertiesUtils.java +++ b/src/main/java/com/google/api/generator/spring/utils/GlobalPropertiesUtils.java @@ -14,6 +14,9 @@ package com.google.api.generator.spring.utils; +import com.google.api.generator.engine.ast.AnnotationNode; +import com.google.api.generator.engine.ast.AssignmentExpr; +import com.google.api.generator.engine.ast.Expr; import com.google.api.generator.engine.ast.ExprStatement; import com.google.api.generator.engine.ast.ScopeNode; import com.google.api.generator.engine.ast.Statement; @@ -21,6 +24,8 @@ import com.google.api.generator.engine.ast.VaporReference; import com.google.api.generator.engine.ast.Variable; import com.google.api.generator.engine.ast.VariableExpr; +import java.util.Collections; +import java.util.List; import java.util.Map; public class GlobalPropertiesUtils { @@ -52,4 +57,44 @@ public static Statement getGlobalPropertiesDeclaration(Map typ .setIsDecl(true) .build()); } + + public static Statement getPrivateVarDeclaration(String varName, TypeNode type, boolean isFinal) { + Variable variable = Variable.builder().setName(varName).setType(type).build(); + return ExprStatement.withExpr( + VariableExpr.builder() + .setVariable(variable) + .setScope(ScopeNode.PRIVATE) + .setIsFinal(true) + .setIsDecl(true) + .build()); + } + + public static ExprStatement createMemberVarStatement( + String varName, + TypeNode varType, + boolean isFinal, + Expr defaultVal, + List annotationNodes) { + Variable memberVar = Variable.builder().setName(varName).setType(varType).build(); + VariableExpr memberVarExpr = + VariableExpr.builder() + .setVariable(memberVar) + .setScope(ScopeNode.PRIVATE) + .setAnnotations(annotationNodes == null ? Collections.emptyList() : annotationNodes) + .setIsDecl(true) + .setIsFinal(isFinal) + .build(); + + if (defaultVal == null) { + return ExprStatement.withExpr(memberVarExpr); + } + AssignmentExpr assignmentExpr = + AssignmentExpr.builder() + .setVariableExpr(memberVarExpr.toBuilder().setIsDecl(true).build()) + .setValueExpr(defaultVal) + .build(); + ExprStatement memberVarStatement = ExprStatement.withExpr(assignmentExpr); + + return memberVarStatement; + } } diff --git a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden index ff75f66afb..970a69cf80 100644 --- a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden +++ b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden @@ -24,7 +24,6 @@ import com.google.api.gax.rpc.HeaderProvider; import com.google.api.gax.rpc.TransportChannelProvider; import com.google.cloud.spring.core.Credentials; import com.google.cloud.spring.core.DefaultCredentialsProvider; -import com.google.cloud.spring.global.GlobalProperties; import com.google.showcase.v1beta1.EchoClient; import com.google.showcase.v1beta1.EchoSettings; import java.io.IOException; @@ -62,35 +61,24 @@ import org.threeten.bp.Duration; @ConditionalOnProperty( value = "com.google.showcase.v1beta1.spring.auto.echo.enabled", matchIfMissing = true) -@EnableConfigurationProperties({EchoSpringProperties.class, GlobalProperties.class}) +@EnableConfigurationProperties(EchoSpringProperties.class) public class EchoSpringAutoConfiguration { private final EchoSpringProperties clientProperties; - private final GlobalProperties globalProperties; + private final CredentialsProvider credentialsProvider; private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfiguration.class); protected EchoSpringAutoConfiguration( - EchoSpringProperties clientProperties, GlobalProperties globalProperties) { + EchoSpringProperties clientProperties, CredentialsProvider credentialsProvider) { this.clientProperties = clientProperties; - this.globalProperties = globalProperties; - } - - /** - * Obtains the default credentials provider. The used key will be obtained from Spring Boot - * configuration data files. - */ - @Bean - @ConditionalOnMissingBean - public CredentialsProvider echoCredentials() throws IOException { if (this.clientProperties.getCredentials().hasKey()) { if (LOGGER.isTraceEnabled()) { LOGGER.trace("Using credentials from Echo-specific configuration"); } - return ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties)); - } - if (LOGGER.isTraceEnabled()) { - LOGGER.trace("Using credentials from global configuration"); + this.credentialsProvider = + ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties)); + } else { + this.credentialsProvider = credentialsProvider; } - return ((CredentialsProvider) new DefaultCredentialsProvider(this.globalProperties)); } /** @@ -116,13 +104,12 @@ public class EchoSpringAutoConfiguration { @Bean @ConditionalOnMissingBean public EchoClient echoClient( - @Qualifier("echoCredentials") CredentialsProvider credentialsProvider, @Qualifier("defaultEchoTransportChannelProvider") TransportChannelProvider defaultTransportChannelProvider) throws IOException { EchoSettings.Builder clientSettingsBuilder = EchoSettings.newBuilder() - .setCredentialsProvider(credentialsProvider) + .setCredentialsProvider(this.credentialsProvider) .setTransportChannelProvider(defaultTransportChannelProvider) .setHeaderProvider(this.userAgentHeaderProvider()); if (this.clientProperties.getQuotaProjectId() != null) { diff --git a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpc.golden b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpc.golden index 1c34352e5a..3eb5ec2ee1 100644 --- a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpc.golden +++ b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpc.golden @@ -7,7 +7,6 @@ import com.google.api.gax.rpc.HeaderProvider; import com.google.api.gax.rpc.TransportChannelProvider; import com.google.cloud.spring.core.Credentials; import com.google.cloud.spring.core.DefaultCredentialsProvider; -import com.google.cloud.spring.global.GlobalProperties; import com.google.showcase.v1beta1.EchoClient; import com.google.showcase.v1beta1.EchoSettings; import java.io.IOException; @@ -42,35 +41,24 @@ import org.threeten.bp.Duration; @ConditionalOnProperty( value = "com.google.showcase.v1beta1.spring.auto.echo.enabled", matchIfMissing = true) -@EnableConfigurationProperties({EchoSpringProperties.class, GlobalProperties.class}) +@EnableConfigurationProperties(EchoSpringProperties.class) public class EchoSpringAutoConfiguration { private final EchoSpringProperties clientProperties; - private final GlobalProperties globalProperties; + private final CredentialsProvider credentialsProvider; private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfiguration.class); protected EchoSpringAutoConfiguration( - EchoSpringProperties clientProperties, GlobalProperties globalProperties) { + EchoSpringProperties clientProperties, CredentialsProvider credentialsProvider) { this.clientProperties = clientProperties; - this.globalProperties = globalProperties; - } - - /** - * Obtains the default credentials provider. The used key will be obtained from Spring Boot - * configuration data files. - */ - @Bean - @ConditionalOnMissingBean - public CredentialsProvider echoCredentials() throws IOException { if (this.clientProperties.getCredentials().hasKey()) { if (LOGGER.isTraceEnabled()) { LOGGER.trace("Using credentials from Echo-specific configuration"); } - return ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties)); - } - if (LOGGER.isTraceEnabled()) { - LOGGER.trace("Using credentials from global configuration"); + this.credentialsProvider = + ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties)); + } else { + this.credentialsProvider = credentialsProvider; } - return ((CredentialsProvider) new DefaultCredentialsProvider(this.globalProperties)); } /** @@ -96,13 +84,12 @@ public class EchoSpringAutoConfiguration { @Bean @ConditionalOnMissingBean public EchoClient echoClient( - @Qualifier("echoCredentials") CredentialsProvider credentialsProvider, @Qualifier("defaultEchoTransportChannelProvider") TransportChannelProvider defaultTransportChannelProvider) throws IOException { EchoSettings.Builder clientSettingsBuilder = EchoSettings.newBuilder() - .setCredentialsProvider(credentialsProvider) + .setCredentialsProvider(this.credentialsProvider) .setTransportChannelProvider(defaultTransportChannelProvider) .setHeaderProvider(this.userAgentHeaderProvider()); if (this.clientProperties.getQuotaProjectId() != null) { diff --git a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpcRest.golden b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpcRest.golden index 00fccb6b39..aad1a802c7 100644 --- a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpcRest.golden +++ b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpcRest.golden @@ -8,7 +8,6 @@ import com.google.api.gax.rpc.HeaderProvider; import com.google.api.gax.rpc.TransportChannelProvider; import com.google.cloud.spring.core.Credentials; import com.google.cloud.spring.core.DefaultCredentialsProvider; -import com.google.cloud.spring.global.GlobalProperties; import com.google.showcase.v1beta1.EchoClient; import com.google.showcase.v1beta1.EchoSettings; import java.io.IOException; @@ -43,35 +42,24 @@ import org.threeten.bp.Duration; @ConditionalOnProperty( value = "com.google.showcase.v1beta1.spring.auto.echo.enabled", matchIfMissing = true) -@EnableConfigurationProperties({EchoSpringProperties.class, GlobalProperties.class}) +@EnableConfigurationProperties(EchoSpringProperties.class) public class EchoSpringAutoConfiguration { private final EchoSpringProperties clientProperties; - private final GlobalProperties globalProperties; + private final CredentialsProvider credentialsProvider; private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfiguration.class); protected EchoSpringAutoConfiguration( - EchoSpringProperties clientProperties, GlobalProperties globalProperties) { + EchoSpringProperties clientProperties, CredentialsProvider credentialsProvider) { this.clientProperties = clientProperties; - this.globalProperties = globalProperties; - } - - /** - * Obtains the default credentials provider. The used key will be obtained from Spring Boot - * configuration data files. - */ - @Bean - @ConditionalOnMissingBean - public CredentialsProvider echoCredentials() throws IOException { if (this.clientProperties.getCredentials().hasKey()) { if (LOGGER.isTraceEnabled()) { LOGGER.trace("Using credentials from Echo-specific configuration"); } - return ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties)); - } - if (LOGGER.isTraceEnabled()) { - LOGGER.trace("Using credentials from global configuration"); + this.credentialsProvider = + ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties)); + } else { + this.credentialsProvider = credentialsProvider; } - return ((CredentialsProvider) new DefaultCredentialsProvider(this.globalProperties)); } /** @@ -97,13 +85,12 @@ public class EchoSpringAutoConfiguration { @Bean @ConditionalOnMissingBean public EchoClient echoClient( - @Qualifier("echoCredentials") CredentialsProvider credentialsProvider, @Qualifier("defaultEchoTransportChannelProvider") TransportChannelProvider defaultTransportChannelProvider) throws IOException { EchoSettings.Builder clientSettingsBuilder = EchoSettings.newBuilder() - .setCredentialsProvider(credentialsProvider) + .setCredentialsProvider(this.credentialsProvider) .setTransportChannelProvider(defaultTransportChannelProvider) .setHeaderProvider(this.userAgentHeaderProvider()); if (this.clientProperties.getQuotaProjectId() != null) { From 2f1aefa794334cb22163a6339e69d45957013f4b Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Fri, 9 Dec 2022 20:56:11 +0000 Subject: [PATCH 3/5] cleanup util method. --- .../SpringAutoConfigClassComposer.java | 7 ++- .../SpringPropertiesClassComposer.java | 43 ++++-------------- ...ropertiesUtils.java => ComposerUtils.java} | 44 +------------------ 3 files changed, 13 insertions(+), 81 deletions(-) rename src/main/java/com/google/api/generator/spring/utils/{GlobalPropertiesUtils.java => ComposerUtils.java} (57%) diff --git a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java index b64c53a2b3..9cd14e3a7f 100644 --- a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java @@ -53,7 +53,7 @@ import com.google.api.generator.gapic.model.Service; import com.google.api.generator.gapic.model.Transport; import com.google.api.generator.spring.composer.comment.SpringAutoconfigCommentComposer; -import com.google.api.generator.spring.utils.GlobalPropertiesUtils; +import com.google.api.generator.spring.utils.ComposerUtils; import com.google.api.generator.spring.utils.LoggerUtils; import com.google.api.generator.spring.utils.Utils; import com.google.cloud.spring.core.Credentials; @@ -143,14 +143,14 @@ private static List createMemberVariables( // private final LanguageProperties clientProperties; ExprStatement clientPropertiesStatement = - GlobalPropertiesUtils.createMemberVarStatement( + ComposerUtils.createMemberVarStatement( "clientProperties", types.get(Utils.getServicePropertiesClassName(service)), true, null, null); Statement credentialProvider = - GlobalPropertiesUtils.createMemberVarStatement( + ComposerUtils.createMemberVarStatement( "credentialsProvider", STATIC_TYPES.get("CredentialsProvider"), true, null, null); Statement loggerStatement = @@ -957,7 +957,6 @@ private static Map createDynamicTypes(Service service, String typeMap.put("ServiceClient", serviceClient); typeMap.put("ServiceSettings", serviceSettings); typeMap.put("ServiceSettingsBuilder", serviceSettingsBuilder); - typeMap.put("GlobalProperties", GlobalPropertiesUtils.getGlobalPropertiesType()); return typeMap; } diff --git a/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java b/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java index ea072bc9e1..679a0e16d9 100644 --- a/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java @@ -43,6 +43,7 @@ import com.google.api.generator.gapic.model.Service; import com.google.api.generator.gapic.model.Transport; import com.google.api.generator.spring.composer.comment.SpringPropertiesCommentComposer; +import com.google.api.generator.spring.utils.ComposerUtils; import com.google.api.generator.spring.utils.Utils; import com.google.cloud.spring.core.Credentials; import com.google.cloud.spring.core.CredentialsSupplier; @@ -106,35 +107,6 @@ public GapicClass generate(GapicContext context, Service service) { // return null; } - private static ExprStatement createMemberVarStatement( - String varName, - TypeNode varType, - boolean isFinal, - Expr defaultVal, - List annotationNodes) { - Variable memberVar = Variable.builder().setName(varName).setType(varType).build(); - VariableExpr memberVarExpr = - VariableExpr.builder() - .setVariable(memberVar) - .setScope(ScopeNode.PRIVATE) - .setAnnotations(annotationNodes == null ? Collections.emptyList() : annotationNodes) - .setIsDecl(true) - .setIsFinal(isFinal) - .build(); - - if (defaultVal == null) { - return ExprStatement.withExpr(memberVarExpr); - } - AssignmentExpr assignmentExpr = - AssignmentExpr.builder() - .setVariableExpr(memberVarExpr.toBuilder().setIsDecl(true).build()) - .setValueExpr(defaultVal) - .build(); - ExprStatement memberVarStatement = ExprStatement.withExpr(assignmentExpr); - - return memberVarStatement; - } - private static List createMemberVariables( Service service, String packageName, @@ -161,7 +133,7 @@ private static List createMemberVariables( List credentialsAnnotations = Arrays.asList(AnnotationNode.withType(STATIC_TYPES.get("NestedConfigurationProperty"))); ExprStatement credentialsStatement = - createMemberVarStatement( + ComposerUtils.createMemberVarStatement( "credentials", STATIC_TYPES.get("Credentials"), true, @@ -170,15 +142,17 @@ private static List createMemberVariables( statements.add(credentialsStatement); // private String quotaProjectId; ExprStatement quotaProjectIdVarStatement = - createMemberVarStatement("quotaProjectId", TypeNode.STRING, false, null, null); + ComposerUtils.createMemberVarStatement( + "quotaProjectId", TypeNode.STRING, false, null, null); statements.add(quotaProjectIdVarStatement); // private Integer executorThreadCount; ExprStatement executorThreadCountVarStatement = - createMemberVarStatement("executorThreadCount", TypeNode.INT_OBJECT, false, null, null); + ComposerUtils.createMemberVarStatement( + "executorThreadCount", TypeNode.INT_OBJECT, false, null, null); statements.add(executorThreadCountVarStatement); if (hasRestOption) { ExprStatement useRestVarStatement = - createMemberVarStatement( + ComposerUtils.createMemberVarStatement( "useRest", TypeNode.BOOLEAN, false, @@ -207,7 +181,8 @@ private static List createMemberVariables( } String propertyName = Joiner.on("").join(methodAndPropertyName); ExprStatement retrySettingsStatement = - createMemberVarStatement(propertyName, propertyType, false, null, null); + ComposerUtils.createMemberVarStatement( + propertyName, propertyType, false, null, null); getterAndSetter.add(retrySettingsStatement); return getterAndSetter; }, diff --git a/src/main/java/com/google/api/generator/spring/utils/GlobalPropertiesUtils.java b/src/main/java/com/google/api/generator/spring/utils/ComposerUtils.java similarity index 57% rename from src/main/java/com/google/api/generator/spring/utils/GlobalPropertiesUtils.java rename to src/main/java/com/google/api/generator/spring/utils/ComposerUtils.java index 44e9ae045d..6bb3aa4a2b 100644 --- a/src/main/java/com/google/api/generator/spring/utils/GlobalPropertiesUtils.java +++ b/src/main/java/com/google/api/generator/spring/utils/ComposerUtils.java @@ -19,55 +19,13 @@ import com.google.api.generator.engine.ast.Expr; import com.google.api.generator.engine.ast.ExprStatement; import com.google.api.generator.engine.ast.ScopeNode; -import com.google.api.generator.engine.ast.Statement; import com.google.api.generator.engine.ast.TypeNode; -import com.google.api.generator.engine.ast.VaporReference; import com.google.api.generator.engine.ast.Variable; import com.google.api.generator.engine.ast.VariableExpr; import java.util.Collections; import java.util.List; -import java.util.Map; -public class GlobalPropertiesUtils { - public static final String GLOBAL_PROPERTIES_CLAZZ_NAME = "GlobalProperties"; - public static final String GLOBAL_PROPERTIES_PAKKAGE_NAME = "com.google.cloud.spring.global"; - - public static TypeNode getGlobalPropertiesType() { - TypeNode loggerType = - TypeNode.withReference( - VaporReference.builder() - .setName(GLOBAL_PROPERTIES_CLAZZ_NAME) - .setPakkage(GLOBAL_PROPERTIES_PAKKAGE_NAME) - .build()); - return loggerType; - } - - public static Statement getGlobalPropertiesDeclaration(Map types) { - Variable globalPropertiesVar = - Variable.builder() - .setName("globalProperties") - .setType(types.get("GlobalProperties")) - .build(); - return ExprStatement.withExpr( - VariableExpr.builder() - .setVariable(globalPropertiesVar) - .setScope(ScopeNode.PRIVATE) - .setIsStatic(false) - .setIsFinal(true) - .setIsDecl(true) - .build()); - } - - public static Statement getPrivateVarDeclaration(String varName, TypeNode type, boolean isFinal) { - Variable variable = Variable.builder().setName(varName).setType(type).build(); - return ExprStatement.withExpr( - VariableExpr.builder() - .setVariable(variable) - .setScope(ScopeNode.PRIVATE) - .setIsFinal(true) - .setIsDecl(true) - .build()); - } +public class ComposerUtils { public static ExprStatement createMemberVarStatement( String varName, From 7b78d522bcb68fbe44e831a7c34019f4871b48d7 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Mon, 12 Dec 2022 15:28:46 +0000 Subject: [PATCH 4/5] fix: add throws exception for constructor. --- .../spring/composer/SpringAutoConfigClassComposer.java | 1 + .../composer/goldens/EchoSpringAutoConfigurationFull.golden | 3 ++- .../composer/goldens/EchoSpringAutoConfigurationGrpc.golden | 3 ++- .../goldens/EchoSpringAutoConfigurationGrpcRest.golden | 3 ++- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java index 9cd14e3a7f..54a0e1ceaa 100644 --- a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java @@ -243,6 +243,7 @@ private static MethodDefinition createConstructor( return MethodDefinition.constructorBuilder() .setScope(ScopeNode.PROTECTED) + .setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(IOException.class))) .setReturnType(types.get(className)) .setArguments( Arrays.asList( diff --git a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden index 970a69cf80..b1a79ad946 100644 --- a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden +++ b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden @@ -68,7 +68,8 @@ public class EchoSpringAutoConfiguration { private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfiguration.class); protected EchoSpringAutoConfiguration( - EchoSpringProperties clientProperties, CredentialsProvider credentialsProvider) { + EchoSpringProperties clientProperties, CredentialsProvider credentialsProvider) + throws IOException { this.clientProperties = clientProperties; if (this.clientProperties.getCredentials().hasKey()) { if (LOGGER.isTraceEnabled()) { diff --git a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpc.golden b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpc.golden index 3eb5ec2ee1..7185ea5ec8 100644 --- a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpc.golden +++ b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpc.golden @@ -48,7 +48,8 @@ public class EchoSpringAutoConfiguration { private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfiguration.class); protected EchoSpringAutoConfiguration( - EchoSpringProperties clientProperties, CredentialsProvider credentialsProvider) { + EchoSpringProperties clientProperties, CredentialsProvider credentialsProvider) + throws IOException { this.clientProperties = clientProperties; if (this.clientProperties.getCredentials().hasKey()) { if (LOGGER.isTraceEnabled()) { diff --git a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpcRest.golden b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpcRest.golden index aad1a802c7..5f8739c9d4 100644 --- a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpcRest.golden +++ b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationGrpcRest.golden @@ -49,7 +49,8 @@ public class EchoSpringAutoConfiguration { private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfiguration.class); protected EchoSpringAutoConfiguration( - EchoSpringProperties clientProperties, CredentialsProvider credentialsProvider) { + EchoSpringProperties clientProperties, CredentialsProvider credentialsProvider) + throws IOException { this.clientProperties = clientProperties; if (this.clientProperties.getCredentials().hasKey()) { if (LOGGER.isTraceEnabled()) { From fc3f7ff19ee75fc15046db9b01405f9f8ee11e37 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Mon, 12 Dec 2022 15:39:49 +0000 Subject: [PATCH 5/5] fix code smell. --- .../com/google/api/generator/spring/utils/ComposerUtils.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/google/api/generator/spring/utils/ComposerUtils.java b/src/main/java/com/google/api/generator/spring/utils/ComposerUtils.java index 6bb3aa4a2b..bf0fb07f55 100644 --- a/src/main/java/com/google/api/generator/spring/utils/ComposerUtils.java +++ b/src/main/java/com/google/api/generator/spring/utils/ComposerUtils.java @@ -51,8 +51,7 @@ public static ExprStatement createMemberVarStatement( .setVariableExpr(memberVarExpr.toBuilder().setIsDecl(true).build()) .setValueExpr(defaultVal) .build(); - ExprStatement memberVarStatement = ExprStatement.withExpr(assignmentExpr); - return memberVarStatement; + return ExprStatement.withExpr(assignmentExpr); } }