/plugins/RubyPlugin/tags/release-0_9_3/src/org/jedit/ruby/utils/CommandUtils.java

# · Java · 178 lines · 128 code · 21 blank · 29 comment · 21 complexity · bfb9e0c5698112529ad80ff01b77423e MD5 · raw file

  1. /*
  2. * CommandUtils.java -
  3. *
  4. * Copyright 2005 Robert McKinnon
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. package org.jedit.ruby.utils;
  21. import org.gjt.sp.jedit.jEdit;
  22. import org.gjt.sp.jedit.textarea.JEditTextArea;
  23. import org.gjt.sp.jedit.textarea.TextArea;
  24. import org.jedit.ruby.RubyPlugin;
  25. import java.io.*;
  26. import java.util.TimerTask;
  27. import java.lang.reflect.InvocationTargetException;
  28. import java.lang.reflect.Method;
  29. /**
  30. * @author robmckinnon at users.sourceforge.net
  31. */
  32. public final class CommandUtils {
  33. private static final String RUBY_DIR = "ruby";
  34. public static boolean isRubyInstalled() {
  35. try {
  36. String result;
  37. if (CommandUtils.isWindows()) {
  38. String text = "exec ruby.bat -v\n";
  39. File commandFile = CommandUtils.getCommandFile("ruby_version.bat", false, text);
  40. String command = '"' + commandFile.getPath() + '"';
  41. result = CommandUtils.getOutput(command, false);
  42. } else {
  43. result = CommandUtils.getOutput("ruby -v", false);
  44. }
  45. RubyPlugin.log("Ruby installed: " + result, CommandUtils.class);
  46. return true;
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. RubyPlugin.error(jEdit.getProperty("ruby.ruby-not-found.message"), CommandUtils.class);
  50. return false;
  51. }
  52. }
  53. public static File getStoragePath(String fileName) {
  54. File storageDirectory = new File(jEdit.getSettingsDirectory() + File.separatorChar + RUBY_DIR);
  55. if (!storageDirectory.exists()) {
  56. storageDirectory.mkdir();
  57. }
  58. return new File(storageDirectory.getPath() + File.separatorChar + fileName);
  59. }
  60. /**
  61. * @return string output of execution of the supplied system command
  62. */
  63. public static String getOutput(String command, boolean retryOnFail) throws IOException, InterruptedException {
  64. return getOutput(command, retryOnFail, 1500);
  65. }
  66. public static String getOutput(String command, boolean retryOnFail, int timeout) throws IOException, InterruptedException {
  67. StringBuffer buffer = new StringBuffer();
  68. Process process = run(command, timeout);
  69. readStream(process.getInputStream(), buffer);
  70. readStream(process.getErrorStream(), buffer);
  71. if (buffer.length() == 0 && retryOnFail) {
  72. return getOutput(command, false);
  73. } else {
  74. return buffer.toString();
  75. }
  76. }
  77. private static void readStream(InputStream stream, StringBuffer buffer) throws IOException {
  78. BufferedReader reader = null;
  79. try {
  80. reader = new BufferedReader(new InputStreamReader(stream));
  81. if (reader.ready()) {
  82. buffer.append(reader.readLine());
  83. while (reader.ready()) {
  84. buffer.append('\n').append(reader.readLine());
  85. }
  86. }
  87. } finally {
  88. if (reader != null) {
  89. reader.close();
  90. }
  91. }
  92. }
  93. /**
  94. * Runs supplied system command and returns process object.
  95. */
  96. private static Process run(String command, int timeout) throws IOException, InterruptedException {
  97. final Process process = Runtime.getRuntime().exec(command);
  98. TimerTask task = null;
  99. if (timeout != -1) {
  100. task = new TimerTask() {
  101. public void run() {
  102. synchronized (process) {
  103. // kills blocked subprocess
  104. process.destroy();
  105. }
  106. }
  107. };
  108. java.util.Timer timer = new java.util.Timer();
  109. timer.schedule(task, timeout);
  110. }
  111. process.waitFor();
  112. if (task != null) {
  113. synchronized (process) {
  114. task.cancel();
  115. }
  116. }
  117. return process;
  118. }
  119. public static File getCommandFile(String fileName, boolean forceCreation, String text) throws IOException, InterruptedException {
  120. File commandFile = getStoragePath(fileName);
  121. if (forceCreation || !commandFile.exists()) {
  122. PrintWriter writer = new PrintWriter(new FileWriter(commandFile));
  123. writer.print(text);
  124. writer.close();
  125. if (!isWindows()) {
  126. RubyPlugin.log(getOutput("chmod +x " + commandFile.getPath(), false), RubyPlugin.class);
  127. }
  128. }
  129. return commandFile;
  130. }
  131. public static boolean isWindows() {
  132. return System.getProperty("os.name").toLowerCase().indexOf("windows") != -1;
  133. }
  134. public static Object invoke(Object buffer, String methodName, Class[] paramTypes, Object... args) throws IllegalAccessException, InvocationTargetException {
  135. Method method = null;
  136. try {
  137. method = buffer.getClass().getMethod(methodName, paramTypes);
  138. } catch (Exception e) {
  139. Method[] methods = buffer.getClass().getMethods();
  140. for (Method m : methods) {
  141. if (m.getName().equals(methodName)) {
  142. method = m;
  143. }
  144. }
  145. }
  146. return method.invoke(buffer, args);
  147. }
  148. public static Object getBuffer(TextArea textArea) {
  149. Object buffer = null;
  150. try {
  151. buffer = invoke(textArea, "getBuffer", null, (Object[])null);
  152. } catch (Exception e) {
  153. RubyPlugin.log("error getting buffer " + e.getMessage(), CommandUtils.class);
  154. }
  155. return buffer;
  156. }
  157. }