Skip to content

Commit

Permalink
FISH-9198 Use jackson dataformat toml
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinchan committed Oct 10, 2024
1 parent d8805f5 commit ff1b846
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 157 deletions.
5 changes: 2 additions & 3 deletions appserver/packager/appserver-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,8 @@
<artifactId>jakarta.jws-api</artifactId>
</dependency>
<dependency>
<groupId>fish.payara.server.internal.packager</groupId>
<artifactId>tomlj-repackaged</artifactId>
<version>${project.version}</version>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-toml</artifactId>
</dependency>
</dependencies>
</project>
1 change: 0 additions & 1 deletion appserver/packager/external/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
<module>jcip</module>
<module>metro-xmlsec</module>
<module>jakarta-ee9-shim</module>
<module>tomlj-repackaged</module>
</modules>
<build>
<plugins>
Expand Down
124 changes: 0 additions & 124 deletions appserver/packager/external/tomlj-repackaged/pom.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>fish.payara.server.internal.packager</groupId>
<artifactId>tomlj-repackaged</artifactId>
<version>${project.version}</version>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-toml</artifactId>
</dependency>

<!-- Test dependencies -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,20 @@
*/
package fish.payara.microprofile.config.extensions.toml;

import com.fasterxml.jackson.dataformat.toml.TomlMapper;
import fish.payara.nucleus.microprofile.config.source.extension.ConfiguredExtensionConfigSource;
import fish.payara.nucleus.microprofile.config.spi.MicroprofileConfigConfiguration;
import jakarta.inject.Inject;
import org.jvnet.hk2.annotations.Service;
import org.tomlj.Toml;
import org.tomlj.TomlArray;
import org.tomlj.TomlParseResult;
import org.tomlj.TomlTable;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

@Service(name = "toml-config-source")
Expand Down Expand Up @@ -97,15 +94,13 @@ public Map<String, String> getProperties() {
try {
properties.clear();
lastModified = tomlFileLastModified;
TomlParseResult tomlParseResult = Toml.parse(tomlFilePath);
if (tomlParseResult.hasErrors()) {
LOGGER.log(Level.WARNING, "Error parsing TOML file: {0}", tomlParseResult.errors().toString());
return properties;
}
flattenToml(tomlParseResult, "", properties, 0, Integer.parseInt(configuration.getDepth()));
TomlMapper tomlMapper = new TomlMapper();
Map<?, ?> config = tomlMapper.readValue(tomlFilePath.toFile(), Map.class);

flattenToml(config, "", properties, 0, Integer.parseInt(configuration.getDepth()));

} catch (IOException e) {
throw new RuntimeException(e);
LOGGER.warning(e.getMessage());
}
return properties;
}
Expand Down Expand Up @@ -146,25 +141,25 @@ private Path getFilePath() {
return file.exists() ? tomlPath : null;
}

private void flattenToml(TomlTable tomlTable, String prefix, Map<String, String> resultMap, int depth, int maxDepth) {
private void flattenToml(Map<?,?> tomlTable, String prefix, Map<String, String> resultMap, int depth, int maxDepth) {
if (depth > maxDepth) {
throw new IllegalArgumentException("Exceeded maximum depth of " + maxDepth);
}
for (String key : tomlTable.keySet()) {
for (Object key : tomlTable.keySet()) {
Object value;
try {
value = tomlTable.get(key);
} catch (IllegalArgumentException ex) {
continue;
}
String fullKey = prefix.isEmpty() ? key : prefix + "." + key;
String fullKey = prefix.isEmpty() ? key.toString() : prefix + "." + key.toString();

if (value instanceof TomlTable) {
if (value instanceof Map) {
// If the value is a TomlTable (nested table), recurse into it
flattenToml((TomlTable) value, fullKey, resultMap, depth + 1, maxDepth);
} else if (value instanceof TomlArray) {
flattenToml((Map<?, ?>) value, fullKey, resultMap, depth + 1, maxDepth);
} else if (value instanceof ArrayList<?>) {
// If the value is a TomlArray, iterate through the array
flattenTomlArray((TomlArray) value, fullKey, resultMap, depth + 1, maxDepth);
flattenTomlArray((ArrayList<?>) value, fullKey, resultMap, depth + 1, maxDepth);
} else {
// Otherwise, add the key-value pair to the result map
if (value == null) {
Expand All @@ -175,18 +170,18 @@ private void flattenToml(TomlTable tomlTable, String prefix, Map<String, String>
}
}

private void flattenTomlArray(TomlArray array, String prefix, Map<String, String> resultMap, int depth, int maxDepth) {
private void flattenTomlArray(ArrayList<?> array, String prefix, Map<String, String> resultMap, int depth, int maxDepth) {
if (depth > maxDepth) {
throw new IllegalArgumentException("Exceeded maximum depth of " + maxDepth);
}
for (int i = 0; i < array.size(); i++) {
Object arrayValue = array.get(i);
String arrayKey = prefix + "[" + i + "]";
if (arrayValue instanceof TomlTable) {
flattenToml((TomlTable) arrayValue, arrayKey, resultMap, depth + 1, maxDepth);
} else if (arrayValue instanceof TomlArray) {
if (arrayValue instanceof Map) {
flattenToml((Map<?, ?>) arrayValue, arrayKey, resultMap, depth + 1, maxDepth);
} else if (arrayValue instanceof ArrayList) {
// Recursively flatten the nested array
flattenTomlArray((TomlArray) arrayValue, arrayKey, resultMap, depth + 1, maxDepth);
flattenTomlArray((ArrayList<?>) arrayValue, arrayKey, resultMap, depth + 1, maxDepth);
} else {
resultMap.put(arrayKey, arrayValue.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2024] [Payara Foundation and/or its affiliates]
// Portions Copyright [2016-2022] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.registration.glassfish;

Expand Down Expand Up @@ -330,7 +330,6 @@ public class ModuleMap {
put("org.objectweb.asm.tree", 271);
put("org.objectweb.asm.tree.analysis", 272);
put("org.objectweb.asm.util", 273);
put("fish.payara.server.core.packager.tomlj-repackaged",274)
}});

public ModuleMap() {
Expand Down

0 comments on commit ff1b846

Please sign in to comment.