}
public Configuration buildHibernateConfiguration(ServiceRegistry serviceRegistry) {
Properties props = new Properties();
props.putAll( configurationValues );
Configuration cfg = new Configuration().setProperties( props );
cfg.setEntityNotFoundDelegate( jpaEntityNotFoundDelegate );
if ( namingStrategy != null ) {
cfg.setNamingStrategy( namingStrategy );
}
if ( sessionFactoryInterceptor != null ) {
cfg.setInterceptor( sessionFactoryInterceptor );
}
final Object strategyProviderValue = props.get( AvailableSettings.IDENTIFIER_GENERATOR_STRATEGY_PROVIDER );
final IdentifierGeneratorStrategyProvider strategyProvider = strategyProviderValue == null
? null
: serviceRegistry.getService( StrategySelector.class )
.resolveStrategy( IdentifierGeneratorStrategyProvider.class, strategyProviderValue );
if ( strategyProvider != null ) {
final MutableIdentifierGeneratorFactory identifierGeneratorFactory = cfg.getIdentifierGeneratorFactory();
for ( Map.Entry<String,Class<?>> entry : strategyProvider.getStrategies().entrySet() ) {
identifierGeneratorFactory.register( entry.getKey(), entry.getValue() );
}
}
if ( grantedJaccPermissions != null ) {
final JaccService jaccService = serviceRegistry.getService( JaccService.class );
for ( GrantedPermission grantedPermission : grantedJaccPermissions ) {
jaccService.addPermission( grantedPermission );
}
}
if ( cacheRegionDefinitions != null ) {
for ( CacheRegionDefinition cacheRegionDefinition : cacheRegionDefinitions ) {
if ( cacheRegionDefinition.cacheType == CacheRegionDefinition.CacheType.ENTITY ) {
cfg.setCacheConcurrencyStrategy(
cacheRegionDefinition.role,
cacheRegionDefinition.usage,
cacheRegionDefinition.region,
cacheRegionDefinition.cacheLazy
);
}
else {
cfg.setCollectionCacheConcurrencyStrategy(
cacheRegionDefinition.role,
cacheRegionDefinition.usage,
cacheRegionDefinition.region
);
}
}
}
// todo : need to have this use the metamodel codebase eventually...
for ( JaxbHibernateConfiguration.JaxbSessionFactory.JaxbMapping jaxbMapping : cfgXmlNamedMappings ) {
if ( jaxbMapping.getClazz() != null ) {
cfg.addAnnotatedClass(
serviceRegistry.getService( ClassLoaderService.class ).classForName( jaxbMapping.getClazz() )
);
}
else if ( jaxbMapping.getResource() != null ) {
cfg.addResource( jaxbMapping.getResource() );
}
else if ( jaxbMapping.getJar() != null ) {
cfg.addJar( new File( jaxbMapping.getJar() ) );
}
else if ( jaxbMapping.getPackage() != null ) {
cfg.addPackage( jaxbMapping.getPackage() );
}
}
List<Class> loadedAnnotatedClasses = (List<Class>) configurationValues.remove( AvailableSettings.LOADED_CLASSES );
if ( loadedAnnotatedClasses != null ) {
for ( Class cls : loadedAnnotatedClasses ) {
cfg.addAnnotatedClass( cls );
}
}
for ( String className : metadataSources.getAnnotatedMappingClassNames() ) {
cfg.addAnnotatedClass( serviceRegistry.getService( ClassLoaderService.class ).classForName( className ) );
}
for ( MetadataSources.ConverterDescriptor converterDescriptor : metadataSources.getConverterDescriptors() ) {
final Class<? extends AttributeConverter> converterClass;
try {
Class theClass = serviceRegistry.getService( ClassLoaderService.class ).classForName( converterDescriptor.converterClassName );
converterClass = (Class<? extends AttributeConverter>) theClass;
}
catch (ClassCastException e) {
throw persistenceException(
String.format(
"AttributeConverter implementation [%s] does not implement AttributeConverter interface",
converterDescriptor.converterClassName
)
);
}
cfg.addAttributeConverter( converterClass, converterDescriptor.autoApply );
}
for ( String resourceName : metadataSources.mappingFileResources ) {
Boolean useMetaInf = null;
try {
if ( resourceName.endsWith( META_INF_ORM_XML ) ) {
useMetaInf = true;
}
cfg.addResource( resourceName );
}
catch( MappingNotFoundException e ) {
if ( ! resourceName.endsWith( META_INF_ORM_XML ) ) {
throw persistenceException( "Unable to find XML mapping file in classpath: " + resourceName );
}
else {
useMetaInf = false;
//swallow it, the META-INF/orm.xml is optional
}
}
catch( MappingException me ) {
throw persistenceException( "Error while reading JPA XML file: " + resourceName, me );
}
if ( Boolean.TRUE.equals( useMetaInf ) ) {
LOG.exceptionHeaderFound( getExceptionHeader(), META_INF_ORM_XML );
}
else if (Boolean.FALSE.equals(useMetaInf)) {
LOG.exceptionHeaderNotFound( getExceptionHeader(), META_INF_ORM_XML );
}
}
for ( NamedInputStream namedInputStream : metadataSources.namedMappingFileInputStreams ) {
try {
//addInputStream has the responsibility to close the stream
cfg.addInputStream( new BufferedInputStream( namedInputStream.getStream() ) );
}
catch ( InvalidMappingException e ) {
// try our best to give the file name
if ( StringHelper.isNotEmpty( namedInputStream.getName() ) ) {
throw new InvalidMappingException(
"Error while parsing file: " + namedInputStream.getName(),
e.getType(),
e.getPath(),
e
);
}
else {
throw e;
}
}
catch (MappingException me) {
// try our best to give the file name
if ( StringHelper.isNotEmpty( namedInputStream.getName() ) ) {
throw new MappingException("Error while parsing file: " + namedInputStream.getName(), me );
}
else {
throw me;
}
}
}
for ( String packageName : metadataSources.packageNames ) {
cfg.addPackage( packageName );
}
final TypeContributorList typeContributorList
= (TypeContributorList) configurationValues.get( TYPE_CONTRIBUTORS );
if ( typeContributorList != null ) {
configurationValues.remove( TYPE_CONTRIBUTORS );
for ( TypeContributor typeContributor : typeContributorList.getTypeContributors() ) {
cfg.registerTypeContributor( typeContributor );
}
}
return cfg;
}