From 7a957c2d1b8736b0c5e14396029bde45524688b3 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Wed, 4 May 2011 21:59:23 +0200 Subject: [PATCH] SONAR-2357 Improve samples of plugins --- .../gwt/viewer/client/SampleViewerPanel.java | 26 +++++++++++++- .../sonar/standard/SamplePlugin.java | 14 ++++++-- .../sonar/standard/SampleSensor.java | 30 ---------------- .../sonar/standard/batch/RandomDecorator.java | 36 +++++++++++++++++++ .../sonar/standard/batch/SampleSensor.java | 20 +++++++++++ .../standard/rules/SampleQualityProfile.java | 25 +++++++++++++ .../standard/rules/SampleRuleRepository.java | 36 +++++++++++++++++++ .../sonar/standard/{ => ui}/SampleFooter.java | 2 +- .../standard/{ => ui}/SampleRubyWidget.java | 3 +- 9 files changed, 157 insertions(+), 35 deletions(-) delete mode 100644 samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleSensor.java create mode 100644 samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/batch/RandomDecorator.java create mode 100644 samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/batch/SampleSensor.java create mode 100644 samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/rules/SampleQualityProfile.java create mode 100644 samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/rules/SampleRuleRepository.java rename samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/{ => ui}/SampleFooter.java (82%) rename samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/{ => ui}/SampleRubyWidget.java (92%) diff --git a/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/viewer/client/SampleViewerPanel.java b/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/viewer/client/SampleViewerPanel.java index a1188d0c085..823dae78c7f 100644 --- a/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/viewer/client/SampleViewerPanel.java +++ b/samples/gwt-plugin/src/main/java/com/mycompany/sonar/gwt/viewer/client/SampleViewerPanel.java @@ -3,12 +3,36 @@ package com.mycompany.sonar.gwt.viewer.client; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.Widget; import org.sonar.gwt.ui.Page; +import org.sonar.wsclient.gwt.AbstractCallback; +import org.sonar.wsclient.gwt.Sonar; +import org.sonar.wsclient.services.Measure; import org.sonar.wsclient.services.Resource; +import org.sonar.wsclient.services.ResourceQuery; public class SampleViewerPanel extends Page { + private Label label; + @Override protected Widget doOnResourceLoad(Resource resource) { - return new Label("This is a sample"); + label = new Label("Loading value"); + loadMeasureFromServer(resource); + return label; + } + + // Ajax call to web service + private void loadMeasureFromServer(Resource resource) { + ResourceQuery query = ResourceQuery.createForResource(resource, "random"); + Sonar.getInstance().find(query, new AbstractCallback() { + @Override + protected void doOnResponse(Resource result) { + Measure measure = result.getMeasure("random"); + if (measure==null || measure.getValue()==null) { + label.setText("No random value"); + } else { + label.setText("Random value inserted during analysis: " + measure.getValue()); + } + } + }); } } \ No newline at end of file diff --git a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SamplePlugin.java b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SamplePlugin.java index e6ca0cc048a..f530e6b38b4 100644 --- a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SamplePlugin.java +++ b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SamplePlugin.java @@ -1,5 +1,11 @@ package com.mycompany.sonar.standard; +import com.mycompany.sonar.standard.batch.RandomDecorator; +import com.mycompany.sonar.standard.batch.SampleSensor; +import com.mycompany.sonar.standard.rules.SampleQualityProfile; +import com.mycompany.sonar.standard.rules.SampleRuleRepository; +import com.mycompany.sonar.standard.ui.SampleFooter; +import com.mycompany.sonar.standard.ui.SampleRubyWidget; import org.sonar.api.SonarPlugin; import java.util.Arrays; @@ -13,9 +19,13 @@ public final class SamplePlugin extends SonarPlugin { // This is where you're going to declare all your Sonar extensions public List getExtensions() { return Arrays.asList( - SampleMetrics.class, SampleSensor.class, SampleRubyWidget.class, + // Definitions + SampleRuleRepository.class, SampleMetrics.class, SampleQualityProfile.class, + + // Batch + SampleSensor.class, RandomDecorator.class, // UI - SampleFooter.class); + SampleFooter.class, SampleRubyWidget.class); } } \ No newline at end of file diff --git a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleSensor.java b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleSensor.java deleted file mode 100644 index f37749e286b..00000000000 --- a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleSensor.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.mycompany.sonar.standard; - -import org.apache.commons.lang.math.RandomUtils; -import org.sonar.api.batch.Sensor; -import org.sonar.api.batch.SensorContext; -import org.sonar.api.measures.Measure; -import org.sonar.api.resources.Project; - -public class SampleSensor implements Sensor { - - public boolean shouldExecuteOnProject(Project project) { - // this sensor is executed on any type of project - return true; - } - - public void analyse(Project project, SensorContext sensorContext) { - saveLabelMeasure(sensorContext); - saveNumericMeasure(sensorContext); - } - - private void saveNumericMeasure(SensorContext context) { - // Sonar API includes many libraries like commons-lang and google-collections - context.saveMeasure(SampleMetrics.RANDOM, RandomUtils.nextDouble()); - } - - private void saveLabelMeasure(SensorContext context) { - Measure measure = new Measure(SampleMetrics.MESSAGE, "Hello World!"); - context.saveMeasure(measure); - } -} diff --git a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/batch/RandomDecorator.java b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/batch/RandomDecorator.java new file mode 100644 index 00000000000..de2b7026bb6 --- /dev/null +++ b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/batch/RandomDecorator.java @@ -0,0 +1,36 @@ +package com.mycompany.sonar.standard.batch; + +import com.mycompany.sonar.standard.SampleMetrics; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.math.RandomUtils; +import org.sonar.api.batch.Decorator; +import org.sonar.api.batch.DecoratorContext; +import org.sonar.api.measures.MeasureUtils; +import org.sonar.api.resources.Java; +import org.sonar.api.resources.Project; +import org.sonar.api.resources.Resource; +import org.sonar.api.resources.Scopes; + +public class RandomDecorator implements Decorator { + + public boolean shouldExecuteOnProject(Project project) { + // Execute only on Java projects + return StringUtils.equals(project.getLanguageKey(), Java.KEY); + } + + public void decorate(Resource resource, DecoratorContext context) { + // This method is executed on the whole tree of resources. + // Bottom-up navigation : Java methods -> Java classes -> files -> packages -> modules -> project + if (Scopes.isBlockUnit(resource)) { + // Sonar API includes many libraries like commons-lang and google-collections + double value = RandomUtils.nextDouble(); + + // Add a measure to the current Java method + context.saveMeasure(SampleMetrics.RANDOM, value); + + } else { + // we sum random values on resources different than method + context.saveMeasure(SampleMetrics.RANDOM, MeasureUtils.sum(true, context.getChildrenMeasures(SampleMetrics.RANDOM))); + } + } +} diff --git a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/batch/SampleSensor.java b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/batch/SampleSensor.java new file mode 100644 index 00000000000..eade5e73ad7 --- /dev/null +++ b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/batch/SampleSensor.java @@ -0,0 +1,20 @@ +package com.mycompany.sonar.standard.batch; + +import com.mycompany.sonar.standard.SampleMetrics; +import org.sonar.api.batch.Sensor; +import org.sonar.api.batch.SensorContext; +import org.sonar.api.measures.Measure; +import org.sonar.api.resources.Project; + +public class SampleSensor implements Sensor { + + public boolean shouldExecuteOnProject(Project project) { + // This sensor is executed on any type of projects + return true; + } + + public void analyse(Project project, SensorContext sensorContext) { + Measure measure = new Measure(SampleMetrics.MESSAGE, "Hello World!"); + sensorContext.saveMeasure(measure); + } +} diff --git a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/rules/SampleQualityProfile.java b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/rules/SampleQualityProfile.java new file mode 100644 index 00000000000..65ba4d9c26d --- /dev/null +++ b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/rules/SampleQualityProfile.java @@ -0,0 +1,25 @@ +package com.mycompany.sonar.standard.rules; + +import org.sonar.api.profiles.ProfileDefinition; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.resources.Java; +import org.sonar.api.rules.RulePriority; +import org.sonar.api.utils.ValidationMessages; + +public class SampleQualityProfile extends ProfileDefinition { + + private SampleRuleRepository ruleRepository; + + // The component ruleRepository is injected at runtime + public SampleQualityProfile(SampleRuleRepository ruleRepository) { + this.ruleRepository = ruleRepository; + } + + @Override + public RulesProfile createProfile(ValidationMessages validation) { + RulesProfile profile = RulesProfile.create("Sample profile", Java.KEY); + profile.activateRule(ruleRepository.getRule1(), RulePriority.MAJOR); + profile.activateRule(ruleRepository.getRule1(), null); + return profile; + } +} diff --git a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/rules/SampleRuleRepository.java b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/rules/SampleRuleRepository.java new file mode 100644 index 00000000000..964a2e8ed79 --- /dev/null +++ b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/rules/SampleRuleRepository.java @@ -0,0 +1,36 @@ +package com.mycompany.sonar.standard.rules; + +import org.sonar.api.resources.Java; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RulePriority; +import org.sonar.api.rules.RuleRepository; + +import java.util.Arrays; +import java.util.List; + +public class SampleRuleRepository extends RuleRepository { + + public static final String REPOSITORY_KEY = "sample"; + + public SampleRuleRepository() { + super(REPOSITORY_KEY, Java.KEY); + setName("Sample"); + } + + @Override + public List createRules() { + // This method is called when server is started. It's used to register rules into database. + // Definition of rules can be loaded from any sources, like XML files. + return Arrays.asList( + getRule1(), + getRule2()); + } + + public Rule getRule1() { + return Rule.create(REPOSITORY_KEY, "fake1", "Fake one").setSeverity(RulePriority.CRITICAL); + } + + public Rule getRule2() { + return Rule.create(REPOSITORY_KEY, "fake2", "Fake two").setDescription("Description of fake two"); + } +} diff --git a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleFooter.java b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/ui/SampleFooter.java similarity index 82% rename from samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleFooter.java rename to samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/ui/SampleFooter.java index ab72e402427..6ed59896e9b 100644 --- a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleFooter.java +++ b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/ui/SampleFooter.java @@ -1,4 +1,4 @@ -package com.mycompany.sonar.standard; +package com.mycompany.sonar.standard.ui; import org.sonar.api.web.Footer; diff --git a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleRubyWidget.java b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/ui/SampleRubyWidget.java similarity index 92% rename from samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleRubyWidget.java rename to samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/ui/SampleRubyWidget.java index 7a97194c534..a6e723bf3ed 100644 --- a/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/SampleRubyWidget.java +++ b/samples/standard-plugin/src/main/java/com/mycompany/sonar/standard/ui/SampleRubyWidget.java @@ -1,9 +1,10 @@ -package com.mycompany.sonar.standard; +package com.mycompany.sonar.standard.ui; import org.sonar.api.web.*; @UserRole(UserRole.USER) @Description("Show how to use Ruby Widget API") +@WidgetCategory("Sample") @WidgetProperties({ @WidgetProperty(key="param1", description="This is a mandatory parameter", -- 2.39.5