From 91e81bbcc80d10aa0920a51dd42dad9861c12e4d Mon Sep 17 00:00:00 2001 From: Toshiya Kobayashi Date: Mon, 2 Sep 2024 19:11:32 +0900 Subject: [PATCH] [incubator-kie-drools-5745] Comptability issue with Spring-boot 3.2 --- .../kie/builder/impl/ClasspathKieProject.java | 4 ++++ .../src/main/java/org/drools/util/JarUtils.java | 16 ++++++++++++++++ .../test/java/org/drools/util/JarUtilsTest.java | 12 ++++++++++++ 3 files changed, 32 insertions(+) diff --git a/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/ClasspathKieProject.java b/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/ClasspathKieProject.java index 7d9a7aa9b6f..c9ad2df0db4 100644 --- a/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/ClasspathKieProject.java +++ b/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/ClasspathKieProject.java @@ -45,6 +45,7 @@ import org.drools.compiler.kie.builder.impl.event.KieServicesEventListerner; import org.drools.compiler.kproject.models.KieModuleModelImpl; import org.drools.util.IoUtils; +import org.drools.util.JarUtils; import org.drools.util.PortablePath; import org.drools.util.StringUtils; import org.kie.api.KieServices; @@ -410,6 +411,9 @@ public static String fixURLFromKProjectPath(URL url) { urlPath = urlPath.substring( 0, urlPath.length() - 1 ); } + // Replace "/!BOOT-INF/" with "!/BOOT-INF/" to make it consistent with the actual path in the jar file + urlPath = JarUtils.replaceNestedPathForSpringBoot32(urlPath); + // remove any remaining protocols, normally only if it was a jar int firstSlash = urlPath.indexOf( '/' ); colonIndex = firstSlash > 0 ? urlPath.lastIndexOf( ":", firstSlash ) : urlPath.lastIndexOf( ":" ); diff --git a/drools-util/src/main/java/org/drools/util/JarUtils.java b/drools-util/src/main/java/org/drools/util/JarUtils.java index c944826d661..eb7789f632b 100644 --- a/drools-util/src/main/java/org/drools/util/JarUtils.java +++ b/drools-util/src/main/java/org/drools/util/JarUtils.java @@ -26,6 +26,9 @@ public class JarUtils { private static final String SPRING_BOOT_PREFIX = "BOOT-INF/classes/"; // Actual path prefix in Spring Boot JAR private static final String SPRING_BOOT_URL_PREFIX = "BOOT-INF/classes!/"; // Spring Boot adds "!" to resource url as a "nest" separator + private static final String SPRING_BOOT_NESTED_PREFIX_BEFORE_3_2 = "!/BOOT-INF/"; // Before Spring Boot 3.2 + private static final String SPRING_BOOT_NESTED_PREFIX_AFTER_3_2 = "/!BOOT-INF/"; // Since Spring Boot 3.2 + private JarUtils() { // Avoid instantiating class } @@ -45,4 +48,17 @@ public static String normalizeSpringBootResourceUrlPath(String resourceUrlPath) return resourceUrlPath; } } + + /** + * Replace the new spring-boot nested path representation "/!BOOT-INF/" (introduced since 3.2) with the old "!/BOOT-INF/". + * Because the new path representation doesn't meet the path manipulation in the drools codebase. + * See https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.2-Release-Notes#nested-jar-support + */ + public static String replaceNestedPathForSpringBoot32(String urlPath) { + if (urlPath.contains(SPRING_BOOT_NESTED_PREFIX_AFTER_3_2)) { + return urlPath.replace(SPRING_BOOT_NESTED_PREFIX_AFTER_3_2, SPRING_BOOT_NESTED_PREFIX_BEFORE_3_2); + } else { + return urlPath; + } + } } diff --git a/drools-util/src/test/java/org/drools/util/JarUtilsTest.java b/drools-util/src/test/java/org/drools/util/JarUtilsTest.java index a2c6794fd28..6453cc595c0 100644 --- a/drools-util/src/test/java/org/drools/util/JarUtilsTest.java +++ b/drools-util/src/test/java/org/drools/util/JarUtilsTest.java @@ -29,4 +29,16 @@ public void normalizeSpringBootResourceUrlPath() { String normalized = JarUtils.normalizeSpringBootResourceUrlPath("BOOT-INF/classes!/org/example/MyClass.class"); assertThat(normalized).isEqualTo("BOOT-INF/classes/org/example/MyClass.class"); } + + @Test + public void replaceNestedPathForSpringBoot32_shouldNotAffectOldPath() { + String result = JarUtils.replaceNestedPathForSpringBoot32("/dir/myapp.jar!/BOOT-INF/lib/mykjar.jar"); + assertThat(result).isEqualTo("/dir/myapp.jar!/BOOT-INF/lib/mykjar.jar"); + } + + @Test + public void replaceNestedPathForSpringBoot32_shouldReplaceNewPath() { + String result = JarUtils.replaceNestedPathForSpringBoot32("/dir/myapp.jar/!BOOT-INF/lib/mykjar.jar"); + assertThat(result).isEqualTo("/dir/myapp.jar!/BOOT-INF/lib/mykjar.jar"); + } } \ No newline at end of file