PageRenderTime 41ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/utils/files/FileExecutor.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 173 lines | 134 code | 17 blank | 22 comment | 27 complexity | 49c1255fe3329d0fa52395009e1c8361 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. package mpv5.utils.files;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import java.util.regex.Matcher;
  14. import java.util.regex.Pattern;
  15. import mpv5.Main;
  16. import mpv5.db.objects.User;
  17. import mpv5.globals.LocalSettings;
  18. import mpv5.logging.Log;
  19. import mpv5.ui.dialogs.Notificator;
  20. /**
  21. *
  22. * @author andreas.weber
  23. */
  24. public class FileExecutor {
  25. /**
  26. * Runs a command,non- blocking, in the yabs home directory, with sudo if
  27. * root password is available and os is unix
  28. *
  29. * @param command
  30. */
  31. public static synchronized void run(String command) {
  32. if (Main.osIsWindows) {
  33. runWin(command);
  34. } else if (Main.osIsMacOsX || Main.osIsLinux || Main.osIsSolaris) {
  35. runUnixWPassword(command);
  36. }
  37. }
  38. /**
  39. * Runs a command,non- blocking, in the yabs home directory, with sudo if
  40. * root password is available and os is unix
  41. *
  42. * @param command
  43. */
  44. public static synchronized void run(String[] commandArgs, List<Process> processHolder) {
  45. runAlternate(commandArgs, processHolder);
  46. }
  47. /**
  48. * Runs a command,non- blocking, in the yabs home directory, with sudo if
  49. * root password is available and os is unix
  50. *
  51. * @param command
  52. */
  53. public static synchronized void run(String[] commandArgs) {
  54. runAlternate(commandArgs, new ArrayList<Process>());
  55. }
  56. private static void runAlternate(String[] commandArrq, final List<Process> processHolder) {
  57. final ProcessBuilder builder = new ProcessBuilder(commandArrq);
  58. Map<String, String> environment = builder.environment();
  59. environment.put("path", ";"); // Clearing the path variable;
  60. environment.put("path", commandArrq[0] + File.pathSeparator);
  61. builder.directory(FileDirectoryHandler.getnewTemporaryDirectory());
  62. Log.Debug(FileExecutor.class, "runAlternate" + Arrays.asList(commandArrq));
  63. Runnable runnable = new Runnable() {
  64. @Override
  65. public void run() {
  66. try {
  67. Log.Print(builder.command());
  68. Log.Print(builder.directory());
  69. Log.Print(builder.environment());
  70. Process oos = builder.start();
  71. processHolder.add(oos);
  72. Main.addProcessToClose(oos);
  73. InputStream is = oos.getErrorStream();
  74. InputStreamReader isr = new InputStreamReader(is);
  75. BufferedReader br = new BufferedReader(isr);
  76. String line;
  77. while ((line = br.readLine()) != null) {
  78. mpv5.logging.Log.Print(line);
  79. }
  80. } catch (IOException ex) {
  81. Notificator.raiseNotification(ex, true);
  82. }
  83. }
  84. };
  85. new Thread(runnable).start();
  86. }
  87. private static void runWin(String command) {
  88. String output = null;
  89. File workDir = new File(Main.MPPATH);
  90. try {
  91. Process p = Runtime.getRuntime().exec(command, null, workDir);
  92. Main.addProcessToClose(p);
  93. int exitval = p.waitFor();
  94. if (exitval == 0) {
  95. BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
  96. while ((output = stdInput.readLine()) != null) {
  97. Log.Print(output);
  98. }
  99. } else {
  100. BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  101. while ((output = stdErr.readLine()) != null) {
  102. Log.Print(output);
  103. }
  104. }
  105. } catch (Exception e) {
  106. Log.Debug(e);
  107. }
  108. }
  109. private static void runUnixWPassword(String command) {
  110. String pw = null;
  111. if (LocalSettings.hasProperty(LocalSettings.CMD_PASSWORD)
  112. && LocalSettings.getProperty(LocalSettings.CMD_PASSWORD).length() > 0) {
  113. pw = LocalSettings.getProperty("cmdpassword");
  114. } else if (User.getCurrentUser().getProperties().hasProperty("cmdpassword")) {
  115. pw = User.getCurrentUser().getProperties().getProperty("cmdpassword");
  116. }
  117. if (pw != null) {
  118. String script = "#!/usr/bin/expect -d" + "\n"
  119. + "set password " + pw + "\n"
  120. + "set timeout -1" + "\n"
  121. + "spawn ssh root@localhost \"" + command + "\"" + "\n"
  122. + "match_max 100000" + "\n"
  123. + "expect \"*?assword:*\"" + "\n"
  124. + "send -- \"$password\\r\"" + "\n"
  125. + "send -- \"\\r\"" + "\n"
  126. + "expect eof";
  127. File rw = FileDirectoryHandler.getTempFile("sh");
  128. FileReaderWriter frw = new FileReaderWriter(rw);
  129. frw.writeOnce(script);
  130. rw.setExecutable(true);
  131. command = rw.getPath();
  132. Log.Debug(FileExecutor.class, "Running script: " + rw);
  133. }
  134. String output = null;
  135. File workDir = new File(Main.MPPATH);
  136. Log.Debug(FileExecutor.class, command);
  137. try {
  138. Process p = Runtime.getRuntime().exec(command, null, workDir);
  139. Main.addProcessToClose(p);
  140. int exitval = p.waitFor();
  141. if (exitval == 0) {
  142. BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
  143. while ((output = stdInput.readLine()) != null) {
  144. Log.Print(output);
  145. }
  146. } else {
  147. BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  148. while ((output = stdErr.readLine()) != null) {
  149. Log.Print(output);
  150. }
  151. }
  152. } catch (Exception e) {
  153. Log.Debug(e);
  154. }
  155. }
  156. }