|
| 1 | +package org.springframework.boot.actuate.autoconfigure.web; |
| 2 | + |
| 3 | +import com.squareup.javapoet.ClassName; |
| 4 | +import com.squareup.javapoet.CodeBlock.Builder; |
| 5 | + |
| 6 | +import org.springframework.aot.boot.actuate.web.AotManagementContextFactory; |
| 7 | +import org.springframework.aot.context.bootstrap.generator.ApplicationContextAotProcessor; |
| 8 | +import org.springframework.aot.context.bootstrap.generator.bean.BeanRegistrationWriter; |
| 9 | +import org.springframework.aot.context.bootstrap.generator.bean.DefaultBeanRegistrationWriter; |
| 10 | +import org.springframework.aot.context.bootstrap.generator.bean.descriptor.BeanInstanceDescriptor; |
| 11 | +import org.springframework.aot.context.bootstrap.generator.infrastructure.BootstrapWriterContext; |
| 12 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 13 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 14 | +import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextSuppliers; |
| 15 | +import org.springframework.context.support.GenericApplicationContext; |
| 16 | + |
| 17 | +/** |
| 18 | + * A {@link BeanRegistrationWriter} that generates the child context that a |
| 19 | + * {@link ManagementContextFactory} handles. |
| 20 | + * |
| 21 | + * @author Stephane Nicoll |
| 22 | + */ |
| 23 | +class ManagementContextBeanRegistrationWriter implements BeanRegistrationWriter { |
| 24 | + |
| 25 | + private static final String MANAGEMENT_BOOSTRAP_CLASS_NAME = "ManagementContextBoostrapInitializer"; |
| 26 | + |
| 27 | + private final GenericApplicationContext parent; |
| 28 | + |
| 29 | + private final String beanName; |
| 30 | + |
| 31 | + private final boolean reactive; |
| 32 | + |
| 33 | + private final BeanDefinition beanDefinition; |
| 34 | + |
| 35 | + private final BeanInstanceDescriptor beanInstanceDescriptor; |
| 36 | + |
| 37 | + ManagementContextBeanRegistrationWriter(GenericApplicationContext parent, String beanName, boolean reactive) { |
| 38 | + this.parent = parent; |
| 39 | + this.beanName = beanName; |
| 40 | + this.reactive = reactive; |
| 41 | + this.beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(AotManagementContextFactory.class).getBeanDefinition(); |
| 42 | + this.beanInstanceDescriptor = BeanInstanceDescriptor.of(beanDefinition.getResolvableType()).build(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void writeBeanRegistration(BootstrapWriterContext context, Builder code) { |
| 47 | + ClassName managementBootstrapType = processManagementContext(context); |
| 48 | + new DefaultBeanRegistrationWriter(this.beanName, this.beanDefinition, this.beanInstanceDescriptor) { |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void writeInstanceSupplier(Builder code) { |
| 52 | + code.add("() -> new $T(", AotManagementContextFactory.class); |
| 53 | + code.add("() -> new $T(), $L", managementBootstrapType, reactive); |
| 54 | + code.add(")"); |
| 55 | + } |
| 56 | + }.writeBeanRegistration(context, code); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Process the dedicated management context using the specified {@code context}. |
| 61 | + * @param context the writer context to use |
| 62 | + */ |
| 63 | + private ClassName processManagementContext(BootstrapWriterContext context) { |
| 64 | + GenericApplicationContext managementContext = createManagementContext(); |
| 65 | + ApplicationContextAotProcessor processor = new ApplicationContextAotProcessor(this.parent.getClassLoader()); |
| 66 | + BootstrapWriterContext writerContext = context.fork(MANAGEMENT_BOOSTRAP_CLASS_NAME); |
| 67 | + processor.process(managementContext, writerContext); |
| 68 | + return writerContext.getMainBootstrapClass().getClassName(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public BeanInstanceDescriptor getBeanInstanceDescriptor() { |
| 73 | + return this.beanInstanceDescriptor; |
| 74 | + } |
| 75 | + |
| 76 | + private GenericApplicationContext createManagementContext() { |
| 77 | + return this.reactive ? ManagementContextSuppliers.Reactive.createManagementContext(this.parent) |
| 78 | + : ManagementContextSuppliers.Servlet.createManagementContext(this.parent); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments