SCT-33 Sonar Cleanup

* 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 spring-cloud/spring-cloud-task#33
This commit is contained in:
Glenn Renfro
2015-12-14 17:15:52 -05:00
committed by Michael Minella
parent ec096b473c
commit 5c3795e7c7
4 changed files with 34 additions and 36 deletions

View File

@@ -82,54 +82,51 @@ public class SimpleTaskConfiguration {
* 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;
}
}

View File

@@ -83,11 +83,11 @@ public class TaskExecution {
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() {
@@ -115,19 +115,19 @@ public class TaskExecution {
}
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() {

View File

@@ -70,7 +70,7 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
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,
@@ -91,7 +91,7 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
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,
@@ -131,11 +131,8 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
* 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);
}

View File

@@ -29,7 +29,7 @@ import org.springframework.jdbc.support.MetaDataAccessException;
* @author Glenn Renfro
*/
public class TaskDatabaseInitializer {
public final class TaskDatabaseInitializer {
private static final Log logger = LogFactory.getLog(TaskDatabaseInitializer.class);
@@ -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);