Skip to content

Commit

Permalink
Refactor properties v2 parser to use PropertiesSubset
Browse files Browse the repository at this point in the history
The `PropertiesV1ConfigurationParser` uses the `PropertiesSubset` abstraction to pass subsets of properties between methods, while keeping track of the global name of each property.

We refactor the `PropertiesV2ConfigurationParser` to use the same abstraction, which allows us to eliminate many helper methods in `PropertiesUtils`.
  • Loading branch information
ppkarwasz committed Dec 16, 2024
1 parent b71af98 commit b018a8e
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,20 @@
*/
package org.apache.logging.converter.config.internal;

import java.util.Map;
import java.util.Comparator;
import java.util.Properties;
import java.util.stream.Stream;
import org.apache.logging.converter.config.ConfigurationConverterException;
import org.apache.logging.converter.config.spi.v1.PropertiesSubset;
import org.apache.logging.converter.config.spi.PropertiesSubset;
import org.jspecify.annotations.Nullable;

public final class PropertiesUtils {

public static @Nullable String getAndRemove(Properties properties, String key) {
return (String) properties.remove(key);
}

public static String getLastComponent(String name) {
int idx = name.lastIndexOf('.');
return idx == -1 ? name : name.substring(idx + 1);
}

public static Properties extractSubset(Properties properties, String prefix) {
Properties subset = org.apache.logging.log4j.util.PropertiesUtil.extractSubset(properties, prefix);
String value = getAndRemove(properties, prefix);
if (value != null) {
subset.setProperty("", value);
}
return subset;
}

public static @Nullable String extractProperty(PropertiesSubset subset, String key) {
return (String) subset.getProperties().remove(key);
}
Expand All @@ -51,25 +38,24 @@ public static PropertiesSubset extractSubset(PropertiesSubset parentSubset, Stri
Properties parentProperties = parentSubset.getProperties();
Properties properties =
org.apache.logging.log4j.util.PropertiesUtil.extractSubset(parentProperties, childPrefix);
String value = getAndRemove(parentProperties, childPrefix);
String value = (String) parentProperties.remove(childPrefix);
if (value != null) {
properties.setProperty("", value);
}
return PropertiesSubset.of(addPrefixes(parentSubset.getPrefix(), childPrefix), properties);
}

public static Map<String, Properties> partitionOnCommonPrefixes(Properties properties) {
return org.apache.logging.log4j.util.PropertiesUtil.partitionOnCommonPrefixes(properties, true);
}

public static Stream<PropertiesSubset> partitionOnCommonPrefixes(PropertiesSubset parentSubset) {
String parentPrefix = parentSubset.getPrefix();
String effectivePrefix = parentPrefix.isEmpty() ? parentPrefix : parentPrefix + ".";
return org.apache.logging.log4j.util.PropertiesUtil.partitionOnCommonPrefixes(
Stream<PropertiesSubset> partitioned = org.apache.logging.log4j.util.PropertiesUtil.partitionOnCommonPrefixes(
parentSubset.getProperties(), true)
.entrySet()
.stream()
.map(entry -> PropertiesSubset.of(effectivePrefix + entry.getKey(), entry.getValue()));
.map(entry -> PropertiesSubset.of(effectivePrefix + entry.getKey(), entry.getValue()))
.sorted(Comparator.comparing(PropertiesSubset::getPrefix));
parentSubset.getProperties().clear();
return partitioned;
}

private static String addPrefixes(String left, String right) {
Expand All @@ -94,5 +80,19 @@ public static void throwIfNotEmpty(PropertiesSubset subset) {
}
}

/**
* @return {@code true} if the subset contains no properties.
*/
public static boolean isEmpty(PropertiesSubset subset) {
return subset.getProperties().isEmpty();
}

/**
* @return {@code true} if the subset contains some properties.
*/
public static boolean isNotEmpty(PropertiesSubset subset) {
return !isEmpty(subset);
}

private PropertiesUtils() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import org.apache.logging.converter.config.internal.StringUtils;
import org.apache.logging.converter.config.internal.XmlUtils;
import org.apache.logging.converter.config.spi.ConfigurationNode;
import org.apache.logging.converter.config.spi.PropertiesSubset;
import org.apache.logging.converter.config.spi.v1.Log4j1ComponentParser;
import org.apache.logging.converter.config.spi.v1.Log4j1ParserContext;
import org.apache.logging.converter.config.spi.v1.PropertiesSubset;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.apache.logging.converter.config.internal.PropertiesUtils;
import org.apache.logging.converter.config.spi.ConfigurationNode;
import org.apache.logging.converter.config.spi.ConfigurationParser;
import org.apache.logging.converter.config.spi.v1.PropertiesSubset;
import org.apache.logging.converter.config.spi.PropertiesSubset;

@ServiceProvider(value = ConfigurationParser.class, resolution = Resolution.MANDATORY)
public class PropertiesV1ConfigurationParser extends AbstractV1ConfigurationParser {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.apache.logging.converter.config.internal.PropertiesUtils;
import org.apache.logging.converter.config.internal.v1.AbstractComponentParser;
import org.apache.logging.converter.config.spi.ConfigurationNode;
import org.apache.logging.converter.config.spi.PropertiesSubset;
import org.apache.logging.converter.config.spi.v1.Log4j1ParserContext;
import org.apache.logging.converter.config.spi.v1.PropertiesSubset;
import org.jspecify.annotations.Nullable;
import org.w3c.dom.Element;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.apache.logging.converter.config.internal.ComponentUtils;
import org.apache.logging.converter.config.internal.v1.AbstractComponentParser;
import org.apache.logging.converter.config.spi.ConfigurationNode;
import org.apache.logging.converter.config.spi.PropertiesSubset;
import org.apache.logging.converter.config.spi.v1.Log4j1ComponentParser;
import org.apache.logging.converter.config.spi.v1.PropertiesSubset;
import org.w3c.dom.Element;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import org.apache.logging.converter.config.internal.ComponentUtils;
import org.apache.logging.converter.config.internal.ComponentUtils.ConfigurationNodeBuilder;
import org.apache.logging.converter.config.spi.ConfigurationNode;
import org.apache.logging.converter.config.spi.PropertiesSubset;
import org.apache.logging.converter.config.spi.v1.Log4j1ComponentParser;
import org.apache.logging.converter.config.spi.v1.PropertiesSubset;
import org.w3c.dom.Element;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import org.apache.logging.converter.config.internal.ComponentUtils;
import org.apache.logging.converter.config.internal.ComponentUtils.ConfigurationNodeBuilder;
import org.apache.logging.converter.config.spi.ConfigurationNode;
import org.apache.logging.converter.config.spi.PropertiesSubset;
import org.apache.logging.converter.config.spi.v1.Log4j1ComponentParser;
import org.apache.logging.converter.config.spi.v1.PropertiesSubset;
import org.w3c.dom.Element;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.apache.logging.converter.config.internal.ComponentUtils;
import org.apache.logging.converter.config.internal.ComponentUtils.ConfigurationNodeBuilder;
import org.apache.logging.converter.config.spi.ConfigurationNode;
import org.apache.logging.converter.config.spi.PropertiesSubset;
import org.apache.logging.converter.config.spi.v1.Log4j1ComponentParser;
import org.apache.logging.converter.config.spi.v1.PropertiesSubset;
import org.jspecify.annotations.Nullable;
import org.w3c.dom.Element;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import java.lang.invoke.MethodHandle;
import java.util.Map;
import org.apache.logging.converter.config.internal.v1.AbstractComponentParser;
import org.apache.logging.converter.config.spi.PropertiesSubset;
import org.apache.logging.converter.config.spi.v1.Log4j1ComponentParser;
import org.apache.logging.converter.config.spi.v1.PropertiesSubset;
import org.w3c.dom.Element;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.apache.logging.converter.config.internal.StringUtils;
import org.apache.logging.converter.config.internal.v1.AbstractComponentParser;
import org.apache.logging.converter.config.spi.ConfigurationNode;
import org.apache.logging.converter.config.spi.PropertiesSubset;
import org.apache.logging.converter.config.spi.v1.Log4j1ComponentParser;
import org.apache.logging.converter.config.spi.v1.PropertiesSubset;
import org.w3c.dom.Element;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.apache.logging.converter.config.internal.ComponentUtils;
import org.apache.logging.converter.config.internal.v1.AbstractComponentParser;
import org.apache.logging.converter.config.spi.ConfigurationNode;
import org.apache.logging.converter.config.spi.PropertiesSubset;
import org.apache.logging.converter.config.spi.v1.Log4j1ComponentParser;
import org.apache.logging.converter.config.spi.v1.PropertiesSubset;
import org.w3c.dom.Element;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.apache.logging.converter.config.internal.ComponentUtils;
import org.apache.logging.converter.config.internal.v1.AbstractComponentParser;
import org.apache.logging.converter.config.spi.ConfigurationNode;
import org.apache.logging.converter.config.spi.PropertiesSubset;
import org.apache.logging.converter.config.spi.v1.Log4j1ComponentParser;
import org.apache.logging.converter.config.spi.v1.PropertiesSubset;
import org.w3c.dom.Element;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.apache.logging.converter.config.internal.StringUtils;
import org.apache.logging.converter.config.internal.v1.AbstractComponentParser;
import org.apache.logging.converter.config.spi.ConfigurationNode;
import org.apache.logging.converter.config.spi.PropertiesSubset;
import org.apache.logging.converter.config.spi.v1.Log4j1ComponentParser;
import org.apache.logging.converter.config.spi.v1.PropertiesSubset;
import org.jspecify.annotations.Nullable;
import org.w3c.dom.Element;

Expand Down
Loading

0 comments on commit b018a8e

Please sign in to comment.