Skip to content

Commit

Permalink
Refactored code to reduce warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Nov 2, 2024
1 parent 9c7c850 commit 2f1a93c
Show file tree
Hide file tree
Showing 29 changed files with 761 additions and 342 deletions.
4 changes: 0 additions & 4 deletions cli-processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@
<groupId>nl.talsmasoftware</groupId>
<artifactId>lazy4j</artifactId>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import gov.nist.secauto.metaschema.cli.processor.command.ExtraArgument;
import gov.nist.secauto.metaschema.cli.processor.command.ICommand;
import gov.nist.secauto.metaschema.cli.processor.command.ICommandExecutor;
import gov.nist.secauto.metaschema.core.util.AutoCloser;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.IVersionInfo;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
Expand Down Expand Up @@ -624,31 +625,33 @@ protected String buildHelpCliSyntax() {
return retval;
}

/**
* Output the help text to the console.
*/
public void showHelp() {

HelpFormatter formatter = new HelpFormatter();
formatter.setLongOptSeparator("=");

@SuppressWarnings("resource")
AnsiPrintStream out = AnsiConsole.out();
int terminalWidth = Math.max(out.getTerminalWidth(), 40);

@SuppressWarnings("resource")
PrintWriter writer = new PrintWriter( // NOPMD not owned
out,
try (PrintWriter writer = new PrintWriter( // NOPMD not owned
AutoCloser.preventClose(out),
true,
StandardCharsets.UTF_8);
formatter.printHelp(
writer,
terminalWidth,
buildHelpCliSyntax(),
buildHelpHeader(),
toOptions(),
HelpFormatter.DEFAULT_LEFT_PAD,
HelpFormatter.DEFAULT_DESC_PAD,
buildHelpFooter(),
false);
writer.flush();
StandardCharsets.UTF_8)) {
formatter.printHelp(
writer,
Math.max(out.getTerminalWidth(), 50),
buildHelpCliSyntax(),
buildHelpHeader(),
toOptions(),
HelpFormatter.DEFAULT_LEFT_PAD,
HelpFormatter.DEFAULT_DESC_PAD,
buildHelpFooter(),
false);
writer.flush();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ protected static Path getCurrentWorkingDirectory() {

@NonNull
protected static Path resolveAgainstCWD(@NonNull Path path) {
return getCurrentWorkingDirectory().resolve(path).normalize();
return ObjectUtils.notNull(getCurrentWorkingDirectory().resolve(path).normalize());
}

@NonNull
protected static URI resolveAgainstCWD(@NonNull URI uri) {
return getCurrentWorkingDirectory().toUri().resolve(uri.normalize());
return ObjectUtils.notNull(getCurrentWorkingDirectory().toUri().resolve(uri.normalize()));
}

@NonNull
protected static URI resolveAgainstCWD(@NonNull String uri) throws URISyntaxException {
return UriUtils.toUri(uri, getCurrentWorkingDirectory().toUri());
return UriUtils.toUri(uri, ObjectUtils.notNull(getCurrentWorkingDirectory().toUri()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* SPDX-FileCopyrightText: none
* SPDX-License-Identifier: CC0-1.0
*/

package gov.nist.secauto.metaschema.cli.processor.command;

import gov.nist.secauto.metaschema.cli.processor.ExitCode;
import gov.nist.secauto.metaschema.cli.processor.ExitStatus;

import edu.umd.cs.findbugs.annotations.NonNull;

/**
* For use in commands to short-circut command execution.
*/
public class CommandExecutionException
extends Exception {
private final ExitCode exitCode;

/**
* the serial version UID.
*/
private static final long serialVersionUID = 1L;

/**
* Constructs a new exception with the provided {@code code}, and no message or
* cause.
*
* @param code
* the exit code associated with this error
*/
public CommandExecutionException(@NonNull ExitCode code) {
this.exitCode = code;
}

/**
* Constructs a new exception with the provided {@code code}, {@code message},
* and no cause.
*
* @param code
* the exit code associated with this error
* @param message
* the exception message
*/
public CommandExecutionException(@NonNull ExitCode code, String message) {
super(message);
this.exitCode = code;
}

/**
* Constructs a new exception with the no message and provided the {@code code}
* and {@code cause}.
*
* @param code
* the exit code associated with this error
* @param cause
* the original exception cause
*/
public CommandExecutionException(@NonNull ExitCode code, Throwable cause) {
super(cause);
this.exitCode = code;
}

/**
* Constructs a new exception with the provided {@code code}, {@code message},
* and {@code cause}.
*
* @param code
* the exit code associated with this error
* @param message
* the exception message
* @param cause
* the original exception cause
*/
public CommandExecutionException(@NonNull ExitCode code, String message, Throwable cause) {
super(message, cause);
this.exitCode = code;
}

/**
* Generate an {@link ExitStatus} based on this exception.
*
* @return the exit status
*/
@NonNull
public ExitStatus toExitStatus() {
String message = getLocalizedMessage();

ExitStatus retval = message == null
? exitCode.exit()
: exitCode.exitMessage(message);

Throwable cause = getCause();
if (cause != null) {
retval.withThrowable(cause);
}
return retval;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* SPDX-FileCopyrightText: none
* SPDX-License-Identifier: CC0-1.0
*/

package gov.nist.secauto.metaschema.core.util;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
* Used to perform cleanup on shutdown.
*/
@SuppressWarnings("PMD.DoNotUseThreads")
public final class DeleteOnShutdown {
private static Set<Path> paths = new LinkedHashSet<>();
private static final Lock LOCK = new ReentrantLock();

static {
Runtime.getRuntime().addShutdownHook(
new Thread(DeleteOnShutdown::shutdownHook));
}

@SuppressWarnings("PMD.NullAssignment")
private static void shutdownHook() {
LOCK.lock();
try {
Set<Path> localSet = new LinkedHashSet<>(paths);
paths = null;
localSet.forEach(path -> {
try {
Files.walkFileTree(path,
new SimpleFileVisitor<>() {
@Override
public FileVisitResult postVisitDirectory(
Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(
Path file, BasicFileAttributes attrs)
throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException ex) {
// this is a best effort
}
});
} finally {
LOCK.unlock();
}
}

/**
* Register a new path to be deleted on JVM termination.
* <p>
* If the path is a directory, then its contents will also be deleted.
*
* @param path
* the path to delete
*/
public static void register(Path path) {
LOCK.lock();
try {
if (paths == null) {
throw new IllegalStateException("ShutdownHook already in progress.");
}
paths.add(path);
} finally {
LOCK.unlock();
}
}

private DeleteOnShutdown() {
// disable construction
}

}
Loading

0 comments on commit 2f1a93c

Please sign in to comment.