Search in sources :

Example 1 with PersistenceGenerator

use of org.kie.kogito.codegen.process.persistence.PersistenceGenerator in project kogito-runtimes by kiegroup.

the class ProcessClassesMojo method execute.

@Override
public void execute() throws MojoExecutionException {
    try {
        JavaCompilerSettings settings = new JavaCompilerSettings();
        List<URL> pathUrls = new ArrayList<>();
        for (String path : project.getRuntimeClasspathElements()) {
            File pathFile = new File(path);
            pathUrls.add(pathFile.toURI().toURL());
            settings.addClasspath(pathFile);
        }
        URL[] urlsForClassLoader = pathUrls.toArray(new URL[pathUrls.size()]);
        // need to define parent classloader which knows all dependencies of the plugin
        try (URLClassLoader cl = new URLClassLoader(urlsForClassLoader, Thread.currentThread().getContextClassLoader())) {
            ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.addUrls(cl.getURLs());
            builder.addClassLoader(cl);
            Reflections reflections = new Reflections(builder);
            @SuppressWarnings({ "rawtype", "unchecked" }) Set<Class<?>> modelClasses = (Set) reflections.getSubTypesOf(Model.class);
            // collect constructor parameters so the generated class can create constructor with injection
            Class<?> persistenceClass = reflections.getSubTypesOf(ProcessInstancesFactory.class).stream().filter(c -> !c.isInterface()).findFirst().orElse(null);
            ReflectionProtoGenerator protoGenerator = ReflectionProtoGenerator.builder().withPersistenceClass(persistenceClass).build(modelClasses);
            KogitoBuildContext context = discoverKogitoRuntimeContext(cl);
            // Generate persistence files
            PersistenceGenerator persistenceGenerator = new PersistenceGenerator(context, protoGenerator, new ReflectionMarshallerGenerator(context, protoGenerator.getDataClasses()));
            Collection<GeneratedFile> persistenceFiles = persistenceGenerator.generate();
            validateGeneratedFileTypes(persistenceFiles, asList(GeneratedFileType.Category.SOURCE, GeneratedFileType.Category.INTERNAL_RESOURCE, GeneratedFileType.Category.STATIC_HTTP_RESOURCE));
            Collection<GeneratedFile> generatedClasses = persistenceFiles.stream().filter(x -> x.category().equals(GeneratedFileType.Category.SOURCE)).collect(Collectors.toList());
            Collection<GeneratedFile> generatedResources = persistenceFiles.stream().filter(x -> x.category().equals(GeneratedFileType.Category.INTERNAL_RESOURCE) || x.category().equals(GeneratedFileType.Category.STATIC_HTTP_RESOURCE)).collect(Collectors.toList());
            // Compile and write persistence files
            compileAndWriteClasses(generatedClasses, cl, settings);
            // Dump resources
            generatedResources.forEach(this::writeGeneratedFile);
            // Json schema generation
            Stream<Class<?>> processClassStream = reflections.getTypesAnnotatedWith(ProcessInput.class).stream();
            generateJsonSchema(processClassStream).forEach(this::writeGeneratedFile);
            Stream<Class<?>> userTaskClassStream = reflections.getTypesAnnotatedWith(UserTask.class).stream();
            generateJsonSchema(userTaskClassStream).forEach(this::writeGeneratedFile);
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Error during processing model classes", e);
    }
}
Also used : ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) KiePath(org.kie.memorycompiler.resources.KiePath) Arrays(java.util.Arrays) URL(java.net.URL) CompilationResult(org.kie.memorycompiler.CompilationResult) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) UserTask(org.kie.kogito.UserTask) Parameter(org.apache.maven.plugins.annotations.Parameter) Reflections(org.reflections.Reflections) JavaCompilerFactory(org.kie.memorycompiler.JavaCompilerFactory) ArrayList(java.util.ArrayList) GeneratedFileValidation.validateGeneratedFileTypes(org.kie.kogito.codegen.core.utils.GeneratedFileValidation.validateGeneratedFileTypes) Mojo(org.apache.maven.plugins.annotations.Mojo) URLClassLoader(java.net.URLClassLoader) Arrays.asList(java.util.Arrays.asList) JsonSchemaGenerator(org.kie.kogito.codegen.json.JsonSchemaGenerator) ProcessInput(org.kie.kogito.ProcessInput) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) ResolutionScope(org.apache.maven.plugins.annotations.ResolutionScope) JavaCompiler(org.kie.memorycompiler.JavaCompiler) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) PersistenceGenerator(org.kie.kogito.codegen.process.persistence.PersistenceGenerator) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) GeneratedFileType(org.kie.kogito.codegen.api.GeneratedFileType) Collection(java.util.Collection) Set(java.util.Set) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Collectors(java.util.stream.Collectors) File(java.io.File) MojoFailureException(org.apache.maven.plugin.MojoFailureException) List(java.util.List) Stream(java.util.stream.Stream) Model(org.kie.kogito.Model) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) JavaCompilerSettings(org.kie.memorycompiler.JavaCompilerSettings) ProcessInstancesFactory(org.kie.kogito.process.ProcessInstancesFactory) JavaConfiguration(org.kie.memorycompiler.JavaConfiguration) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) Set(java.util.Set) ArrayList(java.util.ArrayList) URL(java.net.URL) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ReflectionProtoGenerator(org.kie.kogito.codegen.process.persistence.proto.ReflectionProtoGenerator) ProcessInput(org.kie.kogito.ProcessInput) Reflections(org.reflections.Reflections) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) PersistenceGenerator(org.kie.kogito.codegen.process.persistence.PersistenceGenerator) JavaCompilerSettings(org.kie.memorycompiler.JavaCompilerSettings) UserTask(org.kie.kogito.UserTask) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) URLClassLoader(java.net.URLClassLoader) Model(org.kie.kogito.Model) ReflectionMarshallerGenerator(org.kie.kogito.codegen.process.persistence.marshaller.ReflectionMarshallerGenerator) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) File(java.io.File) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile)

Example 2 with PersistenceGenerator

use of org.kie.kogito.codegen.process.persistence.PersistenceGenerator in project kogito-runtimes by kiegroup.

the class ProcessesAssetsProcessor method getGeneratedPersistenceFiles.

private Collection<GeneratedFile> getGeneratedPersistenceFiles(IndexView index, KogitoBuildContext context, BuildProducer<RunTimeConfigurationDefaultBuildItem> runTimeConfiguration, BuildProducer<NativeImageResourcePatternsBuildItem> resourcePatterns) {
    ClassInfo persistenceClass = index.getClassByName(persistenceFactoryClass);
    Collection<ClassInfo> modelClasses = index.getAllKnownImplementors(DotName.createSimple(Model.class.getCanonicalName()));
    JandexProtoGenerator protoGenerator = JandexProtoGenerator.builder(index).withPersistenceClass(persistenceClass).build(modelClasses);
    PersistenceGenerator persistenceGenerator = new PersistenceGenerator(context, protoGenerator, new JandexMarshallerGenerator(context, modelClasses));
    if (persistenceGenerator.persistenceType().equals(PersistenceGenerator.POSTGRESQL_PERSISTENCE_TYPE) || persistenceGenerator.persistenceType().equals(PersistenceGenerator.JDBC_PERSISTENCE_TYPE)) {
        resourcePatterns.produce(new NativeImageResourcePatternsBuildItem.Builder().includeGlob("sql/*.sql").build());
    } else if (persistenceGenerator.persistenceType().equals(PersistenceGenerator.KAFKA_PERSISTENCE_TYPE)) {
        String processIds = protoGenerator.getProcessIds().stream().map(s -> "kogito.process." + s).collect(joining(","));
        runTimeConfiguration.produce(new RunTimeConfigurationDefaultBuildItem(PersistenceGenerator.QUARKUS_KAFKA_STREAMS_TOPICS_PROP, processIds));
    }
    return persistenceGenerator.generate();
}
Also used : PersistenceGenerator(org.kie.kogito.codegen.process.persistence.PersistenceGenerator) NativeImageResourcePatternsBuildItem(io.quarkus.deployment.builditem.nativeimage.NativeImageResourcePatternsBuildItem) RunTimeConfigurationDefaultBuildItem(io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem) ClassInfo(org.jboss.jandex.ClassInfo)

Aggregations

PersistenceGenerator (org.kie.kogito.codegen.process.persistence.PersistenceGenerator)2 RunTimeConfigurationDefaultBuildItem (io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem)1 NativeImageResourcePatternsBuildItem (io.quarkus.deployment.builditem.nativeimage.NativeImageResourcePatternsBuildItem)1 File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 LifecyclePhase (org.apache.maven.plugins.annotations.LifecyclePhase)1 Mojo (org.apache.maven.plugins.annotations.Mojo)1 Parameter (org.apache.maven.plugins.annotations.Parameter)1