}
return realm;
} catch (UnsupportedClassVersionError e) {
- throw new SonarException("The plugin " + plugin.getKey() + " is not supported with Java " + SystemUtils.JAVA_VERSION_TRIMMED, e);
+ throw new SonarException(String.format("The plugin %s is not supported with Java %s", plugin.getKey(),
+ SystemUtils.JAVA_VERSION_TRIMMED), e);
} catch (Exception e) {
- throw new SonarException("Fail to build the classloader of " + plugin.getKey(), e);
+ throw new SonarException(String.format("Fail to build the classloader of %s", plugin.getKey()), e);
}
}
ClassRealm base = world.getRealm(plugin.getBasePlugin());
if (base == null) {
// Ignored, because base plugin is not installed
- LOG.warn("Plugin " + plugin.getKey() + " is ignored because base plugin is not installed: " + plugin.getBasePlugin());
+ LOG.warn(String.format("Plugin %s is ignored because base plugin is not installed: %s",
+ plugin.getKey(), plugin.getBasePlugin()));
return false;
}
// we create new realm to be able to return it by key without conversion to baseKey
}
return true;
} catch (UnsupportedClassVersionError e) {
- throw new SonarException("The plugin " + plugin.getKey() + " is not supported with Java " + SystemUtils.JAVA_VERSION_TRIMMED, e);
+ throw new SonarException(String.format("The plugin %s is not supported with Java %s",
+ plugin.getKey(), SystemUtils.JAVA_VERSION_TRIMMED), e);
} catch (Exception e) {
- throw new SonarException("Fail to extend the plugin " + plugin.getBasePlugin() + " for " + plugin.getKey(), e);
+ throw new SonarException(String.format("Fail to extend the plugin %s for %s",
+ plugin.getBasePlugin(), plugin.getKey()), e);
}
}
return (Plugin) clazz.newInstance();
} catch (UnsupportedClassVersionError e) {
- throw new SonarException("The plugin " + plugin.getKey() + " is not supported with Java " + SystemUtils.JAVA_VERSION_TRIMMED, e);
+ throw new SonarException(String.format("The plugin %s is not supported with Java %s",
+ plugin.getKey(), SystemUtils.JAVA_VERSION_TRIMMED), e);
} catch (Exception e) {
- throw new SonarException("Fail to load plugin " + plugin.getKey(), e);
+ throw new SonarException(String.format("Fail to load plugin %s", plugin.getKey()), e);
}
}
return session.getMapper(MeasureMapper.class).countByKey(key) > 0;
}
- public List<MeasureDto> findByComponentKeyAndMetricKeys(String componentKey, List<String> metricKeys, DbSession session){
+ public List<MeasureDto> findByComponentKeyAndMetricKeys(String componentKey, List<String> metricKeys, DbSession session) {
if (metricKeys.isEmpty()) {
return Collections.emptyList();
}
@Override
protected MeasureDto doInsert(DbSession session, MeasureDto item) {
- throw notImplemented();
+ throw notImplemented();
}
@Override
throw notImplemented();
}
- private static RuntimeException notImplemented(){
+ private static RuntimeException notImplemented() {
throw new IllegalStateException("Not implemented yet");
}
}
ActiveRuleDto activeRule;
ActiveRuleDao dao = db.activeRuleDao();
activeRule = ActiveRuleDto.createFor(context.profile(), context.rule());
- activeRule.setSeverity(change.getSeverity());
- if (change.getInheritance() != null) {
- activeRule.setInheritance(change.getInheritance().name());
+ String severity = change.getSeverity();
+ if (severity != null) {
+ activeRule.setSeverity(severity);
+ }
+ ActiveRule.Inheritance inheritance = change.getInheritance();
+ if (inheritance != null) {
+ activeRule.setInheritance(inheritance.name());
}
dao.insert(dbSession, activeRule);
for (Map.Entry<String, String> param : change.getParameters().entrySet()) {
}
private ActiveRuleDto doUpdate(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
- ActiveRuleDto activeRule;
ActiveRuleDao dao = db.activeRuleDao();
- activeRule = context.activeRule();
+ ActiveRuleDto activeRule = context.activeRule();
String severity = change.getSeverity();
if (severity != null) {
activeRule.setSeverity(severity);
import org.sonar.core.qualityprofile.db.ActiveRuleMapper;
import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
import org.sonar.core.qualityprofile.db.QualityProfileDao;
+import org.sonar.core.qualityprofile.db.QualityProfileDto;
import org.sonar.core.rule.RuleDto;
import org.sonar.server.db.BaseDao;
import org.sonar.server.rule.db.RuleDao;
@Deprecated
public ActiveRuleDto getById(DbSession session, int activeRuleId) {
ActiveRuleDto activeRule = mapper(session).selectById(activeRuleId);
-
if (activeRule != null) {
- activeRule.setKey(ActiveRuleKey.of(
- profileDao.getById(activeRule.getProfileId(), session).getKey(),
- ruleDao.getById(session, activeRule.getRulId()).getKey()));
+ QualityProfileDto profile = profileDao.getById(activeRule.getProfileId(), session);
+ RuleDto rule = ruleDao.getById(session, activeRule.getRulId());
+ if (profile != null && rule != null) {
+ activeRule.setKey(ActiveRuleKey.of(profile.getKey(), rule.getKey()));
+ return activeRule;
+ }
}
- return activeRule;
+ return null;
}
@Override
@Override
protected void doDeleteByKey(DbSession session, ActiveRuleKey key) {
- ActiveRuleDto rule = this.getNullableByKey(session, key);
- mapper(session).deleteParameters(rule.getId());
- mapper(session).delete(rule.getId());
+ ActiveRuleDto activeRule = getNullableByKey(session, key);
+ if (activeRule != null) {
+ mapper(session).deleteParameters(activeRule.getId());
+ mapper(session).delete(activeRule.getId());
+ }
}
/**
public void removeParamByKeyAndName(DbSession session, ActiveRuleKey key, String param) {
//TODO SQL rewrite to delete by key
ActiveRuleDto activeRule = getNullableByKey(session, key);
- ActiveRuleParamDto activeRuleParam = mapper(session).selectParamByActiveRuleAndKey(activeRule.getId(), param);
- Preconditions.checkNotNull(activeRuleParam.getId(), ACTIVE_RULE_PARAM_IS_NOT_PERSISTED);
- mapper(session).deleteParameter(activeRuleParam.getId());
+ if (activeRule != null) {
+ ActiveRuleParamDto activeRuleParam = mapper(session).selectParamByActiveRuleAndKey(activeRule.getId(), param);
+ if (activeRuleParam != null) {
+ mapper(session).deleteParameter(activeRuleParam.getId());
+ }
+ }
}
public void updateParam(DbSession session, ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam) {
return mapper(session).selectParamsByActiveRuleId(activeRule.getId());
}
+ @CheckForNull
public ActiveRuleParamDto getParamByKeyAndName(ActiveRuleKey key, String name, DbSession session) {
Preconditions.checkNotNull(key, ACTIVE_RULE_KEY_CANNOT_BE_NULL);
Preconditions.checkNotNull(name, PARAMETER_NAME_CANNOT_BE_NULL);
ActiveRuleDto activeRule = getNullableByKey(session, key);
- return mapper(session).selectParamByActiveRuleAndKey(activeRule.getId(), name);
+ if (activeRule!=null) {
+ return mapper(session).selectParamByActiveRuleAndKey(activeRule.getId(), name);
+ }
+ return null;
}
}
import org.sonar.server.db.DbClient;
import org.sonar.server.user.UserSession;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+import java.util.Set;
public class RuleUpdater implements ServerComponent {
this.system = system;
}
+ /**
+ * Update manual rules and custom rules (rules instantiated from templates)
+ */
public boolean update(RuleUpdate update, UserSession userSession) {
if (update.isEmpty()) {
return false;
if (update.isChangeParameters() && update.isCustomRule()) {
RuleDto customRule = context.rule;
RuleDto templateRule = dbClient.ruleDao().getTemplate(customRule, dbSession);
+ if (templateRule == null) {
+ throw new IllegalStateException(String.format("Template %s of rule %s does not exist",
+ customRule.getTemplateId(), customRule.getKey()));
+ }
List<RuleParamDto> templateRuleParams = dbClient.ruleDao().findRuleParamsByRuleKey(dbSession, templateRule.getKey());
List<String> paramKeys = new ArrayList<String>();
}
private void deleteOrUpdateParameters(DbSession dbSession, RuleUpdate update, RuleDto customRule, List<String> paramKeys,
- Multimap<RuleDto, ActiveRuleDto> activeRules, Multimap<ActiveRuleDto, ActiveRuleParamDto> activeRuleParams){
+ Multimap<RuleDto, ActiveRuleDto> activeRules, Multimap<ActiveRuleDto, ActiveRuleParamDto> activeRuleParams) {
for (RuleParamDto ruleParamDto : dbClient.ruleDao().findRuleParamsByRuleKey(dbSession, update.getRuleKey())) {
String key = ruleParamDto.getName();
String value = update.parameter(key);
}
private void createNewParameters(DbSession dbSession, RuleUpdate update, RuleDto customRule, List<RuleParamDto> templateRuleParams, List<String> paramKeys,
- Multimap<RuleDto, ActiveRuleDto> activeRules){
+ Multimap<RuleDto, ActiveRuleDto> activeRules) {
for (RuleParamDto templateRuleParam : templateRuleParams) {
String key = templateRuleParam.getName();
if (!paramKeys.contains(key)) {