Skip to content

Commit

Permalink
Add support for different subsystems
Browse files Browse the repository at this point in the history
  • Loading branch information
marekkopecky committed Sep 10, 2024
1 parent ce08561 commit dcf0d3b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ public final void apply(OfflineCommandContext ctx) throws CommandFailedException
.parameter("enableAfterCreation", enableAfterCreation)
.parameter("replaceExisting", replaceExisting)

.parameter("subsystemVersions", ctx.subsystemVersions)

.build();

ctx.client.apply(transform);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import org.wildfly.extras.creaper.core.offline.NameSpaceVersion

securityInElements = subsystemVersions.get("datasources").lessThen(new NameSpaceVersion(7, 2))

// attributes of <datasource>
datasourceAttrs = ['pool-name': poolName]
if (nn(jta)) datasourceAttrs['jta'] = jta
Expand Down Expand Up @@ -45,9 +49,15 @@ def dsDefinition = {
}
if (nn(userName, password, securityDomain)) {
securityAttrs = [:]
if (nn(userName)) securityAttrs['user-name'] = userName
if (nn(password)) securityAttrs['password'] = password
if (!securityInElements) {
if (nn(userName)) securityAttrs['user-name'] = userName
if (nn(password)) securityAttrs['password'] = password
}
security(securityAttrs) {
if (securityInElements) {
if (nn(userName)) 'user-name'(userName)
if (nn(password)) 'password'(password)
}
if (nn(securityDomain)) 'security-domain'(securityDomain)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.wildfly.extras.creaper.core.offline;

public class NameSpaceVersion {
private int major;
private int minor;

public NameSpaceVersion(int major, int minor) {
this.major = major;
this.minor = minor;
}

/**
* Detect whether current version is less then another version.
* @param comparison Second version for comparison.
* @return True or false based on the check.
*/
public boolean lessThen(NameSpaceVersion comparison) {
if (this.major < comparison.major) {
return true;
} else if (this.major == comparison.major) {
if (this.minor < comparison.minor) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
import org.wildfly.extras.creaper.core.ServerVersion;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public final class OfflineCommandContext {
public final OfflineManagementClient client;
public final OfflineOptions options; // same as client.options()
public final ServerVersion version;
public final File configurationFile; // same as client.options().configurationFile
public final Map<String, NameSpaceVersion> subsystemVersions;

OfflineCommandContext(OfflineManagementClient client, ServerVersion version) {
this(client, version, new HashMap<>());
}

OfflineCommandContext(OfflineManagementClient client, ServerVersion version,
Map<String, NameSpaceVersion> subsystemVersions) {
this.client = client;
this.options = client.options();
this.version = version;
this.configurationFile = client.options().configurationFile;
this.subsystemVersions = subsystemVersions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

final class OfflineManagementClientImpl implements OfflineManagementClient {
private static final Logger log = Logger.getLogger(OfflineManagementClient.class);

private final OfflineOptions options;
private final ServerVersion version;
private final Map<String, NameSpaceVersion> subsystemVersions;

OfflineManagementClientImpl(OfflineOptions options) throws IOException {
File configurationFile = options.configurationFile;
Expand All @@ -22,6 +24,7 @@ final class OfflineManagementClientImpl implements OfflineManagementClient {

this.options = options;
this.version = OfflineServerVersion.discover(configurationFile);
this.subsystemVersions = OfflineServerVersion.discoverSubsystems(configurationFile);
}

@Override
Expand All @@ -42,7 +45,7 @@ public void apply(OfflineCommand... commands) throws CommandFailedException {
@Override
public void apply(Iterable<OfflineCommand> commands) throws CommandFailedException {
try {
OfflineCommandContext ctx = new OfflineCommandContext(this, version);
OfflineCommandContext ctx = new OfflineCommandContext(this, version, subsystemVersions);
for (OfflineCommand command : commands) {
log.infof("Applying command %s", command);
command.apply(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -25,6 +27,13 @@ final class OfflineServerVersion {

private static final Pattern ROOT_XMLNS = Pattern.compile("[\"']urn:jboss:domain:(?:community:|preview:|experimental:)?(\\d+)\\.(\\d+)[\"']");

private static final Pattern DATASOURCE_XMLNS = Pattern.compile("[\"']urn:jboss:domain:datasources:(?:community:|preview:|experimental:)?(\\d+)\\.(\\d+)[\"']");

/**
* Content of configuration file
*/
private static String content = null;

private OfflineServerVersion() {
// avoid instantiation
}
Expand All @@ -36,7 +45,9 @@ private OfflineServerVersion() {
*/
static ServerVersion discover(File configurationFile) throws IOException {
// this is not entirely cheap, but it only occurs once, during an operation that is supposed to be costly anyway
String content = Files.toString(configurationFile, Charsets.UTF_8);
if (content == null) {
content = Files.toString(configurationFile, Charsets.UTF_8);
}
Matcher matcher = ROOT_XMLNS.matcher(content);
if (matcher.find()) {
String majorStr = matcher.group(1);
Expand Down Expand Up @@ -73,4 +84,24 @@ static ServerVersion discover(File configurationFile) throws IOException {

throw new IllegalArgumentException("Missing or bad schema version in configuration file " + configurationFile);
}

static Map<String, NameSpaceVersion> discoverSubsystems(File configurationFile) throws IOException {
// this is not entirely cheap, but it only occurs once, during an operation that is supposed to be costly anyway
if (content == null) {
content = Files.toString(configurationFile, Charsets.UTF_8);
}
Map<String, NameSpaceVersion> subsystemVersions = new HashMap<>();
Matcher matcher = DATASOURCE_XMLNS.matcher(content);
if (matcher.find()) {
String majorStr = matcher.group(1);
String minorStr = matcher.group(2);

int major = Integer.parseInt(majorStr);
int minor = Integer.parseInt(minorStr);

subsystemVersions.put("datasources", new NameSpaceVersion(major, minor));
}

return subsystemVersions;
}
}

0 comments on commit dcf0d3b

Please sign in to comment.