Skip to content

Commit

Permalink
wip: loading from <domain_dir>/lib/warlibs
Browse files Browse the repository at this point in the history
  • Loading branch information
lprimak committed Dec 2, 2024
1 parent d2fbb5a commit 3e213ee
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 50 deletions.
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-2023] [Payara Foundation and/or its affiliates]
// Portions Copyright [2016-2024] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.glassfish.web;

Expand All @@ -56,6 +56,7 @@
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
Expand All @@ -79,6 +80,9 @@
import org.glassfish.api.deployment.archive.ArchiveDetector;
import org.glassfish.api.deployment.archive.ReadableArchive;
import org.glassfish.deployment.common.DeploymentProperties;
import org.glassfish.deployment.common.DeploymentUtils;
import org.glassfish.deployment.common.InstalledLibrariesResolver;
import org.glassfish.internal.deployment.Deployment;
import org.glassfish.loader.util.ASClassLoaderUtil;
import org.glassfish.web.loader.LogFacade;
import org.glassfish.web.loader.WebappClassLoader;
Expand Down Expand Up @@ -128,6 +132,8 @@ public class WarHandler extends AbstractArchiveHandler {

@Inject
private ServerEnvironment serverEnvironment;
@Inject
private Deployment deployment;

@Override
public String getArchiveType() {
Expand Down Expand Up @@ -191,6 +197,12 @@ public WebappClassLoader run() {
WebXmlParser webXmlParser = getWebXmlParser(context.getSource(), application);
configureLoaderAttributes(cloader, webXmlParser, base);
configureLoaderProperties(cloader, webXmlParser, base);
if (!cloader.isInternal() && DeploymentUtils.useWarLibraries(deployment.getCurrentDeploymentContext())) {
for (Path warLibrary : InstalledLibrariesResolver.getWarLibraries()) {
cloader.addJar(warLibrary.toString(), new JarFile(warLibrary.toFile()), warLibrary.toFile());
cloader.closeJARs(true);
}
}

configureContextXmlAttribute(cloader, base, context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1176,16 +1176,7 @@ public boolean modified() {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Predicate<String> matchesInternal = str -> str.matches(".*generated/.*__.*");
boolean isInternal = false;
if (repositoryURLs != null) {
isInternal = Arrays.stream(repositoryURLs).map(URL::toString)
.filter(matchesInternal).findAny().isPresent();
}
if (canonicalLoaderDir != null && matchesInternal.test(canonicalLoaderDir)) {
isInternal = true;
}
if (isInternal) {
if (isInternal()) {
sb.append("(internal) ");
}
sb.append("WebappClassLoader (delegate=");
Expand Down Expand Up @@ -2109,6 +2100,17 @@ public void stop() throws Exception {
}
}

public boolean isInternal() {
Predicate<String> matchesInternal = str -> str.matches(".*generated/.*__.*");
if (repositoryURLs != null) {
return Arrays.stream(repositoryURLs).map(URL::toString)
.filter(matchesInternal).findAny().isPresent();
}
if (canonicalLoaderDir != null && matchesInternal.test(canonicalLoaderDir)) {
return true;
}
return false;
}

/**
* Used to periodically signal to the classloader to release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,15 @@ public void process(File archiveFile, WebBundleDescriptor webBundleDesc,
public void process(ReadableArchive readableArchive, WebBundleDescriptor webBundleDesc,
ClassLoader classLoader, Parser parser) throws IOException {

this.archiveFile = new File(readableArchive.getURI());
WebFragmentDescriptor webFragmentDesc = new WebFragmentDescriptor();
if (webBundleDesc instanceof WebFragmentDescriptor) {
webFragmentDesc = (WebFragmentDescriptor) webBundleDesc;
}
if (webFragmentDesc.isWarLibrary()) {
this.archiveFile = new File(webFragmentDesc.getWarLibraryPath());
} else {
this.archiveFile = new File(readableArchive.getURI());
}
this.classLoader = classLoader;
setParser(parser);

Expand All @@ -108,7 +116,7 @@ public void process(ReadableArchive readableArchive, WebBundleDescriptor webBund
AnnotationUtils.getLogger().log(Level.FINE, "classLoader is {0}", classLoader);
}

if (!archiveFile.isDirectory()) {
if (!webFragmentDesc.isWarLibrary() && !archiveFile.isDirectory()) {
// on client side
return;
}
Expand All @@ -122,16 +130,19 @@ public void process(ReadableArchive readableArchive, WebBundleDescriptor webBund
File webinf = new File(archiveFile, "WEB-INF");

if (webBundleDesc instanceof WebFragmentDescriptor) {
WebFragmentDescriptor webFragmentDesc = (WebFragmentDescriptor)webBundleDesc;
File lib = new File(webinf, "lib");
if (lib.exists()) {
File jarFile = new File(lib, webFragmentDesc.getJarName());
if (jarFile.exists()) {
// support exploded jar file
if (jarFile.isDirectory()) {
addScanDirectory(jarFile);
} else {
addScanJar(jarFile);
if (webFragmentDesc.isWarLibrary()) {
addScanJar(archiveFile);
} else {
File lib = new File(webinf, "lib");
if (lib.exists()) {
File jarFile = new File(lib, webFragmentDesc.getJarName());
if (jarFile.exists()) {
// support exploded jar file
if (jarFile.isDirectory()) {
addScanDirectory(jarFile);
} else {
addScanJar(jarFile);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@

package org.glassfish.web.deployment.archivist;

import com.sun.enterprise.deploy.shared.ArchiveFactory;
import com.sun.enterprise.deployment.Application;
import com.sun.enterprise.deployment.EarType;
import org.glassfish.api.deployment.DeployCommandParameters;
import org.glassfish.deployment.common.InstalledLibrariesResolver;
import org.glassfish.deployment.common.RootDeploymentDescriptor;
import com.sun.enterprise.deployment.EjbBundleDescriptor;
import com.sun.enterprise.deployment.EjbDescriptor;
import com.sun.enterprise.deployment.WebComponentDescriptor;
import com.sun.enterprise.deployment.annotation.impl.ModuleScanner;
import org.glassfish.hk2.classmodel.reflect.Parser;
import org.glassfish.internal.deployment.Deployment;
import org.glassfish.web.deployment.annotation.impl.WarScanner;
import com.sun.enterprise.deployment.archivist.Archivist;
import com.sun.enterprise.deployment.archivist.ArchivistFor;
Expand Down Expand Up @@ -105,6 +110,10 @@ public class WebArchivist extends Archivist<WebBundleDescriptorImpl> {
private ServerEnvironment env;

private WebBundleDescriptorImpl defaultWebXmlBundleDescriptor = null;
@Inject
private ArchiveFactory archiveFactory;
@Inject
private Deployment deployment;

/**
* @return the module type handled by this archivist
Expand Down Expand Up @@ -307,6 +316,10 @@ public Set<String> getLibraries(ReadableArchive archive) throws IOException {
// EAR shared libraries
extractLibraries(parentArchive.getSubArchive("lib"), false, libraries);
}
// Webapp shared libraries
if(DeploymentUtils.useWarLibraries(deployment.getCurrentDeploymentContext())) {
InstalledLibrariesResolver.getWarLibraries().forEach(warLibrary -> libraries.add(warLibrary.toString()));
}
return libraries;
}

Expand Down Expand Up @@ -344,10 +357,21 @@ protected void postAnnotationProcess(WebBundleDescriptorImpl descriptor,
// all web fragment metadata-complete
// should be overridden and be true also
if (descriptor.isFullAttribute()) {
wfDesc.setFullAttribute(
String.valueOf(descriptor.isFullAttribute()));
wfDesc.setFullAttribute(
String.valueOf(descriptor.isFullAttribute()));
}
ReadableArchive warArchive = null;
try {
if (wfDesc.isWarLibrary()) {
warArchive = archiveFactory.openArchive(new File(wfDesc.getWarLibraryPath()));
warArchive.setExtraData(Parser.class, archive.getExtraData(Parser.class));
}
super.readAnnotations(warArchive != null ? warArchive : archive, wfDesc, localExtensions);
} finally {
if (warArchive != null) {
warArchive.close();
}
}
super.readAnnotations(archive, wfDesc, localExtensions);
}

// scan manifest classpath
Expand Down Expand Up @@ -400,8 +424,16 @@ private List<WebFragmentDescriptor> readStandardFragments(WebBundleDescriptorImp
wfArchivist.setAnnotationProcessingRequested(false);

WebFragmentDescriptor wfDesc = null;
ReadableArchive embeddedArchive = lib.startsWith("WEB-INF")
? archive.getSubArchive(lib) : archive.getParentArchive().getSubArchive("lib").getSubArchive(lib);
ReadableArchive embeddedArchive = null;
boolean isWarLibrary = false;
if (lib.startsWith("WEB-INF")) {
embeddedArchive = archive.getSubArchive(lib);
} else if (archive.getParentArchive() != null) {
embeddedArchive = archive.getParentArchive().getSubArchive("lib").getSubArchive(lib);
} else if (lib.startsWith("/") && lib.contains("/warlibs/")) {
embeddedArchive = archiveFactory.openArchive(new File(lib));
isWarLibrary = true;
}
try {
if (embeddedArchive != null &&
wfArchivist.hasStandardDeploymentDescriptor(embeddedArchive)) {
Expand All @@ -422,6 +454,9 @@ private List<WebFragmentDescriptor> readStandardFragments(WebBundleDescriptorImp
}
}
wfDesc.setJarName(lib.substring(lib.lastIndexOf('/') + 1));
if (isWarLibrary) {
wfDesc.setWarLibraryPath(lib);
}
wfList.add(wfDesc);

descriptor.putJarNameWebFragmentNamePair(wfDesc.getJarName(), wfDesc.getName());
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 [2014-2019] [Payara Foundation and/or its affiliates]
// Portions Copyright [2014-2024] [Payara Foundation and/or its affiliates]

package org.glassfish.web.deployment.descriptor;

Expand All @@ -60,6 +60,7 @@ public class WebFragmentDescriptor extends WebBundleDescriptorImpl
private String jarName = null;
private OrderingDescriptor ordering = null;
private boolean exists = true;
private String warLibraryPath;

/**
* Constrct an empty web app [{0}].
Expand Down Expand Up @@ -93,6 +94,18 @@ public void setExists(boolean exists) {
this.exists = exists;
}

public boolean isWarLibrary() {
return warLibraryPath != null;
}

public String getWarLibraryPath() {
return warLibraryPath;
}

public void setWarLibraryPath(String warLibraryPath) {
this.warLibraryPath = warLibraryPath;
}

@Override
protected WebComponentDescriptor combineWebComponentDescriptor(
WebComponentDescriptor webComponentDescriptor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Portions Copyright [2024] Payara Foundation and/or affiliates

package org.apache.naming.resources;

Expand Down Expand Up @@ -322,6 +323,9 @@ protected JarFileEntry lookupFromJars(String name) {
String jeName = getAbsoluteJarResourceName(name);
for (JarFile jarFile : jarFiles) {
JarEntry jarEntry = null;
if (jarFile == null) {
return null;
}
if (jeName.charAt(jeName.length() - 1) != '/') {
jarEntry = jarFile.getJarEntry(jeName + '/');
if (jarEntry != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Supplier;
Expand Down Expand Up @@ -793,30 +794,16 @@ private void processBdasForAppLibs( ReadableArchive archive, DeploymentContext c
for ( URI oneAppLib : appLibs ) {
for ( String oneInstalledLibrary : installedLibraries ) {
if ( oneAppLib.getPath().endsWith( oneInstalledLibrary ) ) {
ReadableArchive libArchive = null;
try {
libArchive = archiveFactory.openArchive(oneAppLib);
if ( libArchive.exists( WeldUtils.META_INF_BEANS_XML ) ) {
String bdaId = archive.getName() + "_" + libArchive.getName();
RootBeanDeploymentArchive rootBda =
new RootBeanDeploymentArchive(libArchive,
Collections.emptyList(),
context,
bdaId );
libBdas.add(rootBda);
}
} finally {
if ( libArchive != null ) {
try {
libArchive.close();
} catch ( Exception ignore ) {}
}
}
addLibBDA(archive, context, oneAppLib, libBdas);
break;
}
}
}
}

for(Path warLibrary : InstalledLibrariesResolver.getWarLibraries()) {
addLibBDA(archive, context, warLibrary.toUri(), libBdas);
}
} catch (URISyntaxException | IOException e) {
//todo: log error
}
Expand All @@ -826,6 +813,28 @@ private void processBdasForAppLibs( ReadableArchive archive, DeploymentContext c
}
}

private void addLibBDA(ReadableArchive archive, DeploymentContext context, URI oneAppLib, List<RootBeanDeploymentArchive> libBdas) throws IOException {
ReadableArchive libArchive = null;
try {
libArchive = archiveFactory.openArchive(oneAppLib);
if ( libArchive.exists( WeldUtils.META_INF_BEANS_XML ) || WeldUtils.isImplicitBeanArchive(context, libArchive) ) {
String bdaId = archive.getName() + "_" + libArchive.getName();
RootBeanDeploymentArchive rootBda =
new RootBeanDeploymentArchive(libArchive,
Collections.emptyList(),
context,
bdaId );
libBdas.add(rootBda);
}
} finally {
if ( libArchive != null ) {
try {
libArchive.close();
} catch ( Exception ignore ) {}
}
}
}

protected void addDeployedEjbs( Collection<EjbDescriptor> ejbs ) {
if ( ejbs != null ) {
deployedEjbs.addAll( ejbs );
Expand Down
Empty file.
Loading

0 comments on commit 3e213ee

Please sign in to comment.