Skip to content

Commit

Permalink
Merge pull request #7208 from gastaldi/integrator
Browse files Browse the repository at this point in the history
Fixed support for Integrators and ServiceContributors in Hibernate ORM
  • Loading branch information
Sanne authored Feb 17, 2020
2 parents d75e85a + 6261ea2 commit 6eb9db1
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ public final class HibernateOrmProcessor {
private static final DotName PERSISTENCE_UNIT = DotName.createSimple(PersistenceUnit.class.getName());
private static final DotName PRODUCES = DotName.createSimple(Produces.class.getName());

private static final String INTEGRATOR_SERVICE_FILE = "META-INF/services/org.hibernate.integrator.spi.Integrator";
private static final String SERVICE_CONTRIBUTOR_SERVICE_FILE = "META-INF/services/org.hibernate.service.spi.ServiceContributor";

/**
* Hibernate ORM configuration
*/
Expand All @@ -125,6 +128,9 @@ public SystemPropertyBuildItem enforceDisableRuntimeEnhancer() {
List<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles(LaunchModeBuildItem launchMode) {
List<HotDeploymentWatchedFileBuildItem> watchedFiles = new ArrayList<>();
watchedFiles.add(new HotDeploymentWatchedFileBuildItem("META-INF/persistence.xml"));
watchedFiles.add(new HotDeploymentWatchedFileBuildItem(INTEGRATOR_SERVICE_FILE));
watchedFiles.add(new HotDeploymentWatchedFileBuildItem(SERVICE_CONTRIBUTOR_SERVICE_FILE));

getSqlLoadScript(launchMode.getLaunchMode()).ifPresent(script -> {
watchedFiles.add(new HotDeploymentWatchedFileBuildItem(script));
});
Expand Down Expand Up @@ -222,17 +228,16 @@ public void build(RecorderContext recorderContext, HibernateOrmRecorder recorder
recorderContext.registerNonDefaultConstructor(ParsedPersistenceXmlDescriptor.class.getDeclaredConstructor(URL.class),
(i) -> Collections.singletonList(i.getPersistenceUnitRootUrl()));

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// inspect service files for additional integrators
Collection<Class<? extends Integrator>> integratorClasses = new LinkedHashSet<>();
for (String integratorClassName : ServiceUtil.classNamesNamedIn(getClass().getClassLoader(),
"META-INF/services/org.hibernate.integrator.spi.Integrator")) {
for (String integratorClassName : ServiceUtil.classNamesNamedIn(classLoader, INTEGRATOR_SERVICE_FILE)) {
integratorClasses.add((Class<? extends Integrator>) recorderContext.classProxy(integratorClassName));
}

// inspect service files for service contributors
Collection<Class<? extends ServiceContributor>> serviceContributorClasses = new LinkedHashSet<>();
for (String serviceContributorClassName : ServiceUtil.classNamesNamedIn(getClass().getClassLoader(),
"META-INF/services/org.hibernate.service.spi.ServiceContributor")) {
for (String serviceContributorClassName : ServiceUtil.classNamesNamedIn(classLoader,
SERVICE_CONTRIBUTOR_SERVICE_FILE)) {
serviceContributorClasses
.add((Class<? extends ServiceContributor>) recorderContext.classProxy(serviceContributorClassName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.BeforeDestroyed;
import javax.enterprise.event.Observes;
import javax.inject.Singleton;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
Expand Down Expand Up @@ -74,7 +75,7 @@ boolean isJtaEnabled() {
*
* @param event ignored
*/
void destroy(@BeforeDestroyed(ApplicationScoped.class) Object event) {
void destroy(@Observes @BeforeDestroyed(ApplicationScoped.class) Object event) {
for (LazyPersistenceUnit factory : persistenceUnits.values()) {
try {
factory.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.it.jpa.integrator;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/integrator")
@ApplicationScoped
public class IntegratorResource {

@Inject
EntityManager em;

@GET
@Produces(MediaType.TEXT_PLAIN)
public int create() {
return TestIntegrator.COUNTER.get();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.it.jpa.integrator;

import java.util.concurrent.atomic.AtomicInteger;

import org.hibernate.boot.Metadata;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.service.spi.SessionFactoryServiceRegistry;

public class TestIntegrator implements Integrator {
public static final AtomicInteger COUNTER = new AtomicInteger();

@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
COUNTER.incrementAndGet();
}

@Override
public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
COUNTER.decrementAndGet();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.it.jpa.integrator.TestIntegrator
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.it.jpa.integrator;

import static io.restassured.RestAssured.when;
import static org.hamcrest.core.Is.is;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class JPAIntegratorTest {

@Test
public void testInjection() {
when().get("/jpa-test/integrator").then()
.statusCode(200)
.body(is("1"));
}

}

0 comments on commit 6eb9db1

Please sign in to comment.