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.batch.languages.Language;
023    
024    import org.picocontainer.Startable;
025    import org.slf4j.Logger;
026    import org.slf4j.LoggerFactory;
027    import org.sonar.api.CoreProperties;
028    import org.sonar.api.batch.fs.internal.DefaultFileSystem;
029    import org.sonar.api.config.Settings;
030    import org.sonar.api.utils.MessageException;
031    import org.sonar.batch.languages.LanguagesReferential;
032    
033    /**
034     * Verifies that the property sonar.language is valid
035     */
036    public class LanguageVerifier implements Startable {
037    
038      private static final Logger LOG = LoggerFactory.getLogger(LanguageVerifier.class);
039    
040      private final Settings settings;
041      private final LanguagesReferential languages;
042      private final DefaultFileSystem fs;
043    
044      public LanguageVerifier(Settings settings, LanguagesReferential languages, DefaultFileSystem fs) {
045        this.settings = settings;
046        this.languages = languages;
047        this.fs = fs;
048      }
049    
050      @Override
051      public void start() {
052        if (settings.hasKey(CoreProperties.PROJECT_LANGUAGE_PROPERTY)) {
053          String languageKey = settings.getString(CoreProperties.PROJECT_LANGUAGE_PROPERTY);
054          LOG.info("Language is forced to {}", languageKey);
055          Language language = languages.get(languageKey);
056          if (language == null) {
057            throw MessageException.of("You must install a plugin that supports the language '" + languageKey + "'");
058          }
059    
060          // force the registration of the language, even if there are no related source files
061          fs.addLanguages(languageKey);
062        }
063      }
064    
065      @Override
066      public void stop() {
067        // nothing to do
068      }
069    }