From 93220189058a1725ccbdc762a1a0896fd2ba9375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=94=B7?= Date: Thu, 26 Oct 2023 02:53:55 +0800 Subject: [PATCH] deal with default value correctly --- .../org/jberet/repository/JdbcRepository.java | 52 +++++++++++-------- .../org/jberet/se/BatchSEEnvironmentTest.java | 12 ++++- .../src/test/resources/jberet.properties | 1 + 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/jberet-core/src/main/java/org/jberet/repository/JdbcRepository.java b/jberet-core/src/main/java/org/jberet/repository/JdbcRepository.java index a66717317..435524e5a 100644 --- a/jberet-core/src/main/java/org/jberet/repository/JdbcRepository.java +++ b/jberet-core/src/main/java/org/jberet/repository/JdbcRepository.java @@ -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`. @@ -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; } @@ -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 { @@ -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); @@ -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(result, jobExecutionReferenceQueue, jobExecutionId)); } @@ -659,10 +663,10 @@ public List 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(jobExecution1, jobExecutionReferenceQueue, executionId)); } @@ -681,9 +685,9 @@ public List 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; } @@ -906,8 +910,9 @@ public List 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) { @@ -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) { @@ -1086,7 +1092,7 @@ public void executeStatements(final String statements, final String statementsRe } private List getJobExecutions0(final String selectSql, final String jobName, final boolean runningExecutionsOnly, - final Integer limit) { + final Integer limit) { final List result = new ArrayList<>(); Connection connection = null; ResultSet rs = null; diff --git a/jberet-se/src/test/java/org/jberet/se/BatchSEEnvironmentTest.java b/jberet-se/src/test/java/org/jberet/se/BatchSEEnvironmentTest.java index 912d7178a..eca5d6965 100644 --- a/jberet-se/src/test/java/org/jberet/se/BatchSEEnvironmentTest.java +++ b/jberet-se/src/test/java/org/jberet/se/BatchSEEnvironmentTest.java @@ -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 @@ -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())); + } + } diff --git a/jberet-se/src/test/resources/jberet.properties b/jberet-se/src/test/resources/jberet.properties index 57aa967b2..eb3fd5ff7 100644 --- a/jberet-se/src/test/resources/jberet.properties +++ b/jberet-se/src/test/resources/jberet.properties @@ -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