Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed support for Integrators and ServiceContributors in Hibernate ORM #7208

Merged
merged 3 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}

}