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.

Command.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Sonar Runner - API
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner.api;
  21. import java.io.File;
  22. import java.util.*;
  23. class Command {
  24. private final String executable;
  25. private final List<String> arguments;
  26. private final Map<String, String> env;
  27. private final File directory;
  28. private Command(Builder builder) {
  29. this.executable = builder.executable;
  30. this.arguments = Collections.unmodifiableList(builder.arguments);
  31. this.env = Collections.unmodifiableMap(builder.env);
  32. this.directory = builder.directory;
  33. }
  34. File directory() {
  35. return directory;
  36. }
  37. String executable() {
  38. return executable;
  39. }
  40. List<String> arguments() {
  41. return arguments;
  42. }
  43. /**
  44. * Environment variables that are propagated during command execution.
  45. *
  46. * @return a non-null and immutable map of variables
  47. */
  48. Map<String, String> envVariables() {
  49. return env;
  50. }
  51. String[] toStrings() {
  52. String[] strings = new String[1 + arguments.size()];
  53. strings[0] = executable;
  54. for (int index = 0; index < arguments.size(); index++) {
  55. strings[index + 1] = arguments.get(index);
  56. }
  57. return strings;
  58. }
  59. @Override
  60. public String toString() {
  61. return Utils.join(toStrings(), " ");
  62. }
  63. static Builder builder() {
  64. return new Builder();
  65. }
  66. static class Builder {
  67. private String executable;
  68. private final List<String> arguments = new ArrayList<String>();
  69. private final Map<String, String> env = new HashMap<String, String>();
  70. private File directory;
  71. private Builder() {
  72. }
  73. Builder setExecutable(String s) {
  74. this.executable = s;
  75. return this;
  76. }
  77. Builder addArguments(String... args) {
  78. arguments.addAll(Arrays.asList(args));
  79. return this;
  80. }
  81. Builder addArguments(List<String> args) {
  82. arguments.addAll(args);
  83. return this;
  84. }
  85. Builder setEnvVariable(String key, String value) {
  86. env.put(key, value);
  87. return this;
  88. }
  89. Builder addEnvVariables(Map<String, String> map) {
  90. env.putAll(map);
  91. return this;
  92. }
  93. Builder setDirectory(File d) {
  94. this.directory = d;
  95. return this;
  96. }
  97. Command build() {
  98. if (executable == null) {
  99. throw new IllegalArgumentException("Command executable is not defined");
  100. }
  101. return new Command(this);
  102. }
  103. }
  104. }