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.

MeasureSensor.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  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 License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.xoo.lang;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.Serializable;
  24. import java.util.List;
  25. import org.apache.commons.io.FileUtils;
  26. import org.apache.commons.lang.StringUtils;
  27. import org.sonar.api.batch.fs.InputComponent;
  28. import org.sonar.api.batch.fs.InputDir;
  29. import org.sonar.api.batch.fs.InputFile;
  30. import org.sonar.api.batch.measure.MetricFinder;
  31. import org.sonar.api.batch.sensor.Sensor;
  32. import org.sonar.api.batch.sensor.SensorContext;
  33. import org.sonar.api.batch.sensor.SensorDescriptor;
  34. import org.sonar.api.batch.sensor.measure.NewMeasure;
  35. import org.sonar.api.utils.log.Logger;
  36. import org.sonar.api.utils.log.Loggers;
  37. import org.sonar.xoo.Xoo;
  38. import org.sonar.xoo.Xoo2;
  39. /**
  40. * Parse files *.xoo.measures
  41. */
  42. public class MeasureSensor implements Sensor {
  43. private static final Logger LOG = Loggers.get(MeasureSensor.class);
  44. private static final String MEASURES_EXTENSION = ".measures";
  45. private MetricFinder metricFinder;
  46. public MeasureSensor(MetricFinder metricFinder) {
  47. this.metricFinder = metricFinder;
  48. }
  49. private void processFileMeasures(InputComponent component, File measureFile, SensorContext context) {
  50. if (measureFile.exists()) {
  51. LOG.debug("Processing " + measureFile.getAbsolutePath());
  52. try {
  53. List<String> lines = FileUtils.readLines(measureFile, context.fileSystem().encoding().name());
  54. int lineNumber = 0;
  55. for (String line : lines) {
  56. lineNumber++;
  57. if (StringUtils.isBlank(line) || line.startsWith("#")) {
  58. continue;
  59. }
  60. processMeasure(component, context, measureFile, lineNumber, line);
  61. }
  62. } catch (IOException e) {
  63. throw new IllegalStateException(e);
  64. }
  65. }
  66. }
  67. private void processMeasure(InputComponent component, SensorContext context, File measureFile, int lineNumber, String line) {
  68. try {
  69. String metricKey = StringUtils.substringBefore(line, ":");
  70. String value = line.substring(metricKey.length() + 1);
  71. saveMeasure(context, component, metricKey, value);
  72. } catch (Exception e) {
  73. LOG.error("Error processing line " + lineNumber + " of file " + measureFile.getAbsolutePath(), e);
  74. throw new IllegalStateException("Error processing line " + lineNumber + " of file " + measureFile.getAbsolutePath(), e);
  75. }
  76. }
  77. private void saveMeasure(SensorContext context, InputComponent component, String metricKey, String value) {
  78. org.sonar.api.batch.measure.Metric<Serializable> metric = metricFinder.findByKey(metricKey);
  79. if (metric == null) {
  80. throw new IllegalStateException("Unknown metric with key: " + metricKey);
  81. }
  82. NewMeasure<Serializable> newMeasure = context.newMeasure()
  83. .forMetric(metric)
  84. .on(component);
  85. if (Boolean.class.equals(metric.valueType())) {
  86. newMeasure.withValue(Boolean.parseBoolean(value));
  87. } else if (Integer.class.equals(metric.valueType())) {
  88. newMeasure.withValue(Integer.valueOf(value));
  89. } else if (Double.class.equals(metric.valueType())) {
  90. newMeasure.withValue(Double.valueOf(value));
  91. } else if (String.class.equals(metric.valueType())) {
  92. newMeasure.withValue(value);
  93. } else if (Long.class.equals(metric.valueType())) {
  94. newMeasure.withValue(Long.valueOf(value));
  95. } else {
  96. throw new UnsupportedOperationException("Unsupported type :" + metric.valueType());
  97. }
  98. newMeasure.save();
  99. }
  100. @Override
  101. public void describe(SensorDescriptor descriptor) {
  102. descriptor
  103. .name("Xoo Measure Sensor")
  104. .onlyOnLanguages(Xoo.KEY, Xoo2.KEY);
  105. }
  106. @Override
  107. public void execute(SensorContext context) {
  108. for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY, Xoo2.KEY))) {
  109. File ioFile = file.file();
  110. File measureFile = new File(ioFile.getParentFile(), ioFile.getName() + MEASURES_EXTENSION);
  111. processFileMeasures(file, measureFile, context);
  112. InputDir inputDir = context.fileSystem().inputDir(ioFile.getParentFile());
  113. if (inputDir != null) {
  114. processFileMeasures(inputDir, new File(ioFile.getParentFile(), "folder" + MEASURES_EXTENSION), context);
  115. }
  116. }
  117. processFileMeasures(context.module(), new File(context.fileSystem().baseDir(), "module" + MEASURES_EXTENSION), context);
  118. }
  119. }