Skip to content

Commit

Permalink
bugfix: fix EAR deployment failure when multiple WARs use multible CD…
Browse files Browse the repository at this point in the history
…I-enabled library JARs
  • Loading branch information
lprimak committed Oct 28, 2024
1 parent 369d437 commit 93071cf
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2024] [Payara Foundation and/or its affiliates]

package org.glassfish.javaee.full.deployment;

Expand Down Expand Up @@ -101,7 +102,7 @@
@Service
@PerLookup
public class EarDeployer implements Deployer {

public static final String PER_BDA_METADATA_KEY = "[PerBDA]";
// private static final Class GLASSFISH_APPCLIENT_GROUP_FACADE_CLASS =
// org.glassfish.appclient.client.AppClientGroupFacade.class;
// Currently using a string instead of a Class constant to avoid a circular
Expand Down Expand Up @@ -457,15 +458,21 @@ public <U extends OpsParams> U getCommandParameters(Class<U> commandParametersTy
@Override
public void addTransientAppMetaData(String metaDataKey,
Object metaData) {
context.addTransientAppMetaData(metaDataKey,
metaData);
if (metaDataKey.startsWith(PER_BDA_METADATA_KEY)) {
super.addTransientAppMetaData(metaDataKey, metaData);
} else {
context.addTransientAppMetaData(metaDataKey, metaData);
}
}

@Override
public <T> T getTransientAppMetaData(String metaDataKey,
Class<T> metadataType) {
return context.getTransientAppMetaData(metaDataKey,
metadataType);
if (metaDataKey.startsWith(PER_BDA_METADATA_KEY)) {
return super.getTransientAppMetaData(metaDataKey, metadataType);
} else {
return context.getTransientAppMetaData(metaDataKey, metadataType);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*
* Portions Copyright [2017-2019] Payara Foundation and/or affiliates
* Portions Copyright [2017-2024] Payara Foundation and/or affiliates
*/

package org.glassfish.weld;
Expand Down Expand Up @@ -108,11 +108,11 @@ public ClassLoader run()
@Override
public T get( String id )
{
ClassLoader acl = getClassLoader();
T instance = store.get(acl);
T instance = storeById.get(id);
if (instance == null)
{
instance = storeById.get(id);
ClassLoader acl = getClassLoader();
instance = store.get(acl);
if (instance == null) {
throw new IllegalStateException("Singleton not set for " + acl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2022] [Payara Foundation and/or its affiliates]
// Portions Copyright [2016-2024] [Payara Foundation and/or its affiliates]

package org.glassfish.weld;

Expand All @@ -64,7 +64,6 @@
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
Expand Down Expand Up @@ -102,9 +101,8 @@ public class BeanDeploymentArchiveImpl implements BeanDeploymentArchive {

private BDAType bdaType = BDAType.UNKNOWN;

private DeploymentContext context;

private WeldBootstrap weldBootstrap;
DeploymentContext context;
WeldBootstrap weldBootstrap;

private final Map<AnnotatedType<?>, InjectionTarget<?>> itMap = new HashMap<>();

Expand Down Expand Up @@ -272,7 +270,7 @@ public BeansXml getBeansXml() {
if (beansXmlURLs.size() == 1) {
result = weldBootstrap.parse(beansXmlURLs.get(0));
} else {
// This method attempts to performs a merge, but loses some
// This method attempts to perform a merge, but loses some
// information (e.g., version, bean-discovery-mode)
result = weldBootstrap.parse(beansXmlURLs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2024] [Payara Foundation and/or its affiliates]

package org.glassfish.weld;

Expand Down Expand Up @@ -93,7 +94,9 @@ public Object handle(String name) throws NamingException {

if( inv != null ) {

JndiNameEnvironment componentEnv = compEnvManager.getJndiNameEnvironment(inv.getComponentId());
JndiNameEnvironment componentEnv = inv.getComponentId() != null
? compEnvManager.getJndiNameEnvironment(inv.getComponentId())
: null;

if( componentEnv != null ) {

Expand All @@ -112,7 +115,7 @@ public Object handle(String name) throws NamingException {
if( bundle != null ) {
BeanDeploymentArchive bda = weldDeployer.getBeanDeploymentArchiveForBundle(bundle);
if( bda != null ) {
WeldBootstrap bootstrap = weldDeployer.getBootstrapForApp(bundle.getApplication());
WeldBootstrap bootstrap = weldDeployer.getBootstrapForArchive(bda);
//System.out.println("BeanManagerNamingProxy:: getting BeanManagerImpl for" + bda);
beanManager = bootstrap.getManager(bda);
}
Expand All @@ -122,7 +125,6 @@ public Object handle(String name) throws NamingException {
throw new IllegalStateException("Cannot resolve bean manager");
}


} else {
throw new IllegalStateException("No invocation context found");
}
Expand All @@ -137,6 +139,4 @@ public Object handle(String name) throws NamingException {

return beanManager;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2022] [Payara Foundation and/or its affiliates]
// Portions Copyright [2016-2024] [Payara Foundation and/or its affiliates]

package org.glassfish.weld;

Expand Down Expand Up @@ -97,12 +97,12 @@ public class DeploymentImpl implements CDI11Deployment {

// Keep track of our BDAs for this deployment
private List<RootBeanDeploymentArchive> rarRootBdas;
private List<RootBeanDeploymentArchive> ejbRootBdas;
List<RootBeanDeploymentArchive> ejbRootBdas;
private List<RootBeanDeploymentArchive> warRootBdas;
private List<RootBeanDeploymentArchive> libJarRootBdas = null;

private List<BeanDeploymentArchive> beanDeploymentArchives = new ArrayList<>();
private DeploymentContext context;
DeploymentContext context;

// A convenience Map to get BDA for a given BDA ID
private Map<String, BeanDeploymentArchive> idToBeanDeploymentArchive = new HashMap<>();
Expand Down Expand Up @@ -164,24 +164,80 @@ public DeploymentImpl(ReadableArchive archive,
createModuleBda(archive, ejbs, context, contextId);
}

private DeploymentImpl(InjectionManager injectionManager) {
this.injectionManager = injectionManager;
}

DeploymentImpl filter(RootBeanDeploymentArchive rootBDA) {
DeploymentImpl filteredDeployment = new DeploymentImpl(injectionManager);

List<String> nonRooIDs = List.of(rootBDA.getId(), rootBDA.getModuleBda().getId());

filteredDeployment.rarRootBdas = filterBDAs(rarRootBdas, nonRooIDs);
filteredDeployment.ejbRootBdas = filterBDAs(ejbRootBdas, nonRooIDs);
filteredDeployment.warRootBdas = filterBDAs(warRootBdas, nonRooIDs);
filteredDeployment.libJarRootBdas = filterBDAs(libJarRootBdas, nonRooIDs);
filteredDeployment.beanDeploymentArchives = filterBDAs(beanDeploymentArchives, nonRooIDs);
filteredDeployment.extensions = extensions;
filteredDeployment.dynamicExtensions = dynamicExtensions;
filteredDeployment.deployedEjbs = deployedEjbs;
filteredDeployment.archiveFactory = archiveFactory;
filteredDeployment.context = rootBDA.context;
filteredDeployment.appName = appName;
filteredDeployment.contextId = rootBDA.getId() + ".bda";
filteredDeployment.earContextAppLibBdasProcessed = earContextAppLibBdasProcessed;
filteredDeployment.idToBeanDeploymentArchive = idToBeanDeploymentArchive;
filteredDeployment.simpleServiceRegistry = simpleServiceRegistry;

return filteredDeployment;
}

List<RootBeanDeploymentArchive> getRootBDAs() {
if (warRootBdas != null) {
return warRootBdas;
} else if (ejbRootBdas != null) {
return ejbRootBdas;
} else if (rarRootBdas != null) {
return rarRootBdas;
} else if (libJarRootBdas != null) {
return libJarRootBdas;
} else {
return Collections.emptyList();
}
}

private <TT extends BeanDeploymentArchive> List<TT> filterBDAs(List<TT> bdas, List<String> bda) {
if (bdas == null) {
return null;
}
return bdas.stream()
.filter(b -> bda.stream().anyMatch(b.getId()::startsWith))
.collect(toList());
}

private void addBeanDeploymentArchives(RootBeanDeploymentArchive bda) {
rootBDAs(bda).add(bda);
}

private List<RootBeanDeploymentArchive> rootBDAs(RootBeanDeploymentArchive bda) {
BDAType moduleBDAType = bda.getModuleBDAType();
if (moduleBDAType.equals(BDAType.WAR)) {
if (warRootBdas == null) {
warRootBdas = new ArrayList<>();
}
warRootBdas.add(bda);
return warRootBdas;
} else if (moduleBDAType.equals(BDAType.JAR)) {
if (ejbRootBdas == null) {
ejbRootBdas = new ArrayList<>();
}
ejbRootBdas.add(bda);
return ejbRootBdas;
} else if (moduleBDAType.equals(BDAType.RAR)) {
if (rarRootBdas == null) {
rarRootBdas = new ArrayList<>();
}
rarRootBdas.add(bda);
return rarRootBdas;
}
throw new IllegalArgumentException("Unknown BDAType: " + moduleBDAType);
}

/**
Expand Down Expand Up @@ -765,9 +821,9 @@ public BeanDeploymentArchive getBeanDeploymentArchive(Class<?> beanClass) {
return null;
}

for ( BeanDeploymentArchive oneBda : beanDeploymentArchives ) {
for (BeanDeploymentArchive oneBda : beanDeploymentArchives) {
BeanDeploymentArchiveImpl beanDeploymentArchiveImpl = (BeanDeploymentArchiveImpl) oneBda;
if ( beanDeploymentArchiveImpl.getKnownClasses().contains(beanClass.getName()) ) {
if (beanDeploymentArchiveImpl.getKnownClasses().contains(beanClass.getName())) {
return oneBda;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2024] [Payara Foundation and/or its affiliates]

package org.glassfish.weld;

import com.sun.enterprise.deployment.BundleDescriptor;
import com.sun.enterprise.deployment.EjbDescriptor;
import com.sun.enterprise.deployment.WebBundleDescriptor;
import org.glassfish.api.invocation.InvocationManager;
import org.glassfish.internal.api.Globals;
import org.jboss.weld.Container;
import org.jboss.weld.SimpleCDI;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
Expand All @@ -54,7 +60,16 @@
* @author <a href="mailto:j.j.snyder@oracle.com">JJ Snyder</a>
*/
public class GlassFishWeldProvider implements CDIProvider {
private static final WeldDeployer weldDeployer = Globals.get(WeldDeployer.class);
private static final InvocationManager invocationManager = Globals.get(InvocationManager.class);

private static class GlassFishEnhancedWeld extends SimpleCDI {
GlassFishEnhancedWeld() {
}

GlassFishEnhancedWeld(String contextId) {
super(contextId == null ? Container.instance() : Container.instance(contextId));
}

@Override
protected BeanManagerImpl unsatisfiedBeanManager(String callerClassName) {
Expand Down Expand Up @@ -92,15 +107,30 @@ protected BeanManagerImpl unsatisfiedBeanManager(String callerClassName) {

@Override
public CDI<Object> getCDI() {
try {
return new GlassFishEnhancedWeld();
} catch ( Throwable throwable ) {
Throwable cause = throwable.getCause();
if ( cause instanceof IllegalStateException ) {
return null;
try {
BundleDescriptor bundle = null;
Object componentEnv = invocationManager.getCurrentInvocation().getJNDIEnvironment();
if( componentEnv instanceof EjbDescriptor) {
bundle = (BundleDescriptor)
((EjbDescriptor) componentEnv).getEjbBundleDescriptor().
getModuleDescriptor().getDescriptor();

} else if( componentEnv instanceof WebBundleDescriptor) {
bundle = (BundleDescriptor) componentEnv;
}

BeanDeploymentArchive bda = weldDeployer.getBeanDeploymentArchiveForBundle(bundle);
if (bda == null) {
return new GlassFishEnhancedWeld();
} else {
return new GlassFishEnhancedWeld(weldDeployer.getContextIdForArchive(bda));
}
} catch ( Throwable throwable ) {
Throwable cause = throwable.getCause();
if ( cause instanceof IllegalStateException ) {
return null;
}
throw throwable;
}
throw throwable;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2024] [Payara Foundation and/or its affiliates]

package org.glassfish.weld;

Expand All @@ -48,7 +49,6 @@
import org.glassfish.api.invocation.ComponentInvocation;
import org.glassfish.api.invocation.InvocationManager;
import org.glassfish.api.naming.NamedNamingObjectProxy;
import org.glassfish.api.naming.NamespacePrefixes;
import org.glassfish.hk2.api.ServiceLocator;
import org.jboss.weld.bootstrap.WeldBootstrap;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
Expand All @@ -59,9 +59,7 @@
import jakarta.inject.Inject;
import jakarta.inject.Named;
import javax.naming.NamingException;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorContext;
import jakarta.validation.ValidatorFactory;
import java.util.Set;

Expand Down Expand Up @@ -189,7 +187,7 @@ private synchronized BeanManager obtainBeanManager() throws NamingException {
if( bundle != null ) {
BeanDeploymentArchive bda = weldDeployer.getBeanDeploymentArchiveForBundle(bundle);
if( bda != null ) {
WeldBootstrap bootstrap = weldDeployer.getBootstrapForApp(bundle.getApplication());
WeldBootstrap bootstrap = weldDeployer.getBootstrapForArchive(bda);

beanManager = bootstrap.getManager(bda);
}
Expand Down
Loading

0 comments on commit 93071cf

Please sign in to comment.