Skip to content

Commit

Permalink
Reworked the plugin to create injection points for each defined Persi…
Browse files Browse the repository at this point in the history
…stenceUnit found in the config file
  • Loading branch information
Eddie Carpenter committed Jun 21, 2024
1 parent 95073d9 commit baca9e8
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 184 deletions.
1 change: 1 addition & 0 deletions jpalite-quarkus-extension/deployment/lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lombok.log.fieldName=LOG
7 changes: 6 additions & 1 deletion jpalite-quarkus-extension/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
~ limitations under the License.
-->

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
Expand Down Expand Up @@ -59,6 +60,10 @@
<artifactId>jpalite-extension</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,38 @@

package io.jpalite.extension.deployment;

import io.jpalite.agroal.AgroalDataSourceProvider;
import io.jpalite.extension.PropertyPersistenceUnitProvider;
import io.jpalite.*;
import io.jpalite.extension.JPALiteConfigMapping;
import io.jpalite.extension.JPALiteRecorder;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Default;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.transaction.TransactionManager;
import jakarta.transaction.TransactionSynchronizationRegistry;
import org.jboss.jandex.ClassType;
import org.jboss.jandex.DotName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import static io.quarkus.deployment.annotations.ExecutionTime.RUNTIME_INIT;

class JPALiteExtensionProcessor
{
private static final DotName ENTITY_MANAGER_FACTORY = DotName.createSimple("jakarta.persistence.EntityManagerFactory");
public static final DotName ENTITY_MANAGER = DotName.createSimple("jakarta.persistence.EntityManager");
private static final Logger LOG = LoggerFactory.getLogger(JPALiteExtensionProcessor.class);

private static final String DEFAULT_NAME = "<default>";

private static final String FEATURE = "jpalite-extension";

Expand All @@ -46,8 +69,102 @@ NativeImageResourceBuildItem nativeImageResourceBuildItem()
@BuildStep
ReflectiveClassBuildItem reflection()
{
return ReflectiveClassBuildItem.builder(AgroalDataSourceProvider.class,
PropertyPersistenceUnitProvider.class)
return ReflectiveClassBuildItem.builder(DataSourceProvider.class,
PersistenceUnitProvider.class,
FieldConvertType.class,
EntityMetaDataManager.class)
.build();
}

@Record(RUNTIME_INIT)
@BuildStep
void generateJPABeans(JPALiteRecorder recorder,
JPALiteConfigMapping jpaConfigMapping,
BuildProducer<SyntheticBeanBuildItem> syntheticBeanBuildItemBuildProducer)
{
if (jpaConfigMapping.defaultPersistenceUnit() != null) {
//Define the default EntityManagerFactory producer
syntheticBeanBuildItemBuildProducer
.produce(createSyntheticBean(DEFAULT_NAME,
false,
EntityManagerFactory.class,
List.of(ENTITY_MANAGER_FACTORY)
, true)
.createWith(recorder.entityManagerFactorySupplier(DEFAULT_NAME))
.done());

//Define the default EntityManager producer
syntheticBeanBuildItemBuildProducer
.produce(createSyntheticBean(DEFAULT_NAME,
false,
EntityManager.class,
List.of(ENTITY_MANAGER)
, true)
.createWith(recorder.entityManagerSupplier(DEFAULT_NAME))
.addInjectionPoint(ClassType.create(DotName.createSimple(TransactionManager.class)))
.addInjectionPoint(ClassType.create(DotName.createSimple(TransactionSynchronizationRegistry.class)))
.done());
}//if


if (jpaConfigMapping.namedPersistenceUnits() != null) {
for (String unitName : jpaConfigMapping.namedPersistenceUnits().keySet()) {
//Define the named EntityManagerFactory producer
syntheticBeanBuildItemBuildProducer
.produce(createSyntheticBean(unitName,
true,
EntityManagerFactory.class,
List.of(ENTITY_MANAGER_FACTORY)
, false)
.createWith(recorder.entityManagerFactorySupplier(unitName))
.done());

//Define the named EntityManager producer
syntheticBeanBuildItemBuildProducer
.produce(createSyntheticBean(unitName,
true,
EntityManager.class,
List.of(ENTITY_MANAGER)
, false)
.createWith(recorder.entityManagerSupplier(unitName))
.addInjectionPoint(ClassType.create(DotName.createSimple(TransactionManager.class)))
.addInjectionPoint(ClassType.create(DotName.createSimple(TransactionSynchronizationRegistry.class)))
.done());

}
}//if
}

private static <T> SyntheticBeanBuildItem.ExtendedBeanConfigurator createSyntheticBean(String persistenceUnitName,
boolean isNamedPersistenceUnit,
Class<T> type,
List<DotName> allExposedTypes,
boolean defaultBean)
{
LOG.info("Creating Synthetic Bean for {}(\"{}\")", type.getSimpleName(), persistenceUnitName);
SyntheticBeanBuildItem.ExtendedBeanConfigurator configurator = SyntheticBeanBuildItem
.configure(type)
.scope(ApplicationScoped.class)
.unremovable()
.setRuntimeInit();

for (DotName exposedType : allExposedTypes) {
configurator.addType(exposedType);
}

if (defaultBean) {
configurator.defaultBean();
}

if (isNamedPersistenceUnit) {
// configurator.addQualifier(Default.class);
configurator.addQualifier().annotation(PersistenceUnit.class).addValue("value", persistenceUnitName).done();
}
else {
configurator.addQualifier(Default.class);
configurator.addQualifier().annotation(PersistenceUnit.class).done();
}

return configurator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@


@SuppressWarnings("ClassExplicitlyAnnotation")
class PersistenceUnitLiteral extends AnnotationLiteral<PersistenceUnit> implements PersistenceUnit
final class PersistenceUnitLiteral extends AnnotationLiteral<PersistenceUnit> implements PersistenceUnit
{

private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.Map;

@ConfigMapping(prefix = "jpalite.persistenceUnit")
@ConfigRoot(phase = ConfigPhase.RUN_TIME)
@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public interface JPALiteConfigMapping
{
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.jpalite.extension;

import io.quarkus.arc.SyntheticCreationalContext;
import io.quarkus.runtime.annotations.Recorder;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import jakarta.transaction.TransactionManager;
import jakarta.transaction.TransactionSynchronizationRegistry;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

@Slf4j
@Recorder
public class JPALiteRecorder
{
private final Map<String, EntityManagerFactory> entityManagerFactoryList = new ConcurrentHashMap<>();

@Inject
TransactionManager transactionManager;

Check warning on line 41 in jpalite-quarkus-extension/runtime/src/main/java/io/jpalite/extension/JPALiteRecorder.java

View workflow job for this annotation

GitHub Actions / qodana

Injection point with ambiguous dependencies

Ambiguous dependency: there are multiple beans that match the injection point

@Inject
TransactionSynchronizationRegistry transactionSynchronizationRegistry;

Check warning on line 44 in jpalite-quarkus-extension/runtime/src/main/java/io/jpalite/extension/JPALiteRecorder.java

View workflow job for this annotation

GitHub Actions / qodana

Injection point with ambiguous dependencies

Ambiguous dependency: there are multiple beans that match the injection point

public Function<SyntheticCreationalContext<EntityManagerFactory>, EntityManagerFactory> entityManagerFactorySupplier(String persistenceUnitName)
{
return context -> entityManagerFactoryList.computeIfAbsent(persistenceUnitName, Persistence::createEntityManagerFactory);
}

public EntityManager getEntityManager(String persistenceUnit)
{
EntityManagerFactory factory = entityManagerFactoryList.computeIfAbsent(persistenceUnit, Persistence::createEntityManagerFactory);
return new TransactionScopedEntityManagerImpl(factory,
transactionManager,
transactionSynchronizationRegistry);
}//getEntityManager

public Function<SyntheticCreationalContext<EntityManager>, EntityManager> entityManagerSupplier(String persistenceUnitName)
{
return context -> {
TransactionManager transactionManager = context.getInjectedReference(TransactionManager.class);
TransactionSynchronizationRegistry transactionSynchronizationRegistry = context.getInjectedReference(TransactionSynchronizationRegistry.class);
EntityManagerFactory factory = entityManagerFactoryList.computeIfAbsent(persistenceUnitName, Persistence::createEntityManagerFactory);
return new TransactionScopedEntityManagerImpl(factory,
transactionManager,
transactionSynchronizationRegistry);
};
}
}
Loading

0 comments on commit baca9e8

Please sign in to comment.