-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Includes little fixes and changes that don't warrant separate commits. - Javadoc is added for all classes and fixed where present - Reformat all files and save them w/ UTF8 encoding
- Loading branch information
1 parent
7a40a93
commit aa2d985
Showing
10 changed files
with
173 additions
and
38 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
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 |
---|---|---|
|
@@ -31,20 +31,44 @@ | |
import java.util.logging.Level; | ||
|
||
/** | ||
* An interface to allow running programs from the OS. Implementations have to set the | ||
* {@link CommandLine} for the application to be executed, by overriding the | ||
* {@link AppRunner#setCommand()} and calling {@link AppRunner#setCommand(CommandLine)} inside the | ||
* method. | ||
* | ||
* @author Shubham Rao <[email protected]> | ||
* @see OSLib | ||
* @author Shubham Rao ([email protected]) | ||
*/ | ||
abstract class AppRunner { | ||
public abstract class AppRunner { | ||
|
||
private static final java.util.logging.Logger logger = Log.logger; | ||
|
||
/** | ||
* Sets the {@link CommandLine} to execute. | ||
* | ||
* This function must be called by implementations inside {@link AppRunner#setCommand()} when it | ||
* is overridden. | ||
* | ||
* @param command command to execute | ||
*/ | ||
protected void setCommand(CommandLine command) { | ||
this.command = command; | ||
} | ||
|
||
private CommandLine command; | ||
|
||
/** | ||
* Current Architecture | ||
* | ||
* @see OSLib#getCurrentArchitecture() | ||
*/ | ||
public final OSLib.Architecture arch; | ||
|
||
/** | ||
* Current Operating System | ||
* | ||
* @see OSLib#getCurrentOS() | ||
*/ | ||
public final OSLib.OperatingSystem os; | ||
|
||
AppRunner() { | ||
|
@@ -58,6 +82,10 @@ CommandLine getCommand() { | |
return command; | ||
} | ||
|
||
/** | ||
* Runs the OS specific Command. Exit code is logged. | ||
* | ||
*/ | ||
public void run() { | ||
setCommand(); | ||
ProcessBuilder pb = new ProcessBuilder(command.getFullCommand()); | ||
|
@@ -67,18 +95,22 @@ public void run() { | |
new Thread(() -> { | ||
try { | ||
logger.log(Level.INFO, "Exit Code: {0} ", p.waitFor()); | ||
} | ||
catch (InterruptedException ex) { | ||
logger.log(Level.SEVERE, null, ex); | ||
} catch (InterruptedException ex) { | ||
logger.log(Level.SEVERE, "Error getting exit code", ex); | ||
} | ||
} | ||
).start(); | ||
} | ||
catch (IOException ex) { | ||
logger.log(Level.SEVERE, null, ex); | ||
} catch (IOException ex) { | ||
logger.log(Level.SEVERE, "Error in running program.", ex); | ||
} | ||
} | ||
|
||
/** | ||
* Class representing a Command along with its arguments. | ||
* | ||
* Encapsulates an OS dependent Command, with executable's name and its arguments. | ||
* @author Shubham Rao ([email protected]) | ||
*/ | ||
protected class CommandLine { | ||
|
||
private String commandName; | ||
|
@@ -99,38 +131,83 @@ protected class CommandLine { | |
this(command, ""); | ||
} | ||
|
||
/** | ||
* Gets the name of executable. | ||
* | ||
* @return name of executable | ||
*/ | ||
public String getCommandName() { | ||
return commandName; | ||
} | ||
|
||
/** | ||
* Sets the name of executable. | ||
* | ||
* @param commandName name of executable | ||
*/ | ||
public void setCommandName(String commandName) { | ||
this.commandName = commandName; | ||
updateFullCommand(); | ||
} | ||
|
||
/** | ||
* Gets the arguments | ||
* | ||
* @return arguments | ||
*/ | ||
public List<String> getArguments() { | ||
return arguments; | ||
} | ||
|
||
/** | ||
* Adds arguments to the command. | ||
* | ||
* @param args arguments | ||
*/ | ||
public final void addArguments(String... args) { | ||
arguments.addAll(Arrays.asList(args)); | ||
updateFullCommand(); | ||
} | ||
|
||
/** | ||
* Replaces arguments of the command. | ||
* | ||
* @param arguments list containing arguments | ||
*/ | ||
public void setArguments(ArrayList<String> arguments) { | ||
this.arguments = arguments; | ||
updateFullCommand(); | ||
} | ||
|
||
/** | ||
* Returns the command name along with its arguments. | ||
* | ||
* First element is the command name, remaining elements are its arguments. | ||
* | ||
* @return command with arguments included. | ||
*/ | ||
public List<String> getFullCommand() { | ||
logger.log(Level.INFO, "Command: {0}", this.toString()); | ||
return fullCommand; | ||
} | ||
|
||
/** | ||
* Sets the command name along with its arguments. | ||
* | ||
* First element must be the command name, | ||
* remaining elements, its arguments. | ||
* | ||
* @param fullCommand command along with arguments | ||
*/ | ||
public void setFullCommand(ArrayList<String> fullCommand) { | ||
this.fullCommand = fullCommand; | ||
} | ||
|
||
/** | ||
* Returns command and its arguments as a String. | ||
* | ||
* @return command and its arguments as String | ||
*/ | ||
@Override | ||
public String toString() { | ||
StringBuilder command = new StringBuilder(this.fullCommand.get(0)); | ||
|
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 |
---|---|---|
|
@@ -24,7 +24,6 @@ | |
package com.github.cshubhamrao.AUtDv2.os; | ||
|
||
import com.github.cshubhamrao.AUtDv2.util.Log; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
|
@@ -38,14 +37,20 @@ | |
import java.util.stream.Stream; | ||
|
||
/** | ||
* Runs mysqldump to create DB dumps. The dumps are stored as {@code dbName.sql} in the current | ||
* directory. The database is assumed to exist. | ||
* | ||
* @author Shubham Rao | ||
* @author Shubham Rao ([email protected]) | ||
*/ | ||
public class MySqlDumpRunner extends AppRunner { | ||
|
||
private static final Logger logger = Log.logger; | ||
private final String dbName; | ||
|
||
/** | ||
* | ||
* @param dbName name of database to dump | ||
*/ | ||
public MySqlDumpRunner(String dbName) { | ||
this.dbName = dbName; | ||
} | ||
|
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 |
---|---|---|
|
@@ -39,15 +39,21 @@ | |
import java.util.stream.Stream; | ||
|
||
/** | ||
* Finds and runs mysql to restore DBs from backup | ||
* | ||
* @author "Shubham Rao <[email protected]>" | ||
* @author Shubham Rao ([email protected]) | ||
*/ | ||
public class MySqlImportRunner extends AppRunner { | ||
|
||
private static final Logger logger = Log.logger; | ||
private final String sqlFile; | ||
private final String dbName; | ||
|
||
/** | ||
* | ||
* @param sqlFile Path to .sql file containing DB Dump. | ||
* @param dbName Name of database to create. | ||
*/ | ||
public MySqlImportRunner(String sqlFile, String dbName) { | ||
this.sqlFile = sqlFile; | ||
this.dbName = dbName; | ||
|
@@ -61,7 +67,7 @@ void setCommand() { | |
String cmd = Paths.get(System.getenv("WINDIR"), "system32", "cmd.exe").toString(); | ||
command.setCommandName(cmd); | ||
command.addArguments("/C"); | ||
command.addArguments("start", "\"Creating MySQL Dump\""); | ||
command.addArguments("start", "\"Importing from MySQL Dump\""); | ||
command.addArguments("/D", windowsLocation()); | ||
command.addArguments("cmd /K", "mysql.exe"); | ||
command.addArguments("--user=root", "--password", "--verbose"); | ||
|
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 |
---|---|---|
|
@@ -36,8 +36,9 @@ | |
import java.util.stream.Stream; | ||
|
||
/** | ||
* Finds and runs the mysql Command Line | ||
* | ||
* @author "Shubham Rao <[email protected]>" | ||
* @author Shubham Rao ([email protected]) | ||
*/ | ||
public class MySqlRunner extends AppRunner { | ||
|
||
|
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 |
---|---|---|
|
@@ -34,17 +34,14 @@ | |
import java.util.logging.Level; | ||
|
||
/** | ||
* Finds and runs NetBeans | ||
* | ||
* @author Shubham Rao <[email protected]> | ||
* @author Shubham Rao ([email protected]) | ||
*/ | ||
public class NetBeansRunner extends AppRunner { | ||
|
||
private static final java.util.logging.Logger logger = Log.logger; | ||
|
||
public NetBeansRunner() { | ||
super(); | ||
} | ||
|
||
@Override | ||
void setCommand() { | ||
String location = ""; | ||
|
@@ -73,8 +70,7 @@ private String windowsLocation() { | |
nbLocs.add(pat); | ||
logger.log(Level.INFO, "Added {0} to nbLocs", pat.toString()); | ||
}); | ||
} | ||
catch (IOException ex) { | ||
} catch (IOException ex) { | ||
logger.log(Level.SEVERE, null, ex); | ||
} | ||
} | ||
|
Oops, something went wrong.