You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExtProcess.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2016 Brian P. Hinz. All Rights Reserved.
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  17. * USA.
  18. */
  19. package com.tigervnc.vncviewer;
  20. import java.io.BufferedReader;
  21. import java.io.File;
  22. import java.io.InputStream;
  23. import java.io.InputStreamReader;
  24. import java.util.*;
  25. import com.tigervnc.rdr.*;
  26. import com.tigervnc.rfb.*;
  27. import com.tigervnc.rfb.Exception;
  28. import com.tigervnc.network.*;
  29. import static com.tigervnc.vncviewer.Parameters.*;
  30. public class ExtProcess implements Runnable {
  31. private String cmd = null;
  32. private LogWriter vlog = null;
  33. private boolean shutdown = false;
  34. private Process pid = null;
  35. private static class MyProcessLogger extends Thread {
  36. private final BufferedReader err;
  37. private final LogWriter vlog;
  38. public MyProcessLogger(Process p, LogWriter vlog) {
  39. InputStreamReader reader =
  40. new InputStreamReader(p.getErrorStream());
  41. err = new BufferedReader(reader);
  42. this.vlog = vlog;
  43. }
  44. @Override
  45. public void run() {
  46. try {
  47. while (true) {
  48. String msg = err.readLine();
  49. if (msg != null)
  50. vlog.info(msg);
  51. }
  52. } catch(java.io.IOException e) {
  53. vlog.info(e.getMessage());
  54. } finally {
  55. try {
  56. if (err != null)
  57. err.close();
  58. } catch (java.io.IOException e ) { }
  59. }
  60. }
  61. }
  62. private static class MyShutdownHook extends Thread {
  63. private Process proc = null;
  64. public MyShutdownHook(Process p) {
  65. proc = p;
  66. }
  67. @Override
  68. public void run() {
  69. try {
  70. proc.exitValue();
  71. } catch (IllegalThreadStateException e) {
  72. try {
  73. // wait for CConn to shutdown the socket
  74. Thread.sleep(500);
  75. } catch(InterruptedException ie) { }
  76. proc.destroy();
  77. }
  78. }
  79. }
  80. public ExtProcess(String command, LogWriter vlog, boolean shutdown) {
  81. cmd = command;
  82. this.vlog = vlog;
  83. this.shutdown = shutdown;
  84. }
  85. public ExtProcess(String command, LogWriter vlog) {
  86. this(command, vlog, false);
  87. }
  88. public ExtProcess(String command) {
  89. this(command, null, false);
  90. }
  91. public void run() {
  92. try {
  93. Runtime runtime = Runtime.getRuntime();
  94. pid = runtime.exec(cmd);
  95. if (shutdown)
  96. runtime.addShutdownHook(new MyShutdownHook(pid));
  97. if (vlog != null)
  98. new MyProcessLogger(pid, vlog).start();
  99. pid.waitFor();
  100. } catch(InterruptedException e) {
  101. vlog.info(e.getMessage());
  102. } catch(java.io.IOException e) {
  103. vlog.info(e.getMessage());
  104. }
  105. }
  106. //static LogWriter vlog = new LogWriter("ExtProcess");
  107. }