Skip to content

Commit

Permalink
New ScatteredWebArchivist
Browse files Browse the repository at this point in the history
- evolved from EmbeddedWebArchivist, because that conflicted with generics
  in following commits (used class typing to resolve that, but it is not
  possible now any more).
- EmbeddedDecorator duplicated EmbeddedInhabitantsParser and wasn't used

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Sep 18, 2022
1 parent 63b2f68 commit 0a95c36
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2012, 2018 Oracle 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 com.sun.enterprise.deployment;

import org.glassfish.api.deployment.archive.ArchiveType;

import org.jvnet.hk2.annotations.Service;

/**
* {@link ArchiveType} .
*
* @author sanjeeb.sahoo@oracle.com
*/
@Service(name = ScatteredWarType.ARCHIVE_TYPE)
// don't change to imports, it is easy to make a mistake and replace it with ejb's Singleton
@jakarta.inject.Singleton
public class ScatteredWarType extends ArchiveType {

public static final String ARCHIVE_TYPE = "scattered-war";
public static final String ARCHIVE_EXTENSION = "";

public ScatteredWarType() {
super(ARCHIVE_TYPE, ARCHIVE_EXTENSION);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,13 +17,7 @@

package org.glassfish.web.embed.impl;

import org.apache.catalina.startup.Constants;
import org.glassfish.internal.api.ServerContext;
import jakarta.inject.Inject;
import org.glassfish.hk2.api.PostConstruct;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;
Expand All @@ -31,6 +26,12 @@
import java.util.HashMap;
import java.util.Map;

import org.apache.catalina.startup.Constants;
import org.glassfish.hk2.api.PostConstruct;
import org.glassfish.internal.api.ServerContext;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;

/**
* For Embedded GlassFish, override loading of known DTDs via
* getClass().getResource() whenever there is no installRoot/lib/dtds
Expand All @@ -39,17 +40,15 @@
* @author bhavanishankar@dev.java.net
* @see org.glassfish.web.WebEntityResolver#resolveEntity(String, String)
*/
//@Service(name="web")
//@ContractsProvided({EmbeddedWebEntityResolver.class, EntityResolver.class})
public class EmbeddedWebEntityResolver implements EntityResolver, PostConstruct {

@Inject
ServerContext serverContext;
private ServerContext serverContext;

private File dtdDir;

public static final Map<String/*public id*/, String/*bare file name*/> knownDTDs =
new HashMap<String, String>();
new HashMap<>();

static {
knownDTDs.put(Constants.TldDtdPublicId_11, "web-jsptaglibrary_1_1.dtd");
Expand All @@ -58,6 +57,7 @@ public class EmbeddedWebEntityResolver implements EntityResolver, PostConstruct
knownDTDs.put(Constants.WebDtdPublicId_23, "web-app_2_3.dtd");
}

@Override
public void postConstruct() {
if (serverContext != null) {
File root = serverContext.getInstallRoot();
Expand All @@ -66,22 +66,22 @@ public void postConstruct() {
}
}


/**
* Fetch the DTD via getClass().getResource() if the DTD is not
*
* @param publicId
* @param systemId
* @return
* @throws SAXException
* @return {@link InputSource}
* @throws IOException
*/
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
InputSource resolvedEntity = __resolveEntity(publicId, systemId);
@Override
public InputSource resolveEntity(String publicId, String systemId) throws IOException {
InputSource resolvedEntity = resolveEntityAsFile(publicId, systemId);
if (resolvedEntity == null) {
String fileName = knownDTDs.get(publicId);
URL url = this.getClass().getResource("/dtds/" + fileName);
InputStream stream = url != null ? url.openStream() : null;
InputStream stream = url == null ? null : url.openStream();
if (stream != null) {
resolvedEntity = new InputSource(stream);
resolvedEntity.setSystemId(url.toString());
Expand All @@ -90,11 +90,11 @@ public InputSource resolveEntity(String publicId, String systemId)
return resolvedEntity;
}


/**
* Try to fetch DTD from installRoot. Copied from org.glassfish.web.WebEntityResolver
*/
public InputSource __resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
private InputSource resolveEntityAsFile(String publicId, String systemId) {
String fileName = knownDTDs.get(publicId);
if (fileName != null && dtdDir != null) {
File dtd = new File(dtdDir, fileName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
* 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
Expand All @@ -17,7 +16,9 @@

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;
Expand All @@ -28,26 +29,29 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import org.glassfish.apf.AnnotationProcessorException;
import org.glassfish.apf.ProcessingResult;
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.embedded.ScatteredArchive;
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
public class EmbeddedWebArchivist extends WebArchivist {
@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) {
Expand All @@ -56,7 +60,7 @@ static void setDefaultWebXml(URL defaultWebXml) {


@Override
protected URL getDefaultWebXML() throws IOException {
public URL getDefaultWebXML() throws IOException {
if (defaultWebXmlLocation != null) {
return defaultWebXmlLocation;
}
Expand All @@ -68,14 +72,19 @@ protected URL getDefaultWebXML() throws IOException {


@Override
protected ProcessingResult processAnnotations(WebBundleDescriptorImpl bundleDesc,
ModuleScanner<WebBundleDescriptorImpl> scanner, ReadableArchive archive)
throws AnnotationProcessorException, IOException {
// in embedded mode, I ignore all scanners and parse all possible classes.
if (archive instanceof ScatteredArchive) {
return super.processAnnotations(bundleDesc, this.embeddedScanner, archive);
}
return super.processAnnotations(bundleDesc, scanner, archive);
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<WebBundleDescriptorImpl> {
Expand All @@ -84,8 +93,8 @@ private static class EmbeddedWebScanner extends ModuleScanner<WebBundleDescripto
private ClassLoader classLoader;

@Override
public void process(ReadableArchive archiveFile, WebBundleDescriptorImpl descriptor, ClassLoader classLoader, Parser parser)
throws IOException {
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<String> fileEntries = archiveFile.entries();
Expand All @@ -99,19 +108,18 @@ public void process(ReadableArchive archiveFile, WebBundleDescriptorImpl descrip
}
}
}

}


@Override
protected void process(File archiveFile, WebBundleDescriptorImpl descriptor, ClassLoader classLoader) throws IOException {
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("/", ".");

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ public Object run(com.sun.enterprise.config.serverbeans.VirtualServer avs)

}

EmbeddedWebArchivist.setDefaultWebXml(config.getDefaultWebXml());
ScatteredWebArchivist.setDefaultWebXml(config.getDefaultWebXml());

embedded.setDirectoryListing(config.getListings());

Expand Down

0 comments on commit 0a95c36

Please sign in to comment.