Package Torello.Java
Class OSExtras
- java.lang.Object
-
- Torello.Java.OSExtras
-
- All Implemented Interfaces:
java.lang.Cloneable
public class OSExtras extends java.lang.Object implements java.lang.Cloneable
This class allows for the user to provide redirects, environment variables, and output/error unification (meaning the Standard-Output and Error-Output Streams are printed together, as if they were being printed to the terminal).
File Redirects:
This class can handle redirected input or output from files located on the file-system. As long as a instance-object of Javaclass java.io.File
can be created, then input or output to any UNIX Operating-System process may be redirected to or from that file using the methods in this class.
Other Pipes:
In the days of yore, a UNIX programmer would often find himself piping the input of one command into the output of another. Here, if the input or output of a method is not going to be an, actual, physical file located on disk (the file-system), then the method's in this class will not be of any help. The reality is that since this method is largely a wrapper around Java'sjava.lang.ProcessBuilder
class, the amount of complexity increases so dramatically trying to 'wrap' a Java A.P.I. that already works just fine (so I didn't write it).
Below is an example that uses this class'TAR'
function to create an archive of all'.java'
files in theNodeSearch
package of this jar-library's source-file directory. Please note, at the UNIX command terminal, a simple wild-card expression would suffice, but when invoking a shell command from Java, the wild-card expression(s) would not be expanded - that is unless code as seen in the example below is used.
Example usingTAR
:
NOTE: The value of using theShell.XL
version of UNIXTAR
command (over the standardShell.TAR(...)
) is that the current working directory in which this script is executed may be changed. This means that the when the script runs, the files that are included in the output.tar
will not have all of the annoying parent-directory string's prepended to their file-names in the.tar
that's created.
Example:
import Torello.Java.*; import static Torello.Java.FileNode.RetTypeChoice.*; import java.util.stream.*; import java.io.IOException; import java.io.File; import java.lang.ProcessBuilder.Redirect; public class t { public static void main(String[] argv) throws IOException { // Build a new instance of Shell.XL Shell shell = new Shell.XL(System.out, System.err); // This uses the 'FileNode' class to retrieve all '.java' source-files that are // found in the 'searchLoops/' sub-directory. Stream<String> fileNames = FileNode .createRoot("Torello/HTML/NodeSearch/") .loadTree() .pollDir("searchLoops") .flattenJustFiles(FULLPATH_STREAM, -1, (FileNode fn) -> fn.name.endsWith(".java")); // These are the rest of the parameters for the UNIX 'tar' (file-archive) command Stream<String> command = Stream.of("-cvf", "MySourceFiles.tar"); // Concat the Streams, here, for readability String[] args = Stream.concat(command, fileNames).toArray(String[]::new); // Creates a '.tar' of all java source files that were found. // // NOTE: The value of using the "OSExtras" is that the root working directory is set to // "Torello/HTML/NodeSearch" - which means that none of the parent-directories will be // prepended to the files in the '.tar' that is generated! OSExtras osExtras = new OSExtras(); osExtras.currentWorkingDirectory = new File("Torello/HTML/NodeSearch/"); shell.odExtras = osExtras; shell.TAR(args); } }
The above code prints the following output to a file named'out.txt'
. Note that for brevity, the text printed to terminal is not included in this sample output text. Below is merely the text left in the output file'out.txt'
Also, the'tar'
command, itself, will leave a tar-file named'MySourceFiles.tar'
in theNodeSearch
directory.
UNIX or DOS Shell Command:
$ cat out.txt searchLoops/TNRemove.java searchLoops/ITFind.java searchLoops/CmtNPoll.java searchLoops/ITGetIncl.java searchLoops/ITFindIncl.java searchLoops/TNFindL1Incl.java searchLoops/TNGetL1Incl.java searchLoops/TNPeekL1Incl.java searchLoops/ITGet.java searchLoops/TxNPeek.java searchLoops/TNPeek.java searchLoops/TxNGet.java searchLoops/TNCount.java searchLoops/TNPoll.java searchLoops/TNGetIncl.java searchLoops/TNFindIncl.java searchLoops/CmtNFind.java searchLoops/TxNFind.java searchLoops/TNFind.java searchLoops/TxNCount.java searchLoops/TNGet.java searchLoops/TNRemoveIncl.java searchLoops/ITPeek.java searchLoops/ITCount.java searchLoops/TNPeekIncl.java searchLoops/ITPeekIncl.java searchLoops/TxNPoll.java searchLoops/ITPollIncl.java searchLoops/TNPollIncl.java searchLoops/ITRemoveIncl.java searchLoops/TxNRemove.java searchLoops/CmtNRemove.java searchLoops/CmtNGet.java searchLoops/CmtNCount.java searchLoops/ITPoll.java searchLoops/CmtNPeek.java searchLoops/ITRemove.java
Hi-Lited Source-Code:- View Here: Torello/Java/OSExtras.java
- Open New Browser-Tab: Torello/Java/OSExtras.java
File Size: 5,841 Bytes Line Count: 136 '\n' Characters Found
-
-
Field Summary
Fields Modifier and Type Field File
currentWorkingDirectory
Consumer<Map<String,String>>
environmentVariableUpdater
ProcessBuilder.Redirect
errorRedirect
ProcessBuilder.Redirect
inputRedirect
boolean
mergeStdErrorAndStdOut
ProcessBuilder.Redirect
outputRedirect
-
Constructor Summary
Constructors Constructor Description OSExtras()
Builds a new instance of this class, and keeps the default values assigned.OSExtras(File currentWorkingDirectory, boolean mergeStdErrorAndStdOut, ProcessBuilder.Redirect outputRedirect, ProcessBuilder.Redirect inputRedirect, ProcessBuilder.Redirect errorRedirect, Consumer<Map<String,String>> environmentVariableUpdater)
Constructs an instance of this class
-
-
-
Field Detail
-
currentWorkingDirectory
public java.io.File currentWorkingDirectory
ThisUNIX Shell
command will be run with'currentWorkingDirectory'
set as the directory from whence this command is initiated.null
may be passed to this parameter. If it is, it will be ignored, and the process will be run in the whatever directory the O/S has asserted is the current working directory.
-
mergeStdErrorAndStdOut
public boolean mergeStdErrorAndStdOut
If thisboolean
is TRUE, bothStandard Output
andError Output
will be merged into a single stream of text. This can be helpful since error messages are usually printed adjacent-to or immediately-after relevant text from the standard output stream.
NOTE: The value of thisboolean
supersedes any redirect reference passed to parameteroutputRedirect
. When TRUE is passed here, the error stream will be empty, and the error redirect file or pipe will also remain empty. (And vice-versa, when FALSE,Standard Output
andError Output
are printed to independent output streams, and both heed the redirect parameters).
-
outputRedirect
public java.lang.ProcessBuilder.Redirect outputRedirect
Read the Java-Doc information for Javaclass java.lang.ProcessBuilder.Redirect
to learn how to redirect thisProcess'
STANDARD OUTPUT to a file on the file-system.null
may be passed to this parameter. If it is, it will be ignored.
-
inputRedirect
public java.lang.ProcessBuilder.Redirect inputRedirect
Read the Java-Doc information for Javaclass java.lang.ProcessBuilder.Redirect
to learn how to redirect file-system files as INPUT to thisProcess
. This parameter may receive'null'
.
-
errorRedirect
public java.lang.ProcessBuilder.Redirect errorRedirect
Read the Java-Doc information for Javaclass java.lang.ProcessBuilder.Redirect
to learn how to redirect thisProcess'
ERROR OUTPUT to a file on the file-system.null
may be passed to this parameter. If it is, it will be ignored.
-
environmentVariableUpdater
public java.util.function.Consumer<java.util.Map<java.lang.String,java.lang.String>> environmentVariableUpdater
Thisjava.util.function.Consumer
shall recieve ajava.util.Map<String, String>
containing all relevant environment variables, and their values. It may be modified to suit the purposes of thisUNIX
command. Any implementingclass
, or a lambda expression, is acceptable here.null
may be passed to this parameter. If it is, it will be ignored - and no environment-variable modifications shall occur.
-
-
Constructor Detail
-
OSExtras
public OSExtras()
Builds a new instance of this class, and keeps the default values assigned.
-
OSExtras
public OSExtras (java.io.File currentWorkingDirectory, boolean mergeStdErrorAndStdOut, java.lang.ProcessBuilder.Redirect outputRedirect, java.lang.ProcessBuilder.Redirect inputRedirect, java.lang.ProcessBuilder.Redirect errorRedirect, java.util.function.Consumer<java.util.Map<java.lang.String,java.lang.String>> environmentVariableUpdater)
Constructs an instance of this class- Parameters:
currentWorkingDirectory
- ThisUNIX Shell
command will be run with'currentWorkingDirectory'
set as the directory from whence this command is initiated.null
may be passed to this parameter. If it is, it will be ignored, and the process will be run in the whatever directory the O/S has asserted is the current working directory.mergeStdErrorAndStdOut
- If thisboolean
is TRUE, bothStandard Output
andError Output
will be merged into a single stream of text. This can be helpful since error messages are usually printed adjacent-to or immediately-after relevant text from the standard output stream.
NOTE: The value of thisboolean
supersedes any redirect reference passed to parameteroutputRedirect
. When TRUE is passed here, the error stream will be empty, and the error redirect file or pipe will also remain empty. (And vice-versa, when FALSE,Standard Output
andError Output
are printed to independent output streams, and both heed the redirect parameters).outputRedirect
- Read the Java-Doc information for Javaclass java.lang.ProcessBuilder.Redirect
to learn how to redirect thisProcess'
STANDARD OUTPUT to a file on the file-system.null
may be passed to this parameter. If it is, it will be ignored.inputRedirect
- Read the Java-Doc information for Javaclass java.lang.ProcessBuilder.Redirect
to learn how to redirect file-system files as INPUT to thisProcess
. This parameter may receive'null'
.errorRedirect
- Read the Java-Doc information for Javaclass java.lang.ProcessBuilder.Redirect
to learn how to redirect thisProcess'
ERROR OUTPUT to a file on the file-system.null
may be passed to this parameter. If it is, it will be ignored.environmentVariableUpdater
- Thisjava.util.function.Consumer
shall recieve ajava.util.Map<String, String>
containing all relevant environment variables, and their values. It may be modified to suit the purposes of thisUNIX
command. Any implementingclass
, or a lambda expression, is acceptable here.null
may be passed to this parameter. If it is, it will be ignored - and no environment-variable modifications shall occur.- Code:
- Exact Constructor Body:
this.currentWorkingDirectory = currentWorkingDirectory; this.mergeStdErrorAndStdOut = mergeStdErrorAndStdOut; this.outputRedirect = outputRedirect; this.inputRedirect = inputRedirect; this.errorRedirect = errorRedirect; this.environmentVariableUpdater = environmentVariableUpdater;
-
-
Method Detail
-
toString
public java.lang.String toString()
Generates aString
this class. The returnedString
merely encodes the class-names of the non-null fields.- Overrides:
toString
in classjava.lang.Object
- Returns:
- A simple representation of this class, as a
java.lang.String
- Code:
- Exact Method Body:
// private static String toStrApp(Appendable a) // { return (a == null) ? "null\n" : (a.getClass().getName() + '\n'); } return "currentWorkingDirectory: " + toStrApp(this.currentWorkingDirectory) + "mergeStdErrorAndStdOut: " + this.mergeStdErrorAndStdOut + '\n' + "outputRedirect: " + toStrApp(this.outputRedirect) + "inputRedirect: " + toStrApp(this.inputRedirect) + "errorRedirect: " + toStrApp(this.errorRedirect) + "environmentVariableUpdater: " + toStrApp(this.environmentVariableUpdater);
-
equals
public boolean equals(java.lang.Object other)
Checks for equality based on whether two instances have identical references.- Overrides:
equals
in classjava.lang.Object
- Parameters:
other
- Any Java Object. Only a valid sub-class ofOSExtras
could possibly produce aTRUE
return value- Returns:
TRUE
if and only if each of theOSExtras
fields are identical references in both'this'
and'other'
.- Code:
- Exact Method Body:
if (other == null) return false; if (! OSExtras.class.isAssignableFrom(other.getClass())) return false; OSExtras o = (OSExtras) other; return (this.currentWorkingDirectory == o.currentWorkingDirectory) && (this.mergeStdErrorAndStdOut == o.mergeStdErrorAndStdOut) && (this.outputRedirect == o.outputRedirect) && (this.inputRedirect == o.inputRedirect) && (this.errorRedirect == o.errorRedirect) && (this.environmentVariableUpdater == o.environmentVariableUpdater);
-
clone
public OSExtras clone()
Makes a copy of'this'
instance and returns it.- Overrides:
clone
in classjava.lang.Object
- Returns:
- a clone of
'this'
instance - Code:
- Exact Method Body:
return new OSExtras( this.currentWorkingDirectory, this.mergeStdErrorAndStdOut, this.outputRedirect, this.inputRedirect, this.errorRedirect, this.environmentVariableUpdater );
-
-