PageRenderTime 30ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/src/org/babelserver/nb/rebolmodule/settings/RebolExecutable.java

https://bitbucket.org/hallyhaa/rebol-module
Java | 107 lines | 77 code | 18 blank | 12 comment | 10 complexity | 4e1175c1f8ef9f1cfbfe353269ae980b MD5 | raw file
  1. package org.babelserver.nb.rebolmodule.settings;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.Serializable;
  7. import org.netbeans.api.extexecution.ExternalProcessBuilder;
  8. /**
  9. * Represents a Rebol executable file
  10. */
  11. public final class RebolExecutable implements Serializable {
  12. private String path;
  13. private String displayName;
  14. private boolean standardRunRebolAction;
  15. /* Nullary constructor for XML serialization purposes */
  16. public RebolExecutable() {
  17. }
  18. public RebolExecutable(String path) throws IOException, NullPointerException, NotRebolException {
  19. setPath(path);
  20. setDisplayName(displayNameForPath(path));
  21. setStandardRunRebolAction(false);
  22. }
  23. public boolean getTHEStandardRunRebolAction() {
  24. return true; //RebolPanel.
  25. }
  26. public boolean isStandardRunRebolAction() {
  27. return standardRunRebolAction;
  28. }
  29. public void setStandardRunRebolAction(boolean standardRunRebolAction) {
  30. this.standardRunRebolAction = standardRunRebolAction;
  31. }
  32. public String getPath() {
  33. return path;
  34. }
  35. public void setPath(String path) {
  36. this.path = path;
  37. }
  38. public String getDisplayName() {
  39. return displayName;
  40. }
  41. public void setDisplayName(String displayName) {
  42. this.displayName = displayName;
  43. }
  44. @Override
  45. public String toString() {
  46. return getDisplayName();
  47. }
  48. @Override
  49. public boolean equals(Object obj) {
  50. String os = System.getProperty("os.name");
  51. if (os != null && os.toLowerCase().contains("windows")) {
  52. // windows platform fs is case insensitive for path names, so IgnoreCase here:
  53. return (obj instanceof RebolExecutable && ((RebolExecutable)obj).getPath().equalsIgnoreCase(path));
  54. }
  55. return (obj instanceof RebolExecutable && ((RebolExecutable)obj).getPath().equals(path));
  56. }
  57. private String displayNameForPath(String path) throws IOException, NullPointerException, NotRebolException {
  58. String product = getSystemInformation("product", path).toLowerCase();
  59. if (!(product.startsWith("core") || product.startsWith("view") || product.startsWith("rebol 3"))) {
  60. throw new NotRebolException(String.format("Given file doesn't seem to be a rebol executable: %s", path));
  61. }
  62. if (product.startsWith("rebol 3")) {
  63. product = "R3";
  64. }
  65. String version = getSystemInformation("version", path).toLowerCase();
  66. if (version.startsWith("rebol 3")) {
  67. version = version.substring(8); // removes "REBOL 3 "
  68. }
  69. return String.format("%s/%s", product, version);
  70. }
  71. private String getSystemInformation(String information, String path) throws IOException {
  72. ExternalProcessBuilder processBuilder = new ExternalProcessBuilder(path).addArgument("-cqvw").addArgument("--do").addArgument(String.format("print system/%s", information));
  73. Process p = processBuilder.call();
  74. BufferedInputStream buffer = new BufferedInputStream(p.getInputStream());
  75. BufferedReader result = new BufferedReader(new InputStreamReader(buffer));
  76. // we're only supposed to get one line of response, so here goes:
  77. String s = result.readLine();
  78. // instead of:
  79. // String s = "", line = "";
  80. // while ((line = result.readLine()) != null) {
  81. // s += line;
  82. // }
  83. // then clean up:
  84. result.close();
  85. buffer.close();
  86. p.destroy();
  87. return s;
  88. }
  89. }