Skip to content

Commit

Permalink
Extracted constant values, more clean ups
Browse files Browse the repository at this point in the history
  • Loading branch information
reinhapa committed Jul 1, 2024
1 parent 2256e20 commit b9d09b4
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions exist-core/src/main/java/org/exist/util/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -241,6 +240,7 @@ public class Configuration implements ErrorHandler {
private static final String XQUERY_BUILTIN_MODULES_CONFIGURATION_MODULE_ELEMENT_NAME = "module";
private static final String CANNOT_CONVERT_VALUE_TO_INTEGER = "Cannot convert {} value to integer: {}";
private static final String ORG_EXIST_STORAGE_STARTUP_TRIGGER = "org.exist.storage.StartupTrigger";
private static final String ERROR_READING_CONFIGURATION_FILE_LINE = "Error occurred while reading configuration file [line: {}]:{}";

private final Map<String, Object> config = new HashMap<>(); //Configuration

Expand Down Expand Up @@ -425,13 +425,15 @@ public static boolean parseBoolean(@Nullable final String value, final boolean d
return booleanValue.booleanValue();
}

@Nullable
private static Boolean asBoolean(@Nullable final String value) {
if (value != null) {
return Boolean.valueOf("yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value));
}
return null;
}

@Nullable
private static Integer asInteger(@Nullable final String value) {
if (value != null) {
try {
Expand All @@ -443,6 +445,7 @@ private static Integer asInteger(@Nullable final String value) {
return null;
}

@Nullable
private static Long asLong(@Nullable final String value) {
if (value != null) {
try {
Expand Down Expand Up @@ -1079,11 +1082,8 @@ private void configureStartup(final Element startup) throws DatabaseConfiguratio
}

// Initialize trigger configuration
List<StartupTriggerConfig> startupTriggers = (List<StartupTriggerConfig>) config.get(PROPERTY_STARTUP_TRIGGERS);
if (startupTriggers == null) {
startupTriggers = new ArrayList<>();
setProperty(PROPERTY_STARTUP_TRIGGERS, startupTriggers);
}
var startupTriggers = (List<StartupTriggerConfig>) config
.computeIfAbsent(PROPERTY_STARTUP_TRIGGERS, key -> new ArrayList<StartupTriggerConfig>());

// Iterate over <trigger> elements
for (int i = 0; i < nlTrigger.getLength(); i++) {
Expand All @@ -1094,15 +1094,9 @@ private void configureStartup(final Element startup) throws DatabaseConfiguratio
// Get @class
final String startupTriggerClass = trigger.getAttribute("class");

boolean isStartupTrigger = false;
try {
// Verify if class is StartupTrigger
for (final Class<?> iface : Class.forName(startupTriggerClass).getInterfaces()) {
if (ORG_EXIST_STORAGE_STARTUP_TRIGGER.equals(iface.getName())) {
isStartupTrigger = true;
break;
}
}
final boolean isStartupTrigger = isStartupTrigger(startupTriggerClass);

// if it actually is a StartupTrigger
if (isStartupTrigger) {
Expand All @@ -1126,6 +1120,15 @@ private void configureStartup(final Element startup) throws DatabaseConfiguratio
});
}

private static boolean isStartupTrigger(String startupTriggerClass) throws ClassNotFoundException {
for (final Class<?> iface : Class.forName(startupTriggerClass).getInterfaces()) {
if (ORG_EXIST_STORAGE_STARTUP_TRIGGER.equals(iface.getName())) {
return true;
}
}
return false;
}

private void configurePool(final Element pool) {
configureProperty(pool, MIN_CONNECTIONS_ATTRIBUTE, PROPERTY_MIN_CONNECTIONS, Configuration::asInteger, null);
configureProperty(pool, MAX_CONNECTIONS_ATTRIBUTE, PROPERTY_MAX_CONNECTIONS, Configuration::asInteger, null);
Expand Down Expand Up @@ -1328,11 +1331,11 @@ private void configureProperty(final Element element, final String attributeName
*/
private String getAttributeSystemPropertyName(Element element, String attributeName) {
final StringBuilder property = new StringBuilder(attributeName);
Node parent = element.getParentNode();

property.insert(0, ".");
property.insert(0, element.getLocalName());

Node parent = element.getParentNode();
while (parent instanceof Element) {
final String parentName = parent.getLocalName();

Expand Down Expand Up @@ -1382,7 +1385,7 @@ public int getInteger(final String name) {

public int getInteger(final String name, final int defaultValue) {
return Optional.ofNullable(getProperty(name))
.filter(v -> v instanceof Integer)
.filter(Integer.class::isInstance)
.map(v -> (int) v)
.orElse(defaultValue);
}
Expand All @@ -1396,8 +1399,7 @@ public int getInteger(final String name, final int defaultValue) {
*/
@Override
public void error(SAXParseException exception) throws SAXException {
LOG.error("error occurred while reading configuration file [line: {}]:{}",
exception.getLineNumber(), exception.getMessage(), exception);
LOG.error(ERROR_READING_CONFIGURATION_FILE_LINE, exception.getLineNumber(), exception.getMessage(), exception);
}

/**
Expand All @@ -1409,8 +1411,7 @@ public void error(SAXParseException exception) throws SAXException {
*/
@Override
public void fatalError(SAXParseException exception) throws SAXException {
LOG.error("error occurred while reading configuration file [line: {}]:{}",
exception.getLineNumber(), exception.getMessage(), exception);
LOG.error(ERROR_READING_CONFIGURATION_FILE_LINE, exception.getLineNumber(), exception.getMessage(), exception);
}

/**
Expand All @@ -1422,8 +1423,7 @@ public void fatalError(SAXParseException exception) throws SAXException {
*/
@Override
public void warning(SAXParseException exception) throws SAXException {
LOG.error("error occurred while reading configuration file [line: {}]:{}",
exception.getLineNumber(), exception.getMessage(), exception);
LOG.error(ERROR_READING_CONFIGURATION_FILE_LINE, exception.getLineNumber(), exception.getMessage(), exception);
}

public record StartupTriggerConfig(String clazz, Map<String, List<? extends Object>> params) {
Expand Down

0 comments on commit b9d09b4

Please sign in to comment.