Skip to content

Commit

Permalink
PAYARA 1739 Pre and Post boot command parsing problem (#1665)
Browse files Browse the repository at this point in the history
* Corrected the two parsing methods to the same one

* Changed comment parsing line to still read commands before comment on same line

* Changed parsing to allow for multiple spaces and corrected comment removal code
  • Loading branch information
MattGill98 authored and smillidge committed Jun 20, 2017
1 parent e666def commit 30dc4e9
Showing 1 changed file with 42 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,7 @@ private void startConsole() throws IOException {
continue;
}
CommandRunner cmdRunner = gf.getCommandRunner();
String[] tokens = command.split("\\s");
CommandResult result = cmdRunner.run(tokens[0], Arrays.copyOfRange(tokens, 1, tokens.length));
System.out.println(result.getExitStatus());
System.out.println(result.getOutput());
if (result.getFailureCause() != null) {
result.getFailureCause().printStackTrace();
}
runCommand(cmdRunner, command);
}
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -218,6 +212,43 @@ public void run() {
});

}

/**
* Runs a command read from a string
* @param cmdRunner
* @param line
* @throws GlassFishException
*/
private void runCommand(CommandRunner cmdRunner, String line) throws GlassFishException, IOException {

line = cleanCommand(line);
if(line == null) {
return;
}

System.out.println("Running command: " + line);
String[] tokens = line.split("\\s+");
CommandResult result = cmdRunner.run(tokens[0], Arrays.copyOfRange(tokens, 1, tokens.length));
System.out.println(result.getOutput());
if(result.getFailureCause() != null) {
result.getFailureCause().printStackTrace();
}
}

/**
* Cleans a command read from a string
* @param line
*/
private String cleanCommand(String line) {
if(line == null) {
return null;
}
line = line.replaceAll("#.*", ""); // Removes comments
if (line.isEmpty() || line.replaceAll("\\s", "").isEmpty()) {
return null;
}
return line;
}

/**
* Runs a series of commands from a file
Expand All @@ -230,25 +261,11 @@ private void doBootCommands(String file) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {

System.out.println("Reading in commandments from " + file);
String line = reader.readLine();;
String line = reader.readLine();
CommandRunner cmdRunner = gf.getCommandRunner();

while (line != null) {

String[] commmandParts = cleanCommand(line);
CommandRunner runner = gf.getCommandRunner();
CommandResult result;
if (!(commmandParts[0].isEmpty() || commmandParts[0].equals(" "))){

if (commmandParts.length == 1){
result = runner.run(commmandParts[0]);
} else {
String[] argv = commmandParts[1].split(" ");
result = runner.run(commmandParts[0], argv);
}
System.out.println(result.getOutput());
if (result.getFailureCause() != null){
throw result.getFailureCause();
}
}
runCommand(cmdRunner, line);
line = reader.readLine();
}
} catch (IOException ex) {
Expand All @@ -258,22 +275,6 @@ private void doBootCommands(String file) {
}
}

/**
* Cleans up a single line command to be space-seperated, comments removed etc.
* @param command
* @return
*/
private String[] cleanCommand(String command){
String line = command.split("#")[0];
line = line.trim();
if (!line.startsWith("set ")){
line = line.replaceAll("=", " ");
}
line = line.replaceAll("(\\s+)", " ");
String[] split = line.split(" ", 2);
return split;
}


}

Expand Down

0 comments on commit 30dc4e9

Please sign in to comment.