@@ -48,72 +48,54 @@ public ReactiveIdentifierGeneratorFactory(ServiceRegistry serviceRegistry) {
48
48
public Generator createIdentifierGenerator (String strategy , Type type , Properties config ) {
49
49
Object generator ;
50
50
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
+ }
54
65
}
55
66
56
67
//FIXME: Not sure why we need all these instanceof
57
68
if ( generator instanceof BeforeExecutionGenerator ) {
58
- return augmentWithReactiveGenerator ( (BeforeExecutionGenerator )generator , type , config );
69
+ return augmentWithReactiveGenerator ( (BeforeExecutionGenerator ) generator , type , config );
59
70
}
60
71
61
72
if ( generator instanceof OnExecutionGenerator ) {
62
- return augmentWithReactiveGenerator ( (OnExecutionGenerator )generator , type , config );
73
+ return augmentWithReactiveGenerator ( (OnExecutionGenerator ) generator , type , config );
63
74
}
64
75
65
76
if ( generator instanceof ReactiveIdentifierGenerator ) {
66
- return new ReactiveGeneratorWrapper ( (ReactiveIdentifierGenerator ) generator , type . getReturnedClass () );
77
+ return new ReactiveGeneratorWrapper ( (ReactiveIdentifierGenerator <?> ) generator );
67
78
}
68
79
69
80
final String entityName = config .getProperty ( IdentifierGenerator .ENTITY_NAME );
70
81
throw new MappingException ( String .format ( "Not an id generator [entity-name=%s]" , entityName ) );
71
82
}
72
83
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
93
85
@ Override
94
86
public Class <? extends Generator > getIdentifierGeneratorClass (String strategy ) {
95
87
try {
96
- return super .getIdentifierGeneratorClass (strategy );
97
- } catch (MappingException ignored ) {
98
- return fallbackGetIdentifierGeneratorClass (strategy );
88
+ return super .getIdentifierGeneratorClass ( strategy );
99
89
}
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 );
109
93
}
110
- final String resolvedStrategy = "native" .equals ( strategy )
111
- ? getDialect ().getNativeIdentifierGeneratorStrategy ()
112
- : strategy ;
94
+ }
113
95
96
+ protected Class <? extends Generator > generatorClassForName (String strategy ) {
114
97
try {
115
- return serviceRegistry .getService ( ClassLoaderService .class )
116
- .classForName ( resolvedStrategy );
98
+ return serviceRegistry .getService ( ClassLoaderService .class ).classForName ( strategy );
117
99
}
118
100
catch ( ClassLoadingException e ) {
119
101
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
125
107
}
126
108
127
109
public static Generator augmentWithReactiveGenerator (ServiceRegistry serviceRegistry , Generator generator , Type type , Properties params ) {
128
- ReactiveIdentifierGenerator <?> reactiveGenerator ;
110
+ final ReactiveIdentifierGenerator <?> reactiveGenerator ;
129
111
if ( generator instanceof SequenceStyleGenerator ) {
130
- DatabaseStructure structure = ( (SequenceStyleGenerator ) generator ).getDatabaseStructure ();
112
+ final DatabaseStructure structure = ( (SequenceStyleGenerator ) generator ).getDatabaseStructure ();
131
113
if ( structure instanceof TableStructure ) {
132
114
reactiveGenerator = new EmulatedSequenceReactiveIdentifierGenerator ();
133
115
}
@@ -151,22 +133,22 @@ else if ( generator instanceof SelectGenerator ) {
151
133
152
134
//this is not the way ORM does this: instead it passes a
153
135
//SqlStringGenerationContext to IdentifierGenerator.initialize()
154
- ConfigurationService cs = serviceRegistry .getService ( ConfigurationService .class );
136
+ final ConfigurationService cs = serviceRegistry .getService ( ConfigurationService .class );
155
137
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 );
157
139
if ( schema != null ) {
158
140
params .put ( PersistentIdentifierGenerator .SCHEMA , schema );
159
141
}
160
142
}
161
143
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 );
163
145
if ( catalog != null ) {
164
146
params .put ( PersistentIdentifierGenerator .CATALOG , catalog );
165
147
}
166
148
}
167
149
168
150
( (Configurable ) reactiveGenerator ).configure ( type , params , serviceRegistry );
169
- return new ReactiveGeneratorWrapper ( reactiveGenerator , (IdentifierGenerator ) generator , type . getReturnedClass () );
151
+ return new ReactiveGeneratorWrapper ( reactiveGenerator , (IdentifierGenerator ) generator );
170
152
}
171
153
172
154
}
0 commit comments