001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar 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     * Sonar 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
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.batch;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.sonar.api.config.Settings;
024    import org.sonar.api.profiles.RulesProfile;
025    import org.sonar.api.resources.Project;
026    import org.sonar.api.rules.ActiveRule;
027    import org.sonar.api.utils.SonarException;
028    import org.sonar.jpa.dao.ProfilesDao;
029    
030    public class DefaultProfileLoader implements ProfileLoader {
031      private ProfilesDao dao;
032      private Settings settings;
033    
034      public DefaultProfileLoader(ProfilesDao dao, Settings settings) {
035        this.dao = dao;
036        this.settings = settings;
037      }
038    
039      public RulesProfile load(Project project) {
040        String profileName = StringUtils.defaultIfBlank(
041          settings.getString("sonar.profile"),
042          settings.getString("sonar.profile." + project.getLanguageKey())
043        );
044    
045        if (StringUtils.isBlank(profileName)) {
046          // This means that the current language is not supported by any installed plugin, otherwise at least a
047          // "Default <Language Name>" profile would have been created by ActivateDefaultProfiles class.
048          throw new SonarException("You must install a plugin that supports the language '" + project.getLanguageKey() + "'");
049        }
050    
051        RulesProfile profile = dao.getProfile(project.getLanguageKey(), profileName);
052        if (profile == null) {
053          throw new SonarException("Quality profile not found : " + profileName + ", language " + project.getLanguageKey());
054        }
055    
056        return hibernateHack(profile);
057      }
058    
059      private RulesProfile hibernateHack(RulesProfile profile) {
060        // hack to lazy initialize the profile collections
061        profile.getActiveRules().size();
062        for (ActiveRule activeRule : profile.getActiveRules()) {
063          activeRule.getActiveRuleParams().size();
064          activeRule.getRule().getParams().size();
065        }
066        profile.getAlerts().size();
067        return profile;
068      }
069    
070    }