Search in sources :

Example 1 with CodeGenData

use of io.quarkus.deployment.codegen.CodeGenData in project quarkus by quarkusio.

the class CodeGenerator method init.

public static List<CodeGenData> init(ClassLoader deploymentClassLoader, PathCollection sourceParentDirs, Path generatedSourcesDir, Path buildDir, Consumer<Path> sourceRegistrar) throws CodeGenException {
    return callWithClassloader(deploymentClassLoader, () -> {
        List<CodeGenData> result = new ArrayList<>();
        Class<? extends CodeGenProvider> codeGenProviderClass;
        try {
            // noinspection unchecked
            codeGenProviderClass = (Class<? extends CodeGenProvider>) deploymentClassLoader.loadClass(CodeGenProvider.class.getName());
        } catch (ClassNotFoundException e) {
            throw new CodeGenException("Failed to load CodeGenProvider class from deployment classloader", e);
        }
        for (CodeGenProvider provider : ServiceLoader.load(codeGenProviderClass, deploymentClassLoader)) {
            Path outputDir = codeGenOutDir(generatedSourcesDir, provider, sourceRegistrar);
            for (Path sourceParentDir : sourceParentDirs) {
                result.add(new CodeGenData(provider, outputDir, sourceParentDir.resolve(provider.inputDirectory()), buildDir));
            }
        }
        return result;
    });
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) CodeGenException(io.quarkus.bootstrap.prebuild.CodeGenException) CodeGenData(io.quarkus.deployment.codegen.CodeGenData)

Example 2 with CodeGenData

use of io.quarkus.deployment.codegen.CodeGenData in project quarkus by quarkusio.

the class IsolatedDevModeMain method accept.

// the main entry point, but loaded inside the augmentation class loader
@Override
public void accept(CuratedApplication o, Map<String, Object> params) {
    // setup the dev mode thread pool for NIO
    System.setProperty("java.nio.channels.DefaultThreadPool.threadFactory", "io.quarkus.dev.io.NioThreadPoolThreadFactory");
    Timing.staticInitStarted(o.getBaseRuntimeClassLoader(), false);
    // https://github.com/quarkusio/quarkus/issues/9748
    // if you have an app with all daemon threads then the app thread
    // may be the only thread keeping the JVM alive
    // during the restart process when this thread is stopped then
    // the JVM will die
    // we start this thread to keep the JVM alive until the shutdown hook is run
    // even for command mode we still want the JVM to live until it receives
    // a signal to make the 'press enter to restart' function to work
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                shutdownLatch.await();
            } catch (InterruptedException ignore) {
            }
        }
    }, "Quarkus Devmode keep alive thread").start();
    try {
        curatedApplication = o;
        Object potentialContext = params.get(DevModeContext.class.getName());
        if (potentialContext instanceof DevModeContext) {
            context = (DevModeContext) potentialContext;
        } else {
            // this was from the external class loader
            // we need to copy it into this one
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(out);
            oo.writeObject(potentialContext);
            context = (DevModeContext) new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())).readObject();
        }
        augmentAction = new AugmentActionImpl(curatedApplication, Collections.singletonList(new Consumer<BuildChainBuilder>() {

            @Override
            public void accept(BuildChainBuilder buildChainBuilder) {
                buildChainBuilder.addBuildStep(new BuildStep() {

                    @Override
                    public void execute(BuildContext context) {
                        // we need to make sure all hot reloadable classes are application classes
                        context.produce(new ApplicationClassPredicateBuildItem(new Predicate<String>() {

                            @Override
                            public boolean test(String s) {
                                QuarkusClassLoader cl = (QuarkusClassLoader) Thread.currentThread().getContextClassLoader();
                                // if the class file is present in this (and not the parent) CL then it is an application class
                                List<ClassPathElement> res = cl.getElementsWithResource(s.replace('.', '/') + ".class", true);
                                return !res.isEmpty();
                            }
                        }));
                    }
                }).produces(ApplicationClassPredicateBuildItem.class).build();
            }
        }), Collections.emptyList());
        List<CodeGenData> codeGens = new ArrayList<>();
        QuarkusClassLoader deploymentClassLoader = curatedApplication.createDeploymentClassLoader();
        for (DevModeContext.ModuleInfo module : context.getAllModules()) {
            if (!module.getSourceParents().isEmpty() && module.getPreBuildOutputDir() != null) {
                // it's null for remote dev
                codeGens.addAll(CodeGenerator.init(deploymentClassLoader, module.getSourceParents(), Paths.get(module.getPreBuildOutputDir()), Paths.get(module.getTargetDir()), sourcePath -> module.addSourcePathFirst(sourcePath.toAbsolutePath().toString())));
            }
        }
        RuntimeUpdatesProcessor.INSTANCE = setupRuntimeCompilation(context, (Path) params.get(APP_ROOT), (DevModeType) params.get(DevModeType.class.getName()));
        if (RuntimeUpdatesProcessor.INSTANCE != null) {
            RuntimeUpdatesProcessor.INSTANCE.checkForFileChange();
            RuntimeUpdatesProcessor.INSTANCE.checkForChangedClasses(true);
        }
        firstStart(deploymentClassLoader, codeGens);
        // doStart(false, Collections.emptySet());
        if (deploymentProblem != null || RuntimeUpdatesProcessor.INSTANCE.getCompileProblem() != null) {
            if (context.isAbortOnFailedStart()) {
                throw new RuntimeException(deploymentProblem == null ? RuntimeUpdatesProcessor.INSTANCE.getCompileProblem() : deploymentProblem);
            }
        }
        shutdownThread = new Thread(new Runnable() {

            @Override
            public void run() {
                shutdownLatch.countDown();
                synchronized (DevModeMain.class) {
                    if (runner != null) {
                        try {
                            close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }, "Quarkus Shutdown Thread");
        Runtime.getRuntime().addShutdownHook(shutdownThread);
    } catch (Exception e) {
        close();
        throw new RuntimeException(e);
    }
}
Also used : BiFunction(java.util.function.BiFunction) ObjectInputStream(java.io.ObjectInputStream) ClassTransformingBuildStep(io.quarkus.deployment.steps.ClassTransformingBuildStep) CommandLineUtils(org.apache.maven.shared.utils.cli.CommandLineUtils) ConfigProviderResolver(org.eclipse.microprofile.config.spi.ConfigProviderResolver) ApplicationLifecycleManager(io.quarkus.runtime.ApplicationLifecycleManager) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) AugmentAction(io.quarkus.bootstrap.app.AugmentAction) ConsoleStateManager(io.quarkus.deployment.console.ConsoleStateManager) DevConsoleManager(io.quarkus.dev.console.DevConsoleManager) InitialConfigurator(io.quarkus.bootstrap.logging.InitialConfigurator) Path(java.nio.file.Path) ConsoleHandler(org.jboss.logmanager.handlers.ConsoleHandler) LoggingSetupRecorder(io.quarkus.runtime.logging.LoggingSetupRecorder) Predicate(java.util.function.Predicate) Collection(java.util.Collection) AugmentActionImpl(io.quarkus.runner.bootstrap.AugmentActionImpl) Set(java.util.Set) BLUE(io.quarkus.deployment.dev.testing.MessageFormat.BLUE) ServiceLoader(java.util.ServiceLoader) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) StartupAction(io.quarkus.bootstrap.app.StartupAction) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MessageFormat(io.quarkus.deployment.dev.testing.MessageFormat) BuildStep(io.quarkus.builder.BuildStep) ColorPatternFormatter(org.jboss.logmanager.formatters.ColorPatternFormatter) BuildContext(io.quarkus.builder.BuildContext) CuratedApplication(io.quarkus.bootstrap.app.CuratedApplication) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RunningQuarkusApplication(io.quarkus.bootstrap.app.RunningQuarkusApplication) DevModeType(io.quarkus.dev.spi.DevModeType) Logger(org.jboss.logging.Logger) DeploymentFailedStartHandler(io.quarkus.dev.spi.DeploymentFailedStartHandler) LaunchMode(io.quarkus.runtime.LaunchMode) ClassChangeInformation(io.quarkus.bootstrap.app.ClassChangeInformation) Timing(io.quarkus.bootstrap.runner.Timing) BindException(java.net.BindException) TestSupport(io.quarkus.deployment.dev.testing.TestSupport) CodeGenData(io.quarkus.deployment.codegen.CodeGenData) ArrayList(java.util.ArrayList) BuildChainBuilder(io.quarkus.builder.BuildChainBuilder) ApplicationClassPredicateBuildItem(io.quarkus.deployment.builditem.ApplicationClassPredicateBuildItem) BiConsumer(java.util.function.BiConsumer) ObjectOutputStream(java.io.ObjectOutputStream) ApplicationStartException(io.quarkus.dev.appstate.ApplicationStartException) QuarkusConfigFactory(io.quarkus.runtime.configuration.QuarkusConfigFactory) HotReplacementSetup(io.quarkus.dev.spi.HotReplacementSetup) Properties(java.util.Properties) ClassPathElement(io.quarkus.bootstrap.classloading.ClassPathElement) CommandLineException(org.apache.maven.shared.utils.cli.CommandLineException) IOException(java.io.IOException) ConsoleCommand(io.quarkus.deployment.console.ConsoleCommand) Consumer(java.util.function.Consumer) Paths(java.nio.file.Paths) Closeable(java.io.Closeable) CodeGenerator(io.quarkus.deployment.CodeGenerator) Comparator(java.util.Comparator) FSWatchUtil(io.quarkus.deployment.util.FSWatchUtil) Collections(java.util.Collections) ArrayList(java.util.ArrayList) ClassTransformingBuildStep(io.quarkus.deployment.steps.ClassTransformingBuildStep) BuildStep(io.quarkus.builder.BuildStep) ApplicationClassPredicateBuildItem(io.quarkus.deployment.builditem.ApplicationClassPredicateBuildItem) ClassPathElement(io.quarkus.bootstrap.classloading.ClassPathElement) ObjectOutputStream(java.io.ObjectOutputStream) Predicate(java.util.function.Predicate) AugmentActionImpl(io.quarkus.runner.bootstrap.AugmentActionImpl) Path(java.nio.file.Path) CodeGenData(io.quarkus.deployment.codegen.CodeGenData) ByteArrayOutputStream(java.io.ByteArrayOutputStream) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) BindException(java.net.BindException) ApplicationStartException(io.quarkus.dev.appstate.ApplicationStartException) CommandLineException(org.apache.maven.shared.utils.cli.CommandLineException) IOException(java.io.IOException) BuildContext(io.quarkus.builder.BuildContext) ByteArrayInputStream(java.io.ByteArrayInputStream) BuildChainBuilder(io.quarkus.builder.BuildChainBuilder) DevModeType(io.quarkus.dev.spi.DevModeType) ObjectInputStream(java.io.ObjectInputStream)

Example 3 with CodeGenData

use of io.quarkus.deployment.codegen.CodeGenData in project quarkus by quarkusio.

the class IsolatedDevModeMain method startCodeGenWatcher.

private void startCodeGenWatcher(QuarkusClassLoader classLoader, List<CodeGenData> codeGens, Map<String, String> propertyMap) {
    Collection<FSWatchUtil.Watcher> watchers = new ArrayList<>();
    Properties properties = new Properties();
    properties.putAll(propertyMap);
    for (CodeGenData codeGen : codeGens) {
        watchers.add(new FSWatchUtil.Watcher(codeGen.sourceDir, codeGen.provider.inputExtension(), modifiedPaths -> {
            try {
                CodeGenerator.trigger(classLoader, codeGen, curatedApplication.getApplicationModel(), properties, LaunchMode.DEVELOPMENT, false);
            } catch (Exception any) {
                log.warn("Code generation failed", any);
            }
        }));
    }
    fsWatchUtil.observe(watchers, 500);
}
Also used : BiFunction(java.util.function.BiFunction) ObjectInputStream(java.io.ObjectInputStream) ClassTransformingBuildStep(io.quarkus.deployment.steps.ClassTransformingBuildStep) CommandLineUtils(org.apache.maven.shared.utils.cli.CommandLineUtils) ConfigProviderResolver(org.eclipse.microprofile.config.spi.ConfigProviderResolver) ApplicationLifecycleManager(io.quarkus.runtime.ApplicationLifecycleManager) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) AugmentAction(io.quarkus.bootstrap.app.AugmentAction) ConsoleStateManager(io.quarkus.deployment.console.ConsoleStateManager) DevConsoleManager(io.quarkus.dev.console.DevConsoleManager) InitialConfigurator(io.quarkus.bootstrap.logging.InitialConfigurator) Path(java.nio.file.Path) ConsoleHandler(org.jboss.logmanager.handlers.ConsoleHandler) LoggingSetupRecorder(io.quarkus.runtime.logging.LoggingSetupRecorder) Predicate(java.util.function.Predicate) Collection(java.util.Collection) AugmentActionImpl(io.quarkus.runner.bootstrap.AugmentActionImpl) Set(java.util.Set) BLUE(io.quarkus.deployment.dev.testing.MessageFormat.BLUE) ServiceLoader(java.util.ServiceLoader) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) StartupAction(io.quarkus.bootstrap.app.StartupAction) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MessageFormat(io.quarkus.deployment.dev.testing.MessageFormat) BuildStep(io.quarkus.builder.BuildStep) ColorPatternFormatter(org.jboss.logmanager.formatters.ColorPatternFormatter) BuildContext(io.quarkus.builder.BuildContext) CuratedApplication(io.quarkus.bootstrap.app.CuratedApplication) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RunningQuarkusApplication(io.quarkus.bootstrap.app.RunningQuarkusApplication) DevModeType(io.quarkus.dev.spi.DevModeType) Logger(org.jboss.logging.Logger) DeploymentFailedStartHandler(io.quarkus.dev.spi.DeploymentFailedStartHandler) LaunchMode(io.quarkus.runtime.LaunchMode) ClassChangeInformation(io.quarkus.bootstrap.app.ClassChangeInformation) Timing(io.quarkus.bootstrap.runner.Timing) BindException(java.net.BindException) TestSupport(io.quarkus.deployment.dev.testing.TestSupport) CodeGenData(io.quarkus.deployment.codegen.CodeGenData) ArrayList(java.util.ArrayList) BuildChainBuilder(io.quarkus.builder.BuildChainBuilder) ApplicationClassPredicateBuildItem(io.quarkus.deployment.builditem.ApplicationClassPredicateBuildItem) BiConsumer(java.util.function.BiConsumer) ObjectOutputStream(java.io.ObjectOutputStream) ApplicationStartException(io.quarkus.dev.appstate.ApplicationStartException) QuarkusConfigFactory(io.quarkus.runtime.configuration.QuarkusConfigFactory) HotReplacementSetup(io.quarkus.dev.spi.HotReplacementSetup) Properties(java.util.Properties) ClassPathElement(io.quarkus.bootstrap.classloading.ClassPathElement) CommandLineException(org.apache.maven.shared.utils.cli.CommandLineException) IOException(java.io.IOException) ConsoleCommand(io.quarkus.deployment.console.ConsoleCommand) Consumer(java.util.function.Consumer) Paths(java.nio.file.Paths) Closeable(java.io.Closeable) CodeGenerator(io.quarkus.deployment.CodeGenerator) Comparator(java.util.Comparator) FSWatchUtil(io.quarkus.deployment.util.FSWatchUtil) Collections(java.util.Collections) FSWatchUtil(io.quarkus.deployment.util.FSWatchUtil) ArrayList(java.util.ArrayList) CodeGenData(io.quarkus.deployment.codegen.CodeGenData) Properties(java.util.Properties) BindException(java.net.BindException) ApplicationStartException(io.quarkus.dev.appstate.ApplicationStartException) CommandLineException(org.apache.maven.shared.utils.cli.CommandLineException) IOException(java.io.IOException)

Example 4 with CodeGenData

use of io.quarkus.deployment.codegen.CodeGenData in project quarkus by quarkusio.

the class CodeGenerator method initAndRun.

// used by Gradle and Maven
public static void initAndRun(ClassLoader classLoader, PathCollection sourceParentDirs, Path generatedSourcesDir, Path buildDir, Consumer<Path> sourceRegistrar, ApplicationModel appModel, Properties properties, String launchMode, boolean test) throws CodeGenException {
    List<CodeGenData> generators = init(classLoader, sourceParentDirs, generatedSourcesDir, buildDir, sourceRegistrar);
    for (CodeGenData generator : generators) {
        generator.setRedirectIO(true);
        trigger(classLoader, generator, appModel, properties, LaunchMode.valueOf(launchMode), test);
    }
}
Also used : CodeGenData(io.quarkus.deployment.codegen.CodeGenData)

Aggregations

CodeGenData (io.quarkus.deployment.codegen.CodeGenData)4 Path (java.nio.file.Path)3 AugmentAction (io.quarkus.bootstrap.app.AugmentAction)2 ClassChangeInformation (io.quarkus.bootstrap.app.ClassChangeInformation)2 CuratedApplication (io.quarkus.bootstrap.app.CuratedApplication)2 RunningQuarkusApplication (io.quarkus.bootstrap.app.RunningQuarkusApplication)2 StartupAction (io.quarkus.bootstrap.app.StartupAction)2 ClassPathElement (io.quarkus.bootstrap.classloading.ClassPathElement)2 QuarkusClassLoader (io.quarkus.bootstrap.classloading.QuarkusClassLoader)2 InitialConfigurator (io.quarkus.bootstrap.logging.InitialConfigurator)2 Timing (io.quarkus.bootstrap.runner.Timing)2 BuildChainBuilder (io.quarkus.builder.BuildChainBuilder)2 BuildContext (io.quarkus.builder.BuildContext)2 BuildStep (io.quarkus.builder.BuildStep)2 CodeGenerator (io.quarkus.deployment.CodeGenerator)2 ApplicationClassPredicateBuildItem (io.quarkus.deployment.builditem.ApplicationClassPredicateBuildItem)2 ConsoleCommand (io.quarkus.deployment.console.ConsoleCommand)2 ConsoleStateManager (io.quarkus.deployment.console.ConsoleStateManager)2 MessageFormat (io.quarkus.deployment.dev.testing.MessageFormat)2 BLUE (io.quarkus.deployment.dev.testing.MessageFormat.BLUE)2