From 9b3a0b336b8008d7b7353f9ebb8a39bd03a44f50 Mon Sep 17 00:00:00 2001 From: nravilla Date: Wed, 25 Sep 2024 16:16:53 -0400 Subject: [PATCH] Fixed NullPointerException --- .../maven/plugins/utility/ContentHelper.java | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/ContentHelper.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/ContentHelper.java index 3b6be802..46a31572 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/ContentHelper.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/ContentHelper.java @@ -7,14 +7,13 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; - import org.apache.commons.io.FileUtils; import org.apache.maven.plugin.MojoExecutionException; import org.openmrs.maven.plugins.model.Artifact; import org.openmrs.maven.plugins.model.DistroProperties; /** - * This class is downloads and moves content backend config to respective configuration folders + * This class downloads and moves content backend config to respective configuration folders. */ public class ContentHelper { @@ -31,38 +30,42 @@ private static File unpackArtifact(Artifact contentArtifact) throws MojoExecutio } catch (IOException e) { throw new MojoExecutionException("Exception while trying to create temporary directory", e); } - + + // Ensure moduleInstaller is set before calling this + if (moduleInstaller == null) { + throw new MojoExecutionException("ModuleInstaller is not initialized."); + } moduleInstaller.installAndUnpackModule(contentArtifact, sourceDir.getAbsolutePath()); return sourceDir; } - private static void moveBackendConfig(Artifact contentArtifact, ModuleInstaller moduleInstaller, File targetDir) throws MojoExecutionException { - File sourceDir = unpackArtifact (contentArtifact); + private static void moveBackendConfig(Artifact contentArtifact, File targetDir) throws MojoExecutionException { + File sourceDir = unpackArtifact(contentArtifact); try { File backendConfigFiles = sourceDir.toPath().resolve(BACKEND_CONFIG_FOLDER).toFile(); Path targetPath = targetDir.toPath().toAbsolutePath(); if (backendConfigFiles.exists()) { - File[] configDirectories = backendConfigFiles.listFiles(File::isDirectory); + File[] configDirectories = backendConfigFiles.listFiles(File::isDirectory); if (configDirectories != null) { for (File config : configDirectories) { Path destDir = targetPath.resolve(config.getName()).resolve(contentArtifact.getArtifactId()); Files.createDirectories(destDir); - //copy config files to the matching configuration folder + // Copy config files to the matching configuration folder FileUtils.copyDirectory(config, destDir.toFile()); } } } - } - catch (IOException e) { + } catch (IOException e) { throw new MojoExecutionException("Error copying backend configuration: " + e.getMessage(), e); + } finally { + FileUtils.deleteQuietly(sourceDir); } - FileUtils.deleteQuietly(sourceDir); } public static List extractAndGetAllContentFrontendConfigs(Artifact contentArtifact) throws MojoExecutionException { - File sourceDir = unpackArtifact (contentArtifact); + File sourceDir = unpackArtifact(contentArtifact); List configFiles = new ArrayList<>(); File frontendConfigFiles = sourceDir.toPath().resolve(FRONTEND_CONFIG_FOLDER).toFile(); @@ -76,13 +79,14 @@ public static List extractAndGetAllContentFrontendConfigs(Artifact content } } } else { - throw new MojoExecutionException("Error copying Frontend configuration"); + throw new MojoExecutionException("Error: Frontend configuration folder not found."); } return configFiles; } + // This method now sets the static moduleInstaller public static void downloadAndMoveContentBackendConfig(File serverDirectory, DistroProperties distroProperties, ModuleInstaller moduleInst, Wizard wizard) throws MojoExecutionException { - moduleInstaller = moduleInst; + moduleInstaller = moduleInst; // Set the static variable if (distroProperties != null) { File targetDir = new File(serverDirectory, SDKConstants.OPENMRS_SERVER_CONFIGURATION); List contents = distroProperties.getContentArtifacts(); @@ -90,15 +94,14 @@ public static void downloadAndMoveContentBackendConfig(File serverDirectory, Dis if (contents != null) { for (Artifact content : contents) { wizard.showMessage("Downloading Content: " + content + "\n"); - moveBackendConfig(content, moduleInstaller, targetDir); + moveBackendConfig(content, targetDir); } } } - } public static List collectFrontendConfigs(DistroProperties distroProperties) throws MojoExecutionException { - List allConfigFiles = new ArrayList(); + List allConfigFiles = new ArrayList<>(); if (distroProperties != null) { List contents = distroProperties.getContentArtifacts(); if (contents != null) { @@ -108,5 +111,10 @@ public static List collectFrontendConfigs(DistroProperties distroPropertie } } return allConfigFiles; + } + + public static String assembleWithFrontendConfig(String program, File buildTargetDir, List configFiles, File spaConfigFile) { + // TODO: Implement this method + return null; } }