PageRenderTime 29ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/gmail/ianspigeon/applauncher/launchers/Launcher.java

https://bitbucket.org/ianspigeon/applauncher
Java | 153 lines | 129 code | 23 blank | 1 comment | 8 complexity | f9d8bc558ddf6f7ef3b46c358cbe8be0 MD5 | raw file
  1. package com.gmail.ianspigeon.applauncher.launchers;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.Arrays;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. public abstract class Launcher {
  8. protected final String SUCCESS = "SUCCESS";
  9. protected final String ERROR_NOT_PRIVILEGED = "Insufficient privileges to launch command.";
  10. protected static boolean isWindows() {
  11. String os = System.getProperty("os.name").toLowerCase();
  12. return (os.indexOf("windows") >= 0);
  13. }
  14. protected static boolean isLinux() {
  15. String os = System.getProperty("os.name").toLowerCase();
  16. return (os.indexOf("linux") >= 0);
  17. }
  18. protected void validateParameter(String name, String value, String validationRegEx) throws Exception {
  19. if (value == null || value.length() == 0) {
  20. throw new Exception("Required parameter '" + name + "' missing.");
  21. } else {
  22. Pattern pattern = Pattern.compile(validationRegEx);
  23. Matcher matcher = pattern.matcher(value);
  24. if (! matcher.matches()) {
  25. throw new Exception("Required parameter '" + name + "' has invalid value.");
  26. }
  27. }
  28. }
  29. protected String executeWindowsCommand(final String[] command){
  30. try {
  31. String result = java.security.AccessController.doPrivileged(
  32. new java.security.PrivilegedAction<String>() {
  33. public String run() {
  34. try {
  35. File location = new File(command[0]);
  36. if ( location.exists() ) {
  37. System.out.println("Executing privileged command, \'" + Arrays.toString(command) + "\'." );
  38. Runtime.getRuntime().exec(command);
  39. return SUCCESS;
  40. } else {
  41. // Check x86 dir on 64bit systems if not found
  42. File x86Path = new File("C:\\Program Files (x86)\\");
  43. if ( x86Path.exists() && command[0].indexOf(x86Path.toString()) == 0) {
  44. String[] x86cmd = {};
  45. System.arraycopy(command, 0, x86cmd, 0, command.length);
  46. x86cmd[0] = x86cmd[0].replace("C:\\Program Files\\", "C:\\Program Files (x86)\\");
  47. location = new File(command[0]);
  48. if ( location.exists() ) {
  49. System.out.println("Executing privileged command, \'" + Arrays.toString(x86cmd) + "\'." );
  50. Runtime.getRuntime().exec(command);
  51. return SUCCESS;
  52. }
  53. }
  54. return "Command not found, \'" + command[0] + "\'.";
  55. }
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. return "Recieved IOException: " + e.getMessage();
  59. }
  60. }
  61. }
  62. );
  63. return result;
  64. } catch ( SecurityException e ) {
  65. return ERROR_NOT_PRIVILEGED;
  66. }
  67. }
  68. protected String executeLinuxCommand(final String[] command){
  69. try {
  70. String result = java.security.AccessController.doPrivileged(
  71. new java.security.PrivilegedAction<String>() {
  72. public String run() {
  73. try {
  74. File location = new File(command[0]);
  75. if ( location.exists() ) {
  76. System.out.println("Executing privileged command, \'" + Arrays.toString(command) + "\'." );
  77. Runtime runtime = Runtime.getRuntime();
  78. runtime.exec(command);
  79. return SUCCESS;
  80. } else {
  81. return "Command not found, \'" + command[0] + "\'.";
  82. }
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. return "Recieved IOException: " + e.getMessage();
  86. }
  87. }
  88. }
  89. );
  90. return result;
  91. } catch ( SecurityException e ) {
  92. return ERROR_NOT_PRIVILEGED;
  93. }
  94. }
  95. }