Created
September 30, 2014 14:46
-
-
Save Sythelux/afd85bb2e68e334c90df to your computer and use it in GitHub Desktop.
Class to convert Processbuilder to Linux Shell commands.
This file contains hidden or 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
import java.io.File; | |
import java.lang.ProcessBuilder.Redirect.Type; | |
import java.util.Arrays; | |
import org.apache.log4j.BasicConfigurator; | |
import org.apache.log4j.Level; | |
import org.apache.log4j.Logger; | |
/** This Class Provides a Way to Serialize and deserialize Simple Processbuilder Commands. For this it will use the Bash Syntax. | |
* | |
* @author Aron Schaub */ | |
public class ProcessBuilderSerializer { | |
private static Logger Log = Logger.getLogger(ProcessBuilderSerializer.class); | |
/** just for Example Purpose. | |
* | |
* @param args will not be used */ | |
public static void main(String[] args) { | |
BasicConfigurator.configure(); | |
Logger.getRootLogger().setLevel(Level.ALL); | |
String t1 = "echo 20 > /dev/null"; | |
String t2 = "echo < /etc/myFile"; | |
String t3 = "echo 20 2> /dev/null"; | |
String t4 = "echo 20"; | |
String t5 = "echo 20 > /dev/null < /etc/myFile"; | |
String t6 = "echo 20 < /etc/myFile > /dev/null"; | |
String t7 = "echo 20 2>&1 > /dev/null"; | |
Log.debug("--------------------------------------------"); | |
Log.debug(t1); | |
Log.debug(ProcessBuilderToBash(BashToProcessBuilder(t1))); | |
Log.debug("--------------------------------------------"); | |
Log.debug(t2); | |
Log.debug(ProcessBuilderToBash(BashToProcessBuilder(t2))); | |
Log.debug("--------------------------------------------"); | |
Log.debug(t3); | |
Log.debug(ProcessBuilderToBash(BashToProcessBuilder(t3))); | |
Log.debug("--------------------------------------------"); | |
Log.debug(t4); | |
Log.debug(ProcessBuilderToBash(BashToProcessBuilder(t4))); | |
Log.debug("--------------------------------------------"); | |
Log.debug(t5); | |
Log.debug(ProcessBuilderToBash(BashToProcessBuilder(t5))); | |
Log.debug("--------------------------------------------"); | |
Log.debug(t6); | |
Log.debug(ProcessBuilderToBash(BashToProcessBuilder(t6))); | |
Log.debug("--------------------------------------------"); | |
Log.debug(t7); | |
Log.debug(ProcessBuilderToBash(BashToProcessBuilder(t7))); | |
} | |
/** creates a String out of the given ProcessBuilder Object@return | |
* | |
* @param pb Fully functional Processbuilder Object | |
* @return a String without \n or other special Characters. */ | |
public static String ProcessBuilderToBash(ProcessBuilder pb) { | |
StringBuilder sb = new StringBuilder(); | |
for (String string : pb.command()) { | |
sb.append(string).append(" "); | |
} | |
if (pb.redirectErrorStream()) { | |
sb.append("2>&1 "); | |
} | |
if (pb.redirectInput().file() != null) { | |
sb.append("< ").append(pb.redirectInput().file()).append(" "); | |
} | |
if (pb.redirectOutput().file() != null) { | |
if (pb.redirectOutput().type().equals(Type.WRITE)) { | |
sb.append("> "); | |
} else if (pb.redirectOutput().type().equals(Type.APPEND)) { | |
sb.append(">> "); | |
} | |
sb.append(pb.redirectOutput().file()).append(" "); | |
} | |
if (pb.redirectError().file() != null) { | |
sb.append("2> ").append(pb.redirectError().file()); | |
} | |
String ret = sb.toString(); | |
if (ret.endsWith(" ")) { | |
ret = ret.substring(0, ret.lastIndexOf(" ")); | |
} | |
return ret; | |
} | |
/** creates A Processbuilder Object out of a String | |
* | |
* @param s String representing a simple Bash command. | |
* @return Processbuilder object, that can be run. */ | |
public static ProcessBuilder BashToProcessBuilder(String s) { | |
String splitted[] = s.split("(?=2>&1|<|2>|>>| >)"); | |
ProcessBuilder pb = new ProcessBuilder(); | |
for (String string : splitted) { | |
Log.debug("\t" + string); | |
if (string.contains("2>&1")) { | |
pb.redirectErrorStream(true); | |
} else if (string.contains("< ")) { | |
pb.redirectInput(new File(string.replace("< ", ""))); | |
} else if (string.contains(">> ")) { | |
pb.redirectOutput(new File(string.replace(">> ", ""))); | |
} else if (string.contains("2> ")) { | |
pb.redirectError(new File(string.replace("2> ", ""))); | |
} else if (string.contains(" > ")) { | |
pb.redirectOutput(new File(string.replace(" > ", ""))); | |
} else { | |
pb.command().addAll(Arrays.asList(string.split("\\s"))); | |
} | |
} | |
return pb; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment