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);
}
}
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();
}
Aggregations