Skip to content

Commit

Permalink
deal with default value correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
liweinan committed Oct 25, 2023
1 parent a383d51 commit 9322018
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
52 changes: 29 additions & 23 deletions jberet-core/src/main/java/org/jberet/repository/JdbcRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ public JdbcRepository(final Properties configProperties) {
}

/**
*
* @param propVal The propVal to process. For example: `${DB_PASS:xyz}`, this method will try to fetch the
* environment variable `DB_PASS` firstly, and if it does not exists, the method will return
* the default value `xyz`.
Expand All @@ -184,7 +183,12 @@ public static String parseProp(String propVal) {
if (matcher.find()) {
String prop = matcher.group(1);
String defaultVal = matcher.group(2);
return WildFlySecurityManager.getEnvPropertyPrivileged(prop, defaultVal);
String retVal = WildFlySecurityManager.getEnvPropertyPrivileged(prop, defaultVal);
if (retVal == null && defaultVal != null) {
return defaultVal;
} else {
return retVal;
}
} else {
return propVal;
}
Expand Down Expand Up @@ -234,7 +238,7 @@ private void createTables(final Properties configProperties) {
final String tablePrefix = configProperties.getProperty(DB_TABLE_PREFIX_KEY, "").trim();
final String tableSuffix = configProperties.getProperty(DB_TABLE_SUFFIX_KEY, "").trim();
final Pattern tableNamesPattern = tablePrefix.length() > 0 || tableSuffix.length() > 0 ?
Pattern.compile("JOB_INSTANCE|JOB_EXECUTION|STEP_EXECUTION|PARTITION_EXECUTION"): null;
Pattern.compile("JOB_INSTANCE|JOB_EXECUTION|STEP_EXECUTION|PARTITION_EXECUTION") : null;

final InputStream sqlResource = getClassLoader(false).getResourceAsStream(sqlFile);
try {
Expand Down Expand Up @@ -317,7 +321,7 @@ private void createTables(final Properties configProperties) {
countJobInstancesStatement.setString(1, "A");
rs = countJobInstancesStatement.executeQuery();
BatchLogger.LOGGER.tracef(
"This invocation needed to create tables since they didn't exit, but failed to create because they've been created by another concurrent invocation, so ignore the exception and return normally: %s", e1);
"This invocation needed to create tables since they didn't exit, but failed to create because they've been created by another concurrent invocation, so ignore the exception and return normally: %s", e1);
} catch (final SQLException sqle) {
//still cannot access the table, so fail it
throw BatchMessages.MESSAGES.failToCreateTables(e1, databaseProductName, ddlFile);
Expand Down Expand Up @@ -592,15 +596,15 @@ public JobExecutionImpl getJobExecution(final long jobExecutionId) {
if (result.getEndTime() == null && rs.getTimestamp(TableColumns.ENDTIME) != null) {
final Properties jobParameters1 = BatchUtil.stringToProperties(rs.getString(TableColumns.JOBPARAMETERS));
result = new JobExecutionImpl(getJobInstance(jobInstanceId),
jobExecutionId,
BatchUtil.stringToProperties(rs.getString(TableColumns.JOBPARAMETERS)),
rs.getTimestamp(TableColumns.CREATETIME),
rs.getTimestamp(TableColumns.STARTTIME),
rs.getTimestamp(TableColumns.ENDTIME),
rs.getTimestamp(TableColumns.LASTUPDATEDTIME),
rs.getString(TableColumns.BATCHSTATUS),
rs.getString(TableColumns.EXITSTATUS),
rs.getString(TableColumns.RESTARTPOSITION));
jobExecutionId,
BatchUtil.stringToProperties(rs.getString(TableColumns.JOBPARAMETERS)),
rs.getTimestamp(TableColumns.CREATETIME),
rs.getTimestamp(TableColumns.STARTTIME),
rs.getTimestamp(TableColumns.ENDTIME),
rs.getTimestamp(TableColumns.LASTUPDATEDTIME),
rs.getString(TableColumns.BATCHSTATUS),
rs.getString(TableColumns.EXITSTATUS),
rs.getString(TableColumns.RESTARTPOSITION));
jobExecutions.replace(jobExecutionId,
new SoftReference<JobExecutionImpl, Long>(result, jobExecutionReferenceQueue, jobExecutionId));
}
Expand Down Expand Up @@ -659,10 +663,10 @@ public List<JobExecution> getJobExecutions(final JobInstance jobInstance) {
final Properties jobParameters1 = BatchUtil.stringToProperties(rs.getString(TableColumns.JOBPARAMETERS));
jobExecution1 =
new JobExecutionImpl(getJobInstance(jobInstanceId), executionId, jobParameters1,
rs.getTimestamp(TableColumns.CREATETIME), rs.getTimestamp(TableColumns.STARTTIME),
rs.getTimestamp(TableColumns.ENDTIME), rs.getTimestamp(TableColumns.LASTUPDATEDTIME),
rs.getString(TableColumns.BATCHSTATUS), rs.getString(TableColumns.EXITSTATUS),
rs.getString(TableColumns.RESTARTPOSITION));
rs.getTimestamp(TableColumns.CREATETIME), rs.getTimestamp(TableColumns.STARTTIME),
rs.getTimestamp(TableColumns.ENDTIME), rs.getTimestamp(TableColumns.LASTUPDATEDTIME),
rs.getString(TableColumns.BATCHSTATUS), rs.getString(TableColumns.EXITSTATUS),
rs.getString(TableColumns.RESTARTPOSITION));
jobExecutions.replace(executionId,
new SoftReference<JobExecutionImpl, Long>(jobExecution1, jobExecutionReferenceQueue, executionId));
}
Expand All @@ -681,9 +685,9 @@ public List<JobExecution> getJobExecutions(final JobInstance jobInstance) {
private boolean isExecutionStale(final JobExecutionImpl jobExecution) {
final BatchStatus jobStatus = jobExecution.getBatchStatus();
if (jobStatus.equals(BatchStatus.COMPLETED) ||
jobStatus.equals(BatchStatus.FAILED) ||
jobStatus.equals(BatchStatus.STOPPED) ||
jobStatus.equals(BatchStatus.ABANDONED) || jobExecution.getStepExecutions().size() >= 1) {
jobStatus.equals(BatchStatus.FAILED) ||
jobStatus.equals(BatchStatus.STOPPED) ||
jobStatus.equals(BatchStatus.ABANDONED) || jobExecution.getStepExecutions().size() >= 1) {
return false;
}

Expand Down Expand Up @@ -906,8 +910,9 @@ public List<PartitionExecutionImpl> getPartitionExecutions(final long stepExecut

/**
* Updates the partition execution in job repository, using the {@code updateSql} passed in.
*
* @param partitionExecution the partition execution to update to job repository
* @param updateSql the update sql to use
* @param updateSql the update sql to use
* @return the number of rows affected by this update sql execution
*/
private int updatePartitionExecution(final PartitionExecutionImpl partitionExecution, final String updateSql) {
Expand All @@ -934,8 +939,9 @@ private int updatePartitionExecution(final PartitionExecutionImpl partitionExecu

/**
* Updates the step execution in job repository, using the {@code updateSql} passed in.
*
* @param stepExecution the step execution to update to job repository
* @param updateSql the update sql to use
* @param updateSql the update sql to use
* @return the number of rows affected by this update sql execution
*/
private int updateStepExecution0(final StepExecution stepExecution, final String updateSql) {
Expand Down Expand Up @@ -1086,7 +1092,7 @@ public void executeStatements(final String statements, final String statementsRe
}

private List<Long> getJobExecutions0(final String selectSql, final String jobName, final boolean runningExecutionsOnly,
final Integer limit) {
final Integer limit) {
final List<Long> result = new ArrayList<>();
Connection connection = null;
ResultSet rs = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executo
}

@Test
public void testResolveAttribute() {
public void testResolveDbPass() {
BatchSEEnvironment batchSEObject = new BatchSEEnvironment();
Assert.assertEquals("foopass",
JdbcRepository.parseProp(batchSEObject
Expand All @@ -186,4 +186,14 @@ public void testResolveAttribute() {
.toString()));
}

@Test
public void testResolveFooProp() {
BatchSEEnvironment batchSEObject = new BatchSEEnvironment();
Assert.assertEquals("foo",
JdbcRepository.parseProp(batchSEObject
.getBatchConfigurationProperties()
.get("foo")
.toString()));
}

}
1 change: 1 addition & 0 deletions jberet-se/src/test/resources/jberet.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ db-url = jdbc:h2:./target/jberet-repo
db-user =
db-password = ${DB_PASS:}
db-properties =
foo = ${NONE:foo}

# Optional, prefix and suffix for jdbc job repository database table names.
# If specified, they should be kept very short, and must not contain reserved characters
Expand Down

0 comments on commit 9322018

Please sign in to comment.