HHH-15949 - Make MetadataBuilderContributor discoverable

This commit is contained in:
Steve Ebersole 2023-01-20 17:45:11 -06:00
parent b5022f94d3
commit 35f0c57f07
5 changed files with 57 additions and 29 deletions

View File

@ -21,6 +21,8 @@ import org.hibernate.service.ServiceRegistry;
* {@link org.hibernate.cfg.Configuration#registerTypeContributor(TypeContributor)}
* or even {@link org.hibernate.boot.MetadataBuilder#applyTypes(TypeContributor)}.
* <li>
* When bootstrapping Hibernate via JPA or {@link org.hibernate.cfg.Configuration},
*
* Finally, in the JPA boostrap process, {@code TypeContributor}s may be
* listed via {@link org.hibernate.jpa.boot.spi.JpaSettings#TYPE_CONTRIBUTORS}.
* </ul>

View File

@ -7,24 +7,27 @@
package org.hibernate.boot.spi;
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.model.FunctionContributor;
import org.hibernate.boot.model.TypeContributor;
/**
* A bootstrap process hook for contributing settings to {@link MetadataBuilder}.
* <p>
* In implementation may be registered with the JPA provider using the property
* {@value org.hibernate.jpa.boot.spi.JpaSettings#METADATA_BUILDER_CONTRIBUTOR}.
*
* @apiNote Currently, this API is only supported in the JPA persistence provider.
* It's unfortunate that implementations are not discoverable like
* {@link org.hibernate.boot.model.TypeContributor} and
* {@link org.hibernate.boot.model.FunctionContributor}.
* <p/>
* Generally this is used from JPA bootstrapping where {@link MetadataBuilder} is not accessible.
* <p/>
* Implementations can be {@linkplain java.util.ServiceLoader discovered}. For historical reasons,
* an implementation can also be named using the
* {@value org.hibernate.jpa.boot.spi.JpaSettings#METADATA_BUILDER_CONTRIBUTOR} setting, though
* discovery should be preferred.
*
* @author Vlad Mihalcea
*
* @since 5.3
*
* @deprecated Use
* @deprecated Use settings, {@link TypeContributor}, {@link FunctionContributor} or
* {@link AdditionalMappingContributor} instead depending on need
*/
@Deprecated(forRemoval = true)
public interface MetadataBuilderContributor {
/**
* Perform the process of contributing to the {@link MetadataBuilder}.

View File

@ -32,6 +32,7 @@ import org.hibernate.boot.beanvalidation.BeanValidationIntegrator;
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.TypeContributor;
import org.hibernate.boot.model.convert.internal.ClassBasedConverterDescriptor;
import org.hibernate.boot.model.convert.spi.ConverterDescriptor;
import org.hibernate.boot.model.process.spi.ManagedResources;
@ -357,22 +358,23 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil
}
private void applyMetadataBuilderContributor() {
final Object metadataBuilderContributorSetting = configurationValues.get( METADATA_BUILDER_CONTRIBUTOR );
if ( metadataBuilderContributorSetting != null ) {
final MetadataBuilderContributor metadataBuilderContributor = loadSettingInstance(
METADATA_BUILDER_CONTRIBUTOR,
metadataBuilderContributorSetting,
MetadataBuilderContributor.class
);
Object metadataBuilderContributorSetting = configurationValues.get( METADATA_BUILDER_CONTRIBUTOR );
if ( metadataBuilderContributorSetting == null ) {
return;
if ( metadataBuilderContributor != null ) {
metadataBuilderContributor.contribute( metamodelBuilder );
}
}
MetadataBuilderContributor metadataBuilderContributor = loadSettingInstance(
METADATA_BUILDER_CONTRIBUTOR,
metadataBuilderContributorSetting,
MetadataBuilderContributor.class
);
if ( metadataBuilderContributor != null ) {
metadataBuilderContributor.contribute( metamodelBuilder );
}
final StandardServiceRegistry serviceRegistry = metamodelBuilder.getBootstrapContext().getServiceRegistry();
final ClassLoaderService cls = serviceRegistry.getService( ClassLoaderService.class );
final Collection<MetadataBuilderContributor> contributors = cls.loadJavaServices( MetadataBuilderContributor.class );
contributors.forEach( (contributor) -> contributor.contribute( metamodelBuilder ) );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -1328,18 +1330,28 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil
mergedSettings.cacheRegionDefinitions.forEach( metamodelBuilder::applyCacheRegionDefinition );
}
final TypeContributorList typeContributorList = (TypeContributorList) configurationValues.remove(
TYPE_CONTRIBUTORS
);
if ( typeContributorList != null ) {
typeContributorList.getTypeContributors().forEach( metamodelBuilder::applyTypes );
}
applyTypeContributors();
if ( converterDescriptors != null ) {
converterDescriptors.forEach( metamodelBuilder::applyAttributeConverter );
}
}
private void applyTypeContributors() {
final TypeContributorList typeContributorList = (TypeContributorList) configurationValues.remove(
TYPE_CONTRIBUTORS
);
if ( typeContributorList != null ) {
typeContributorList.getTypeContributors().forEach( metamodelBuilder::applyTypes );
}
final StandardServiceRegistry serviceRegistry = metamodelBuilder.getBootstrapContext().getServiceRegistry();
final ClassLoaderService cls = serviceRegistry.getService( ClassLoaderService.class );
final Collection<TypeContributor> typeContributors = cls.loadJavaServices( TypeContributor.class );
typeContributors.forEach( metamodelBuilder::applyTypes );
}
// Phase 2 concerns ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.jpa.boot.spi;
import org.hibernate.boot.model.TypeContributor;
import org.hibernate.boot.spi.MetadataBuilderContributor;
/**
@ -30,12 +31,19 @@ public class JpaSettings {
/**
* Names a {@link TypeContributorList}
*
* @deprecated Consider using {@linkplain java.util.ServiceLoader discovery} instead to
* dynamically locate {@linkplain TypeContributor contributors}.
*/
@Deprecated(forRemoval = true)
public static final String TYPE_CONTRIBUTORS = "hibernate.type_contributors";
/**
* Names a {@link MetadataBuilderContributor}
*
* @deprecated Use {@linkplain java.util.ServiceLoader discovery} instead.
*/
@Deprecated(forRemoval = true)
public static final String METADATA_BUILDER_CONTRIBUTOR = "hibernate.metadata_builder_contributor";
}

View File

@ -16,9 +16,12 @@ import org.hibernate.boot.model.TypeContributor;
* An implementation may be registered with the JPA provider using the property
* {@value org.hibernate.jpa.boot.spi.JpaSettings#TYPE_CONTRIBUTORS}.
*
* @deprecated Consider using {@linkplain java.util.ServiceLoader discovery} instead to
* dynamically locate {@linkplain TypeContributor contributors}.
*
* @author Brett Meyer
*/
// TODO: Not a fan of this name or entry point into EMFBuilderImpl
@Deprecated(forRemoval = true)
public interface TypeContributorList {
List<TypeContributor> getTypeContributors();
}