]> source.dussan.org Git - sonarqube.git/blob
4ad673c0bc53acb5851e5ac662480085773821b4
[sonarqube.git] /
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
22 import com.google.common.collect.LinkedHashMultimap;
23 import com.google.common.collect.Lists;
24 import com.google.common.collect.Multimap;
25 import org.apache.commons.codec.digest.DigestUtils;
26 import org.apache.commons.lang.StringUtils;
27 import org.sonar.api.batch.*;
28 import org.sonar.api.database.model.RuleFailureModel;
29 import org.sonar.api.database.model.SnapshotSource;
30 import org.sonar.api.resources.Project;
31 import org.sonar.api.resources.Resource;
32 import org.sonar.api.rules.Violation;
33 import org.sonar.batch.components.PastViolationsLoader;
34 import org.sonar.batch.index.ViolationPersister;
35
36 import java.util.*;
37
38 @DependsUpon({DecoratorBarriers.END_OF_VIOLATIONS_GENERATION, DecoratorBarriers.START_VIOLATION_TRACKING})
39 @DependedUpon(DecoratorBarriers.END_OF_VIOLATION_TRACKING)
40 public class ViolationPersisterDecorator implements Decorator {
41
42   /**
43    * Those chars would be ignored during generation of checksums.
44    */
45   private static final String SPACE_CHARS = "\t\n\r ";
46
47   private PastViolationsLoader pastViolationsLoader;
48   private ViolationPersister violationPersister;
49
50   List<String> checksums;
51
52   public ViolationPersisterDecorator(PastViolationsLoader pastViolationsLoader, ViolationPersister violationPersister) {
53     this.pastViolationsLoader = pastViolationsLoader;
54     this.violationPersister = violationPersister;
55   }
56
57   public boolean shouldExecuteOnProject(Project project) {
58     return true;
59   }
60
61   public void decorate(Resource resource, DecoratorContext context) {
62     if (context.getViolations().isEmpty()) {
63       return;
64     }
65     // Load new violations
66     List<Violation> newViolations = context.getViolations();
67
68     // Load past violations
69     List<RuleFailureModel> pastViolations = pastViolationsLoader.getPastViolations(resource);
70
71     // Load current source code and calculate checksums for each line
72     checksums = getChecksums(pastViolationsLoader.getSource(resource));
73
74     // Map new violations with old ones
75     Map<Violation, RuleFailureModel> violationMap = mapViolations(newViolations, pastViolations);
76
77     for (Violation newViolation : newViolations) {
78       String checksum = getChecksumForLine(checksums, newViolation.getLineId());
79       violationPersister.saveViolation(context.getProject(), newViolation, violationMap.get(newViolation), checksum);
80     }
81     violationPersister.commit();
82     // Clear cache
83     checksums.clear();
84   }
85
86   Map<Violation, RuleFailureModel> mapViolations(List<Violation> newViolations, List<RuleFailureModel> pastViolations) {
87     Map<Violation, RuleFailureModel> violationMap = new IdentityHashMap<Violation, RuleFailureModel>();
88
89     Multimap<Integer, RuleFailureModel> pastViolationsByRule = LinkedHashMultimap.create();
90     for (RuleFailureModel pastViolation : pastViolations) {
91       pastViolationsByRule.put(pastViolation.getRuleId(), pastViolation);
92     }
93
94     // Try first an exact matching : same rule, same message, same line and same checkum
95     for (Violation newViolation : newViolations) {
96       mapViolation(newViolation,
97           findPastViolationWithSameLineAndChecksumAndMessage(newViolation, pastViolationsByRule.get(newViolation.getRule().getId())),
98           pastViolationsByRule, violationMap);
99     }
100
101     // If each new violation matches an old one we can stop the matching mechanism
102     if (violationMap.size() != newViolations.size()) {
103
104       // Try then to match violations on same rule with same message and with same checkum
105       for (Violation newViolation : newViolations) {
106         if (isNotAlreadyMapped(newViolation, violationMap)) {
107           mapViolation(newViolation,
108               findPastViolationWithSameChecksumAndMessage(newViolation, pastViolationsByRule.get(newViolation.getRule().getId())),
109               pastViolationsByRule, violationMap);
110         }
111       }
112
113       // Try then to match violations on same rule with same line and with same message
114       for (Violation newViolation : newViolations) {
115         if (isNotAlreadyMapped(newViolation, violationMap)) {
116           mapViolation(newViolation,
117               findPastViolationWithSameLineAndMessage(newViolation, pastViolationsByRule.get(newViolation.getRule().getId())),
118               pastViolationsByRule, violationMap);
119         }
120       }
121     }
122
123     return violationMap;
124   }
125
126   private final boolean isNotAlreadyMapped(Violation newViolation, Map<Violation, RuleFailureModel> violationMap) {
127     return violationMap.get(newViolation) == null;
128   }
129
130   private RuleFailureModel findPastViolationWithSameLineAndMessage(Violation newViolation, Collection<RuleFailureModel> pastViolations) {
131     for (RuleFailureModel pastViolation : pastViolations) {
132       if (isSameLine(newViolation, pastViolation) && isSameMessage(newViolation, pastViolation)) {
133         return pastViolation;
134       }
135     }
136     return null;
137   }
138
139   private RuleFailureModel findPastViolationWithSameChecksumAndMessage(Violation newViolation, Collection<RuleFailureModel> pastViolations) {
140     for (RuleFailureModel pastViolation : pastViolations) {
141       if (isSameChecksum(newViolation, pastViolation) && isSameMessage(newViolation, pastViolation)) {
142         return pastViolation;
143       }
144     }
145     return null;
146   }
147
148   private RuleFailureModel findPastViolationWithSameLineAndChecksumAndMessage(Violation newViolation,
149       Collection<RuleFailureModel> pastViolations) {
150     for (RuleFailureModel pastViolation : pastViolations) {
151       if (isSameLine(newViolation, pastViolation) && isSameChecksum(newViolation, pastViolation)
152           && isSameMessage(newViolation, pastViolation)) {
153         return pastViolation;
154       }
155     }
156     return null;
157   }
158
159   private boolean isSameChecksum(Violation newViolation, RuleFailureModel pastViolation) {
160     return pastViolation.getChecksum()!=null && StringUtils.equals(pastViolation.getChecksum(), getChecksumForLine(checksums, newViolation.getLineId()));
161   }
162
163   private boolean isSameLine(Violation newViolation, RuleFailureModel pastViolation) {
164     return pastViolation.getLine() == newViolation.getLineId();
165   }
166
167   private boolean isSameMessage(Violation newViolation, RuleFailureModel pastViolation) {
168     return StringUtils.equals(RuleFailureModel.abbreviateMessage(newViolation.getMessage()), pastViolation.getMessage());
169   }
170
171   private void mapViolation(Violation newViolation, RuleFailureModel pastViolation,
172       Multimap<Integer, RuleFailureModel> pastViolationsByRule, Map<Violation, RuleFailureModel> violationMap) {
173     if (pastViolation != null) {
174       pastViolationsByRule.remove(newViolation.getRule().getId(), pastViolation);
175       violationMap.put(newViolation, pastViolation);
176     }
177   }
178
179   /**
180    * @return checksums, never null
181    */
182   private List<String> getChecksums(SnapshotSource source) {
183     return source == null || source.getData() == null ? Collections.<String> emptyList() : getChecksums(source.getData());
184   }
185
186   /**
187    * @param data can't be null
188    */
189   static List<String> getChecksums(String data) {
190     String[] lines = data.split("\r?\n|\r", -1);
191     List<String> result = Lists.newArrayList();
192     for (String line : lines) {
193       result.add(getChecksum(line));
194     }
195     return result;
196   }
197
198   static String getChecksum(String line) {
199     String reducedLine = StringUtils.replaceChars(line, SPACE_CHARS, "");
200     return DigestUtils.md5Hex(reducedLine);
201   }
202
203   /**
204    * @return checksum or null if checksum not exists for line
205    */
206   private String getChecksumForLine(List<String> checksums, Integer line) {
207     if (line == null || line < 1 || line > checksums.size()) {
208       return null;
209     }
210     return checksums.get(line - 1);
211   }
212
213   @Override
214   public String toString() {
215     return getClass().getSimpleName();
216   }
217
218 }