Skip to content
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.stream.Stream;

import org.mycore.common.MCRClassTools;
import org.mycore.common.config.MCRInstanceConfiguration.Option;
import org.mycore.common.config.MCRInstanceConfiguration.Options;
import org.mycore.common.function.MCRTriConsumer;

/**
Expand Down Expand Up @@ -131,11 +133,11 @@ public static Map<String, String> getSubPropertiesMap(String propertyPrefix) {
* @throws MCRConfigurationException if the class can not be loaded or instantiated
*/
public static <S> Optional<S> getInstanceOf(Class<S> superClass, String name) throws MCRConfigurationException {
return getInstanceOf(superClass, name, MCRConfigurableInstanceHelper.NO_OPTIONS);
return getInstanceOf(superClass, name, Options.NONE);
}

private static <S> Optional<S> getInstanceOf(Class<S> superClass, String name,
Set<MCRConfigurableInstanceHelper.Option> options) {
Set<Option> options) {
if (MCRConfigurableInstanceHelper.isSingleton(superClass)) {
return getSingleInstanceOf(superClass, name, options);
} else {
Expand All @@ -153,7 +155,7 @@ private static <S> Optional<S> getInstanceOf(Class<S> superClass, String name,
* or the configuration property is not set
*/
public static <S> S getInstanceOfOrThrow(Class<S> superClass, String name) throws MCRConfigurationException {
return getInstanceOf(superClass, name, MCRConfigurableInstanceHelper.ADD_IMPLICIT_CLASS_PROPERTIES)
return getInstanceOf(superClass, name, Options.IMPLICIT)
.orElseThrow(() -> createConfigurationException(name));
}

Expand All @@ -167,11 +169,11 @@ public static <S> S getInstanceOfOrThrow(Class<S> superClass, String name) throw
* @throws MCRConfigurationException if the class can not be loaded or instantiated
*/
public static <S> Optional<S> getSingleInstanceOf(Class<S> superClass, String name) {
return getSingleInstanceOf(superClass, name, MCRConfigurableInstanceHelper.NO_OPTIONS);
return getSingleInstanceOf(superClass, name, Options.NONE);
}

private static <S> Optional<S> getSingleInstanceOf(Class<S> superClass, String name,
Set<MCRConfigurableInstanceHelper.Option> options) {
Set<Option> options) {
return getString(name)
.map(className -> new ConfigSingletonKey(name, className))
.map(key -> (S) instanceHolder.computeIfAbsent(key,
Expand All @@ -189,7 +191,7 @@ private static <S> Optional<S> getSingleInstanceOf(Class<S> superClass, String n
* or the configuration property is not set
*/
public static <S> S getSingleInstanceOfOrThrow(Class<S> superClass, String name) {
return getSingleInstanceOf(superClass, name, MCRConfigurableInstanceHelper.ADD_IMPLICIT_CLASS_PROPERTIES)
return getSingleInstanceOf(superClass, name, Options.IMPLICIT)
.orElseThrow(() -> createConfigurationException(name));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

package org.mycore.common.config;

import org.mycore.common.MCRException;

import java.io.Serial;

import org.mycore.common.MCRException;

/**
* Instances of this class represent an exception thrown because of an error in the MyCoRe configuration. Normally this
* will be the case when a configuration property that is required is not set or has an illegal value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Represents an extract of properties (typically {@link MCRConfiguration2#getPropertiesMap()}) used to
Expand Down Expand Up @@ -389,6 +390,9 @@ public Map<String, MCRInstanceConfiguration> nestedConfigurationMap() {
* @return the nested configuration map
*/
public Map<String, MCRInstanceConfiguration> nestedConfigurationMap(String commonPrefix) {
if (commonPrefix.isEmpty()) {
return nestedConfigurationMap();
}
String commonSuffixWithDelimiter = commonPrefix + ".";
Map<String, MCRInstanceConfiguration> nestedConfigurationMap = new HashMap<>();
for (Map.Entry<String, String> entry : properties().entrySet()) {
Expand Down Expand Up @@ -444,4 +448,28 @@ public String toString() {
"#fullProperties=" + fullProperties.size() + "}";
}

public enum Option {

/**
* If a class name is required to be in the configuration properties (for example, because of usage of
* {@link MCRConfiguration2#getInstanceOfOrThrow(Class, String)} or because of an annotation with
* <code>required=true</code>) and the expected super class is a final class (meaning, if the property
* containing the class name is required to exist and can only have the class name of that final class),
* assume that that property exists (if it doesn't).
*/
IMPLICIT

}

public static final class Options {

public static final Set<Option> NONE = Set.of();

public static final Set<Option> IMPLICIT = Set.of(Option.IMPLICIT);

private Options() {
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
* @return The {@link MCRSentinel} for the configured property.
*/
MCRSentinel sentinel() default @MCRSentinel(enabled = false);

/**
* @return The order in which the annotated fields or methods are processed. The higher the value, the later the
* field or method is processed. All fields are processed first, then all methods are processed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
* The default property must be absolute, e.g. <code>MCR.Foo.Bar</code>.
*/
String defaultName() default "";

/**
* @return The {@link MCRSentinel} for the configured properties.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@

package org.mycore.common.config.annotation;

import org.mycore.common.config.MCRConfigurationException;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.mycore.common.config.MCRConfigurationException;

/**
* This annotation is used to mark fields or methods that should be set to or called with
* a map of string values from the configuration properties.
Expand Down
Loading
Loading