Skip to content

Commit

Permalink
Native work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Oct 8, 2024
1 parent 25d2355 commit e7f83b7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;

Expand All @@ -23,15 +24,22 @@
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
import io.quarkus.deployment.builditem.IndexDependencyBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBundleBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourcePatternsBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedPackageBuildItem;
import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem;
import io.quarkus.deployment.pkg.builditem.UberJarMergedResourceBuildItem;
import io.quarkus.logging.Log;
import net.sf.jasperreports.compilers.ReportExpressionEvaluationData;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JRReportCompileData;
import net.sf.jasperreports.engine.util.JRLoader;

class JasperReportsProcessor {

Expand Down Expand Up @@ -150,7 +158,7 @@ void registerForReflection(BuildProducer<ReflectiveClassBuildItem> reflectiveCla

//@formatter:on
final TreeSet<String> uniqueClasses = new TreeSet<>(classNames);
Log.infof("Reflection: %s", uniqueClasses);
Log.debugf("Reflection: %s", uniqueClasses);

reflectiveClass.produce(
ReflectiveClassBuildItem.builder(uniqueClasses.toArray(new String[0])).constructors().methods().fields()
Expand Down Expand Up @@ -222,12 +230,66 @@ void registerResourceBuildItems(BuildProducer<NativeImageResourceBuildItem> nati
}

@BuildStep
void registerReports(BuildProducer<NativeImageResourcePatternsBuildItem> nativeImageResourcePatterns) {
void registerReports(BuildProducer<NativeImageResourcePatternsBuildItem> nativeImageResourcePatterns,
BuildProducer<GeneratedClassBuildItem> additionalClasses,
OutputTargetBuildItem outputTarget) {
final NativeImageResourcePatternsBuildItem.Builder builder = NativeImageResourcePatternsBuildItem.builder();
builder.includeGlob("*." + ReportFileBuildItem.EXT_REPORT);
builder.includeGlob("*." + ReportFileBuildItem.EXT_COMPILED);
builder.includeGlob("*." + ReportFileBuildItem.EXT_STYLE);
nativeImageResourcePatterns.produce(builder.build());

Path startDir = findProjectRoot(outputTarget.getOutputDirectory());
Log.tracef("Start Directory: %s", startDir);
Set<Path> foundFiles = new HashSet<>();

try {
// compiled - .jasper
Files.walkFileTree(startDir, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
// Check if the file has one of the desired extensions
String filePath = file.toString();
if (filePath.endsWith(ReportFileBuildItem.EXT_COMPILED)) {
Log.info(filePath);
foundFiles.add(file);
}
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
Log.error("Error looking for report files.", e);
}

foundFiles.forEach((file) -> {
try {
String jasperFilePath = file.toFile().getAbsolutePath();
JasperReport report = (JasperReport) JRLoader.loadObject(JRLoader.getLocationInputStream(jasperFilePath));
JRReportCompileData reportData = (JRReportCompileData) report.getCompileData();
ReportExpressionEvaluationData mainData = (ReportExpressionEvaluationData) reportData
.getMainDatasetCompileData();
String reportDataClass = mainData.getCompileName();
if (StringUtils.isNotBlank(reportDataClass)) {
byte[] bytes = (byte[]) mainData.getCompileData();
Log.infof("Report Data Class: %s Size: %d", reportDataClass, bytes.length);
additionalClasses.produce(new GeneratedClassBuildItem(false, reportDataClass, bytes));
}

} catch (JRException e) {
Log.error("Error loading report file class.", e);
}
});
}

static Path findProjectRoot(Path outputDirectory) {
Path currentPath = outputDirectory.getParent();
Log.tracef("Current Directory: %s", currentPath);
Path root = Paths.get(DEFAULT_ROOT_PATH);
if (Files.exists(currentPath.resolve(root))) {
return currentPath.resolve(root).normalize();
} else {
return outputDirectory;
}
}

@BuildStep
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ quarkus.log.file.path=it.log
quarkus.log.file.format=%d{yyyy-MM-dd HH:mm:ss} %-5p [%c] (%t) %s%e%n
quarkus.log.level=INFO
%prod.quarkus.log.category."net.sf.jasperreports".level=DEBUG
quarkus.log.category."net.sf.jasperreports.engine.util.JRClassLoader".level=DEBUG
quarkus.log.category."net.sf.jasperreports.engine.util.JRClassLoader".level=DEBUG
quarkus.log.category."io.quarkus.deployment.steps.ReflectiveHierarchyStep".level=ERROR

0 comments on commit e7f83b7

Please sign in to comment.