-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Allow injection of EMF into CDI beans #23
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this new class is doing the same job as
JPABootstrapInGraalITCase
. Is that correct? If yes I will removeJPABootstrapInGraalITCase
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I thought I had, it I must have missed commiting the deletion.