From 49abd678f03e502cbd395a74b489a77764b74207 Mon Sep 17 00:00:00 2001 From: kosakak Date: Mon, 12 Jun 2023 20:15:32 +0900 Subject: [PATCH] Fixed Embedded Glassfish annotation loading process --- .../web/embed/impl/ScatteredWebArchivist.java | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 launcher-impl/glassfish/src/main/java/org/glassfish/web/embed/impl/ScatteredWebArchivist.java diff --git a/launcher-impl/glassfish/src/main/java/org/glassfish/web/embed/impl/ScatteredWebArchivist.java b/launcher-impl/glassfish/src/main/java/org/glassfish/web/embed/impl/ScatteredWebArchivist.java new file mode 100644 index 0000000..b429b85 --- /dev/null +++ b/launcher-impl/glassfish/src/main/java/org/glassfish/web/embed/impl/ScatteredWebArchivist.java @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2023 Fujitsu Limited. + * Copyright (c) 2022 Eclipse Foundation and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.web.embed.impl; + +import com.sun.enterprise.deployment.ScatteredWarType; +import com.sun.enterprise.deployment.annotation.impl.ModuleScanner; +import com.sun.enterprise.deployment.archivist.ArchivistFor; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.glassfish.api.deployment.archive.ArchiveType; +import org.glassfish.api.deployment.archive.ReadableArchive; +import org.glassfish.hk2.api.PerLookup; +import org.glassfish.hk2.classmodel.reflect.Parser; +import org.glassfish.internal.api.Globals; +import org.glassfish.web.LogFacade; +import org.glassfish.web.deployment.archivist.WebArchivist; +import org.glassfish.web.deployment.descriptor.WebBundleDescriptorImpl; +import org.jvnet.hk2.annotations.Service; + + +/** + * @author Jerome Dochez + * @author David Matejcek + */ +@Service +@PerLookup +@ArchivistFor(ScatteredWarType.ARCHIVE_TYPE) +public class ScatteredWebArchivist extends WebArchivist { + + private static final Logger LOG = LogFacade.getLogger(); + private static URL defaultWebXmlLocation; + + private final EmbeddedWebScanner embeddedScanner = new EmbeddedWebScanner(); + + static void setDefaultWebXml(URL defaultWebXml) { + defaultWebXmlLocation = defaultWebXml; + } + + + @Override + public URL getDefaultWebXML() throws IOException { + if (defaultWebXmlLocation != null) { + return defaultWebXmlLocation; + } + URL defaultWebXml = super.getDefaultWebXML(); + return defaultWebXml == null + ? getClass().getClassLoader().getResource("org/glassfish/web/embed/default-web.xml") + : defaultWebXml; + } + + + @Override + public ArchiveType getModuleType() { + return Globals.getDefaultHabitat().getService(ArchiveType.class, ScatteredWarType.ARCHIVE_TYPE); + } + + + /** + * @return the scanner for this archivist, usually it is the scanner regitered + * with the same module type as this archivist, but subclasses can return + * a different version + */ + @Override + public EmbeddedWebScanner getScanner() { + return this.embeddedScanner; + } + + private static class EmbeddedWebScanner extends ModuleScanner { + + private final Set> elements = new HashSet<>(); + private ClassLoader classLoader; + + @Override + public void process(ReadableArchive archiveFile, WebBundleDescriptorImpl descriptor, ClassLoader classLoader, + Parser parser) throws IOException { + this.classLoader = classLoader; + // in embedded mode, we don't scan archive, we just process all classes. + Enumeration fileEntries = archiveFile.entries(); + while (fileEntries.hasMoreElements()) { + String entry = fileEntries.nextElement(); + if (entry.endsWith(".class")) { + try { + elements.add(classLoader.loadClass(toClassName(entry))); + } catch (ClassNotFoundException | NoClassDefFoundError e) { + LOG.log(Level.WARNING, "Cannot load class " + entry, e); + } + } + } + } + + + @Override + protected void process(File archiveFile, WebBundleDescriptorImpl descriptor, ClassLoader classLoader) + throws IOException { + } + + + private String toClassName(String entryName) { + String name = entryName.substring("WEB-INF/classes/".length(), entryName.length() - ".class".length()); + return name.replaceAll("/", "."); + } + + + @Override + public Set> getElements() { + return elements; + } + + + @Override + public ClassLoader getClassLoader() { + return classLoader; + } + } +} \ No newline at end of file