Skip to content

Commit

Permalink
Propagate the "configuration name" of persistence unit throughout the…
Browse files Browse the repository at this point in the history
… build

We'll need these at runtime in the next few commits.
  • Loading branch information
yrodiere committed Aug 4, 2022
1 parent a2c90f8 commit 0bc9711
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,10 @@ public void configurationDescriptorBuilding(

// First produce the PUs having a persistence.xml: these are not reactive, as we don't allow using a persistence.xml for them.
for (PersistenceXmlDescriptorBuildItem persistenceXmlDescriptorBuildItem : persistenceXmlDescriptors) {
ParsedPersistenceXmlDescriptor xmlDescriptor = persistenceXmlDescriptorBuildItem.getDescriptor();
persistenceUnitDescriptors
.produce(new PersistenceUnitDescriptorBuildItem(persistenceXmlDescriptorBuildItem.getDescriptor(),
.produce(new PersistenceUnitDescriptorBuildItem(xmlDescriptor,
xmlDescriptor.getName(),
Optional.of(DataSourceUtil.DEFAULT_DATASOURCE_NAME),
getMultiTenancyStrategy(Optional.ofNullable(persistenceXmlDescriptorBuildItem.getDescriptor()
.getProperties().getProperty(AvailableSettings.MULTI_TENANT))),
Expand Down Expand Up @@ -1165,7 +1167,8 @@ private static void producePersistenceUnitDescriptorFromConfig(
String.valueOf(persistenceUnitConfig.discriminator.ignoreExplicitForJoined));

persistenceUnitDescriptors.produce(
new PersistenceUnitDescriptorBuildItem(descriptor, jdbcDataSource.map(JdbcDataSourceBuildItem::getName),
new PersistenceUnitDescriptorBuildItem(descriptor, descriptor.getName(),
jdbcDataSource.map(JdbcDataSourceBuildItem::getName),
multiTenancyStrategy,
persistenceUnitConfig.multitenantSchemaDatasource.orElse(null),
xmlMappings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
public final class PersistenceUnitDescriptorBuildItem extends MultiBuildItem {

private final ParsedPersistenceXmlDescriptor descriptor;

// The default PU in Hibernate Reactive is named "default-reactive" instead of "<default>",
// but everything related to configuration (e.g. getAllPersistenceUnitConfigsAsMap() still
// use the name "<default>", so we need to convert between those.
private final String configurationName;
private final Optional<String> dataSource;
private final MultiTenancyStrategy multiTenancyStrategy;
private final String multiTenancySchemaDataSource;
Expand All @@ -31,20 +36,23 @@ public final class PersistenceUnitDescriptorBuildItem extends MultiBuildItem {
private final boolean isReactive;
private final boolean fromPersistenceXml;

public PersistenceUnitDescriptorBuildItem(ParsedPersistenceXmlDescriptor descriptor,
public PersistenceUnitDescriptorBuildItem(ParsedPersistenceXmlDescriptor descriptor, String configurationName,
List<RecordableXmlMapping> xmlMappings,
Map<String, String> quarkusConfigUnsupportedProperties,
boolean isReactive, boolean fromPersistenceXml) {
this(descriptor, Optional.of(DataSourceUtil.DEFAULT_DATASOURCE_NAME), MultiTenancyStrategy.NONE, null,
this(descriptor, configurationName,
Optional.of(DataSourceUtil.DEFAULT_DATASOURCE_NAME), MultiTenancyStrategy.NONE, null,
xmlMappings, quarkusConfigUnsupportedProperties, isReactive, fromPersistenceXml);
}

public PersistenceUnitDescriptorBuildItem(ParsedPersistenceXmlDescriptor descriptor, Optional<String> dataSource,
public PersistenceUnitDescriptorBuildItem(ParsedPersistenceXmlDescriptor descriptor, String configurationName,
Optional<String> dataSource,
MultiTenancyStrategy multiTenancyStrategy, String multiTenancySchemaDataSource,
List<RecordableXmlMapping> xmlMappings,
Map<String, String> quarkusConfigUnsupportedProperties,
boolean isReactive, boolean fromPersistenceXml) {
this.descriptor = descriptor;
this.configurationName = configurationName;
this.dataSource = dataSource;
this.multiTenancyStrategy = multiTenancyStrategy;
this.multiTenancySchemaDataSource = multiTenancySchemaDataSource;
Expand Down Expand Up @@ -84,7 +92,8 @@ public boolean hasXmlMappings() {

public QuarkusPersistenceUnitDefinition asOutputPersistenceUnitDefinition(
List<HibernateOrmIntegrationStaticDescriptor> integrationStaticDescriptors) {
return new QuarkusPersistenceUnitDefinition(descriptor, dataSource, multiTenancyStrategy, xmlMappings,
return new QuarkusPersistenceUnitDefinition(descriptor, configurationName, dataSource, multiTenancyStrategy,
xmlMappings,
quarkusConfigUnsupportedProperties, isReactive, fromPersistenceXml, integrationStaticDescriptors);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.quarkus.datasource.common.runtime.DataSourceUtil;
import io.quarkus.hibernate.orm.runtime.RuntimeSettings.Builder;
import io.quarkus.hibernate.orm.runtime.boot.FastBootEntityManagerFactoryBuilder;
import io.quarkus.hibernate.orm.runtime.boot.RuntimePersistenceUnitDescriptor;
import io.quarkus.hibernate.orm.runtime.boot.registry.PreconfiguredServiceRegistryBuilder;
import io.quarkus.hibernate.orm.runtime.integration.HibernateOrmIntegrationRuntimeDescriptor;
import io.quarkus.hibernate.orm.runtime.integration.HibernateOrmIntegrationRuntimeInitListener;
Expand Down Expand Up @@ -136,7 +137,7 @@ private EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String
verifyProperties(properties);

// These are pre-parsed during image generation:
final List<PersistenceUnitDescriptor> units = PersistenceUnitsHolder.getPersistenceUnitDescriptors();
final List<RuntimePersistenceUnitDescriptor> units = PersistenceUnitsHolder.getPersistenceUnitDescriptors();

log.debugf("Located %s persistence units; checking each", units.size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import org.hibernate.boot.archive.scan.spi.Scanner;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor;

import io.quarkus.hibernate.orm.runtime.boot.FastBootMetadataBuilder;
import io.quarkus.hibernate.orm.runtime.boot.QuarkusPersistenceUnitDefinition;
import io.quarkus.hibernate.orm.runtime.boot.RuntimePersistenceUnitDescriptor;
import io.quarkus.hibernate.orm.runtime.proxies.PreGeneratedProxies;
import io.quarkus.hibernate.orm.runtime.recording.RecordedState;

Expand All @@ -36,14 +36,14 @@ public final class PersistenceUnitsHolder {
static void initializeJpa(List<QuarkusPersistenceUnitDefinition> puDefinitions,
Scanner scanner, Collection<Class<? extends Integrator>> additionalIntegrators,
PreGeneratedProxies preGeneratedProxies) {
final List<PersistenceUnitDescriptor> units = convertPersistenceUnits(puDefinitions);
final List<RuntimePersistenceUnitDescriptor> units = convertPersistenceUnits(puDefinitions);
final Map<String, RecordedState> metadata = constructMetadataAdvance(puDefinitions, scanner, additionalIntegrators,
preGeneratedProxies);

persistenceUnits = new PersistenceUnits(units, metadata);
}

public static List<PersistenceUnitDescriptor> getPersistenceUnitDescriptors() {
public static List<RuntimePersistenceUnitDescriptor> getPersistenceUnitDescriptors() {
checkJPAInitialization();
return persistenceUnits.units;
}
Expand All @@ -57,7 +57,7 @@ public static RecordedState popRecordedState(String persistenceUnitName) {
return persistenceUnits.recordedStates.remove(key);
}

private static List<PersistenceUnitDescriptor> convertPersistenceUnits(
private static List<RuntimePersistenceUnitDescriptor> convertPersistenceUnits(
final List<QuarkusPersistenceUnitDefinition> parsedPersistenceXmlDescriptors) {
return parsedPersistenceXmlDescriptors.stream().map(QuarkusPersistenceUnitDefinition::getActualHibernateDescriptor)
.collect(Collectors.toList());
Expand Down Expand Up @@ -103,11 +103,12 @@ public static RecordedState createMetadata(QuarkusPersistenceUnitDefinition unit

private static class PersistenceUnits {

private final List<PersistenceUnitDescriptor> units;
private final List<RuntimePersistenceUnitDescriptor> units;

private final Map<String, RecordedState> recordedStates;

public PersistenceUnits(final List<PersistenceUnitDescriptor> units, final Map<String, RecordedState> recordedStates) {
public PersistenceUnits(final List<RuntimePersistenceUnitDescriptor> units,
final Map<String, RecordedState> recordedStates) {
this.units = units;
this.recordedStates = recordedStates;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
public final class QuarkusPersistenceUnitDefinition {

private final LightPersistenceXmlDescriptor actualHibernateDescriptor;
private final RuntimePersistenceUnitDescriptor actualHibernateDescriptor;
private final Optional<String> dataSource;
private final MultiTenancyStrategy multitenancyStrategy;
private final List<RecordableXmlMapping> xmlMappings;
Expand All @@ -27,14 +27,16 @@ public final class QuarkusPersistenceUnitDefinition {
private final List<HibernateOrmIntegrationStaticDescriptor> integrationStaticDescriptors;
private final Map<String, String> quarkusConfigUnsupportedProperties;

public QuarkusPersistenceUnitDefinition(PersistenceUnitDescriptor persistenceUnitDescriptor, Optional<String> dataSource,
public QuarkusPersistenceUnitDefinition(PersistenceUnitDescriptor persistenceUnitDescriptor,
String configurationName, Optional<String> dataSource,
MultiTenancyStrategy multitenancyStrategy, List<RecordableXmlMapping> xmlMappings,
Map<String, String> quarkusConfigUnsupportedProperties,
boolean isReactive, boolean fromPersistenceXml,
List<HibernateOrmIntegrationStaticDescriptor> integrationStaticDescriptors) {
Objects.requireNonNull(persistenceUnitDescriptor);
Objects.requireNonNull(multitenancyStrategy);
this.actualHibernateDescriptor = LightPersistenceXmlDescriptor.validateAndReadFrom(persistenceUnitDescriptor);
this.actualHibernateDescriptor = RuntimePersistenceUnitDescriptor.validateAndReadFrom(persistenceUnitDescriptor,
configurationName);
this.dataSource = dataSource;
this.multitenancyStrategy = multitenancyStrategy;
this.xmlMappings = xmlMappings;
Expand All @@ -45,7 +47,7 @@ public QuarkusPersistenceUnitDefinition(PersistenceUnitDescriptor persistenceUni
}

@RecordableConstructor
public QuarkusPersistenceUnitDefinition(LightPersistenceXmlDescriptor actualHibernateDescriptor,
public QuarkusPersistenceUnitDefinition(RuntimePersistenceUnitDescriptor actualHibernateDescriptor,
Optional<String> dataSource,
MultiTenancyStrategy multitenancyStrategy,
List<RecordableXmlMapping> xmlMappings,
Expand All @@ -66,7 +68,7 @@ public QuarkusPersistenceUnitDefinition(LightPersistenceXmlDescriptor actualHibe
this.integrationStaticDescriptors = integrationStaticDescriptors;
}

public PersistenceUnitDescriptor getActualHibernateDescriptor() {
public RuntimePersistenceUnitDescriptor getActualHibernateDescriptor() {
return actualHibernateDescriptor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

import io.quarkus.runtime.annotations.RecordableConstructor;

public final class LightPersistenceXmlDescriptor implements PersistenceUnitDescriptor {
public final class RuntimePersistenceUnitDescriptor implements PersistenceUnitDescriptor {

private final String name;
private final String configurationName;
private final String providerClassName;
private final boolean useQuotedIdentifiers;
private final PersistenceUnitTransactionType transactionType;
Expand All @@ -33,11 +34,13 @@ public final class LightPersistenceXmlDescriptor implements PersistenceUnitDescr
*/
@Deprecated
@RecordableConstructor
public LightPersistenceXmlDescriptor(String name, String providerClassName, boolean useQuotedIdentifiers,
public RuntimePersistenceUnitDescriptor(String name, String configurationName,
String providerClassName, boolean useQuotedIdentifiers,
PersistenceUnitTransactionType transactionType,
ValidationMode validationMode, SharedCacheMode sharedCacheMode, List<String> managedClassNames,
Properties properties) {
this.name = name;
this.configurationName = configurationName;
this.providerClassName = providerClassName;
this.useQuotedIdentifiers = useQuotedIdentifiers;
this.transactionType = transactionType;
Expand All @@ -52,14 +55,16 @@ public LightPersistenceXmlDescriptor(String name, String providerClassName, bool
* several options that Quarkus does not support are not set.
*
* @param toClone the descriptor to clone
* @param configurationName the name of this PU in Quarkus configuration
* @return a new instance of LightPersistenceXmlDescriptor
* @throws UnsupportedOperationException on unsupported configurations
*/
@SuppressWarnings("deprecated")
public static LightPersistenceXmlDescriptor validateAndReadFrom(PersistenceUnitDescriptor toClone) {
public static RuntimePersistenceUnitDescriptor validateAndReadFrom(PersistenceUnitDescriptor toClone,
String configurationName) {
Objects.requireNonNull(toClone);
verifyIgnoredFields(toClone);
return new LightPersistenceXmlDescriptor(toClone.getName(), toClone.getProviderClassName(),
return new RuntimePersistenceUnitDescriptor(toClone.getName(), configurationName, toClone.getProviderClassName(),
toClone.isUseQuotedIdentifiers(),
toClone.getTransactionType(), toClone.getValidationMode(), toClone.getSharedCacheMode(),
Collections.unmodifiableList(toClone.getManagedClassNames()), toClone.getProperties());
Expand All @@ -75,6 +80,10 @@ public String getName() {
return name;
}

public String getConfigurationName() {
return configurationName;
}

@Override
public String getProviderClassName() {
return providerClassName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public void buildReactivePersistenceUnit(
// - we don't support starting Hibernate Reactive from a persistence.xml
// - we don't support Hibernate Envers with Hibernate Reactive
persistenceUnitDescriptors.produce(new PersistenceUnitDescriptorBuildItem(reactivePU,
PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME,
jpaModel.getXmlMappings(reactivePU.getName()),
persistenceUnitConfig.unsupportedProperties,
true, false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.quarkus.hibernate.orm.runtime.PersistenceUnitsHolder;
import io.quarkus.hibernate.orm.runtime.RuntimeSettings;
import io.quarkus.hibernate.orm.runtime.RuntimeSettings.Builder;
import io.quarkus.hibernate.orm.runtime.boot.RuntimePersistenceUnitDescriptor;
import io.quarkus.hibernate.orm.runtime.integration.HibernateOrmIntegrationRuntimeDescriptor;
import io.quarkus.hibernate.orm.runtime.integration.HibernateOrmIntegrationRuntimeInitListener;
import io.quarkus.hibernate.orm.runtime.recording.PrevalidatedQuarkusMetadata;
Expand Down Expand Up @@ -77,7 +78,7 @@ public EntityManagerFactory createEntityManagerFactory(String emName, Map proper
properties = new HashMap<Object, Object>();
try {
// These are pre-parsed during image generation:
final List<PersistenceUnitDescriptor> units = PersistenceUnitsHolder.getPersistenceUnitDescriptors();
final List<RuntimePersistenceUnitDescriptor> units = PersistenceUnitsHolder.getPersistenceUnitDescriptors();

for (PersistenceUnitDescriptor unit : units) {
//if the provider is not set, don't use it as people might want to use Hibernate ORM
Expand Down Expand Up @@ -106,7 +107,7 @@ private EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String
verifyProperties(properties);

// These are pre-parsed during image generation:
final List<PersistenceUnitDescriptor> units = PersistenceUnitsHolder.getPersistenceUnitDescriptors();
final List<RuntimePersistenceUnitDescriptor> units = PersistenceUnitsHolder.getPersistenceUnitDescriptors();

log.debugf("Located %s persistence units; checking each", units.size());

Expand Down

0 comments on commit 0bc9711

Please sign in to comment.