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.filesystem;
021    
022    import com.google.common.annotations.VisibleForTesting;
023    import com.google.common.base.CharMatcher;
024    import com.google.common.io.Files;
025    import org.slf4j.Logger;
026    import org.slf4j.LoggerFactory;
027    import org.sonar.api.BatchComponent;
028    import org.sonar.api.CoreProperties;
029    import org.sonar.api.batch.SonarIndex;
030    import org.sonar.api.batch.fs.FileSystem;
031    import org.sonar.api.batch.fs.InputFile;
032    import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile;
033    import org.sonar.api.config.Settings;
034    import org.sonar.api.resources.File;
035    import org.sonar.api.resources.Languages;
036    import org.sonar.api.resources.Project;
037    import org.sonar.api.resources.Resource;
038    import org.sonar.api.utils.SonarException;
039    import org.sonar.batch.index.ResourceKeyMigration;
040    import org.sonar.batch.util.DeprecatedKeyUtils;
041    
042    /**
043     * Index all files/directories of the module in SQ database and importing source code.
044     *
045     * @since 4.2
046     */
047    public class ComponentIndexer implements BatchComponent {
048    
049      private static final Logger LOG = LoggerFactory.getLogger(ComponentIndexer.class);
050    
051      private final Languages languages;
052      private final Settings settings;
053      private final SonarIndex sonarIndex;
054      private final ResourceKeyMigration migration;
055      private final Project module;
056    
057      public ComponentIndexer(Project module, Languages languages, SonarIndex sonarIndex, Settings settings, ResourceKeyMigration migration) {
058        this.module = module;
059        this.languages = languages;
060        this.sonarIndex = sonarIndex;
061        this.settings = settings;
062        this.migration = migration;
063      }
064    
065      public void execute(FileSystem fs) {
066        migration.migrateIfNeeded(module, fs);
067    
068        boolean shouldImportSource = settings.getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY);
069        if (!shouldImportSource) {
070          LOG.warn("/!\\ Property '" + CoreProperties.CORE_IMPORT_SOURCES_PROPERTY
071            + "' is deprecated. Not importing source will prevent issues to be properly tracked between consecutive analyses.");
072        }
073        for (InputFile inputFile : fs.inputFiles(fs.predicates().all())) {
074          String languageKey = inputFile.language();
075          boolean unitTest = InputFile.Type.TEST == inputFile.type();
076          String pathFromSourceDir = ((DeprecatedDefaultInputFile) inputFile).pathRelativeToSourceDir();
077          if (pathFromSourceDir == null) {
078            pathFromSourceDir = inputFile.relativePath();
079          }
080          Resource sonarFile = File.create(inputFile.relativePath(), pathFromSourceDir, languages.get(languageKey), unitTest);
081          if ("java".equals(languageKey)) {
082            sonarFile.setDeprecatedKey(DeprecatedKeyUtils.getJavaFileDeprecatedKey(pathFromSourceDir));
083          } else {
084            sonarFile.setDeprecatedKey(pathFromSourceDir);
085          }
086          sonarIndex.index(sonarFile);
087    
088          importSources(fs, shouldImportSource, inputFile, sonarFile);
089        }
090      }
091    
092      @VisibleForTesting
093      void importSources(FileSystem fs, boolean shouldImportSource, InputFile inputFile, Resource sonarFile) {
094        try {
095          // TODO this part deserves optimization.
096          // No need to read full content in memory when shouldImportSource=false
097          // We should try to remove BOM and count lines in a single pass
098          String source = Files.toString(inputFile.file(), fs.encoding());
099          // SONAR-3860 Remove BOM character from source
100          source = CharMatcher.anyOf("\uFEFF").removeFrom(source);
101          if (shouldImportSource) {
102            sonarIndex.setSource(sonarFile, source);
103          }
104        } catch (Exception e) {
105          throw new SonarException("Unable to read and import the source file : '" + inputFile.absolutePath() + "' with the charset : '"
106            + fs.encoding() + "'.", e);
107        }
108      }
109    }