Skip to content

Commit

Permalink
- read web-fragment.xml from EAR-libs
Browse files Browse the repository at this point in the history
- de-duplicate BDAs in CDI processing by using LinkedHashSet intead of ArrayList
- correctly copy BDA sets for each war in EAR
- Make WAR's CDI beans available in EAR-libs
- made some structures final (cleanup)
  • Loading branch information
lprimak committed Nov 13, 2024
1 parent 93071cf commit 4c255ef
Show file tree
Hide file tree
Showing 5 changed files with 358 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2014-2021] [Payara Foundation and/or its affiliates]
// Portions Copyright [2014-2024] [Payara Foundation and/or its affiliates]

package org.glassfish.web.deployment.archivist;

import com.sun.enterprise.deployment.Application;
import com.sun.enterprise.deployment.EarType;
import org.glassfish.deployment.common.RootDeploymentDescriptor;
import com.sun.enterprise.deployment.EjbBundleDescriptor;
import com.sun.enterprise.deployment.EjbDescriptor;
Expand Down Expand Up @@ -75,9 +76,10 @@
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.Set;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -296,24 +298,33 @@ protected String getArchiveExtension() {
/**
* @return a list of libraries included in the archivist
*/
public Vector<String> getLibraries(Archive archive) {
public Set<String> getLibraries(ReadableArchive archive) throws IOException {
Set<String> libraries = new LinkedHashSet<>();
// WAR libraries
extractLibraries(archive, true, libraries);
ReadableArchive parentArchive = archive.getParentArchive();
if (parentArchive != null && parentArchive.getExtraData(ArchiveType.class).toString().equals(EarType.ARCHIVE_TYPE)) {
// EAR shared libraries
extractLibraries(parentArchive.getSubArchive("lib"), false, libraries);
}
return libraries;
}

Enumeration<String> entries = archive.entries();
private static void extractLibraries(Archive archive, boolean hasWebInfPrefix, Set<String> libs) {
Enumeration<String> entries = archive != null ? archive.entries() : null;
if (entries==null)
return null;
return;

Vector<String> libs = new Vector<String>();
while (entries.hasMoreElements()) {

String entryName = entries.nextElement();
if (!entryName.startsWith("WEB-INF/lib")) {
continue; // not in WEB-INF...
if (hasWebInfPrefix && !entryName.startsWith("WEB-INF/lib")) {
continue; // not in prefix (i.e. WEB-INF)...
}
if (entryName.endsWith(".jar")) {
libs.add(entryName);
}
}
return libs;
}

@Override
Expand Down Expand Up @@ -381,54 +392,50 @@ private List<WebFragmentDescriptor> readStandardFragments(WebBundleDescriptorImp
ReadableArchive archive) throws IOException {

List<WebFragmentDescriptor> wfList = new ArrayList<WebFragmentDescriptor>();
Vector libs = getLibraries(archive);
if (libs != null && libs.size() > 0) {

for (int i = 0; i < libs.size(); i++) {
String lib = (String)libs.get(i);
Archivist wfArchivist = new WebFragmentArchivist(this, habitat);
wfArchivist.setRuntimeXMLValidation(this.getRuntimeXMLValidation());
wfArchivist.setRuntimeXMLValidationLevel(
this.getRuntimeXMLValidationLevel());
wfArchivist.setAnnotationProcessingRequested(false);

WebFragmentDescriptor wfDesc = null;
ReadableArchive embeddedArchive = archive.getSubArchive(lib);
try {
if (embeddedArchive != null &&
wfArchivist.hasStandardDeploymentDescriptor(embeddedArchive)) {
try {
wfDesc = (WebFragmentDescriptor)wfArchivist.open(embeddedArchive);
} catch(SAXParseException ex) {
IOException ioex = new IOException();
ioex.initCause(ex);
throw ioex;
}
} else {
wfDesc = new WebFragmentDescriptor();
wfDesc.setExists(false);
}
} finally {
if (embeddedArchive != null) {
embeddedArchive.close();
for (String lib : getLibraries(archive)) {
Archivist wfArchivist = new WebFragmentArchivist(this, habitat);
wfArchivist.setRuntimeXMLValidation(this.getRuntimeXMLValidation());
wfArchivist.setRuntimeXMLValidationLevel(
this.getRuntimeXMLValidationLevel());
wfArchivist.setAnnotationProcessingRequested(false);

WebFragmentDescriptor wfDesc = null;
ReadableArchive embeddedArchive = lib.startsWith("WEB-INF")
? archive.getSubArchive(lib) : archive.getParentArchive().getSubArchive("lib").getSubArchive(lib);
try {
if (embeddedArchive != null &&
wfArchivist.hasStandardDeploymentDescriptor(embeddedArchive)) {
try {
wfDesc = (WebFragmentDescriptor)wfArchivist.open(embeddedArchive);
} catch(SAXParseException ex) {
IOException ioex = new IOException();
ioex.initCause(ex);
throw ioex;
}
} else {
wfDesc = new WebFragmentDescriptor();
wfDesc.setExists(false);
}
} finally {
if (embeddedArchive != null) {
embeddedArchive.close();
}
wfDesc.setJarName(lib.substring(lib.lastIndexOf('/') + 1));
wfList.add(wfDesc);
}
wfDesc.setJarName(lib.substring(lib.lastIndexOf('/') + 1));
wfList.add(wfDesc);

descriptor.putJarNameWebFragmentNamePair(wfDesc.getJarName(), wfDesc.getName());
descriptor.putJarNameWebFragmentNamePair(wfDesc.getJarName(), wfDesc.getName());

}
}

if (((WebBundleDescriptorImpl)descriptor).getAbsoluteOrderingDescriptor() != null) {
wfList = ((WebBundleDescriptorImpl)descriptor).getAbsoluteOrderingDescriptor().order(wfList);
} else {
OrderingDescriptor.sort(wfList);
}
if (((WebBundleDescriptorImpl)descriptor).getAbsoluteOrderingDescriptor() != null) {
wfList = ((WebBundleDescriptorImpl)descriptor).getAbsoluteOrderingDescriptor().order(wfList);
} else {
OrderingDescriptor.sort(wfList);
}

for (WebFragmentDescriptor wf : wfList) {
descriptor.addOrderedLib(wf.getJarName());
}
for (WebFragmentDescriptor wf : wfList) {
descriptor.addOrderedLib(wf.getJarName());
}

return wfList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class BeanDeploymentArchiveImpl implements BeanDeploymentArchive {
private List<Class<?>> beanClasses = null; // Classes identified as Beans through Weld SPI
private List<URL> beansXmlURLs = null;
private final Collection<EjbDescriptor<?>> ejbDescImpls;
private List<BeanDeploymentArchive> beanDeploymentArchives;
private final Set<BeanDeploymentArchive> beanDeploymentArchives;

private SimpleServiceRegistry simpleServiceRegistry = null;

Expand Down Expand Up @@ -145,8 +145,8 @@ public BeanDeploymentArchiveImpl(ReadableArchive archive,
}

this.friendlyId = this.id;
this.ejbDescImpls = new HashSet<>();
this.beanDeploymentArchives = new ArrayList<>();
this.ejbDescImpls = new LinkedHashSet<>();
this.beanDeploymentArchives = new LinkedHashSet<>();
this.context = ctx;
this.weldBootstrap = context.getTransientAppMetaData(WELD_BOOTSTRAP, WeldBootstrap.class);

Expand Down Expand Up @@ -182,8 +182,8 @@ public BeanDeploymentArchiveImpl(String
}

this.beansXmlURLs = beansXmlUrls;
this.ejbDescImpls = new HashSet<>();
this.beanDeploymentArchives = new ArrayList<>();
this.ejbDescImpls = new LinkedHashSet<>();
this.beanDeploymentArchives = new LinkedHashSet<>();
this.context = ctx;
this.weldBootstrap = context.getTransientAppMetaData(WELD_BOOTSTRAP, WeldBootstrap.class);
populateEJBsForThisBDA(ejbs);
Expand All @@ -192,6 +192,60 @@ public BeanDeploymentArchiveImpl(String
getClassLoader();
}

BeanDeploymentArchiveImpl(BeanDeploymentArchiveImpl self) {
this.id = self.id;
this.moduleClasses = new ArrayList<>(self.moduleClasses);
this.beanClasses = new ArrayList<>(self.beanClasses);
this.moduleClassNames = new ArrayList<>(self.moduleClassNames);
this.beanClassNames = new ArrayList<>(self.beanClassNames);
this.beansXmlURLs = new ArrayList<>(self.beansXmlURLs);
this.ejbDescImpls = new LinkedHashSet<>(self.ejbDescImpls);
this.beanDeploymentArchives = new LinkedHashSet<>();
this.context = self.context;
this.weldBootstrap = self.weldBootstrap;
this.moduleClassLoaderForBDA = self.moduleClassLoaderForBDA;
}

BeanDeploymentArchiveImpl deepCopy() {
return copySubBDAs(copyArchive());
}

BeanDeploymentArchiveImpl copyArchive() {
BeanDeploymentArchiveImpl copy = new BeanDeploymentArchiveImpl(this);
copy.ejbDescImpls.addAll(this.ejbDescImpls);
copy.simpleServiceRegistry = this.simpleServiceRegistry;
return copy;
}

private BeanDeploymentArchiveImpl copySubBDAs(BeanDeploymentArchiveImpl copy) {
copy.beanDeploymentArchives.addAll(deepCopy(new HashSet<>()));
return copy;
}

private Set<BeanDeploymentArchive> deepCopy(Set<BeanDeploymentArchive> seen) {
Set<BeanDeploymentArchive> copySet = new HashSet<>();
for (BeanDeploymentArchive bda : this.beanDeploymentArchives) {
BeanDeploymentArchiveImpl bdaImpl = (BeanDeploymentArchiveImpl) bda;
BeanDeploymentArchiveImpl copy = bdaImpl.copyArchive();
copySet.add(copy);
if (!seen.contains(bda)) {
seen.add(copy);
copy.beanDeploymentArchives.addAll(bdaImpl.deepCopy(seen));
} else {
for (var subBda : bdaImpl.beanDeploymentArchives) {
if (seen.contains(subBda)) {
seen.stream().filter(subBda::equals).findFirst().ifPresent(copy.beanDeploymentArchives::add);
} else {
BeanDeploymentArchiveImpl subBdaImpl = (BeanDeploymentArchiveImpl) subBda;
BeanDeploymentArchiveImpl subBdaCopy = subBdaImpl.copyArchive();
seen.add(subBdaCopy);
subBdaCopy.beanDeploymentArchives.addAll(subBdaImpl.deepCopy(seen));
}
}
}
}
return copySet;
}

private void populateEJBsForThisBDA(Collection<com.sun.enterprise.deployment.EjbDescriptor> ejbs) {
for (com.sun.enterprise.deployment.EjbDescriptor next : ejbs) {
Expand Down Expand Up @@ -599,14 +653,13 @@ private void ensureWebLibJarVisibility(List<BeanDeploymentArchiveImpl> webLibBDA
}
//update modified BDA
if (modified) {
int idx = this.beanDeploymentArchives.indexOf(firstBDA);
if (logger.isLoggable(FINE)) {
logger.log(FINE,
CDILoggerInfo.ENSURE_WEB_LIB_JAR_VISIBILITY_ASSOCIATION_UPDATING,
new Object[]{firstBDA.getFriendlyId()});
}
if (idx >= 0) {
this.beanDeploymentArchives.set(idx, firstBDA);
if (this.beanDeploymentArchives.remove(firstBDA)) {
this.beanDeploymentArchives.add(firstBDA);
}
}
}
Expand All @@ -620,9 +673,8 @@ private void ensureWebLibJarVisibility(List<BeanDeploymentArchiveImpl> webLibBDA
CDILoggerInfo.ENSURE_WEB_LIB_JAR_VISIBILITY_ASSOCIATION_INCLUDING,
new Object[]{subBDA.getId(), this.getId()});
}
int idx = this.beanDeploymentArchives.indexOf(subBDA);
if (idx >= 0) {
this.beanDeploymentArchives.set(idx, subBDA);
if (this.beanDeploymentArchives.remove(subBDA)) {
this.beanDeploymentArchives.add(subBDA);
}
}
}
Expand Down Expand Up @@ -851,4 +903,17 @@ static String stripMavenVersion(String name) {
}
return name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BeanDeploymentArchiveImpl)) return false;
BeanDeploymentArchiveImpl that = (BeanDeploymentArchiveImpl) o;
return Objects.equals(id, that.id) && Objects.equals(beanClasses, that.beanClasses);
}

@Override
public int hashCode() {
return Objects.hash(id, beanClasses);
}
}
Loading

0 comments on commit 4c255ef

Please sign in to comment.