private void validateCommand(TaskDefinition def) { String command = def.getCommand(); if (StringUtils.isBlank(command)) { throw new SonarException( "Task definition '" + def.getName() + "' doesn't define task command"); } if (!Pattern.matches(COMMAND_PATTERN, command)) { throw new SonarException( "Command '" + command + "' for task definition '" + def.getName() + "' is not valid and should match " + COMMAND_PATTERN); } if (taskDefByCommand.containsKey(command)) { throw new SonarException( "Task '" + def.getName() + "' uses the same command than task '" + taskDefByCommand.get(command).getName() + "'"); } taskDefByCommand.put(command, def); }
private void validateDescription(TaskDefinition def) { if (StringUtils.isBlank(def.getDescription())) { LOG.warn( "Task definition {} doesn't define a description. Using name as description.", def.getName()); def.setDescription(def.getName()); } }
@Override public void doAfterStart() { // default value is declared in CorePlugin String taskKey = StringUtils.defaultIfEmpty( taskProperties.get(CoreProperties.TASK), CoreProperties.SCAN_TASK); TaskDefinition def = getComponentByType(Tasks.class).definition(taskKey); if (def == null) { throw MessageException.of("Task " + taskKey + " does not exist"); } Task task = getComponentByType(def.taskClass()); if (task != null) { task.execute(); } else { throw new IllegalStateException("Task " + taskKey + " is badly defined"); } }
private void validateTask(TaskDefinition def) { Class<? extends Task> taskClass = def.getTask(); if (taskClass == null) { throw new SonarException( "Task definition '" + def.getName() + "' doesn't define the associated task class"); } if (taskDefByTask.containsKey(taskClass)) { throw new SonarException( "Task '" + def.getTask().getName() + "' is defined twice: first by '" + taskDefByTask.get(taskClass).getName() + "' and then by '" + def.getName() + "'"); } taskDefByTask.put(taskClass, def); }
public class ScanTask implements Task { public static final TaskDefinition DEFINITION = TaskDefinition.builder() .description("Scan project") .key(CoreProperties.SCAN_TASK) .taskClass(ScanTask.class) .build(); private final ComponentContainer taskContainer; private final TaskProperties taskProps; public ScanTask(TaskContainer taskContainer, TaskProperties taskProps) { this.taskContainer = taskContainer; this.taskProps = taskProps; } @Override public void execute() { AnalysisProperties props = new AnalysisProperties( taskProps.properties(), taskProps.property(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); if (isIssuesMode(props)) { String projectKey = getProjectKeyWithBranch(props); new ProjectSyncContainer(taskContainer, projectKey, false).execute(); } new ProjectScanContainer(taskContainer, props).execute(); } @CheckForNull private static String getProjectKeyWithBranch(AnalysisProperties props) { String projectKey = props.property(CoreProperties.PROJECT_KEY_PROPERTY); if (projectKey != null && props.property(CoreProperties.PROJECT_BRANCH_PROPERTY) != null) { projectKey = projectKey + ":" + props.property(CoreProperties.PROJECT_BRANCH_PROPERTY); } return projectKey; } private boolean isIssuesMode(AnalysisProperties props) { DefaultAnalysisMode mode = new DefaultAnalysisMode(taskContainer.getComponentByType(GlobalProperties.class), props); return mode.isIssues(); } }
private void validateName(TaskDefinition def) { if (StringUtils.isBlank(def.getName())) { throw new SonarException( "Task definition for task '" + def.getTask().getName() + "' doesn't define task name"); } }