-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c7c850
commit 2f1a93c
Showing
29 changed files
with
761 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...ain/java/gov/nist/secauto/metaschema/cli/processor/command/CommandExecutionException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
core/src/main/java/gov/nist/secauto/metaschema/core/util/DeleteOnShutdown.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
Oops, something went wrong.