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.

Runner.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 org.sonar.runner.impl.Constants;
  22. import org.sonar.runner.impl.Logs;
  23. import javax.annotation.Nullable;
  24. import java.nio.charset.Charset;
  25. import java.util.Locale;
  26. import java.util.Properties;
  27. /**
  28. * @since 2.2
  29. */
  30. public abstract class Runner<T extends Runner> {
  31. private final Properties properties = new Properties();
  32. protected Runner() {
  33. initProperties();
  34. }
  35. private void initProperties() {
  36. // default values
  37. properties.put(Constants.HOST_URL, "http://localhost:9000");
  38. properties.put(Constants.TASK, "scan");
  39. properties.put(Constants.PROP_APP, "SonarRunner");
  40. properties.put(Constants.PROP_APP_VERSION, RunnerVersion.version());
  41. }
  42. public Properties properties() {
  43. Properties clone = new Properties();
  44. clone.putAll(properties);
  45. return clone;
  46. }
  47. /**
  48. * Declare Sonar properties, for example sonar.projectKey=>foo.
  49. */
  50. public T addProperties(Properties p) {
  51. properties.putAll(p);
  52. return (T) this;
  53. }
  54. public T setProperty(String key, String value) {
  55. properties.setProperty(key, value);
  56. return (T) this;
  57. }
  58. public String property(String key, @Nullable String defaultValue) {
  59. return properties.getProperty(key, defaultValue);
  60. }
  61. /**
  62. * User-agent used in the HTTP requests to the Sonar server
  63. */
  64. public T setApp(String app, String version) {
  65. setProperty(Constants.PROP_APP, app);
  66. setProperty(Constants.PROP_APP_VERSION, version);
  67. return (T) this;
  68. }
  69. public String app() {
  70. return property(Constants.PROP_APP, null);
  71. }
  72. public String appVersion() {
  73. return property(Constants.PROP_APP_VERSION, null);
  74. }
  75. public void execute() {
  76. initSourceEncoding();
  77. doExecute();
  78. }
  79. private void initSourceEncoding() {
  80. String sourceEncoding = property(Constants.SOURCE_ENCODING, null);
  81. boolean platformDependent = false;
  82. if (sourceEncoding == null || sourceEncoding.equals("")) {
  83. sourceEncoding = Charset.defaultCharset().name();
  84. platformDependent = true;
  85. setProperty(Constants.SOURCE_ENCODING, sourceEncoding);
  86. }
  87. Logs.info("Default locale: \"" + Locale.getDefault() + "\", source code encoding: \"" + sourceEncoding + "\""
  88. + (platformDependent ? " (analysis is platform dependent)" : ""));
  89. }
  90. protected abstract void doExecute();
  91. }