Copilot commented on code in PR #4307:
URL: 
https://github.com/apache/incubator-kie-kogito-runtimes/pull/4307#discussion_r3317606920


##########
kogito-codegen-modules/kogito-codegen-manager/src/main/java/org/kie/kogito/codegen/manager/GenerateModelHelper.java:
##########
@@ -115,6 +117,22 @@ public static void generateModel(GenerateModelInfo 
generateModelInfo) {
         /* 6. Compile and persist compiled files in target (maven) or gradle 
(build) */
         CompilerHelper.compileAndDump(compileInfo);
 
+        if 
(SpringBootKogitoBuildContext.CONTEXT_NAME.equals(generateModelInfo.kogitoBuildContext().name()))
 {
+            /* 7. Conditionally add Default KIE Configuration for SpringBoot */
+            Map<String, Collection<GeneratedFile>> 
generatedSpringBootConfigFiles = 
SpringBootKieConfigurationHelper.generateKieSpringBootConfiguration(generateModelInfo.kogitoBuildContext);
+
+            compileInfo =
+                    new 
CompilerHelper.CompileInfo(generatedSpringBootConfigFiles.get(SOURCES),
+                            generatedSpringBootConfigFiles.get(RESOURCES), 
generateModelInfo);
+
+            /* 8. Persist the process extension code-generated source and 
resources files in target (maven) or gradle (build) */

Review Comment:
   The comment "Persist the process extension code-generated source and 
resources files" appears to be copy-pasted from the persistence generation 
block above (step 5 in this method). At this step (8), the files being 
persisted are the Spring Boot KIE configuration, not the process extension. 
Consider updating the comment to accurately describe the Spring Boot 
configuration generation.
   



##########
kogito-codegen-modules/kogito-codegen-manager/src/main/java/org/kie/kogito/codegen/manager/springboot/SpringBootKieConfigurationHelper.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.kie.kogito.codegen.manager.springboot;
+
+import java.io.InputStream;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+import org.drools.codegen.common.GeneratedFile;
+import org.drools.codegen.common.GeneratedFileType;
+import org.kie.kogito.codegen.api.context.KogitoBuildContext;
+import org.kie.kogito.codegen.api.context.impl.SpringBootKogitoBuildContext;
+import org.kie.kogito.codegen.manager.exceptions.KogitoCodegenException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.github.javaparser.ast.CompilationUnit;
+
+import static com.github.javaparser.StaticJavaParser.parse;
+import static org.kie.kogito.codegen.manager.CompilerHelper.RESOURCES;
+import static org.kie.kogito.codegen.manager.CompilerHelper.SOURCES;
+
+public class SpringBootKieConfigurationHelper {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(SpringBootKieConfigurationHelper.class);
+
+    private static final String KIE_SB_CONFIG_FILE_NAME = 
"kie-spring-boot-config.properties";
+    private static final String SPRING_KIE_SB_CONFIGURATION_CLASS_NAME = 
"SpringBootKieConfiguration";
+
+    private SpringBootKieConfigurationHelper() {
+        // Utility class it should not be initialized
+    }
+
+    public static Map<String, Collection<GeneratedFile>> 
generateKieSpringBootConfiguration(KogitoBuildContext context) {
+        if (!SpringBootKogitoBuildContext.CONTEXT_NAME.equals(context.name())) 
{
+            throw new KogitoCodegenException("Attempting to add Spring Boot 
KIE Configuration outside of a Spring Boot Project");
+        }
+
+        return Map.of(SOURCES, 
List.of(generateKieSpringBootConfigurationClass(context)), RESOURCES, 
List.of(generateKieSpringBootConfigProperties(context)));
+    }
+
+    static GeneratedFile 
generateKieSpringBootConfigurationClass(KogitoBuildContext context) {
+        CompilationUnit compilationUnit = 
parse(context.getClassLoader().getResourceAsStream("class-templates/" + 
SPRING_KIE_SB_CONFIGURATION_CLASS_NAME + "Template.java"));
+        compilationUnit.setPackageDeclaration(context.getPackageName());
+
+        String generatedPath = context.getPackageName().replace(".", "/") + 
"/" + SPRING_KIE_SB_CONFIGURATION_CLASS_NAME + ".java";
+
+        return new GeneratedFile(GeneratedFileType.SOURCE, generatedPath, 
compilationUnit.toString());
+    }
+
+    static GeneratedFile 
generateKieSpringBootConfigProperties(KogitoBuildContext context) {
+        try (InputStream stream = 
context.getClassLoader().getResourceAsStream(KIE_SB_CONFIG_FILE_NAME)) {
+            if (stream == null) {
+                LOGGER.error("Could not find Spring Boot KIE configuration 
properties file");
+                throw new KogitoCodegenException("Could not find Spring Boot 
KIE configuration properties file");
+            }
+
+            return new GeneratedFile(GeneratedFileType.INTERNAL_RESOURCE, 
KIE_SB_CONFIG_FILE_NAME, stream.readAllBytes());
+        } catch (Exception ex) {
+            LOGGER.error("Failed to load KIE Spring Boot Config Properties", 
ex);
+            throw new KogitoCodegenException("Failed to load KIE Spring Boot 
Config Properties", ex);
+        }
+    }

Review Comment:
   The `catch (Exception ex)` block at line 76 also catches the 
`KogitoCodegenException` explicitly thrown at line 72 when `stream == null`, 
re-wrapping it with the misleading message "Failed to load KIE Spring Boot 
Config Properties" (replacing the original "Could not find Spring Boot KIE 
configuration properties file" message). Consider narrowing the catch to 
`IOException`, or rethrowing `KogitoCodegenException` unchanged, so the 
original error context is preserved for callers.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to