Skip to content

Commit

Permalink
SCT-33 Sonar Cleanup
Browse files Browse the repository at this point in the history
* Removes all Critical and Major notifications from Spring Cloud Task.
* Removes unused local variables
* Removes What it saw as redundant null checks and subsequent complexity alert.

resolves #33
  • Loading branch information
cppwfs authored and mminella committed Dec 16, 2015
1 parent ec096b4 commit 5c3795e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,54 +82,51 @@ public TaskRepository taskRepository(){
* sensible values as long as a unique DataSource is available.
*/
@PostConstruct
private void initialize() {
protected void initialize() {
if (initialized) {
return;
}
logger.debug("Getting Task Configurer");
TaskConfigurer configurer = getConfigurer(context.getBeansOfType(TaskConfigurer.class).values());
if (configurer == null) {
configurer = getDefaultConfigurer(context.getBeansOfType(TaskConfigurer.class).values());
}
logger.debug(String.format("Using %s TaskConfigurer",
configurer.getClass().getName()));
taskRepository = configurer.getTaskRepository();
initialized = true;
}

private TaskConfigurer getConfigurer(Collection<TaskConfigurer> configurers) {
if (this.configurer != null) {
logger.debug(String.format("Using %s TaskConfigurer",
configurer.getClass().getName()));
return this.configurer;
}
private TaskConfigurer getDefaultConfigurer(Collection<TaskConfigurer> configurers) {
boolean isDataSourceConfigured = (dataSources != null && !dataSources.isEmpty());
verifyEnvironment(configurers);
if (configurers == null || configurers.isEmpty()) {
if (dataSources == null || dataSources.isEmpty()) {
if (!isDataSourceConfigured) {
this.configurer = new DefaultTaskConfigurer();
logger.debug(String.format("Using %s TaskConfigurer, with no datasource",
configurer.getClass().getName()));
return this.configurer;
}
else if (dataSources != null && dataSources.size() == 1) {
else {
DataSource dataSource = dataSources.iterator().next();
if(taskInitializationEnable) {
logger.debug("Initializing Task Schema");
TaskDatabaseInitializer.initializeDatabase(dataSource, resourceLoader);
}
this.configurer = new DefaultTaskConfigurer(dataSource);
logger.debug(String.format("Using %s TaskConfigurer, with datasource",
configurer.getClass().getName()));
return this.configurer;
}
else {
throw new IllegalStateException("To use the default TaskConfigurer the context must contain no more than" +
"one DataSource, found " + dataSources.size());
}
}
this.configurer = configurers.iterator().next();
return this.configurer;
}

private void verifyEnvironment(Collection configurers){
if (dataSources != null && dataSources.size() > 1) {
throw new IllegalStateException("To use the default TaskConfigurer the context must contain no more than" +
"one DataSource, found " + dataSources.size());
}
if (configurers.size() > 1) {
throw new IllegalStateException(
"To use a custom TaskConfigurer the context must contain precisely one, found "
+ configurers.size());
}
this.configurer = configurers.iterator().next();
logger.debug(String.format("More than one Task Configurer available. Using"
+ " first in list: %s TaskConfigurer",
configurer.getClass().getName()));
return this.configurer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ public TaskExecution(String executionId, int exitCode, String taskName,
this.executionId = executionId;
this.exitCode = exitCode;
this.taskName = taskName;
this.startTime = startTime;
this.endTime = endTime;
this.statusCode = statusCode;
this.exitMessage = exitMessage;
this.parameters = parameters;
setStartTime(startTime);
setEndTime(endTime);
}

public String getExecutionId() {
Expand Down Expand Up @@ -115,19 +115,19 @@ public void setTaskName(String taskName) {
}

public Date getStartTime() {
return startTime;
return (startTime != null) ? (Date)startTime.clone() : null;
}

public void setStartTime(Date startTime) {
this.startTime = startTime;
this.startTime = (startTime != null) ? (Date)startTime.clone() : null;
}

public Date getEndTime() {
return endTime;
return (endTime != null) ? (Date)endTime.clone() : null;
}

public void setEndTime(Date endTime) {
this.endTime = endTime;
this.endTime = (endTime != null) ? (Date)endTime.clone() : null;
}

public String getStatusCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void saveTaskExecution(TaskExecution taskExecution) {
taskExecution.getTaskName(), taskExecution.getExitCode(),
taskExecution.getExitMessage(), new Date(),
taskExecution.getStatusCode() };
int addCount = jdbcTemplate.update(
jdbcTemplate.update(
getQuery(SAVE_TASK_EXECUTION),
parameters,
new int[]{ Types.VARCHAR, Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR,
Expand All @@ -91,7 +91,7 @@ public void updateTaskExecution(TaskExecution taskExecution) {
taskExecution.getTaskName(), taskExecution.getExitCode(),
taskExecution.getExitMessage(), new Date(), taskExecution.getStatusCode(),
taskExecution.getExecutionId() };
int count = jdbcTemplate.update(
jdbcTemplate.update(
getQuery(UPDATE_TASK_EXECUTION),
parameters,
new int[]{ Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.INTEGER,
Expand Down Expand Up @@ -131,11 +131,8 @@ private void insertJobParameters(String executionId, List<String> taskParameters
* TASK_EXECUTION_PARAMS table.
*/
private void insertParameter(String executionId, String param) {

Object[] args = new Object[0];
int[] argTypes = new int[]{ Types.VARCHAR, Types.VARCHAR };

args = new Object[]{ executionId, param };
Object[] args = new Object[]{ executionId, param };
jdbcTemplate.update(getQuery(CREATE_TASK_PARAMETER), args, argTypes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @author Glenn Renfro
*/

public class TaskDatabaseInitializer {
public final class TaskDatabaseInitializer {

private static final Log logger = LogFactory.getLog(TaskDatabaseInitializer.class);

Expand All @@ -41,6 +41,10 @@ public class TaskDatabaseInitializer {
*/
private static String schema = DEFAULT_SCHEMA_LOCATION;

private TaskDatabaseInitializer(){

}

public static void initializeDatabase(DataSource dataSource, ResourceLoader resourceLoader) {
if (dataSource != null) {
String platform = getDatabaseType(dataSource);
Expand Down

0 comments on commit 5c3795e

Please sign in to comment.