Search in sources :

Example 1 with StartupTask

use of io.quarkus.runtime.StartupTask in project quarkus by quarkusio.

the class RuntimeConfigSetupBuildStep method setupRuntimeConfig.

/**
 * Generates a StartupTask that sets up the final runtime configuration and thus runs before any StartupTask that uses
 * runtime configuration.
 * If there are recorders that produce a ConfigSourceProvider, these objects are used to set up the final runtime
 * configuration
 */
@BuildStep
@Consume(BootstrapConfigSetupCompleteBuildItem.class)
@Produce(RuntimeConfigSetupCompleteBuildItem.class)
void setupRuntimeConfig(List<RunTimeConfigurationSourceValueBuildItem> runTimeConfigurationSourceValues, BuildProducer<GeneratedClassBuildItem> generatedClass, BuildProducer<MainBytecodeRecorderBuildItem> mainBytecodeRecorder) {
    ClassOutput classOutput = new GeneratedClassGizmoAdaptor(generatedClass, true);
    try (ClassCreator clazz = ClassCreator.builder().classOutput(classOutput).className(RUNTIME_CONFIG_STARTUP_TASK_CLASS_NAME).interfaces(StartupTask.class).build()) {
        try (MethodCreator method = clazz.getMethodCreator("deploy", void.class, StartupContext.class)) {
            method.invokeVirtualMethod(ofMethod(StartupContext.class, "setCurrentBuildStepName", void.class, String.class), method.getMethodParam(0), method.load("RuntimeConfigSetupBuildStep.setupRuntimeConfig"));
            ResultHandle config = method.readStaticField(C_INSTANCE);
            if (runTimeConfigurationSourceValues.isEmpty()) {
                method.invokeVirtualMethod(RunTimeConfigurationGenerator.C_READ_CONFIG, config, method.invokeStaticMethod(ofMethod(Collections.class, "emptyList", List.class)));
            } else {
                ResultHandle startupContext = method.getMethodParam(0);
                ResultHandle configSourcesProvidersList = method.newInstance(ofConstructor(ArrayList.class, int.class), method.load(runTimeConfigurationSourceValues.size()));
                for (RunTimeConfigurationSourceValueBuildItem runTimeConfigurationSourceValue : runTimeConfigurationSourceValues) {
                    RuntimeValue<ConfigSourceProvider> runtimeValue = runTimeConfigurationSourceValue.getConfigSourcesValue();
                    if (runtimeValue instanceof BytecodeRecorderImpl.ReturnedProxy) {
                        String proxyId = ((BytecodeRecorderImpl.ReturnedProxy) runtimeValue).__returned$proxy$key();
                        ResultHandle value = method.invokeVirtualMethod(ofMethod(StartupContext.class, "getValue", Object.class, String.class), startupContext, method.load(proxyId));
                        ResultHandle configSourceProvider = method.invokeVirtualMethod(ofMethod(RuntimeValue.class, "getValue", Object.class), method.checkCast(value, RuntimeValue.class));
                        method.invokeVirtualMethod(MethodDescriptor.ofMethod(ArrayList.class, "add", boolean.class, Object.class), configSourcesProvidersList, method.checkCast(configSourceProvider, ConfigSourceProvider.class));
                    } else {
                        log.warn("RuntimeValue " + runtimeValue + " was not produced by a recorder and it will thus be ignored");
                    }
                }
                method.invokeVirtualMethod(RunTimeConfigurationGenerator.C_READ_CONFIG, config, configSourcesProvidersList);
            }
            method.returnValue(null);
        }
    }
    mainBytecodeRecorder.produce(new MainBytecodeRecorderBuildItem(RUNTIME_CONFIG_STARTUP_TASK_CLASS_NAME));
}
Also used : StartupContext(io.quarkus.runtime.StartupContext) ArrayList(java.util.ArrayList) GeneratedClassGizmoAdaptor(io.quarkus.deployment.GeneratedClassGizmoAdaptor) ClassCreator(io.quarkus.gizmo.ClassCreator) StartupTask(io.quarkus.runtime.StartupTask) ConfigSourceProvider(org.eclipse.microprofile.config.spi.ConfigSourceProvider) MethodCreator(io.quarkus.gizmo.MethodCreator) ClassOutput(io.quarkus.gizmo.ClassOutput) ResultHandle(io.quarkus.gizmo.ResultHandle) RuntimeValue(io.quarkus.runtime.RuntimeValue) MainBytecodeRecorderBuildItem(io.quarkus.deployment.builditem.MainBytecodeRecorderBuildItem) RunTimeConfigurationSourceValueBuildItem(io.quarkus.deployment.builditem.RunTimeConfigurationSourceValueBuildItem) Consume(io.quarkus.deployment.annotations.Consume) Produce(io.quarkus.deployment.annotations.Produce) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 2 with StartupTask

use of io.quarkus.runtime.StartupTask in project quarkus by quarkusio.

the class BootstrapConfigSetupBuildStep method setupBootstrapConfig.

/**
 * Generates a StartupTask that creates a instance of the generated Config class
 * It runs before any StartupTask that uses configuration
 */
@BuildStep
@Produce(BootstrapConfigSetupCompleteBuildItem.class)
void setupBootstrapConfig(BuildProducer<GeneratedClassBuildItem> generatedClass, BuildProducer<MainBytecodeRecorderBuildItem> mainBytecodeRecorder) {
    ClassOutput classOutput = new GeneratedClassGizmoAdaptor(generatedClass, true);
    try (ClassCreator clazz = ClassCreator.builder().classOutput(classOutput).className(BOOTSTRAP_CONFIG_STARTUP_TASK_CLASS_NAME).interfaces(StartupTask.class).build()) {
        try (MethodCreator deploy = clazz.getMethodCreator("deploy", void.class, StartupContext.class)) {
            deploy.invokeVirtualMethod(ofMethod(StartupContext.class, "setCurrentBuildStepName", void.class, String.class), deploy.getMethodParam(0), deploy.load("BootstrapConfigSetupBuildStep.setupBootstrapConfig"));
            deploy.invokeStaticMethod(C_CREATE_BOOTSTRAP_CONFIG);
            deploy.returnValue(null);
        }
    }
    mainBytecodeRecorder.produce(new MainBytecodeRecorderBuildItem(BOOTSTRAP_CONFIG_STARTUP_TASK_CLASS_NAME));
}
Also used : StartupContext(io.quarkus.runtime.StartupContext) MethodCreator(io.quarkus.gizmo.MethodCreator) ClassOutput(io.quarkus.gizmo.ClassOutput) GeneratedClassGizmoAdaptor(io.quarkus.deployment.GeneratedClassGizmoAdaptor) ClassCreator(io.quarkus.gizmo.ClassCreator) MainBytecodeRecorderBuildItem(io.quarkus.deployment.builditem.MainBytecodeRecorderBuildItem) StartupTask(io.quarkus.runtime.StartupTask) Produce(io.quarkus.deployment.annotations.Produce) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 3 with StartupTask

use of io.quarkus.runtime.StartupTask in project quarkus by quarkusio.

the class BytecodeRecorderTestCase method runTest.

void runTest(Consumer<BytecodeRecorderImpl> generator, Object... expected) throws Exception {
    TestRecorder.RESULT.clear();
    TestClassLoader tcl = new TestClassLoader(getClass().getClassLoader());
    BytecodeRecorderImpl recorder = new BytecodeRecorderImpl(tcl, false, TEST_CLASS);
    generator.accept(recorder);
    recorder.writeBytecode(new TestClassOutput(tcl));
    StartupTask task = (StartupTask) tcl.loadClass(TEST_CLASS).getDeclaredConstructor().newInstance();
    task.deploy(new StartupContext());
    assertEquals(expected.length, TestRecorder.RESULT.size());
    for (Object i : expected) {
        if (i.getClass().isArray()) {
            if (i instanceof int[]) {
                assertArrayEquals((int[]) i, (int[]) TestRecorder.RESULT.poll());
            } else if (i instanceof double[]) {
                assertArrayEquals((double[]) i, (double[]) TestRecorder.RESULT.poll(), 0);
            } else if (i instanceof Object[]) {
                assertArrayEquals((Object[]) i, (Object[]) TestRecorder.RESULT.poll());
            } else {
                throw new RuntimeException("not implemented");
            }
        } else {
            assertEquals(i, TestRecorder.RESULT.poll());
        }
    }
}
Also used : StartupContext(io.quarkus.runtime.StartupContext) TestClassLoader(io.quarkus.deployment.TestClassLoader) StartupTask(io.quarkus.runtime.StartupTask)

Aggregations

StartupContext (io.quarkus.runtime.StartupContext)3 StartupTask (io.quarkus.runtime.StartupTask)3 GeneratedClassGizmoAdaptor (io.quarkus.deployment.GeneratedClassGizmoAdaptor)2 BuildStep (io.quarkus.deployment.annotations.BuildStep)2 Produce (io.quarkus.deployment.annotations.Produce)2 MainBytecodeRecorderBuildItem (io.quarkus.deployment.builditem.MainBytecodeRecorderBuildItem)2 ClassCreator (io.quarkus.gizmo.ClassCreator)2 ClassOutput (io.quarkus.gizmo.ClassOutput)2 MethodCreator (io.quarkus.gizmo.MethodCreator)2 TestClassLoader (io.quarkus.deployment.TestClassLoader)1 Consume (io.quarkus.deployment.annotations.Consume)1 RunTimeConfigurationSourceValueBuildItem (io.quarkus.deployment.builditem.RunTimeConfigurationSourceValueBuildItem)1 ResultHandle (io.quarkus.gizmo.ResultHandle)1 RuntimeValue (io.quarkus.runtime.RuntimeValue)1 ArrayList (java.util.ArrayList)1 ConfigSourceProvider (org.eclipse.microprofile.config.spi.ConfigSourceProvider)1