1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.batch.tasks;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.config.Settings;
import org.sonar.api.task.Task;
import org.sonar.api.task.TaskComponent;
import org.sonar.api.task.TaskDefinition;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.scan.ScanTask;
import javax.annotation.Nullable;
import java.util.Map;
import java.util.regex.Pattern;
public class Tasks implements TaskComponent {
private static final Logger LOG = LoggerFactory.getLogger(Tasks.class);
private static final String COMMAND_PATTERN = "[a-zA-Z0-9\\-\\_]+";
private final TaskDefinition[] taskDefinitions;
private final Settings settings;
private final Map<String, TaskDefinition> taskDefByCommand = Maps.newHashMap();
private final Map<Class<? extends Task>, TaskDefinition> taskDefByTask = Maps.newHashMap();
public Tasks(Settings settings, TaskDefinition[] taskDefinitions) {
this.settings = settings;
this.taskDefinitions = taskDefinitions;
}
public TaskDefinition getTaskDefinition(@Nullable String command) {
String finalCommand = StringUtils.defaultIfBlank(command, settings.getString(CoreProperties.TASK));
finalCommand = StringUtils.defaultIfBlank(finalCommand, ScanTask.COMMAND);
if (taskDefByCommand.containsKey(finalCommand)) {
return taskDefByCommand.get(finalCommand);
}
throw new SonarException("No task found for command: " + finalCommand);
}
public TaskDefinition[] getTaskDefinitions() {
return taskDefinitions;
}
/**
* Perform validation of task definitions
*/
public void start() {
for (TaskDefinition def : taskDefinitions) {
validateTask(def);
validateName(def);
validateCommand(def);
validateDescription(def);
}
}
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");
}
}
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());
}
}
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);
}
}
|