001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.batch.scan;
021    
022    import org.sonar.api.batch.DependedUpon;
023    import org.sonar.api.batch.DependsUpon;
024    import org.sonar.api.batch.measure.Metric;
025    import org.sonar.api.batch.sensor.Sensor;
026    import org.sonar.api.batch.sensor.SensorContext;
027    import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
028    import org.sonar.api.resources.Project;
029    import org.sonar.batch.scan2.AnalyzerOptimizer;
030    
031    import java.util.Arrays;
032    import java.util.List;
033    
034    public class SensorWrapper implements org.sonar.api.batch.Sensor {
035    
036      private Sensor wrappedSensor;
037      private SensorContext adaptor;
038      private DefaultSensorDescriptor descriptor;
039      private AnalyzerOptimizer optimizer;
040    
041      public SensorWrapper(Sensor newSensor, SensorContext adaptor, AnalyzerOptimizer optimizer) {
042        this.wrappedSensor = newSensor;
043        this.optimizer = optimizer;
044        descriptor = new DefaultSensorDescriptor();
045        newSensor.describe(descriptor);
046        this.adaptor = adaptor;
047      }
048    
049      public Sensor wrappedSensor() {
050        return wrappedSensor;
051      }
052    
053      @DependedUpon
054      public List<Metric> provides() {
055        return Arrays.asList(descriptor.provides());
056      }
057    
058      @DependsUpon
059      public List<Metric> depends() {
060        return Arrays.asList(descriptor.dependsOn());
061      }
062    
063      @Override
064      public boolean shouldExecuteOnProject(Project project) {
065        return optimizer.shouldExecute(descriptor);
066      }
067    
068      @Override
069      public void analyse(Project module, org.sonar.api.batch.SensorContext context) {
070        wrappedSensor.execute(adaptor);
071      }
072    
073      @Override
074      public String toString() {
075        return descriptor.name() + " (wrapped)";
076      }
077    }