-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow injection of EMF into CDI beans
- Loading branch information
1 parent
76f9126
commit 9a6fb2d
Showing
9 changed files
with
161 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
examples/strict/src/main/java/org/jboss/shamrock/example/jpa/JpaProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.jboss.shamrock.example.jpa; | ||
|
||
import javax.enterprise.inject.Produces; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceUnit; | ||
|
||
public class JpaProducer { | ||
|
||
@Produces | ||
@PersistenceUnit(unitName = "templatePU") | ||
EntityManager entityManager; | ||
} |
10 changes: 10 additions & 0 deletions
10
examples/strict/src/test/java/org/jboss/shamrock/example/test/JPABootstrapITCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.jboss.shamrock.example.test; | ||
|
||
import org.jboss.shamrock.junit.GraalTest; | ||
import org.junit.runner.RunWith; | ||
|
||
|
||
@RunWith(GraalTest.class) | ||
public class JPABootstrapITCase extends JPABootstrapTestCase { | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
examples/strict/src/test/java/org/jboss/shamrock/example/test/JPABootstrapTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.jboss.shamrock.example.test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.jboss.shamrock.example.testutils.URLTester; | ||
import org.jboss.shamrock.junit.GraalTest; | ||
import org.jboss.shamrock.junit.ShamrockTest; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
/** | ||
* Test reflection around JPA entities | ||
* | ||
* @author Emmanuel Bernard [email protected] | ||
*/ | ||
@RunWith(ShamrockTest.class) | ||
public class JPABootstrapTestCase { | ||
|
||
@Test | ||
public void testJpaBootstrap() throws Exception { | ||
assertEquals("OK", URLTester.relative("jpa/testbootstrap").invokeURL().asString()); | ||
} | ||
|
||
} |
82 changes: 0 additions & 82 deletions
82
examples/strict/src/test/java/org/jboss/shamrock/example/test/JPAStoreLoadTestCase.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
jpa/deployment/src/main/java/org/jboss/shamrock/jpa/cdi/HibernateCdiResourceProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package org.jboss.shamrock.jpa.cdi; | ||
|
||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.context.Dependent; | ||
import javax.enterprise.inject.Produces; | ||
import javax.inject.Inject; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.Persistence; | ||
import javax.persistence.PersistenceContext; | ||
import javax.persistence.PersistenceUnit; | ||
|
||
import org.jboss.jandex.AnnotationInstance; | ||
import org.jboss.jandex.AnnotationTarget; | ||
import org.jboss.jandex.DotName; | ||
import org.jboss.protean.gizmo.ClassCreator; | ||
import org.jboss.protean.gizmo.ClassOutput; | ||
import org.jboss.protean.gizmo.MethodCreator; | ||
import org.jboss.protean.gizmo.MethodDescriptor; | ||
import org.jboss.protean.gizmo.ResultHandle; | ||
import org.jboss.shamrock.deployment.ArchiveContext; | ||
import org.jboss.shamrock.deployment.BeanDeployment; | ||
import org.jboss.shamrock.deployment.ProcessorContext; | ||
import org.jboss.shamrock.deployment.ResourceProcessor; | ||
import org.jboss.shamrock.runtime.BeanContainer; | ||
|
||
public class HibernateCdiResourceProcessor implements ResourceProcessor { | ||
|
||
private static final DotName PERSISTENCE_CONTEXT = DotName.createSimple(PersistenceContext.class.getName()); | ||
private static final DotName PERSISTENCE_UNIT = DotName.createSimple(PersistenceUnit.class.getName()); | ||
private static final DotName PRODUCES = DotName.createSimple(Produces.class.getName()); | ||
|
||
@Inject | ||
private BeanDeployment beanDeployment; | ||
|
||
@Override | ||
public void process(ArchiveContext archiveContext, ProcessorContext processorContext) throws Exception { | ||
Set<String> knownUnitNames = new HashSet<>(); | ||
for (DotName nm : new DotName[]{PERSISTENCE_CONTEXT, PERSISTENCE_UNIT}) { | ||
for (AnnotationInstance anno : archiveContext.getCombinedIndex().getAnnotations(nm)) { | ||
if (anno.target().kind() == AnnotationTarget.Kind.METHOD) { | ||
if (anno.target().asMethod().hasAnnotation(PRODUCES)) { | ||
knownUnitNames.add(anno.value("unitName").asString()); | ||
} | ||
} else if (anno.target().kind() == AnnotationTarget.Kind.FIELD) { | ||
for (AnnotationInstance i : anno.target().asField().annotations()) { | ||
if (i.name().equals(PRODUCES)) { | ||
knownUnitNames.add(anno.value("unitName").asString()); | ||
break; | ||
} | ||
} | ||
} else if (anno.target().kind() == AnnotationTarget.Kind.CLASS) { | ||
for (AnnotationInstance i : anno.target().asClass().classAnnotations()) { | ||
if (i.name().equals(PRODUCES)) { | ||
knownUnitNames.add(anno.value("unitName").asString()); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
knownUnitNames.remove(""); //TODO: support for the default PU | ||
//now create producer beans for all of the above unit names | ||
//this is not great, we really need a better way to do this than generating bytecode | ||
for (String name : knownUnitNames) { | ||
String className = getClass().getName() + "$$EMFProducer-" + name; | ||
AtomicReference<byte[]> bytes = new AtomicReference<>(); | ||
try (ClassCreator creator = new ClassCreator(new ClassOutput() { | ||
@Override | ||
public void write(String name, byte[] data) { | ||
try { | ||
bytes.set(data); | ||
processorContext.addGeneratedClass(true, name, data); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
}, className, null, Object.class.getName())) { | ||
|
||
creator.addAnnotation(Dependent.class); | ||
MethodCreator producer = creator.getMethodCreator("producerMethod", EntityManagerFactory.class); | ||
producer.addAnnotation(Produces.class); | ||
producer.addAnnotation(ApplicationScoped.class); | ||
|
||
ResultHandle ret = producer.invokeStaticMethod(MethodDescriptor.ofMethod(Persistence.class, "createEntityManagerFactory", EntityManagerFactory.class, String.class), producer.load(name)); | ||
producer.returnValue(ret); | ||
} | ||
beanDeployment.addGeneratedBean(className, bytes.get()); | ||
} | ||
|
||
|
||
} | ||
|
||
@Override | ||
public int getPriority() { | ||
return 0; | ||
} | ||
} |