Skip to content

Commit

Permalink
Merge pull request #25213 from arjantijms/8.0
Browse files Browse the repository at this point in the history
Cherrypick Inject EntityManager changes from 8.0-JDK17
  • Loading branch information
dmatej authored Nov 18, 2024
2 parents 522f49e + 23036fa commit 9190a3f
Show file tree
Hide file tree
Showing 27 changed files with 262 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@
import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
import com.sun.enterprise.deployment.ServiceReferenceDescriptor;
import com.sun.enterprise.deployment.annotation.handlers.ConcurrencyResourceDefinition;
import com.sun.enterprise.deployment.types.EntityManagerReference;
import com.sun.enterprise.deployment.util.DOLUtils;
import com.sun.enterprise.naming.spi.NamingObjectFactory;
import com.sun.enterprise.naming.spi.NamingUtils;

import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.transaction.TransactionManager;
import jakarta.validation.Validation;
import jakarta.validation.ValidationException;
Expand Down Expand Up @@ -671,12 +673,18 @@ private void addJNDIBindings(final JndiNameEnvironment env, final ScopeType scop
if (!dependencyAppliesToScope(descriptor, scope)) {
continue;
}
SimpleJndiName name = toLogicalJndiName(descriptor);
FactoryForEntityManagerWrapper value = new FactoryForEntityManagerWrapper(descriptor, this);
jndiBindings.add(new CompEnvBinding(name, value));

jndiBindings.add(
new CompEnvBinding(
toLogicalJndiName(descriptor),
createFactoryForEntityManager(descriptor)));
}
}

public FactoryForEntityManagerWrapper createFactoryForEntityManager(EntityManagerReference descriptor) {
return new FactoryForEntityManagerWrapper(descriptor, this);
}

private CompEnvBinding getCompEnvBinding(final ResourceEnvReferenceDescriptor next) {
final SimpleJndiName name = toLogicalJndiName(next);
final Object value;
Expand Down Expand Up @@ -779,28 +787,33 @@ public ApplicationEnvironment getCurrentApplicationEnvironment() {
return invocationManager.peekAppEnvironment();
}

private class FactoryForEntityManagerWrapper implements NamingObjectProxy {
public class FactoryForEntityManagerWrapper implements NamingObjectProxy {

private final EntityManagerReferenceDescriptor entityManagerRefDescriptor;
private final EntityManagerReference entityManagerReference;
private final ComponentEnvManager componentEnvManager;

FactoryForEntityManagerWrapper(EntityManagerReferenceDescriptor refDesc, ComponentEnvManager compEnvMgr) {
this.entityManagerRefDescriptor = refDesc;
FactoryForEntityManagerWrapper(EntityManagerReference entityManagerReference, ComponentEnvManager compEnvMgr) {
this.entityManagerReference = entityManagerReference;
this.componentEnvManager = compEnvMgr;
}

@SuppressWarnings("unchecked")
@Override
public Object create(Context ctx) {
EntityManagerWrapper emWrapper =
new EntityManagerWrapper(transactionManager, invocationManager, componentEnvManager, callFlowAgent);

emWrapper.initializeEMWrapper(
entityManagerRefDescriptor.getUnitName(),
entityManagerRefDescriptor.getPersistenceContextType(),
entityManagerRefDescriptor.getSynchronizationType(),
entityManagerRefDescriptor.getProperties());

return emWrapper;
public EntityManager create(Context ctx) {
EntityManagerWrapper entityManagerWrapper =
new EntityManagerWrapper(
transactionManager,
invocationManager,
componentEnvManager,
callFlowAgent);

entityManagerWrapper.initializeEMWrapper(
entityManagerReference.getUnitName(),
entityManagerReference.getPersistenceContextType(),
entityManagerReference.getSynchronizationType(),
entityManagerReference.getProperties());

return entityManagerWrapper;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public EntityManagerReferenceDescriptor(String name, String unitName, Persistenc
public EntityManagerReferenceDescriptor() {
}

public EntityManagerReferenceDescriptor(String unitName) {
this.unitName = unitName;
}

@Override
public void setUnitName(String unitName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ public class PersistenceUnitDescriptor extends Descriptor {
private String transactionType = "JTA";
private String description;
private String provider;
private String scope;
private SimpleJndiName jtaDataSource;
private SimpleJndiName nonJtaDataSource;

private final List<String> mappingFiles = new ArrayList<>();
private final List<String> jarFiles = new ArrayList<>();
private final List<String> classes = new ArrayList<>();
private final List<String> qualifiers = new ArrayList<>();
private final Properties properties = new Properties();

private boolean excludeUnlistedClasses;
Expand Down Expand Up @@ -115,6 +117,14 @@ public void setProvider(String value) {
this.provider = value;
}

public String getScope() {
return scope;
}

public void setScope(String scope) {
this.scope = scope;
}

public SimpleJndiName getJtaDataSource() {
return jtaDataSource;
}
Expand Down Expand Up @@ -159,6 +169,13 @@ public void addClass(String className) {

}

public List<String> getQualifiers() {
return Collections.unmodifiableList(qualifiers);
}
public void addQualifier(String qualifier) {
qualifiers.add(qualifier);
}

public Properties getProperties() {
return (Properties) properties.clone();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.sun.enterprise.deployment.annotation.factory;

import com.sun.enterprise.deployment.util.DOLUtils;

import jakarta.annotation.PostConstruct;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
Expand All @@ -39,6 +41,8 @@
import org.glassfish.hk2.utilities.BuilderHelper;
import org.jvnet.hk2.annotations.Service;

import static java.lang.System.Logger.Level.DEBUG;

/**
* This factory is responsible for initializing a ready to use AnnotationProcessor.
*
Expand Down Expand Up @@ -113,6 +117,7 @@ private static String getAnnotationHandlerForStringValue(ActiveDescriptor<Annota
if (answers == null || answers.isEmpty()) {
return null;
}
DOLUtils.getLogger().log(DEBUG, "AnnotationHandler: " + onMe + " has matches: " + answers);
return answers.get(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public abstract class AbstractHandler implements AnnotationHandler {
protected AnnotationTypesProvider ejbProvider;

@Override
public final Class<? extends Annotation> getAnnotationType() {
public Class<? extends Annotation> getAnnotationType() {
return getClass().getAnnotation(AnnotationHandlerFor.class).value();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public EntityManagerReferenceHandler() {
protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, ResourceContainerContext[] rcContexts)
throws AnnotationProcessorException {
AnnotatedElementHandler aeHandler = ainfo.getProcessingContext().getHandler();
log(Level.FINE, ainfo, "Processing "+getAnnotationType()+" annotation");
if (aeHandler instanceof AppClientContext) {
// application client does not support @PersistenceContext
String msg = I18N.getLocalString("enterprise.deployment.annotation.handlers.invalidaehandler",
Expand Down Expand Up @@ -164,7 +165,7 @@ protected HandlerProcessingResult processEmRef(AnnotationInfo ainfo, ResourceCon
* Return EntityManagerReferenceDescriptors with given name
* if exists or a new one without name being set.
*/
private EntityManagerReferenceDescriptor[] getEmReferenceDescriptors(String logicalName,
protected EntityManagerReferenceDescriptor[] getEmReferenceDescriptors(String logicalName,
ResourceContainerContext[] rcContexts) {
EntityManagerReferenceDescriptor emRefs[] = new EntityManagerReferenceDescriptor[rcContexts.length];
for (int i = 0; i < rcContexts.length; i++) {
Expand All @@ -180,7 +181,7 @@ private EntityManagerReferenceDescriptor[] getEmReferenceDescriptors(String logi
}


private void processNewEmRefAnnotation(EntityManagerReferenceDescriptor emRef, String logicalName,
protected void processNewEmRefAnnotation(EntityManagerReferenceDescriptor emRef, String logicalName,
PersistenceContext annotation) {
emRef.setName(logicalName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ protected void initializeContext(Archivist other) {
/**
* Set the applicable extension archivists for this archivist
*
* @param descriptor for this archivist instnace
* @param list for this archivist instnace
*/
public void setExtensionArchivists(List<ExtensionsArchivist<?>> list) {
extensionsArchivists = list;
Expand Down Expand Up @@ -345,8 +345,7 @@ public T open(File file) throws IOException, SAXException {
* @param archive the module archive
* @param extensions map of extension archivists
*/
protected void postStandardDDsRead(T descriptor, ReadableArchive archive,
Map<ExtensionsArchivist<?>, RootDeploymentDescriptor> extensions) throws IOException {
protected void postStandardDDsRead(T descriptor, ReadableArchive archive, Map<ExtensionsArchivist<?>, RootDeploymentDescriptor> extensions) throws IOException {
}


Expand All @@ -357,6 +356,7 @@ protected void postStandardDDsRead(T descriptor, ReadableArchive archive,
* @param archive the module archive
*/
protected void postAnnotationProcess(T descriptor, ReadableArchive archive) throws IOException {
logger.fine("postAnnotationProcess: " + descriptor);
}


Expand Down Expand Up @@ -560,6 +560,7 @@ public ProcessingResult processAnnotations(T bundleDesc, ReadableArchive archive
*/
protected ProcessingResult processAnnotations(T bundleDesc, ModuleScanner<T> scanner, ReadableArchive archive)
throws AnnotationProcessorException, IOException {
logger.fine("Begin processing annotations for " + bundleDesc);
if (scanner == null) {
return null;
}
Expand Down Expand Up @@ -614,6 +615,7 @@ protected ProcessingResult processAnnotations(T bundleDesc, ModuleScanner<T> sca
try {
return ap.process(ctx);
} finally {
logger.fine("End processing annotations for " + bundleDesc);
if (originalBundleClassLoader == null) {
bundleDesc.setClassLoader(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ private void initDispatchTable() {
table.put(PersistenceTagNames.CLASS, "addClass");
table.put(PersistenceTagNames.SHARED_CACHE_MODE, "setSharedCacheMode");
table.put(PersistenceTagNames.VALIDATION_MODE, "setValidationMode");
table.put(PersistenceTagNames.SCOPE, "setScope");
table.put(PersistenceTagNames.QUALIFIER, "addQualifier");
this.dispatchTable = table;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ public interface PersistenceTagNames extends TagNames {
public static String TRANSACTION_TYPE = "transaction-type";
public static String SHARED_CACHE_MODE = "shared-cache-mode";
public static String VALIDATION_MODE = "validation-mode";
public static String SCOPE = "scope";
public static String QUALIFIER = "qualifier";

}
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,4 @@
</xsd:restriction>
</xsd:simpleType>

</xsd:schema>
</xsd:schema>
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.glassfish.deployment.common.ModuleDescriptor;
import org.glassfish.deployment.common.RootDeploymentDescriptor;

import static java.util.logging.Level.FINE;
import static java.util.logging.Level.WARNING;
import static org.glassfish.deployment.common.DeploymentUtils.getRelativeEmbeddedModulePath;

Expand Down Expand Up @@ -83,6 +84,7 @@ public PersistenceUnitInfoImpl(PersistenceUnitDescriptor persistenceUnitDescript
} catch (NamingException e) {
throw new RuntimeException(e);
}
logger.log(FINE, () -> "PersistenceUnitInfoImpl created for " + persistenceUnitDescriptor.getName());
}

// Implementation of PersistenceUnitInfo interface
Expand Down Expand Up @@ -327,14 +329,12 @@ public static String getPersistenceProviderClassNameForPuDesc(PersistenceUnitDes

@Override
public String getScopeAnnotationName() {
// TODO Auto-generated method stub
return null;
return persistenceUnitDescriptor.getScope();
}

@Override
public List<String> getQualifierAnnotationNames() {
// TODO Auto-generated method stub
return null;
return persistenceUnitDescriptor.getQualifiers();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class NoInterfaceEJBTestServlet extends HttpServlet {
TestBeanInterface testBeanEJB;

@Inject
@InjectViaAtInject
private EntityManager em;

private @Resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@

package test.util;

import test.beans.artifacts.InjectViaAtInject;

import jakarta.enterprise.inject.Produces;
import jakarta.inject.Singleton;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;


@Singleton
public class JPAResourceProducer {
@Produces @PersistenceContext(unitName="pu1")


@Produces @InjectViaAtInject @PersistenceContext(unitName="pu1")
EntityManager customerDatabasePersistenceUnit;

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class TestBean implements TestBeanInterface{
public static boolean testBeanInvoked = false;

@Inject //@TestDatabase
@Inject @TestDatabase
EntityManager emf;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import test.beans.TestBeanInterface;
import test.beans.artifacts.Preferred;
import test.beans.artifacts.TestDatabase;
import test.util.JpaTest;

@WebServlet(name = "mytest", urlPatterns = { "/myurl" })
Expand All @@ -42,7 +43,7 @@ public class JPAResourceInjectionServlet extends HttpServlet {
private EntityManagerFactory emf_at_pu;

@Inject
//@TestDatabase
@TestDatabase
private EntityManager emf_at_inj;

private @Resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@Singleton
public class JPAResourceProducer {
@Produces @PersistenceContext(unitName="pu1")
//@TestDatabase
@TestDatabase
EntityManager customerDatabasePersistenceUnit;

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
import jakarta.persistence.EntityManager;

import test.beans.artifacts.Preferred;
import test.beans.artifacts.TestDatabase;


@RequestScoped
@Preferred
public class TestBean implements TestBeanInterface{
public static boolean testBeanInvoked = false;

@Inject //@TestDatabase
@Inject @TestDatabase
EntityManager emf;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import test.beans.TestBean;
import test.beans.TestBeanInterface;
import test.beans.artifacts.Preferred;
import test.beans.artifacts.TestDatabase;
import test.beans.wbinflib.AnotherTestBeanInWebInfLib;
import test.beans.wbinflib.TestAlternativeBeanInWebInfLib;
import test.beans.wbinflib.TestBeanInWebInfLib;
Expand All @@ -60,7 +61,7 @@ public class TestServlet extends HttpServlet {
@PersistenceUnit(unitName = "pu1")
private EntityManagerFactory emf_at_pu;

@Inject //@TestDatabase
@Inject @TestDatabase
private EntityManager emf_at_inj;

private @Resource
Expand Down
Loading

0 comments on commit 9190a3f

Please sign in to comment.