Skip to content
Draft
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 @@ -34,7 +34,7 @@
import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor;
import org.hibernate.jpa.boot.spi.PersistenceXmlParser;
import org.mycore.common.config.MCRConfiguration2;
import org.mycore.common.config.MCRInstanceName;
import org.mycore.common.config.instantiator.MCRInstanceName;
import org.mycore.common.events.MCRStartupHandler.AutoExecutable;
import org.mycore.resource.MCRResourceResolver;
import org.mycore.resource.provider.MCRResourceProvider;
Expand Down
12 changes: 5 additions & 7 deletions mycore-base/src/main/java/org/mycore/common/MCRClassTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,18 @@ public static Object loadClassFromURL(File file, String className)

/**
* Loads a class via default ClassLoader or <code>Thread.currentThread().getContextClassLoader()</code>.
* @param classname Name of class
* @param className Name of class
* @param <T> Type of Class
* @return the initialized class
* @throws ClassNotFoundException if both ClassLoader cannot load the Class
*/
public static <T> Class<? extends T> forName(String classname) throws ClassNotFoundException {
@SuppressWarnings("unchecked")
Class<? extends T> forName;
@SuppressWarnings("unchecked")
public static <T> Class<? extends T> forName(String className) throws ClassNotFoundException {
try {
forName = (Class<? extends T>) Class.forName(classname);
return (Class<? extends T>) Class.forName(className);
} catch (ClassNotFoundException cnfe) {
forName = (Class<? extends T>) Class.forName(classname, true, extendedClassLoader);
return (Class<? extends T>) Class.forName(className, true, extendedClassLoader);
}
return forName;
}

/**
Expand Down

This file was deleted.

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

package org.mycore.common.config;

import static org.mycore.common.config.instantiator.MCRInstanceConfiguration.ofName;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand All @@ -32,10 +35,15 @@
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.config.instantiator.MCRInstanceConfiguration;
import org.mycore.common.config.instantiator.MCRInstanceConfiguration.Option;
import org.mycore.common.config.instantiator.MCRInstanceConfiguration.Options;
import org.mycore.common.config.instantiator.MCRInstanceName;
import org.mycore.common.config.instantiator.MCRInstanceName.Suffix;
import org.mycore.common.function.MCRTriConsumer;

import jakarta.inject.Singleton;

/**
* Provides methods to manage and read all configuration properties from the MyCoRe configuration files.
* The Properties used by this class are used from {@link MCRConfigurationBase}.
Expand Down Expand Up @@ -83,7 +91,7 @@
* @since 2018.05
*/
// because of intrusive test case org.mycore.common.config.MCRConfigurationTest.testSingletonMapGet
@SuppressWarnings("PMD.MutableStaticState")
@SuppressWarnings("PMD.MutableStaticState")
public class MCRConfiguration2 {

private static final Map<UUID, EventListener> LISTENERS = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -136,15 +144,6 @@ public static <S> Optional<S> getInstanceOf(Class<S> superClass, String name) th
return getInstanceOf(superClass, name, Options.NONE);
}

private static <S> Optional<S> getInstanceOf(Class<S> superClass, String name,
Set<Option> options) {
if (MCRConfigurableInstanceHelper.isSingleton(superClass)) {
return getSingleInstanceOf(superClass, name, options);
} else {
return MCRConfigurableInstanceHelper.getInstance(superClass, name, options);
}
}

/**
* Returns a new instance of the class specified in the configuration property with the given name.
*
Expand Down Expand Up @@ -172,14 +171,6 @@ public static <S> Optional<S> getSingleInstanceOf(Class<S> superClass, String na
return getSingleInstanceOf(superClass, name, Options.NONE);
}

private static <S> Optional<S> getSingleInstanceOf(Class<S> superClass, String name,
Set<Option> options) {
return getString(name)
.map(className -> new ConfigSingletonKey(name, className))
.map(key -> (S) instanceHolder.computeIfAbsent(key,
k -> MCRConfigurableInstanceHelper.getInstance(superClass, name, options).orElse(null)));
}

/**
* Returns an instance of the class specified in the configuration property with the given name. If the class was
* previously instantiated by this method, that instance is returned.
Expand All @@ -195,13 +186,47 @@ public static <S> S getSingleInstanceOfOrThrow(Class<S> superClass, String name)
.orElseThrow(() -> createConfigurationException(name));
}

private static <S> Optional<S> getInstanceOf(Class<S> superClass, String name, Set<Option> options) {
if (isSingleton(superClass)) {
return getSingleInstanceOf(superClass, name, options);
}
return getInstantiableConfiguration(superClass, name, options).map(MCRInstanceConfiguration::instantiate);

}

@SuppressWarnings("unchecked")
private static <S> Optional<S> getSingleInstanceOf(Class<S> superClass, String name, Set<Option> options) {
return getInstantiableConfiguration(superClass, name, options).map(
configuration -> {
SingletonKey key = new ConfigSingletonKey(name, configuration.valueClass().getName());
return (S) instanceHolder.computeIfAbsent(key, _ -> configuration.instantiate());
});
}

private static <S> Optional<MCRInstanceConfiguration<S>> getInstantiableConfiguration(Class<S> superClass,
String name, Set<Option> options) {
MCRInstanceConfiguration<S> configuration = ofName(superClass, name, getPropertiesMap(), options);
return configuration.instantiatable() ? Optional.of(configuration) : Optional.empty();

}

/**
* Checks if a class is annotated with {@link Singleton}.
*
* @param targetClass the class
* @return true if the class in the property is annotated with {@link Singleton}
*/
public static boolean isSingleton(Class<?> targetClass) {
return targetClass.getDeclaredAnnotation(Singleton.class) != null;
}

/**
* Loads a Java Class defined in property <code>name</code>.
* @param name Name of the property
* @param <T> Supertype of class defined in <code>name</code>
* @return Optional of Class asignable to <code>&lt;T&gt;</code>
* @return Optional of Class assignable to <code>&lt;T&gt;</code>
* @throws MCRConfigurationException
* if the the class can not be loaded or instantiated
* if the class can not be loaded or instantiated
*/
public static <T> Optional<Class<? extends T>> getClass(String name) throws MCRConfigurationException {
return getString(name).map(MCRConfiguration2::<T>getClassObject);
Expand Down Expand Up @@ -427,7 +452,8 @@ public static boolean removePropertyChangeEventListener(UUID uuid) {
}

public static <S> S instantiateClass(Class<S> superClass, String className) {
return MCRConfigurableInstanceHelper.getInstance(superClass, MCRInstanceConfiguration.ofClass(className));
return MCRInstanceConfiguration.ofClassName(superClass, className, "MCR.AnonymousInstance." + className,
Suffix.UPPER_CASE, new HashMap<>(), getPropertiesMap()).instantiate();
}

public static <S> Stream<S> instantiateClasses(Class<S> superClass, String propertyName) {
Expand All @@ -447,11 +473,11 @@ private static <T> Class<? extends T> getClassObject(String classname) {

private static class EventListener {

private Predicate<String> keyPredicate;
private final Predicate<String> keyPredicate;

private MCRTriConsumer<String, Optional<String>, Optional<String>> listener;
private final MCRTriConsumer<String, Optional<String>, Optional<String>> listener;

private UUID uuid;
private final UUID uuid;

EventListener(Predicate<String> keyPredicate,
MCRTriConsumer<String, Optional<String>, Optional<String>> listener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,6 @@ public static synchronized void initialize(Map<String, String> deprecated, Map<S
deprecatedProperties.putAll(deprecated);
checkForDeprecatedProperties(props);

// instance helper cache
if (clear) {
MCRConfigurableInstanceHelper.clearCache();
}

// base properties
if (clear) {
getBaseProperties().clear();
Expand Down
Loading
Loading