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.

ViolationPersisterDecorator.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2008-2011 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar 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. * Sonar 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 Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.plugins.core.timemachine;
  21. import com.google.common.collect.LinkedHashMultimap;
  22. import com.google.common.collect.Lists;
  23. import com.google.common.collect.Multimap;
  24. import org.apache.commons.codec.digest.DigestUtils;
  25. import org.apache.commons.io.IOUtils;
  26. import org.apache.commons.lang.ObjectUtils;
  27. import org.apache.commons.lang.StringUtils;
  28. import org.codehaus.plexus.util.StringInputStream;
  29. import org.sonar.api.batch.*;
  30. import org.sonar.api.database.model.RuleFailureModel;
  31. import org.sonar.api.database.model.SnapshotSource;
  32. import org.sonar.api.resources.Project;
  33. import org.sonar.api.resources.Resource;
  34. import org.sonar.api.rules.Rule;
  35. import org.sonar.api.rules.RuleFinder;
  36. import org.sonar.api.rules.Violation;
  37. import org.sonar.api.utils.SonarException;
  38. import org.sonar.batch.components.PastViolationsLoader;
  39. import org.sonar.batch.index.ViolationPersister;
  40. import java.io.IOException;
  41. import java.util.Collection;
  42. import java.util.Collections;
  43. import java.util.List;
  44. @DependsUpon(DecoratorBarriers.END_OF_VIOLATIONS_GENERATION)
  45. @DependedUpon("ViolationPersisterDecorator") /* temporary workaround - see NewViolationsDecorator */
  46. public class ViolationPersisterDecorator implements Decorator {
  47. /**
  48. * Those chars would be ignored during generation of checksums.
  49. */
  50. private static final String SPACE_CHARS = "\t\n\r ";
  51. private RuleFinder ruleFinder;
  52. private PastViolationsLoader pastViolationsLoader;
  53. private ViolationPersister violationPersister;
  54. List<String> checksums = Lists.newArrayList();
  55. public ViolationPersisterDecorator(RuleFinder ruleFinder, PastViolationsLoader pastViolationsLoader, ViolationPersister violationPersister) {
  56. this.ruleFinder = ruleFinder;
  57. this.pastViolationsLoader = pastViolationsLoader;
  58. this.violationPersister = violationPersister;
  59. }
  60. public boolean shouldExecuteOnProject(Project project) {
  61. return true;
  62. }
  63. public void decorate(Resource resource, DecoratorContext context) {
  64. if (context.getViolations().isEmpty()) {
  65. return;
  66. }
  67. // Load past violations
  68. List<RuleFailureModel> pastViolations = pastViolationsLoader.getPastViolations(resource);
  69. // Load current source and calculate checksums
  70. checksums = getChecksums(pastViolationsLoader.getSource(resource));
  71. // Save violations
  72. compareWithPastViolations(context, pastViolations);
  73. // Clear cache
  74. checksums.clear();
  75. }
  76. private void compareWithPastViolations(DecoratorContext context, List<RuleFailureModel> pastViolations) {
  77. Multimap<Rule, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
  78. for (RuleFailureModel pastViolation : pastViolations) {
  79. Rule rule = ruleFinder.findById(pastViolation.getRuleId());
  80. pastViolationsByRule.put(rule, pastViolation);
  81. }
  82. // for each violation, search equivalent past violation
  83. for (Violation violation : context.getViolations()) {
  84. RuleFailureModel pastViolation = selectPastViolation(violation, pastViolationsByRule);
  85. if (pastViolation != null) {
  86. // remove violation, since would be updated and shouldn't affect other violations anymore
  87. pastViolationsByRule.remove(violation.getRule(), pastViolation);
  88. }
  89. String checksum = getChecksumForLine(checksums, violation.getLineId());
  90. violationPersister.saveViolation(context.getProject(), violation, pastViolation, checksum);
  91. }
  92. violationPersister.commit();
  93. }
  94. /**
  95. * @return checksums, never null
  96. */
  97. private List<String> getChecksums(SnapshotSource source) {
  98. return source == null || source.getData() == null ? Collections.<String>emptyList() : getChecksums(source.getData());
  99. }
  100. static List<String> getChecksums(String data) {
  101. List<String> result = Lists.newArrayList();
  102. StringInputStream stream = new StringInputStream(data);
  103. try {
  104. List<String> lines = IOUtils.readLines(stream);
  105. for (String line : lines) {
  106. result.add(getChecksum(line));
  107. }
  108. } catch (IOException e) {
  109. throw new SonarException("Unable to calculate checksums", e);
  110. } finally {
  111. IOUtils.closeQuietly(stream);
  112. }
  113. return result;
  114. }
  115. static String getChecksum(String line) {
  116. String reducedLine = StringUtils.replaceChars(line, SPACE_CHARS, "");
  117. return DigestUtils.md5Hex(reducedLine);
  118. }
  119. /**
  120. * @return checksum or null if checksum not exists for line
  121. */
  122. private String getChecksumForLine(List<String> checksums, Integer line) {
  123. if (line == null || line < 1 || line > checksums.size()) {
  124. return null;
  125. }
  126. return checksums.get(line - 1);
  127. }
  128. /**
  129. * Search for past violation.
  130. */
  131. RuleFailureModel selectPastViolation(Violation violation, Multimap<Rule, RuleFailureModel> pastViolationsByRule) {
  132. Collection<RuleFailureModel> pastViolations = pastViolationsByRule.get(violation.getRule());
  133. if (pastViolations==null || pastViolations.isEmpty()) {
  134. // skip violation, if there is no past violations with same rule
  135. return null;
  136. }
  137. String dbFormattedMessage = RuleFailureModel.abbreviateMessage(violation.getMessage());
  138. RuleFailureModel found = selectPastViolationUsingLine(violation, dbFormattedMessage, pastViolations);
  139. if (found == null) {
  140. found = selectPastViolationUsingChecksum(violation, dbFormattedMessage, pastViolations);
  141. }
  142. return found;
  143. }
  144. /**
  145. * Search for past violation with same message and line.
  146. */
  147. private RuleFailureModel selectPastViolationUsingLine(Violation violation, String dbFormattedMessage, Collection<RuleFailureModel> pastViolations) {
  148. for (RuleFailureModel pastViolation : pastViolations) {
  149. if (ObjectUtils.equals(violation.getLineId(), pastViolation.getLine()) && StringUtils.equals(dbFormattedMessage, pastViolation.getMessage())) {
  150. return pastViolation;
  151. }
  152. }
  153. return null;
  154. }
  155. /**
  156. * Search for past violation with same message and checksum.
  157. */
  158. private RuleFailureModel selectPastViolationUsingChecksum(Violation violation, String dbFormattedMessage, Collection<RuleFailureModel> pastViolations) {
  159. String checksum = getChecksumForLine(checksums, violation.getLineId());
  160. // skip violation, which not attached to line
  161. if (checksum == null) {
  162. return null;
  163. }
  164. for (RuleFailureModel pastViolation : pastViolations) {
  165. String pastChecksum = pastViolation.getChecksum();
  166. if (StringUtils.equals(checksum, pastChecksum) && StringUtils.equals(dbFormattedMessage, pastViolation.getMessage())) {
  167. return pastViolation;
  168. }
  169. }
  170. return null;
  171. }
  172. @Override
  173. public String toString() {
  174. return getClass().getSimpleName();
  175. }
  176. }