1818
1919package org .mycore .common .config ;
2020
21+ import static org .mycore .common .config .instantiator .MCRInstanceConfiguration .IMPLICIT_OPTION ;
22+ import static org .mycore .common .config .instantiator .MCRInstanceConfiguration .NO_OPTIONS ;
23+ import static org .mycore .common .config .instantiator .MCRInstanceConfiguration .ofName ;
24+
2125import java .util .Collections ;
26+ import java .util .HashMap ;
2227import java .util .Map ;
2328import java .util .Optional ;
2429import java .util .Set ;
3237import java .util .stream .Stream ;
3338
3439import org .mycore .common .MCRClassTools ;
35- import org .mycore .common .config .instantiator .MCRInstantiator ;
40+ import org .mycore .common .config .instantiator .MCRInstanceConfiguration ;
41+ import org .mycore .common .config .instantiator .MCRInstanceConfiguration .Option ;
42+ import org .mycore .common .config .instantiator .MCRInstanceName ;
43+ import org .mycore .common .config .instantiator .MCRInstanceName .Suffix ;
3644import org .mycore .common .function .MCRTriConsumer ;
3745
46+ import jakarta .inject .Singleton ;
47+
3848/**
3949 * Provides methods to manage and read all configuration properties from the MyCoRe configuration files.
4050 * The Properties used by this class are used from {@link MCRConfigurationBase}.
8292 * @since 2018.05
8393 */
8494// because of intrusive test case org.mycore.common.config.MCRConfigurationTest.testSingletonMapGet
85- @ SuppressWarnings ("PMD.MutableStaticState" )
95+ @ SuppressWarnings ("PMD.MutableStaticState" )
8696public class MCRConfiguration2 {
8797
8898 private static final Map <UUID , EventListener > LISTENERS = new ConcurrentHashMap <>();
@@ -132,16 +142,7 @@ public static Map<String, String> getSubPropertiesMap(String propertyPrefix) {
132142 * @throws MCRConfigurationException if the class can not be loaded or instantiated
133143 */
134144 public static <S > Optional <S > getInstanceOf (Class <S > superClass , String name ) throws MCRConfigurationException {
135- return getInstanceOf (superClass , name , MCRInstantiator .NO_OPTIONS );
136- }
137-
138- private static <S > Optional <S > getInstanceOf (Class <S > superClass , String name ,
139- Set <MCRInstantiator .Option > options ) {
140- if (MCRConfigurableInstanceHelper .isSingleton (superClass )) {
141- return getSingleInstanceOf (superClass , name , options );
142- } else {
143- return MCRConfigurableInstanceHelper .getInstance (superClass , name , options );
144- }
145+ return getInstanceOf (superClass , name , NO_OPTIONS );
145146 }
146147
147148 /**
@@ -154,8 +155,7 @@ private static <S> Optional<S> getInstanceOf(Class<S> superClass, String name,
154155 * or the configuration property is not set
155156 */
156157 public static <S > S getInstanceOfOrThrow (Class <S > superClass , String name ) throws MCRConfigurationException {
157- return getInstanceOf (superClass , name , MCRInstantiator .ADD_IMPLICIT_CLASS_PROPERTIES_OPTION )
158- .orElseThrow (() -> createConfigurationException (name ));
158+ return getInstanceOf (superClass , name , IMPLICIT_OPTION ).orElseThrow (() -> createConfigurationException (name ));
159159 }
160160
161161 /**
@@ -168,15 +168,7 @@ public static <S> S getInstanceOfOrThrow(Class<S> superClass, String name) throw
168168 * @throws MCRConfigurationException if the class can not be loaded or instantiated
169169 */
170170 public static <S > Optional <S > getSingleInstanceOf (Class <S > superClass , String name ) {
171- return getSingleInstanceOf (superClass , name , MCRInstantiator .NO_OPTIONS );
172- }
173-
174- private static <S > Optional <S > getSingleInstanceOf (Class <S > superClass , String name ,
175- Set <MCRInstantiator .Option > options ) {
176- return getString (name )
177- .map (className -> new ConfigSingletonKey (name , className ))
178- .map (key -> (S ) instanceHolder .computeIfAbsent (key ,
179- k -> MCRConfigurableInstanceHelper .getInstance (superClass , name , options ).orElse (null )));
171+ return getSingleInstanceOf (superClass , name , NO_OPTIONS );
180172 }
181173
182174 /**
@@ -190,17 +182,51 @@ private static <S> Optional<S> getSingleInstanceOf(Class<S> superClass, String n
190182 * or the configuration property is not set
191183 */
192184 public static <S > S getSingleInstanceOfOrThrow (Class <S > superClass , String name ) {
193- return getSingleInstanceOf (superClass , name , MCRInstantiator . ADD_IMPLICIT_CLASS_PROPERTIES_OPTION )
185+ return getSingleInstanceOf (superClass , name , IMPLICIT_OPTION )
194186 .orElseThrow (() -> createConfigurationException (name ));
195187 }
196188
189+ private static <S > Optional <S > getInstanceOf (Class <S > superClass , String name , Set <Option > options ) {
190+ if (isSingleton (superClass )) {
191+ return getSingleInstanceOf (superClass , name , options );
192+ }
193+ return getInstantiableConfiguration (superClass , name , options ).map (MCRInstanceConfiguration ::instantiate );
194+
195+ }
196+
197+ @ SuppressWarnings ("unchecked" )
198+ private static <S > Optional <S > getSingleInstanceOf (Class <S > superClass , String name , Set <Option > options ) {
199+ return getInstantiableConfiguration (superClass , name , options ).map (
200+ configuration -> {
201+ SingletonKey key = new ConfigSingletonKey (name , configuration .valueClass ().getName ());
202+ return (S ) instanceHolder .computeIfAbsent (key , _ -> configuration .instantiate ());
203+ });
204+ }
205+
206+ private static <S > Optional <MCRInstanceConfiguration <S >> getInstantiableConfiguration (Class <S > superClass ,
207+ String name , Set <Option > options ) {
208+ MCRInstanceConfiguration <S > configuration = ofName (superClass , name , getPropertiesMap (), options );
209+ return configuration .instantiatable () ? Optional .of (configuration ) : Optional .empty ();
210+
211+ }
212+
213+ /**
214+ * Checks if a class is annotated with {@link Singleton}.
215+ *
216+ * @param targetClass the class
217+ * @return true if the class in the property is annotated with {@link Singleton}
218+ */
219+ public static boolean isSingleton (Class <?> targetClass ) {
220+ return targetClass .getDeclaredAnnotation (Singleton .class ) != null ;
221+ }
222+
197223 /**
198224 * Loads a Java Class defined in property <code>name</code>.
199225 * @param name Name of the property
200226 * @param <T> Supertype of class defined in <code>name</code>
201- * @return Optional of Class asignable to <code><T></code>
227+ * @return Optional of Class assignable to <code><T></code>
202228 * @throws MCRConfigurationException
203- * if the the class can not be loaded or instantiated
229+ * if the class can not be loaded or instantiated
204230 */
205231 public static <T > Optional <Class <? extends T >> getClass (String name ) throws MCRConfigurationException {
206232 return getString (name ).map (MCRConfiguration2 ::<T >getClassObject );
@@ -426,7 +452,8 @@ public static boolean removePropertyChangeEventListener(UUID uuid) {
426452 }
427453
428454 public static <S > S instantiateClass (Class <S > superClass , String className ) {
429- return MCRConfigurableInstanceHelper .getInstance (superClass , MCRInstanceConfiguration .ofClass (className ));
455+ return MCRInstanceConfiguration .ofClassName (superClass , className , Suffix .UPPER_CASE ,
456+ new HashMap <>(), getPropertiesMap ()).instantiate ();
430457 }
431458
432459 public static <S > Stream <S > instantiateClasses (Class <S > superClass , String propertyName ) {
@@ -446,11 +473,11 @@ private static <T> Class<? extends T> getClassObject(String classname) {
446473
447474 private static class EventListener {
448475
449- private Predicate <String > keyPredicate ;
476+ private final Predicate <String > keyPredicate ;
450477
451- private MCRTriConsumer <String , Optional <String >, Optional <String >> listener ;
478+ private final MCRTriConsumer <String , Optional <String >, Optional <String >> listener ;
452479
453- private UUID uuid ;
480+ private final UUID uuid ;
454481
455482 EventListener (Predicate <String > keyPredicate ,
456483 MCRTriConsumer <String , Optional <String >, Optional <String >> listener ) {
0 commit comments