-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
MetadataBuilderImpl.java
931 lines (781 loc) · 30.3 KB
/
MetadataBuilderImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.boot.internal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.persistence.AttributeConverter;
import javax.persistence.SharedCacheMode;
import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.MultiTenancyStrategy;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.common.reflection.ClassLoaderDelegate;
import org.hibernate.annotations.common.reflection.ClassLoadingException;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.java.JavaReflectionManager;
import org.hibernate.annotations.common.util.StandardClassLoaderDelegateImpl;
import org.hibernate.boot.CacheRegionDefinition;
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.archive.scan.internal.StandardScanOptions;
import org.hibernate.boot.archive.scan.spi.ScanEnvironment;
import org.hibernate.boot.archive.scan.spi.ScanOptions;
import org.hibernate.boot.archive.scan.spi.Scanner;
import org.hibernate.boot.archive.spi.ArchiveDescriptorFactory;
import org.hibernate.boot.cfgxml.spi.CfgXmlAccessService;
import org.hibernate.boot.cfgxml.spi.LoadedConfig;
import org.hibernate.boot.cfgxml.spi.MappingReference;
import org.hibernate.boot.model.IdGeneratorStrategyInterpreter;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.boot.model.TypeContributor;
import org.hibernate.boot.model.naming.ImplicitNamingStrategy;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.boot.spi.MappingDefaults;
import org.hibernate.boot.spi.MetadataBuilderInitializer;
import org.hibernate.boot.spi.MetadataBuildingOptions;
import org.hibernate.boot.spi.MetadataSourcesContributor;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cfg.AttributeConverterDefinition;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.MetadataSourceType;
import org.hibernate.cfg.annotations.reflection.JPAMetadataProvider;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.BasicType;
import org.hibernate.type.CompositeCustomType;
import org.hibernate.type.CustomType;
import org.hibernate.usertype.CompositeUserType;
import org.hibernate.usertype.UserType;
import org.jboss.jandex.IndexView;
import static org.hibernate.internal.log.DeprecationLogger.DEPRECATION_LOGGER;
/**
* @author Steve Ebersole
*/
public class MetadataBuilderImpl implements MetadataBuilder, TypeContributions {
private static final CoreMessageLogger log = CoreLogging.messageLogger( MetadataBuilderImpl.class );
private final MetadataSources sources;
private final MetadataBuildingOptionsImpl options;
public MetadataBuilderImpl(MetadataSources sources) {
this(
sources,
getStandardServiceRegistry( sources.getServiceRegistry() )
);
}
private static StandardServiceRegistry getStandardServiceRegistry(ServiceRegistry serviceRegistry) {
if ( serviceRegistry == null ) {
throw new HibernateException( "ServiceRegistry passed to MetadataBuilder cannot be null" );
}
if ( StandardServiceRegistry.class.isInstance( serviceRegistry ) ) {
return ( StandardServiceRegistry ) serviceRegistry;
}
else if ( BootstrapServiceRegistry.class.isInstance( serviceRegistry ) ) {
log.debugf(
"ServiceRegistry passed to MetadataBuilder was a BootstrapServiceRegistry; this likely wont end well" +
"if attempt is made to build SessionFactory"
);
return new StandardServiceRegistryBuilder( (BootstrapServiceRegistry) serviceRegistry ).build();
}
else {
throw new HibernateException(
String.format(
"Unexpected type of ServiceRegistry [%s] encountered in attempt to build MetadataBuilder",
serviceRegistry.getClass().getName()
)
);
}
}
public MetadataBuilderImpl(MetadataSources sources, StandardServiceRegistry serviceRegistry) {
this.sources = sources;
this.options = new MetadataBuildingOptionsImpl( serviceRegistry );
for ( MetadataSourcesContributor contributor :
sources.getServiceRegistry()
.getService( ClassLoaderService.class )
.loadJavaServices( MetadataSourcesContributor.class ) ) {
contributor.contribute( sources );
}
// todo : not so sure this is needed anymore.
// these should be set during the StandardServiceRegistryBuilder.configure call
applyCfgXmlValues( serviceRegistry.getService( CfgXmlAccessService.class ) );
final ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
for ( MetadataBuilderInitializer contributor : classLoaderService.loadJavaServices( MetadataBuilderInitializer.class ) ) {
contributor.contribute( this, serviceRegistry );
}
}
private void applyCfgXmlValues(CfgXmlAccessService service) {
final LoadedConfig aggregatedConfig = service.getAggregatedConfig();
if ( aggregatedConfig == null ) {
return;
}
for ( CacheRegionDefinition cacheRegionDefinition : aggregatedConfig.getCacheRegionDefinitions() ) {
applyCacheRegionDefinition( cacheRegionDefinition );
}
}
@Override
public MetadataBuilder applyImplicitSchemaName(String implicitSchemaName) {
options.mappingDefaults.implicitSchemaName = implicitSchemaName;
return this;
}
@Override
public MetadataBuilder applyImplicitCatalogName(String implicitCatalogName) {
options.mappingDefaults.implicitCatalogName = implicitCatalogName;
return this;
}
@Override
public MetadataBuilder applyImplicitNamingStrategy(ImplicitNamingStrategy namingStrategy) {
this.options.implicitNamingStrategy = namingStrategy;
return this;
}
@Override
public MetadataBuilder applyPhysicalNamingStrategy(PhysicalNamingStrategy namingStrategy) {
this.options.physicalNamingStrategy = namingStrategy;
return this;
}
@Override
public MetadataBuilder applyReflectionManager(ReflectionManager reflectionManager) {
this.options.reflectionManager = reflectionManager;
this.options.reflectionManager.injectClassLoaderDelegate( this.options.getHcannClassLoaderDelegate() );
return this;
}
@Override
public MetadataBuilder applySharedCacheMode(SharedCacheMode sharedCacheMode) {
this.options.sharedCacheMode = sharedCacheMode;
return this;
}
@Override
public MetadataBuilder applyAccessType(AccessType implicitCacheAccessType) {
this.options.mappingDefaults.implicitCacheAccessType = implicitCacheAccessType;
return this;
}
@Override
public MetadataBuilder applyIndexView(IndexView jandexView) {
this.options.jandexView = jandexView;
return this;
}
@Override
public MetadataBuilder applyScanOptions(ScanOptions scanOptions) {
this.options.scanOptions = scanOptions;
return this;
}
@Override
public MetadataBuilder applyScanEnvironment(ScanEnvironment scanEnvironment) {
this.options.scanEnvironment = scanEnvironment;
return this;
}
@Override
public MetadataBuilder applyScanner(Scanner scanner) {
this.options.scannerSetting = scanner;
return this;
}
@Override
public MetadataBuilder applyArchiveDescriptorFactory(ArchiveDescriptorFactory factory) {
this.options.archiveDescriptorFactory = factory;
return this;
}
@Override
public MetadataBuilder enableExplicitDiscriminatorsForJoinedSubclassSupport(boolean supported) {
options.explicitDiscriminatorsForJoinedInheritanceSupported = supported;
return this;
}
@Override
public MetadataBuilder enableImplicitDiscriminatorsForJoinedSubclassSupport(boolean supported) {
options.implicitDiscriminatorsForJoinedInheritanceSupported = supported;
return this;
}
@Override
public MetadataBuilder enableImplicitForcingOfDiscriminatorsInSelect(boolean supported) {
options.implicitlyForceDiscriminatorInSelect = supported;
return this;
}
@Override
public MetadataBuilder enableGlobalNationalizedCharacterDataSupport(boolean enabled) {
options.useNationalizedCharacterData = enabled;
return this;
}
@Override
public MetadataBuilder applyBasicType(BasicType type) {
options.basicTypeRegistrations.add( type );
return this;
}
@Override
public MetadataBuilder applyBasicType(UserType type, String[] keys) {
options.basicTypeRegistrations.add( new CustomType( type, keys ) );
return this;
}
@Override
public MetadataBuilder applyBasicType(CompositeUserType type, String[] keys) {
options.basicTypeRegistrations.add( new CompositeCustomType( type, keys ) );
return this;
}
@Override
public MetadataBuilder applyTypes(TypeContributor typeContributor) {
typeContributor.contribute( this, options.serviceRegistry );
return this;
}
@Override
public void contributeType(BasicType type) {
options.basicTypeRegistrations.add( type );
}
@Override
public void contributeType(UserType type, String[] keys) {
options.basicTypeRegistrations.add( new CustomType( type, keys ) );
}
@Override
public void contributeType(CompositeUserType type, String[] keys) {
options.basicTypeRegistrations.add( new CompositeCustomType( type, keys ) );
}
@Override
public MetadataBuilder applyCacheRegionDefinition(CacheRegionDefinition cacheRegionDefinition) {
if ( options.cacheRegionDefinitions == null ) {
options.cacheRegionDefinitions = new ArrayList<CacheRegionDefinition>();
}
options.cacheRegionDefinitions.add( cacheRegionDefinition );
return this;
}
@Override
public MetadataBuilder applyTempClassLoader(ClassLoader tempClassLoader) {
options.tempClassLoader = tempClassLoader;
return this;
}
@Override
public MetadataBuilder applySourceProcessOrdering(MetadataSourceType... sourceTypes) {
options.sourceProcessOrdering.addAll( Arrays.asList( sourceTypes ) );
return this;
}
public MetadataBuilder allowSpecjSyntax() {
this.options.specjProprietarySyntaxEnabled = true;
return this;
}
@Override
public MetadataBuilder applySqlFunction(String functionName, SQLFunction function) {
if ( this.options.sqlFunctionMap == null ) {
// need to use this form as we want to specify the "concurrency level" as 1
// since only one thread will ever (should) be updating this
this.options.sqlFunctionMap = new HashMap<String, SQLFunction>();
}
// HHH-7721: SQLFunctionRegistry expects all lowercase. Enforce,
// just in case a user's customer dialect uses mixed cases.
this.options.sqlFunctionMap.put( functionName.toLowerCase(Locale.ROOT), function );
return this;
}
@Override
public MetadataBuilder applyAuxiliaryDatabaseObject(AuxiliaryDatabaseObject auxiliaryDatabaseObject) {
if ( this.options.auxiliaryDatabaseObjectList == null ) {
this.options.auxiliaryDatabaseObjectList = new ArrayList<AuxiliaryDatabaseObject>();
}
this.options.auxiliaryDatabaseObjectList.add( auxiliaryDatabaseObject );
return this;
}
@Override
public MetadataBuilder applyAttributeConverter(AttributeConverterDefinition definition) {
this.options.addAttributeConverterDefinition( definition );
return this;
}
@Override
public MetadataBuilder applyAttributeConverter(Class<? extends AttributeConverter> attributeConverterClass) {
applyAttributeConverter( AttributeConverterDefinition.from( attributeConverterClass ) );
return this;
}
@Override
public MetadataBuilder applyAttributeConverter(Class<? extends AttributeConverter> attributeConverterClass, boolean autoApply) {
applyAttributeConverter( AttributeConverterDefinition.from( attributeConverterClass, autoApply ) );
return this;
}
@Override
public MetadataBuilder applyAttributeConverter(AttributeConverter attributeConverter) {
applyAttributeConverter( AttributeConverterDefinition.from( attributeConverter ) );
return this;
}
@Override
public MetadataBuilder applyAttributeConverter(AttributeConverter attributeConverter, boolean autoApply) {
applyAttributeConverter( AttributeConverterDefinition.from( attributeConverter, autoApply ) );
return this;
}
@Override
public MetadataBuilder enableNewIdentifierGeneratorSupport(boolean enabled) {
if ( enabled ) {
this.options.idGenerationTypeInterpreter.disableLegacyFallback();
}
else {
this.options.idGenerationTypeInterpreter.enableLegacyFallback();
}
return this;
}
@Override
public MetadataBuilder applyIdGenerationTypeInterpreter(IdGeneratorStrategyInterpreter interpreter) {
this.options.idGenerationTypeInterpreter.addInterpreterDelegate( interpreter );
return this;
}
// public MetadataBuilder with(PersistentAttributeMemberResolver resolver) {
// options.persistentAttributeMemberResolver = resolver;
// return this;
// }
@Override
public MetadataImpl build() {
final CfgXmlAccessService cfgXmlAccessService = options.serviceRegistry.getService( CfgXmlAccessService.class );
if ( cfgXmlAccessService.getAggregatedConfig() != null ) {
if ( cfgXmlAccessService.getAggregatedConfig().getMappingReferences() != null ) {
for ( MappingReference mappingReference : cfgXmlAccessService.getAggregatedConfig().getMappingReferences() ) {
mappingReference.apply( sources );
}
}
}
return MetadataBuildingProcess.build( sources, options );
}
public static class MappingDefaultsImpl implements MappingDefaults {
private String implicitSchemaName;
private String implicitCatalogName;
private boolean implicitlyQuoteIdentifiers;
private AccessType implicitCacheAccessType;
public MappingDefaultsImpl(StandardServiceRegistry serviceRegistry) {
final ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class );
this.implicitSchemaName = configService.getSetting(
AvailableSettings.DEFAULT_SCHEMA,
StandardConverters.STRING,
null
);
this.implicitCatalogName = configService.getSetting(
AvailableSettings.DEFAULT_CATALOG,
StandardConverters.STRING,
null
);
this.implicitlyQuoteIdentifiers = configService.getSetting(
AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS,
StandardConverters.BOOLEAN,
false
);
this.implicitCacheAccessType = configService.getSetting(
AvailableSettings.DEFAULT_CACHE_CONCURRENCY_STRATEGY,
new ConfigurationService.Converter<AccessType>() {
@Override
public AccessType convert(Object value) {
return AccessType.fromExternalName( value.toString() );
}
}
);
}
@Override
public String getImplicitSchemaName() {
return implicitSchemaName;
}
@Override
public String getImplicitCatalogName() {
return implicitCatalogName;
}
@Override
public boolean shouldImplicitlyQuoteIdentifiers() {
return implicitlyQuoteIdentifiers;
}
@Override
public String getImplicitIdColumnName() {
return DEFAULT_IDENTIFIER_COLUMN_NAME;
}
@Override
public String getImplicitTenantIdColumnName() {
return DEFAULT_TENANT_IDENTIFIER_COLUMN_NAME;
}
@Override
public String getImplicitDiscriminatorColumnName() {
return DEFAULT_DISCRIMINATOR_COLUMN_NAME;
}
@Override
public String getImplicitPackageName() {
return null;
}
@Override
public boolean isAutoImportEnabled() {
return true;
}
@Override
public String getImplicitCascadeStyleName() {
return DEFAULT_CASCADE_NAME;
}
@Override
public String getImplicitPropertyAccessorName() {
return DEFAULT_PROPERTY_ACCESS_NAME;
}
@Override
public boolean areEntitiesImplicitlyLazy() {
// for now, just hard-code
return false;
}
@Override
public boolean areCollectionsImplicitlyLazy() {
// for now, just hard-code
return true;
}
@Override
public AccessType getImplicitCacheAccessType() {
return implicitCacheAccessType;
}
}
public static class MetadataBuildingOptionsImpl implements MetadataBuildingOptions {
private final StandardServiceRegistry serviceRegistry;
private final MappingDefaultsImpl mappingDefaults;
private ArrayList<BasicType> basicTypeRegistrations = new ArrayList<BasicType>();
private IndexView jandexView;
private ClassLoader tempClassLoader;
private ScanOptions scanOptions;
private ScanEnvironment scanEnvironment;
private Object scannerSetting;
private ArchiveDescriptorFactory archiveDescriptorFactory;
private ImplicitNamingStrategy implicitNamingStrategy;
private PhysicalNamingStrategy physicalNamingStrategy;
private ReflectionManager reflectionManager;
private ClassLoaderDelegate hcannClassLoaderDelegate;
private SharedCacheMode sharedCacheMode;
private AccessType defaultCacheAccessType;
private MultiTenancyStrategy multiTenancyStrategy;
private ArrayList<CacheRegionDefinition> cacheRegionDefinitions;
private boolean explicitDiscriminatorsForJoinedInheritanceSupported;
private boolean implicitDiscriminatorsForJoinedInheritanceSupported;
private boolean implicitlyForceDiscriminatorInSelect;
private boolean useNationalizedCharacterData;
private boolean specjProprietarySyntaxEnabled;
private ArrayList<MetadataSourceType> sourceProcessOrdering;
private HashMap<String,SQLFunction> sqlFunctionMap;
private ArrayList<AuxiliaryDatabaseObject> auxiliaryDatabaseObjectList;
private HashMap<Class,AttributeConverterDefinition> attributeConverterDefinitionsByClass;
private IdGeneratorInterpreterImpl idGenerationTypeInterpreter = new IdGeneratorInterpreterImpl();
// private PersistentAttributeMemberResolver persistentAttributeMemberResolver =
// StandardPersistentAttributeMemberResolver.INSTANCE;
public MetadataBuildingOptionsImpl(StandardServiceRegistry serviceRegistry) {
this.serviceRegistry = serviceRegistry;
final StrategySelector strategySelector = serviceRegistry.getService( StrategySelector.class );
final ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class );
this.mappingDefaults = new MappingDefaultsImpl( serviceRegistry );
// jandexView = (IndexView) configService.getSettings().get( AvailableSettings.JANDEX_INDEX );
scanOptions = new StandardScanOptions(
(String) configService.getSettings().get( AvailableSettings.SCANNER_DISCOVERY ),
false
);
// ScanEnvironment must be set explicitly
scannerSetting = configService.getSettings().get( AvailableSettings.SCANNER );
if ( scannerSetting == null ) {
scannerSetting = configService.getSettings().get( AvailableSettings.SCANNER_DEPRECATED );
if ( scannerSetting != null ) {
DEPRECATION_LOGGER.logDeprecatedScannerSetting();
}
}
archiveDescriptorFactory = strategySelector.resolveStrategy(
ArchiveDescriptorFactory.class,
configService.getSettings().get( AvailableSettings.SCANNER_ARCHIVE_INTERPRETER )
);
multiTenancyStrategy = MultiTenancyStrategy.determineMultiTenancyStrategy( configService.getSettings() );
implicitDiscriminatorsForJoinedInheritanceSupported = configService.getSetting(
AvailableSettings.IMPLICIT_DISCRIMINATOR_COLUMNS_FOR_JOINED_SUBCLASS,
StandardConverters.BOOLEAN,
false
);
explicitDiscriminatorsForJoinedInheritanceSupported = !configService.getSetting(
AvailableSettings.IGNORE_EXPLICIT_DISCRIMINATOR_COLUMNS_FOR_JOINED_SUBCLASS,
StandardConverters.BOOLEAN,
false
);
implicitlyForceDiscriminatorInSelect = configService.getSetting(
AvailableSettings.FORCE_DISCRIMINATOR_IN_SELECTS_BY_DEFAULT,
StandardConverters.BOOLEAN,
false
);
sharedCacheMode = configService.getSetting(
"javax.persistence.sharedCache.mode",
new ConfigurationService.Converter<SharedCacheMode>() {
@Override
public SharedCacheMode convert(Object value) {
if ( value == null ) {
return null;
}
if ( SharedCacheMode.class.isInstance( value ) ) {
return (SharedCacheMode) value;
}
return SharedCacheMode.valueOf( value.toString() );
}
},
SharedCacheMode.UNSPECIFIED
);
defaultCacheAccessType = configService.getSetting(
AvailableSettings.DEFAULT_CACHE_CONCURRENCY_STRATEGY,
new ConfigurationService.Converter<AccessType>() {
@Override
public AccessType convert(Object value) {
if ( value == null ) {
return null;
}
if ( CacheConcurrencyStrategy.class.isInstance( value ) ) {
return ( (CacheConcurrencyStrategy) value ).toAccessType();
}
if ( AccessType.class.isInstance( value ) ) {
return (AccessType) value;
}
return AccessType.fromExternalName( value.toString() );
}
},
// by default, see if the defined RegionFactory (if one) defines a default
serviceRegistry.getService( RegionFactory.class ) == null
? null
: serviceRegistry.getService( RegionFactory.class ).getDefaultAccessType()
);
specjProprietarySyntaxEnabled = configService.getSetting(
"hibernate.enable_specj_proprietary_syntax",
StandardConverters.BOOLEAN,
false
);
implicitNamingStrategy = strategySelector.resolveDefaultableStrategy(
ImplicitNamingStrategy.class,
configService.getSettings().get( AvailableSettings.IMPLICIT_NAMING_STRATEGY ),
ImplicitNamingStrategyLegacyJpaImpl.INSTANCE
);
physicalNamingStrategy = strategySelector.resolveDefaultableStrategy(
PhysicalNamingStrategy.class,
configService.getSettings().get( AvailableSettings.PHYSICAL_NAMING_STRATEGY ),
PhysicalNamingStrategyStandardImpl.INSTANCE
);
sourceProcessOrdering = resolveInitialSourceProcessOrdering( configService );
final boolean useNewIdentifierGenerators = configService.getSetting(
AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS,
StandardConverters.BOOLEAN,
false
);
if ( useNewIdentifierGenerators ) {
idGenerationTypeInterpreter.disableLegacyFallback();
}
else {
idGenerationTypeInterpreter.enableLegacyFallback();
}
reflectionManager = generateDefaultReflectionManager();
}
private ArrayList<MetadataSourceType> resolveInitialSourceProcessOrdering(ConfigurationService configService) {
final ArrayList<MetadataSourceType> initialSelections = new ArrayList<MetadataSourceType>();
final String sourceProcessOrderingSetting = configService.getSetting(
AvailableSettings.ARTIFACT_PROCESSING_ORDER,
StandardConverters.STRING
);
if ( sourceProcessOrderingSetting != null ) {
final String[] orderChoices = StringHelper.split( ",; ", sourceProcessOrderingSetting, false );
initialSelections.addAll( CollectionHelper.<MetadataSourceType>arrayList( orderChoices.length ) );
for ( String orderChoice : orderChoices ) {
initialSelections.add( MetadataSourceType.parsePrecedence( orderChoice ) );
}
}
if ( initialSelections.isEmpty() ) {
initialSelections.add( MetadataSourceType.HBM );
initialSelections.add( MetadataSourceType.CLASS );
}
return initialSelections;
}
private ReflectionManager generateDefaultReflectionManager() {
final JavaReflectionManager reflectionManager = new JavaReflectionManager();
reflectionManager.setMetadataProvider( new JPAMetadataProvider() );
reflectionManager.injectClassLoaderDelegate( getHcannClassLoaderDelegate() );
return reflectionManager;
}
public ClassLoaderDelegate getHcannClassLoaderDelegate() {
if ( hcannClassLoaderDelegate == null ) {
hcannClassLoaderDelegate = new ClassLoaderDelegate() {
private final ClassLoaderService classLoaderService = getServiceRegistry().getService( ClassLoaderService.class );
@Override
public <T> Class<T> classForName(String className) throws ClassLoadingException {
try {
return classLoaderService.classForName( className );
}
catch (org.hibernate.boot.registry.classloading.spi.ClassLoadingException e) {
return StandardClassLoaderDelegateImpl.INSTANCE.classForName( className );
}
}
};
}
return hcannClassLoaderDelegate;
}
@Override
public StandardServiceRegistry getServiceRegistry() {
return serviceRegistry;
}
@Override
public MappingDefaults getMappingDefaults() {
return mappingDefaults;
}
@Override
public List<BasicType> getBasicTypeRegistrations() {
return basicTypeRegistrations;
}
@Override
public IndexView getJandexView() {
return jandexView;
}
@Override
public ScanOptions getScanOptions() {
return scanOptions;
}
@Override
public ScanEnvironment getScanEnvironment() {
return scanEnvironment;
}
@Override
public Object getScanner() {
return scannerSetting;
}
@Override
public ArchiveDescriptorFactory getArchiveDescriptorFactory() {
return archiveDescriptorFactory;
}
@Override
public ClassLoader getTempClassLoader() {
return tempClassLoader;
}
@Override
public ImplicitNamingStrategy getImplicitNamingStrategy() {
return implicitNamingStrategy;
}
@Override
public PhysicalNamingStrategy getPhysicalNamingStrategy() {
return physicalNamingStrategy;
}
@Override
public ReflectionManager getReflectionManager() {
return reflectionManager;
}
@Override
public SharedCacheMode getSharedCacheMode() {
return sharedCacheMode;
}
@Override
public AccessType getImplicitCacheAccessType() {
return defaultCacheAccessType;
}
@Override
public MultiTenancyStrategy getMultiTenancyStrategy() {
return multiTenancyStrategy;
}
@Override
public IdGeneratorStrategyInterpreter getIdGenerationTypeInterpreter() {
return idGenerationTypeInterpreter;
}
@Override
public List<CacheRegionDefinition> getCacheRegionDefinitions() {
return cacheRegionDefinitions;
}
@Override
public boolean ignoreExplicitDiscriminatorsForJoinedInheritance() {
return !explicitDiscriminatorsForJoinedInheritanceSupported;
}
@Override
public boolean createImplicitDiscriminatorsForJoinedInheritance() {
return implicitDiscriminatorsForJoinedInheritanceSupported;
}
@Override
public boolean shouldImplicitlyForceDiscriminatorInSelect() {
return implicitlyForceDiscriminatorInSelect;
}
@Override
public boolean useNationalizedCharacterData() {
return useNationalizedCharacterData;
}
@Override
public boolean isSpecjProprietarySyntaxEnabled() {
return specjProprietarySyntaxEnabled;
}
@Override
public List<MetadataSourceType> getSourceProcessOrdering() {
return sourceProcessOrdering;
}
@Override
public Map<String, SQLFunction> getSqlFunctions() {
return sqlFunctionMap == null ? Collections.<String, SQLFunction>emptyMap() : sqlFunctionMap;
}
@Override
public List<AuxiliaryDatabaseObject> getAuxiliaryDatabaseObjectList() {
return auxiliaryDatabaseObjectList == null
? Collections.<AuxiliaryDatabaseObject>emptyList()
: auxiliaryDatabaseObjectList;
}
@Override
public List<AttributeConverterDefinition> getAttributeConverters() {
return attributeConverterDefinitionsByClass == null
? Collections.<AttributeConverterDefinition>emptyList()
: new ArrayList<AttributeConverterDefinition>( attributeConverterDefinitionsByClass.values() );
}
public void addAttributeConverterDefinition(AttributeConverterDefinition definition) {
if ( this.attributeConverterDefinitionsByClass == null ) {
this.attributeConverterDefinitionsByClass = new HashMap<Class, AttributeConverterDefinition>();
}
final Object old = this.attributeConverterDefinitionsByClass.put( definition.getAttributeConverter().getClass(), definition );
if ( old != null ) {
throw new AssertionFailure(
String.format(
"AttributeConverter class [%s] registered multiple times",
definition.getAttributeConverter().getClass()
)
);
}
}
public static interface JpaOrmXmlPersistenceUnitDefaults {
public String getDefaultSchemaName();
public String getDefaultCatalogName();
public boolean shouldImplicitlyQuoteIdentifiers();
}
/**
* Yuck. This is needed because JPA lets users define "global building options"
* in {@code orm.xml} mappings. Forget that there are generally multiple
* {@code orm.xml} mappings if using XML approach... Ugh
*/
public void apply(JpaOrmXmlPersistenceUnitDefaults jpaOrmXmlPersistenceUnitDefaults) {
if ( !mappingDefaults.shouldImplicitlyQuoteIdentifiers() ) {
mappingDefaults.implicitlyQuoteIdentifiers = jpaOrmXmlPersistenceUnitDefaults.shouldImplicitlyQuoteIdentifiers();
}
if ( mappingDefaults.getImplicitCatalogName() == null ) {
mappingDefaults.implicitCatalogName = StringHelper.nullIfEmpty(
jpaOrmXmlPersistenceUnitDefaults.getDefaultCatalogName()
);
}
if ( mappingDefaults.getImplicitSchemaName() == null ) {
mappingDefaults.implicitSchemaName = StringHelper.nullIfEmpty(
jpaOrmXmlPersistenceUnitDefaults.getDefaultSchemaName()
);
}
}
// @Override
// public PersistentAttributeMemberResolver getPersistentAttributeMemberResolver() {
// return persistentAttributeMemberResolver;
// }
}
}