Skip to content
Open
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 @@ -19,10 +19,11 @@
package org.mycore.common.config;

import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.Set;

/**
* Represents an extract of properties (typically {@link MCRConfiguration2#getPropertiesMap()}) used to
Expand Down Expand Up @@ -390,6 +391,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 All @@ -407,134 +411,6 @@ public Map<String, MCRInstanceConfiguration> nestedConfigurationMap(String commo
return nestedConfigurationMap;
}

/**
* Returns a {@link List} of configurations for nested instances, ordered by the first name segment
* (which all must be integer values).
* <p>
* Example: Given an {@link MCRInstanceConfiguration}
* representing the {@link MCRInstanceName} <code>Some.Instance.Name</code>, properties
* <ul>
* <li><code>1=some.nested.ClassName1</code></li>
* <li><code>1.Key1=ValueA1</code></li>
* <li><code>1.Key2=ValueA2</code></li>
* <li><code>3=some.nested.ClassName3</code></li>
* <li><code>3.Key1=ValueB1</code></li>
* <li><code>3.Key2=ValueB2</code></li>
* </ul>
* this will return a list containing
* <ol>
* <li>
* at index <code>0</code> an {@link MCRInstanceConfiguration}
* representing the {@link MCRInstanceConfiguration#name()} <code>Some.Instance.Name.Foo.1</code>,
* {@link MCRInstanceConfiguration#className()} <code>some.nested.ClassName1</code> and
* {@link MCRInstanceConfiguration#properties()}
* <ul>
* <li><code>Key1=ValueA1</code></li>
* <li><code>Key2=ValueA2</code></li>
* </ul>
* and {@link MCRInstanceConfiguration#fullProperties()} that are equal to the the full properties of this
* configuration (i.e. the full properties used to create the top level configuration).
* </li>
* <li>
* at index <code>1</code> an {@link MCRInstanceConfiguration}
* representing the {@link MCRInstanceConfiguration#name()} <code>Some.Instance.Name.Foo.3</code>,
* {@link MCRInstanceConfiguration#className()} <code>some.nested.ClassName3</code> and
* {@link MCRInstanceConfiguration#properties()}
* <ul>
* <li><code>Key1=ValueB1</code></li>
* <li><code>Key2=ValueB2</code></li>
* </ul>
* and {@link MCRInstanceConfiguration#fullProperties()} that are equal to the the full properties of this
* configuration (i.e. the full properties used to create the top level configuration).
* </li>
* </ol>
* <p>
* If an {@link MCRInstanceName} with suffix <code>.Class</code> or <code>.class</code> would have been used in
* the top level {@link MCRInstanceConfiguration}, the same suffix is used for nested configurations.
* <p>
* Example: If a property with suffix <code>.Class</code> would have been used to convey the original class name
* in the top level configuration, properties <code>Foo.1.Class</code>/<code>Foo.3.Class</code> would be used to
* convey the class name for the nested configurations and properties
* <code>Foo.1.class</code>/<code>Foo.3.class</code> and <code>Foo.1</code>/<code>Foo.3</code> would be ignored.
* The resulting {@link MCRInstanceConfiguration#properties()} would not contain entries with keys
* <code>class</code> or the empty key, respectively.
*
* @return the nested configuration list
*/
public List<MCRInstanceConfiguration> nestedConfigurationList() {
return mapToList(nestedConfigurationMap());
}

/**
* Returns a {@link List} of configurations for nested instances with a common prefix, ordered by the
* name segment following that common prefix (which all must be integer values).
* <p>
* Example: Given an {@link MCRInstanceConfiguration}
* representing the {@link MCRInstanceName} <code>Some.Instance.Name</code>, properties
* <ul>
* <li><code>Foo.1=some.nested.ClassName1</code></li>
* <li><code>Foo.1.Key1=ValueA1</code></li>
* <li><code>Foo.1.Key2=ValueA2</code></li>
* <li><code>Foo.3=some.nested.ClassName3</code></li>
* <li><code>Foo.3.Key1=ValueB1</code></li>
* <li><code>Foo.3.Key2=ValueB2</code></li>
* <li><code>Bar=UnrelatedValue</code></li>
* </ul>
* and a <em>commonPrefix</em> of <code>Foo</code>, this will return a list containing
* <ol>
* <li>
* at index <code>0</code> an {@link MCRInstanceConfiguration}
* representing the {@link MCRInstanceConfiguration#name()} <code>Some.Instance.Name.Foo.1</code>,
* {@link MCRInstanceConfiguration#className()} <code>some.nested.ClassName1</code> and
* {@link MCRInstanceConfiguration#properties()}
* <ul>
* <li><code>Key1=ValueA1</code></li>
* <li><code>Key2=ValueA2</code></li>
* </ul>
* and {@link MCRInstanceConfiguration#fullProperties()} that are equal to the the full properties of this
* configuration (i.e. the full properties used to create the top level configuration).
* </li>
* <li>
* at index <code>1</code> an {@link MCRInstanceConfiguration}
* representing the {@link MCRInstanceConfiguration#name()} <code>Some.Instance.Name.Foo.3</code>,
* {@link MCRInstanceConfiguration#className()} <code>some.nested.ClassName3</code> and
* {@link MCRInstanceConfiguration#properties()}
* <ul>
* <li><code>Key1=ValueB1</code></li>
* <li><code>Key2=ValueB2</code></li>
* </ul>
* and {@link MCRInstanceConfiguration#fullProperties()} that are equal to the the full properties of this
* configuration (i.e. the full properties used to create the top level configuration).
* </li>
* </ol>
* <p>
* If an {@link MCRInstanceName} with suffix <code>.Class</code> or <code>.class</code> would have been used in
* the top level {@link MCRInstanceConfiguration}, the same suffix is used for nested configurations.
* <p>
* Example: If a property with suffix <code>.Class</code> would have been used to convey the original class name
* in the top level configuration, properties <code>Foo.1.Class</code>/<code>Foo.3.Class</code> would be used to
* convey the class name for the nested configurations and properties
* <code>Foo.1.class</code>/<code>Foo.3.class</code> and <code>Foo.1</code>/<code>Foo.3</code> would be ignored.
* The resulting {@link MCRInstanceConfiguration#properties()} would not contain entries with keys
* <code>class</code> or the empty key, respectively.
*
* @param commonPrefix the common prefix
* @return the nested configuration list
*/
public List<MCRInstanceConfiguration> nestedConfigurationList(String commonPrefix) {
return mapToList(nestedConfigurationMap(commonPrefix));
}

private List<MCRInstanceConfiguration> mapToList(Map<String, MCRInstanceConfiguration> map) {
return map
.entrySet()
.stream()
.map(entry -> Map.entry(Integer.parseInt(entry.getKey()), entry.getValue()))
.sorted(Map.Entry.comparingByKey())
.map(Map.Entry::getValue)
.collect(Collectors.toList());
}

/**
* Returns the configuration with a fixed class name.
* <p>
Expand All @@ -560,7 +436,7 @@ private List<MCRInstanceConfiguration> mapToList(Map<String, MCRInstanceConfigur
* @param fixedClass the class to be used as a class name
* @return the modified configuration
*/
public <S> MCRInstanceConfiguration fixedClass(Class<S> fixedClass) {
public <S> MCRInstanceConfiguration withClass(Class<S> fixedClass) {
return new MCRInstanceConfiguration(name, fixedClass.getName(), properties, fullProperties);
}

Expand All @@ -573,4 +449,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 = EnumSet.noneOf(Option.class);

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

private Options() {
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
*/
String defaultName() default "";

/**
* @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 @@ -63,7 +63,7 @@
String defaultName() default "";

/**
* @return The {@link MCRSentinel} for the configured instances.
* @return The {@link MCRSentinel} for the configured properties.
*/
MCRSentinel sentinel() default @MCRSentinel(enabled = false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
String defaultName() default "";

/**
* @return The {@link MCRSentinel} for the configured instances.
* @return The {@link MCRSentinel} for the configured properties.
*/
MCRSentinel sentinel() default @MCRSentinel(enabled = false);

Expand Down
Loading
Loading