protected Path moveTempFileToDestination()

in disco-java-agent-instrumentation-preprocess/src/main/java/software/amazon/disco/instrumentation/preprocess/export/JarExportStrategy.java [197:229]


    protected Path moveTempFileToDestination(final JarInfo jarInfo, final PreprocessConfig config, final File tempFile) {
        try {
            final String destinationStr = config.getOutputDir() == null ?
                    jarInfo.getFile().getAbsolutePath()
                    : config.getOutputDir() + "/" + jarInfo.getFile().getName();

            final String destinationStrWithSuffix = config.getSuffix() == null ?
                    destinationStr
                    : destinationStr.substring(0, destinationStr.lastIndexOf(PreprocessConstants.JAR_EXTENSION)) + config.getSuffix() + PreprocessConstants.JAR_EXTENSION;

            final Path destination = Paths.get(destinationStrWithSuffix);
            destination.toFile().getParentFile().mkdirs();

            if (jarInfo.getFile().getAbsolutePath().equals(destination.toFile().getAbsolutePath())) {
                log.info(PreprocessConstants.MESSAGE_PREFIX + "Overriding original file: " + jarInfo.getFile().getName());
            }

            final Path filePath = Files.move(
                    Paths.get(tempFile.getAbsolutePath()),
                    destination,
                    StandardCopyOption.REPLACE_EXISTING);

            if (filePath == null) {
                throw new ExportException("Failed to replace existing jar file", null);
            }

            log.debug(PreprocessConstants.MESSAGE_PREFIX + "All transformed classes saved");

            return filePath;
        } catch (IOException e) {
            throw new ExportException("Failed to replace existing jar file", e);
        }
    }