Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
Expand Down Expand Up @@ -107,12 +108,41 @@ public class MCRConfiguration2 {
.softValues()
.build();

private static final AtomicReference<Map<String, String>> PROPERTIES = new AtomicReference<>(null);

static Map<SingletonKey, Object> instanceHolder = new MCRConcurrentHashMap<>();

/**
* @deprecated Use {@link #getRawProperties()} instead.
*/
@Deprecated
public static Map<String, String> getPropertiesMap() {
return getRawProperties();
}

/**
* Returns all configuration properties, including properties with empty values, i.e.
* properties that are not recognized by, for example, {@link #getString(String)}.
*/
public static Map<String, String> getRawProperties() {
return Collections.unmodifiableMap(MCRConfigurationBase.getResolvedProperties().getAsMap());
}

/**
* Returns all configuration properties, excluding properties with empty values, i.e.
* only properties that are recognized by, for example, {@link #getString(String)}.
*/
public static Map<String, String> getAllProperties() {
return PROPERTIES.updateAndGet(existingValue -> {
if (existingValue != null) {
return existingValue;
}
return getRawProperties().entrySet().stream()
.filter(entry -> !entry.getValue().isBlank())
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().trim()));
});
}

/**
* Returns a sub map of properties where key is transformed.
*
Expand All @@ -135,7 +165,7 @@ public static Map<String, String> getPropertiesMap() {
* @return a map of the properties as stated above
*/
public static Map<String, String> getSubPropertiesMap(String propertyPrefix) {
return getPropertiesMap()
return getRawProperties()
.entrySet()
.stream()
.filter(e -> e.getKey().startsWith(propertyPrefix))
Expand Down Expand Up @@ -226,7 +256,7 @@ private static <S> MCRInstanceConfiguration<S> getConfiguration(Class<S> superCl
try {
return ((MCRInstanceConfiguration<S>) CONFIGURATIONS.get(
new ConfigurationKey(superClass.getName(), name),
() -> MCRInstanceConfiguration.ofName(superClass, name, Options.IMPLICIT))).copy();
() -> MCRInstanceConfiguration.ofName(superClass, name, Options.IMPLICIT)));
} catch (ExecutionException | UncheckedExecutionException e) {
Throwables.throwIfUnchecked(e.getCause());
throw new MCRException("Failed to create instance configuration for " + name, e.getCause());
Expand Down Expand Up @@ -494,6 +524,7 @@ private static <T> Class<? extends T> getClassObject(String classname) {
}

static void clearCaches() {
PROPERTIES.set(null);
CONFIGURATIONS.invalidateAll();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private JDOMSource resolveKey(String key) {
final Element entryElement = new Element("entry");

entryElement.setAttribute("key", key);
String value = MCRConfiguration2.getPropertiesMap().get(key);
String value = MCRConfiguration2.getRawProperties().get(key);
if (value != null) {
entryElement.setText(value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* This file is part of *** M y C o R e ***
* See https://www.mycore.de/ for details.
*
* MyCoRe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MyCoRe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MyCoRe. If not, see <http://www.gnu.org/licenses/>.
*/

package org.mycore.common.config.annotation;

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 value of type
* {@link Class} from the configuration properties.
* <p>
* The field or method needs to be public.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD })
@Inherited
public @interface MCRClassProperty {

/**
* @return The name of property containing the string value.
*/
String name();

/**
* @return The class or a superclass of the configured class.
*/
Class<?> valueClass() default Object.class;

/**
* @return true if the property specified by {@link MCRClassProperty#name()} has to be present in the properties.
* {@link MCRConfigurationException} is thrown if the property is required but not present.
*/
boolean required() default true;

/**
* @return true if the property name specified by {@link MCRClassProperty#name()} is absolute and not specific for
* this instance e.g. <code>MCR.Foo.Bar</code>.
*/
boolean absolute() default false;

/**
* @return The name for a default property that should be used as a fallback, if no value is configured.
* A {@link MCRConfigurationException} is thrown if the default property is not configured.
* The default property must be absolute, e.g. <code>MCR.Foo.Bar</code>.
*/
String defaultName() default "";

/**
* @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.
*/
int order() default 0;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* This file is part of *** M y C o R e ***
* See https://www.mycore.de/ for details.
*
* MyCoRe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MyCoRe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MyCoRe. If not, see <http://www.gnu.org/licenses/>.
*/

package org.mycore.common.config.annotation;

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 list of {@link Class} values from the configuration properties.
* <p>
* The field or method needs to be public.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD })
@Inherited
public @interface MCRClassPropertyList {

/**
* @return The prefix for names of properties containing the list values.
*/
String name() default "";

/**
* @return The class or a superclass of the configured class.
*/
Class<?> valueClass() default Object.class;

/**
* @return true if the at least one sub-property of the property specified by {@link MCRClassPropertyList#name()}
* has to be present in the properties. {@link MCRConfigurationException} is thrown if a value is required
* but not present.
*/
boolean required() default true;

/**
* @return true if the property name specified by {@link MCRClassPropertyList#name()} is absolute and not specific
* for this instance e.g. <code>MCR.Foo.Bar</code>.
*/
boolean absolute() default false;

/**
* @return The name for a default property that should be used as a fallback, if no list values are configured.
* A {@link MCRConfigurationException} is thrown if no default property is not configured.
* The default property must be absolute, e.g. <code>MCR.Foo.Bar</code>.
*/
String defaultName() default "";

/**
* @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.
*/
int order() default 0;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* This file is part of *** M y C o R e ***
* See https://www.mycore.de/ for details.
*
* MyCoRe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MyCoRe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MyCoRe. If not, see <http://www.gnu.org/licenses/>.
*/

package org.mycore.common.config.annotation;

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 {@link Class} values from the configuration properties.
* <p>
* The field or method needs to be public.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD })
@Inherited
public @interface MCRClassPropertyMap {

/**
* @return The prefix for names of properties containing the map values.
*/
String name() default "";

/**
* @return The class or a superclass of the configured class.
*/
Class<?> valueClass() default Object.class;

/**
* @return true if the at least one sub-property of the property specified by {@link MCRClassPropertyMap#name()}
* has to be present in the properties. {@link MCRConfigurationException} is thrown if a value is required
* but not present.
*/
boolean required() default true;

/**
* @return true if the property name specified by {@link MCRClassPropertyMap#name()} is absolute and not specific
* for this instance e.g. <code>MCR.Foo.Bar</code>.
*/
boolean absolute() default false;

/**
* @return The name for a default property that should be used as a fallback, if no map values are configured.
* A {@link MCRConfigurationException} is thrown if no default property is not configured.
* The default property must be absolute, e.g. <code>MCR.Foo.Bar</code>.
*/
String defaultName() default "";

/**
* @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.
*/
int order() default 0;

}
Loading
Loading