From 0292f4c83d746e99dc35b1c608c34fe3fd47aac9 Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Mon, 11 Jul 2022 18:14:50 +0200 Subject: [PATCH] Get Specification beans from LoadBalancerClientFactory. Sanitise generated class names. Fix generated statements. --- .../context/named/NamedContextFactory.java | 4 ++ .../aot/ChildContextMappings.java | 66 ------------------- .../LoadBalancerChildContextInitializer.java | 16 +++-- 3 files changed, 13 insertions(+), 73 deletions(-) delete mode 100644 spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/aot/ChildContextMappings.java diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/named/NamedContextFactory.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/named/NamedContextFactory.java index c9936fed..512e8495 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/named/NamedContextFactory.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/named/NamedContextFactory.java @@ -215,6 +215,10 @@ public abstract class NamedContextFactory getConfigurations() { + return configurations; + } + /** * Specification with name and configuration. */ diff --git a/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/aot/ChildContextMappings.java b/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/aot/ChildContextMappings.java deleted file mode 100644 index 1d40cd76..00000000 --- a/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/aot/ChildContextMappings.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2012-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.loadbalancer.aot; - -import java.util.ArrayList; -import java.util.List; - -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.beans.factory.config.ConstructorArgumentValues; -import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClientSpecification; -import org.springframework.context.ConfigurableApplicationContext; - -/** - * @author Olga Maciaszek-Sharma - */ -final class ChildContextMappings { - - private ChildContextMappings() { - throw new IllegalStateException("Can't instantiate a utility class"); - } - - static List getChildContexts(ConfigurableApplicationContext applicationContext) { - ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory(); - String[] beanNames = applicationContext.getBeanNamesForType(LoadBalancerClientSpecification.class); - List contexts = new ArrayList<>(); - for (String beanName : beanNames) { - contexts.add(process(beanFactory.getMergedBeanDefinition(beanName))); - } - return contexts; - } - - private static Entry process(BeanDefinition beanDefinition) { - ConstructorArgumentValues constructorArguments = beanDefinition.getConstructorArgumentValues(); - ConstructorArgumentValues.ValueHolder nameValueHolder = constructorArguments.getIndexedArgumentValue(0, null); - if (nameValueHolder != null) { - String name = (String) nameValueHolder.getValue(); - ConstructorArgumentValues.ValueHolder configurationsValueHolder = constructorArguments - .getIndexedArgumentValue(1, null); - if (configurationsValueHolder != null) { - String[] configurations = (String[]) configurationsValueHolder.getValue(); - return new Entry(name, configurations); - } - } - throw new IllegalArgumentException("Invalid bean definition " + beanDefinition); - } - - record Entry(String name, String[] configurations) { - - } - -} diff --git a/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/aot/LoadBalancerChildContextInitializer.java b/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/aot/LoadBalancerChildContextInitializer.java index 3520a16c..56371add 100644 --- a/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/aot/LoadBalancerChildContextInitializer.java +++ b/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/aot/LoadBalancerChildContextInitializer.java @@ -17,7 +17,6 @@ package org.springframework.cloud.loadbalancer.aot; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -88,14 +87,15 @@ public class LoadBalancerChildContextInitializer && registeredBean.getBeanFactory().equals(applicationBeanFactory))) { return null; } - List contextsMap = ChildContextMappings.getChildContexts(context); - Set childContextAotContributions = contextsMap.stream() + // TODO: add context ids from properties + Set contextIds = loadBalancerClientFactory.getConfigurations().keySet(); + Set childContextAotContributions = contextIds.stream() .map(this::buildChildContext).collect(Collectors.toSet()); return new AotContribution(childContextAotContributions); } - private ConfigurableApplicationContext buildChildContext(ChildContextMappings.Entry entry) { - ConfigurableApplicationContext childContext = loadBalancerClientFactory.buildContext(entry.name()); + private ConfigurableApplicationContext buildChildContext(String contextId) { + ConfigurableApplicationContext childContext = loadBalancerClientFactory.buildContext(contextId); registerBeans(childContext); return childContext; } @@ -130,7 +130,9 @@ public class LoadBalancerChildContextInitializer public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) { ApplicationContextAotGenerator contextAotGenerator = new ApplicationContextAotGenerator(); Map generatedInitializerClassNames = childContexts.stream().map(childContext -> { - GenerationContext childGenerationContext = generationContext.withName(childContext.getDisplayName()); + String name = childContext.getDisplayName(); + name = name.replaceAll("[-]", "_"); + GenerationContext childGenerationContext = generationContext.withName(name); ClassName initializerClassName = contextAotGenerator.generateApplicationContext(childContext, childGenerationContext); return Map.entry(childContext.getDisplayName(), initializerClassName); @@ -143,7 +145,7 @@ public class LoadBalancerChildContextInitializer builder.addParameter(LoadBalancerChildContextInitializer.class, "instance"); builder.returns(LoadBalancerChildContextInitializer.class); builder.addStatement( - "Map applicationContextInitializer> initializers = new HashMap();"); + "Map> initializers = new HashMap()"); generatedInitializerClassNames.keySet() .forEach(contextId -> builder.addStatement("initializers.put($S, new $L())", contextId, generatedInitializerClassNames.get(contextId)));