Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 367 #394

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions jberet-se/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ SPDX-License-Identifier: EPL-2.0
<reuseForks>false</reuseForks>
<!-- This will work for now as Batlet1Test needs to run before JobDataTest, but this should be reworked -->
<runOrder>alphabetical</runOrder>
<environmentVariables>
<DB_PASS>foopass</DB_PASS>
<FOO>foo</FOO>
</environmentVariables>
</configuration>
</plugin>
</plugins>
Expand Down
55 changes: 55 additions & 0 deletions jberet-se/src/main/java/org/jberet/se/BatchSEEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jakarta.transaction.TransactionManager;

import org.jberet.repository.JobRepository;
Expand All @@ -35,6 +38,7 @@
import org.jberet.tools.ChainedJobXmlResolver;
import org.jberet.tools.MetaInfBatchJobsJobXmlResolver;
import org.jberet.tx.LocalTransactionManager;
import org.wildfly.security.manager.WildFlySecurityManager;

/**
* Represents the Java SE batch runtime environment and its services.
Expand All @@ -58,6 +62,10 @@ public final class BatchSEEnvironment implements BatchEnvironment {
private final JobXmlResolver jobXmlResolver;
private final JobExecutor executor;

private static final String PROP_PATTERN_STR = "\\$\\{([0-9a-zA-Z_\\-]+)(:([0-9a-zA-Z_\\-]*))?\\}";

private static final Pattern PROP_PATTERN = Pattern.compile(PROP_PATTERN_STR);

static final String THREAD_POOL_TYPE = "thread-pool-type";
static final String THREAD_POOL_TYPE_CACHED = "Cached";
static final String THREAD_POOL_TYPE_FIXED = "Fixed";
Expand All @@ -72,12 +80,29 @@ public final class BatchSEEnvironment implements BatchEnvironment {
static final String THREAD_POOL_REJECTION_POLICY = "thread-pool-rejection-policy";
static final String THREAD_FACTORY = "thread-factory";

static final String DB_USER_KEY = "db-user";
static final String DB_PASSWORD_KEY = "db-password";

public BatchSEEnvironment() {
configProperties = new Properties();
final InputStream configStream = getClassLoader().getResourceAsStream(CONFIG_FILE_NAME);
if (configStream != null) {
try {
configProperties.load(configStream);
if (configProperties.getProperty(JOB_REPOSITORY_TYPE_KEY).equals(REPOSITORY_TYPE_JDBC) ||
configProperties.getProperty(JOB_REPOSITORY_TYPE_KEY).equals(REPOSITORY_TYPE_MONGODB)) {

String dbUser = configProperties.getProperty(DB_USER_KEY);
String dbPassword = configProperties.getProperty(DB_PASSWORD_KEY);

if (dbUser != null) {
configProperties.setProperty(DB_USER_KEY, parseProp(dbUser));
}

if (dbPassword != null) {
configProperties.setProperty(DB_PASSWORD_KEY, parseProp(dbPassword));
}
}
} catch (final IOException e) {
throw SEBatchMessages.MESSAGES.failToLoadConfig(e, CONFIG_FILE_NAME);
} finally {
Expand All @@ -103,6 +128,36 @@ protected int getMaximumPoolSize() {
this.jobXmlResolver = new ChainedJobXmlResolver(userJobXmlResolvers, DEFAULT_JOB_XML_RESOLVERS);
}


/**
* This method will parse the value like: {@code ${DB_PASS:xyz}}. It will try to fetch the value of the
* environment variable {@code DB_PASS} firstly, and if it does not exist, the method will return the
* default value {@code xyz}.
*
* @param propVal The property value to parse.
* @return Get the parsed value of the {@code propVal}.
*/
static String parseProp(String propVal) {
Matcher matcher = PROP_PATTERN.matcher(propVal);
if (matcher.matches()) {
matcher.reset();
if (matcher.find()) {
String prop = matcher.group(1);
String defaultVal = matcher.group(3);
String retVal = WildFlySecurityManager.getEnvPropertyPrivileged(prop, defaultVal);
if (retVal == null && defaultVal != null) {
return defaultVal;
} else {
return retVal;
}
} else {
return propVal;
}
} else {
return propVal;
}
}

@Override
public ClassLoader getClassLoader() {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.concurrent.TimeUnit;
import jakarta.batch.operations.BatchRuntimeException;

import org.jberet.repository.JdbcRepository;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -141,6 +142,46 @@ public void testCreateThreadPoolExecutor() throws Exception {
}
}

@Test
public void testResolveDbProps() {
BatchSEEnvironment batchSEObject = new BatchSEEnvironment();
Assert.assertEquals("foopass",
batchSEObject
.getBatchConfigurationProperties()
.getProperty(org.jberet.repository.JdbcRepository.DB_PASSWORD_KEY));

Assert.assertEquals("foouser",
batchSEObject
.getBatchConfigurationProperties()
.getProperty(JdbcRepository.DB_USER_KEY));
}
liweinan marked this conversation as resolved.
Show resolved Hide resolved

@Test
public void testPropParsings() {
String p1 = "${FOO:bar}";
String p2 = "${FOO}";
String p3 = "${BAR:defaultVal}";
String p4 = "${INVALID";
String p5 = "$INVALID}";
String p6 = "${{INVALID}";
String p7 = "${INVALID}}";
String p8 = "${BAR:}";
String p9 = "${}";
String p10 = "${BAR}";


Assert.assertEquals("foo", BatchSEEnvironment.parseProp(p1));
Assert.assertEquals("foo", BatchSEEnvironment.parseProp(p2));
Assert.assertEquals("defaultVal", BatchSEEnvironment.parseProp(p3));
Assert.assertEquals("${INVALID", BatchSEEnvironment.parseProp(p4));
Assert.assertEquals("$INVALID}", BatchSEEnvironment.parseProp(p5));
Assert.assertEquals("${{INVALID}", BatchSEEnvironment.parseProp(p6));
Assert.assertEquals("${INVALID}}", BatchSEEnvironment.parseProp(p7));
Assert.assertEquals("", BatchSEEnvironment.parseProp(p8));
Assert.assertEquals("${}", BatchSEEnvironment.parseProp(p9));
Assert.assertNull(BatchSEEnvironment.parseProp(p10));
}

private ThreadPoolExecutor verifyThreadPool(final int coreSize,
final int maxSize,
final long keepAliveTime,
Expand Down
4 changes: 2 additions & 2 deletions jberet-se/src/test/resources/jberet.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ infinispan-xml =
# Use the target directory to store the DB
db-url = jdbc:h2:./target/jberet-repo
#db-url = mongodb://localhost/testData
db-user =
db-password =
db-user = ${NONE:foouser}
db-password = ${DB_PASS}
db-properties =

# Optional, prefix and suffix for jdbc job repository database table names.
Expand Down