diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-01-25 19:10:20 +0100 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-01-25 19:26:43 +0100 |
commit | e7de7db7a3a3de0bf7961f0b5210156182622f10 (patch) | |
tree | a638d3b82ebf27f68184c527df61fff9923502ab /sonar-batch | |
parent | f3b88bb8e5a57a215e59dcc5c38f550433c9a1eb (diff) | |
download | sonarqube-e7de7db7a3a3de0bf7961f0b5210156182622f10.tar.gz sonarqube-e7de7db7a3a3de0bf7961f0b5210156182622f10.zip |
Improve backward-compatibility when adding data to a non-indexed resource
Diffstat (limited to 'sonar-batch')
4 files changed, 75 insertions, 29 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultSensorContext.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultSensorContext.java index a924fba8ac1..4a4acf8bf32 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/DefaultSensorContext.java +++ b/sonar-batch/src/main/java/org/sonar/batch/DefaultSensorContext.java @@ -60,8 +60,8 @@ public class DefaultSensorContext implements SensorContext { return index.isExcluded(reference); } - public boolean isIndexed(Resource reference) { - return index.isIndexed(reference); + public boolean isIndexed(Resource reference, boolean acceptExcluded) { + return index.isIndexed(reference, acceptExcluded); } public Resource getParent(Resource reference) { @@ -155,8 +155,8 @@ public class DefaultSensorContext implements SensorContext { return index.getOutgoingEdges(resourceOrProject(from)); } - public boolean saveSource(Resource reference, String source) throws DuplicatedSourceException { - return index.setSource(reference, source); + public void saveSource(Resource reference, String source) throws DuplicatedSourceException { + index.setSource(reference, source); } public void saveLink(ProjectLink link) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java index 5660bf44ade..01ba7d6b384 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java @@ -157,7 +157,7 @@ public final class DefaultIndex extends SonarIndex { * the measure is updated if it's already registered. */ public Measure addMeasure(Resource resource, Measure measure) { - Bucket bucket = doIndex(resource); + Bucket bucket = checkIndexed(resource); if (bucket != null && !bucket.isExcluded()) { Metric metric = metricFinder.findByKey(measure.getMetricKey()); if (metric == null) { @@ -170,7 +170,6 @@ public final class DefaultIndex extends SonarIndex { if (measure.getPersistenceMode().useDatabase()) { persistence.saveMeasure(resource, measure); } - // TODO keep database measures in cache but remove data } return measure; @@ -295,13 +294,12 @@ public final class DefaultIndex extends SonarIndex { } public void addViolation(Violation violation, boolean force) { - Bucket bucket; Resource resource = violation.getResource(); if (resource == null) { violation.setResource(currentProject); } - bucket = doIndex(violation.getResource()); - if (!bucket.isExcluded()) { + Bucket bucket = checkIndexed(resource); + if (bucket != null && !bucket.isExcluded()) { boolean isIgnored = !force && violationFilters != null && violationFilters.isIgnored(violation); if (!isIgnored) { ActiveRule activeRule = profile.getActiveRule(violation.getRule()); @@ -366,13 +364,11 @@ public final class DefaultIndex extends SonarIndex { return null; } - public boolean setSource(Resource reference, String source) { - boolean result = false; - if (isIndexed(reference)) { + public void setSource(Resource reference, String source) { + Bucket bucket = checkIndexed(reference); + if (bucket != null && !bucket.isExcluded()) { persistence.setSource(reference, source); - result = true; } - return result; } @@ -387,7 +383,7 @@ public final class DefaultIndex extends SonarIndex { public <R extends Resource> R getResource(R reference) { Bucket bucket = buckets.get(reference); if (bucket != null) { - return (R)bucket.getResource(); + return (R) bucket.getResource(); } return null; } @@ -443,8 +439,7 @@ public final class DefaultIndex extends SonarIndex { private Bucket doIndex(Resource resource) { if (resource.getParent() != null) { - // SONAR-2127 backward-compatibility - create automatically parent of files - doIndex(resource.getParent(), currentProject); + doIndex(resource.getParent()); } return doIndex(resource, resource.getParent()); } @@ -469,7 +464,7 @@ public final class DefaultIndex extends SonarIndex { } Bucket parentBucket = getBucket(parent, true); - if (parentBucket==null && parent!=null) { + if (parentBucket == null && parent != null) { LOG.warn("Resource ignored, parent is not indexed: " + resource); return null; } @@ -480,7 +475,7 @@ public final class DefaultIndex extends SonarIndex { boolean excluded = checkExclusion(resource, parentBucket); if (!excluded) { - persistence.saveResource(currentProject, resource, (parentBucket!=null ? parentBucket.getResource() : null)); + persistence.saveResource(currentProject, resource, (parentBucket != null ? parentBucket.getResource() : null)); } return bucket; } @@ -494,13 +489,30 @@ public final class DefaultIndex extends SonarIndex { } } + private Bucket checkIndexed(Resource reference) { + Bucket bucket = getBucket(reference, true); + if (bucket == null) { + if (lock.isLocked()) { + if (lock.isFailWhenLocked()) { + throw new ResourceNotIndexedException(reference); + } + LOG.warn("Resource will be ignored in next Sonar versions, index is locked: " + reference); + } else { + // other languages than Java - keep backward compatibility + bucket = doIndex(reference); + } + } + return bucket; + } + + public boolean isExcluded(Resource reference) { Bucket bucket = getBucket(reference, true); return bucket != null && bucket.isExcluded(); } - public boolean isIndexed(Resource reference) { - return getBucket(reference, false) != null; + public boolean isIndexed(Resource reference, boolean acceptExcluded) { + return getBucket(reference, acceptExcluded) != null; } private Bucket getBucket(Resource resource, boolean acceptExcluded) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ResourceNotIndexedException.java b/sonar-batch/src/main/java/org/sonar/batch/index/ResourceNotIndexedException.java new file mode 100644 index 00000000000..203f4043ff6 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/index/ResourceNotIndexedException.java @@ -0,0 +1,33 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.batch.index; + +import org.sonar.api.resources.Resource; +import org.sonar.api.utils.SonarException; + +/** + * @since 2.6 + */ +public class ResourceNotIndexedException extends SonarException { + + public ResourceNotIndexedException(final Resource reference) { + super(reference.toString()); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java index e90cd8a9b61..ef88932c7e1 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java @@ -85,7 +85,7 @@ public class DefaultIndexTest { Directory reference = new Directory("org/foo"); assertThat(index.getResource(reference).getName(), is("org/foo")); - assertThat(index.isIndexed(reference), is(true)); + assertThat(index.isIndexed(reference, true), is(true)); assertThat(index.isExcluded(reference), is(false)); assertThat(index.getChildren(reference).size(), is(1)); assertThat(index.getParent(reference), is(Project.class)); @@ -103,7 +103,7 @@ public class DefaultIndexTest { File fileRef = new File("org/foo/Bar.java"); assertThat(index.getResource(fileRef).getKey(), is("org/foo/Bar.java")); assertThat(index.getResource(fileRef).getLanguage(), is((Language) Java.INSTANCE)); - assertThat(index.isIndexed(fileRef), is(true)); + assertThat(index.isIndexed(fileRef, true), is(true)); assertThat(index.isExcluded(fileRef), is(false)); assertThat(index.getChildren(fileRef).size(), is(0)); assertThat(index.getParent(fileRef), is(Directory.class)); @@ -116,7 +116,7 @@ public class DefaultIndexTest { Library reference = new Library("junit", "4.8"); assertThat(index.getResource(reference).getQualifier(), is(Qualifiers.LIBRARY)); - assertThat(index.isIndexed(reference), is(true)); + assertThat(index.isIndexed(reference, true), is(true)); assertThat(index.isExcluded(reference), is(false)); } @@ -128,8 +128,8 @@ public class DefaultIndexTest { assertThat(index.index(file, directory), is(false)); File fileRef = new File("org/foo/Bar.java"); - assertThat(index.isIndexed(directory), is(false)); - assertThat(index.isIndexed(fileRef), is(false)); + assertThat(index.isIndexed(directory, true), is(false)); + assertThat(index.isIndexed(fileRef, true), is(false)); assertThat(index.isExcluded(fileRef), is(false)); assertThat(index.getChildren(fileRef).size(), is(0)); assertThat(index.getParent(fileRef), nullValue()); @@ -145,7 +145,7 @@ public class DefaultIndexTest { Directory dir = new Directory("org/foo"); assertThat(index.index(dir), is(true)); - assertThat(index.isIndexed(dir), is(true)); + assertThat(index.isIndexed(dir, true), is(true)); } @Test(expected = SonarException.class) @@ -161,7 +161,8 @@ public class DefaultIndexTest { public void shouldBeExcluded() { File file = new File("org/foo/ExcludedBar.java"); assertThat(index.index(file), is(false)); - assertThat(index.isIndexed(file), is(false)); + assertThat(index.isIndexed(file, true), is(true)); + assertThat(index.isIndexed(file, false), is(false)); assertThat(index.isExcluded(file), is(true)); } @@ -170,7 +171,7 @@ public class DefaultIndexTest { Resource dir = new Directory("org/foo"); index.addMeasure(dir, new Measure("ncloc").setValue(50.0)); - assertThat(index.isIndexed(dir), is(true)); + assertThat(index.isIndexed(dir, true), is(true)); assertThat(index.getMeasures(dir, MeasuresFilters.metric("ncloc")).getIntValue(), is(50)); } } |