1818
1919/**
2020 * Looks for the configuration property {@value AvailableSettings#MERGE_ENTITY_COPY_OBSERVER} and registers
21- * the matching {@link EntityCopyObserverFactory} based on the configuration value .
21+ * the matching {@link EntityCopyObserverFactory} based on the configuration observerClass .
2222 * <p>
2323 * For known implementations some optimisations are possible, such as reusing a singleton for the stateless
2424 * implementations. When a user plugs in a custom {@link EntityCopyObserver} we take a defensive approach.
@@ -32,7 +32,10 @@ public class EntityCopyObserverFactoryInitiator implements StandardServiceInitia
3232 @ Override
3333 public EntityCopyObserverFactory initiateService (final Map <String , Object > configurationValues , final ServiceRegistryImplementor registry ) {
3434 final Object value = getConfigurationValue ( configurationValues );
35- if ( value .equals ( EntityCopyNotAllowedObserver .SHORT_NAME )
35+ if ( value instanceof EntityCopyObserverFactory factory ) {
36+ return factory ;
37+ }
38+ else if ( value .equals ( EntityCopyNotAllowedObserver .SHORT_NAME )
3639 || value .equals ( EntityCopyNotAllowedObserver .class .getName () ) ) {
3740 LOG .debugf ( "Configured EntityCopyObserver strategy: %s" , EntityCopyNotAllowedObserver .SHORT_NAME );
3841 return EntityCopyNotAllowedObserver .FACTORY_OF_SELF ;
@@ -48,15 +51,16 @@ else if ( value.equals( EntityCopyAllowedLoggedObserver.SHORT_NAME )
4851 return EntityCopyAllowedLoggedObserver .FACTORY_OF_SELF ;
4952 }
5053 else {
51- //We load an "example instance" just to get its Class;
52- //this might look excessive, but it also happens to test eagerly (at boot) that we can actually construct these
53- //and that they are indeed of the right type.
54+ // We load an "example instance" just to get its Class;
55+ // this might look excessive, but it also happens to test eagerly
56+ // (at boot) that we can actually construct these and that they
57+ // are indeed of the right type.
5458 final EntityCopyObserver exampleInstance =
5559 registry .requireService ( StrategySelector .class )
5660 .resolveStrategy ( EntityCopyObserver .class , value );
57- final Class <?> observerType = exampleInstance .getClass ();
58- LOG .debugf ( "Configured EntityCopyObserver is a custom implementation of type %s " , observerType .getName () );
59- return new EntityObserversFactoryFromClass ( observerType );
61+ final Class <? extends EntityCopyObserver > observerType = exampleInstance .getClass ();
62+ LOG .debugf ( "Configured EntityCopyObserver is a custom implementation of type '%s' " , observerType .getName () );
63+ return new EntityCopyObserverFactoryFromClass ( observerType );
6064 }
6165 }
6266
@@ -78,23 +82,17 @@ public Class<EntityCopyObserverFactory> getServiceInitiated() {
7882 return EntityCopyObserverFactory .class ;
7983 }
8084
81- private static class EntityObserversFactoryFromClass implements EntityCopyObserverFactory {
82-
83- private final Class <?> value ;
84-
85- public EntityObserversFactoryFromClass (Class <?> value ) {
86- this .value = value ;
87- }
85+ private record EntityCopyObserverFactoryFromClass (Class <? extends EntityCopyObserver > observerClass )
86+ implements EntityCopyObserverFactory {
8887
8988 @ Override
90- public EntityCopyObserver createEntityCopyObserver () {
91- try {
92- return (EntityCopyObserver ) value .newInstance ();
93- }
94- catch (Exception e ) {
95- throw new HibernateException ( "Could not instantiate class of type " + value .getName () );
89+ public EntityCopyObserver createEntityCopyObserver () {
90+ try {
91+ return observerClass .newInstance ();
92+ }
93+ catch (Exception e ) {
94+ throw new HibernateException ( "Could not instantiate class of type " + observerClass .getName () );
95+ }
9696 }
9797 }
98- }
99-
10098}
0 commit comments