-
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 ae4e508
Showing
9 changed files
with
147 additions
and
92 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
13 changes: 13 additions & 0 deletions
13
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,13 @@ | ||
package org.jboss.shamrock.example.jpa; | ||
|
||
import javax.enterprise.inject.Produces; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.PersistenceContext; | ||
import javax.persistence.PersistenceUnit; | ||
|
||
public class JpaProducer { | ||
|
||
@Produces | ||
@PersistenceUnit(unitName = "templatePU") | ||
EntityManagerFactory emf; | ||
} |
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 { | ||
|
||
} |
11 changes: 6 additions & 5 deletions
11
...ample/test/JPABootstrapInGraalITCase.java → ...ck/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 |
---|---|---|
@@ -1,22 +1,23 @@ | ||
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; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Test reflection around JPA entities | ||
* | ||
* @author Emmanuel Bernard [email protected] | ||
*/ | ||
@RunWith(GraalTest.class) | ||
public class JPABootstrapInGraalITCase { | ||
@RunWith(ShamrockTest.class) | ||
public class JPABootstrapTestCase { | ||
|
||
@Test | ||
public void testFieldAndGetterReflectionOnEntityFromServlet() throws Exception { | ||
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
105 changes: 105 additions & 0 deletions
105
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,105 @@ | ||
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; | ||
|
||
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<>(); | ||
Set<String> knownContextNames = new HashSet<>(); | ||
scanForAnnotations(archiveContext, knownUnitNames, PERSISTENCE_UNIT); | ||
scanForAnnotations(archiveContext, knownContextNames, PERSISTENCE_CONTEXT); | ||
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()); | ||
} | ||
|
||
|
||
} | ||
|
||
private void scanForAnnotations(ArchiveContext archiveContext, Set<String> knownUnitNames, DotName nm) { | ||
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; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public int getPriority() { | ||
return 0; | ||
} | ||
} |