18
18
19
19
/**
20
20
* 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 .
22
22
* <p>
23
23
* For known implementations some optimisations are possible, such as reusing a singleton for the stateless
24
24
* implementations. When a user plugs in a custom {@link EntityCopyObserver} we take a defensive approach.
@@ -32,7 +32,10 @@ public class EntityCopyObserverFactoryInitiator implements StandardServiceInitia
32
32
@ Override
33
33
public EntityCopyObserverFactory initiateService (final Map <String , Object > configurationValues , final ServiceRegistryImplementor registry ) {
34
34
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 )
36
39
|| value .equals ( EntityCopyNotAllowedObserver .class .getName () ) ) {
37
40
LOG .debugf ( "Configured EntityCopyObserver strategy: %s" , EntityCopyNotAllowedObserver .SHORT_NAME );
38
41
return EntityCopyNotAllowedObserver .FACTORY_OF_SELF ;
@@ -48,15 +51,16 @@ else if ( value.equals( EntityCopyAllowedLoggedObserver.SHORT_NAME )
48
51
return EntityCopyAllowedLoggedObserver .FACTORY_OF_SELF ;
49
52
}
50
53
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.
54
58
final EntityCopyObserver exampleInstance =
55
59
registry .requireService ( StrategySelector .class )
56
60
.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 );
60
64
}
61
65
}
62
66
@@ -78,23 +82,17 @@ public Class<EntityCopyObserverFactory> getServiceInitiated() {
78
82
return EntityCopyObserverFactory .class ;
79
83
}
80
84
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 {
88
87
89
88
@ 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
+ }
96
96
}
97
97
}
98
- }
99
-
100
98
}
0 commit comments