@@ -48,72 +48,54 @@ public ReactiveIdentifierGeneratorFactory(ServiceRegistry serviceRegistry) {
4848 public Generator createIdentifierGenerator (String strategy , Type type , Properties config ) {
4949 Object generator ;
5050 try {
51- generator = super .createIdentifierGenerator (strategy , type , config );
52- } catch (MappingException ignored ) {
53- generator = fallbackCreateIdentifierGenerator ( strategy , type , config );
51+ generator = super .createIdentifierGenerator ( strategy , type , config );
52+ }
53+ catch ( MappingException ignored ) {
54+ try {
55+ final Class <?> clazz = generatorClassForName ( strategy );
56+ generator = clazz .getConstructor ().newInstance ();
57+ if ( generator instanceof Configurable ) {
58+ ( (Configurable ) generator ).configure ( type , config , serviceRegistry );
59+ }
60+ }
61+ catch ( Exception e ) {
62+ final String entityName = config .getProperty ( IdentifierGenerator .ENTITY_NAME );
63+ throw new MappingException ( String .format ( "Could not instantiate id generator [entity-name=%s]" , entityName ), e );
64+ }
5465 }
5566
5667 //FIXME: Not sure why we need all these instanceof
5768 if ( generator instanceof BeforeExecutionGenerator ) {
58- return augmentWithReactiveGenerator ( (BeforeExecutionGenerator )generator , type , config );
69+ return augmentWithReactiveGenerator ( (BeforeExecutionGenerator ) generator , type , config );
5970 }
6071
6172 if ( generator instanceof OnExecutionGenerator ) {
62- return augmentWithReactiveGenerator ( (OnExecutionGenerator )generator , type , config );
73+ return augmentWithReactiveGenerator ( (OnExecutionGenerator ) generator , type , config );
6374 }
6475
6576 if ( generator instanceof ReactiveIdentifierGenerator ) {
66- return new ReactiveGeneratorWrapper ( (ReactiveIdentifierGenerator ) generator , type . getReturnedClass () );
77+ return new ReactiveGeneratorWrapper ( (ReactiveIdentifierGenerator <?> ) generator );
6778 }
6879
6980 final String entityName = config .getProperty ( IdentifierGenerator .ENTITY_NAME );
7081 throw new MappingException ( String .format ( "Not an id generator [entity-name=%s]" , entityName ) );
7182 }
7283
73- //TODO this was copied from StandardIdentifierGeneratorFactory#createIdentifierGenerator
74- // in order to avoid the !Generator.class.isAssignableFrom( clazz ) check in getIdentifierGeneratorClass
75- // This is suboptimal not only because we are duplicating code, but because this piece cannot access
76- // the private fields of the super method
77- private Object fallbackCreateIdentifierGenerator (String strategy , Type type , Properties parameters ) {
78- try {
79- final Class <?> clazz = fallbackGetIdentifierGeneratorClass ( strategy );
80- Object result = clazz .getConstructor ().newInstance ();
81-
82- if ( result instanceof Configurable ) {
83- ( (Configurable ) result ).configure ( type , parameters , serviceRegistry );
84- }
85- return result ;
86- }
87- catch ( Exception e ) {
88- final String entityName = parameters .getProperty ( IdentifierGenerator .ENTITY_NAME );
89- throw new MappingException ( String .format ( "Could not instantiate id generator [entity-name=%s]" , entityName ), e );
90- }
91- }
92-
84+ //TODO: deleteme, after update to ORM
9385 @ Override
9486 public Class <? extends Generator > getIdentifierGeneratorClass (String strategy ) {
9587 try {
96- return super .getIdentifierGeneratorClass (strategy );
97- } catch (MappingException ignored ) {
98- return fallbackGetIdentifierGeneratorClass (strategy );
88+ return super .getIdentifierGeneratorClass ( strategy );
9989 }
100- }
101-
102- //TODO this was copied from StandardIdentifierGeneratorFactory#createIdentifierGenerator
103- // in order to avoid the !Generator.class.isAssignableFrom( clazz ) check in getIdentifierGeneratorClass
104- // This is suboptimal not only because we are duplicating code, but because this piece cannot access
105- // the private fields of the super method
106- public Class <? extends Generator > fallbackGetIdentifierGeneratorClass (String strategy ) {
107- if ( "hilo" .equals ( strategy ) ) {
108- throw new UnsupportedOperationException ( "Support for 'hilo' generator has been removed" );
90+ catch ( MappingException ignored ) {
91+ // happens because the class does not implement Generator
92+ return generatorClassForName ( strategy );
10993 }
110- final String resolvedStrategy = "native" .equals ( strategy )
111- ? getDialect ().getNativeIdentifierGeneratorStrategy ()
112- : strategy ;
94+ }
11395
96+ protected Class <? extends Generator > generatorClassForName (String strategy ) {
11497 try {
115- return serviceRegistry .getService ( ClassLoaderService .class )
116- .classForName ( resolvedStrategy );
98+ return serviceRegistry .getService ( ClassLoaderService .class ).classForName ( strategy );
11799 }
118100 catch ( ClassLoadingException e ) {
119101 throw new MappingException ( String .format ( "Could not interpret id generator strategy [%s]" , strategy ) );
@@ -125,9 +107,9 @@ public Generator augmentWithReactiveGenerator(Generator generator, Type type, Pr
125107 }
126108
127109 public static Generator augmentWithReactiveGenerator (ServiceRegistry serviceRegistry , Generator generator , Type type , Properties params ) {
128- ReactiveIdentifierGenerator <?> reactiveGenerator ;
110+ final ReactiveIdentifierGenerator <?> reactiveGenerator ;
129111 if ( generator instanceof SequenceStyleGenerator ) {
130- DatabaseStructure structure = ( (SequenceStyleGenerator ) generator ).getDatabaseStructure ();
112+ final DatabaseStructure structure = ( (SequenceStyleGenerator ) generator ).getDatabaseStructure ();
131113 if ( structure instanceof TableStructure ) {
132114 reactiveGenerator = new EmulatedSequenceReactiveIdentifierGenerator ();
133115 }
@@ -151,22 +133,22 @@ else if ( generator instanceof SelectGenerator ) {
151133
152134 //this is not the way ORM does this: instead it passes a
153135 //SqlStringGenerationContext to IdentifierGenerator.initialize()
154- ConfigurationService cs = serviceRegistry .getService ( ConfigurationService .class );
136+ final ConfigurationService cs = serviceRegistry .getService ( ConfigurationService .class );
155137 if ( !params .containsKey ( PersistentIdentifierGenerator .SCHEMA ) ) {
156- String schema = cs .getSetting ( Settings .DEFAULT_SCHEMA , StandardConverters .STRING );
138+ final String schema = cs .getSetting ( Settings .DEFAULT_SCHEMA , StandardConverters .STRING );
157139 if ( schema != null ) {
158140 params .put ( PersistentIdentifierGenerator .SCHEMA , schema );
159141 }
160142 }
161143 if ( !params .containsKey ( PersistentIdentifierGenerator .CATALOG ) ) {
162- String catalog = cs .getSetting ( Settings .DEFAULT_CATALOG , StandardConverters .STRING );
144+ final String catalog = cs .getSetting ( Settings .DEFAULT_CATALOG , StandardConverters .STRING );
163145 if ( catalog != null ) {
164146 params .put ( PersistentIdentifierGenerator .CATALOG , catalog );
165147 }
166148 }
167149
168150 ( (Configurable ) reactiveGenerator ).configure ( type , params , serviceRegistry );
169- return new ReactiveGeneratorWrapper ( reactiveGenerator , (IdentifierGenerator ) generator , type . getReturnedClass () );
151+ return new ReactiveGeneratorWrapper ( reactiveGenerator , (IdentifierGenerator ) generator );
170152 }
171153
172154}
0 commit comments