Skip to content

Commit

Permalink
Instrument parser versioning for servlet/jboss-web.
Browse files Browse the repository at this point in the history
  • Loading branch information
pferraro committed Jul 19, 2024
1 parent eccfc7c commit 2582e4b
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static JBossWebMetaData parse(XMLStreamReader reader, PropertyReplacer pr
if (reader.getEventType() == DTD) {
String dtdLocation = readDTDLocation(reader);
if (dtdLocation != null) {
version = Version.findVersion(dtdLocation);
version = Version.fromLocation(dtdLocation);
}
if (version == null) {
// DTD->getText() is incomplete and not parsable with Xerces from Sun JDK 6,
Expand All @@ -58,7 +58,7 @@ public static JBossWebMetaData parse(XMLStreamReader reader, PropertyReplacer pr
}
String schemaLocation = readSchemaLocation(reader);
if (schemaLocation != null) {
version = Version.findVersion(schemaLocation);
version = Version.fromLocation(schemaLocation);
}
if (version == null) {
version = Version.JBOSS_WEB_6_0;
Expand Down Expand Up @@ -126,7 +126,7 @@ public static JBossWebMetaData parse(XMLStreamReader reader, PropertyReplacer pr
listeners.add(ContainerListenerMetaDataParser.parse(reader, propertyReplacer));
break;
case SESSION_CONFIG:
wmd.setSessionConfig(SessionConfigMetaDataParser.parse(reader, propertyReplacer));
wmd.setSessionConfig(new SessionConfigMetaDataParser(version.getServletVersion()).parse(reader, propertyReplacer));
break;
case VALVE:
List<ValveMetaData> valves = wmd.getValves();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public class Location {
*/
@Deprecated
public static Version getVersion(String location) {
return Version.findVersion(location);
return Version.fromLocation(location);
}
}
103 changes: 71 additions & 32 deletions web/src/main/java/org/jboss/metadata/parser/jbossweb/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,87 @@
package org.jboss.metadata.parser.jbossweb;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public enum Version {
JBOSS_WEB_3_0("http://www.jboss.org/j2ee/dtd/jboss-web_3_0.dtd"),
JBOSS_WEB_3_2("http://www.jboss.org/j2ee/dtd/jboss-web_3_2.dtd"),
JBOSS_WEB_4_0("http://www.jboss.org/j2ee/dtd/jboss-web_4_0.dtd"),
JBOSS_WEB_4_2("http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd"),
JBOSS_WEB_5_0("http://www.jboss.org/j2ee/dtd/jboss-web_5_0.dtd"),
JBOSS_WEB_5_1(5, 1),
JBOSS_WEB_6_0(6, 0),
JBOSS_WEB_7_0(7, 0),
JBOSS_WEB_7_1(7, 1),
JBOSS_WEB_7_2(7, 2),
JBOSS_WEB_8_0(8, 0),
JBOSS_WEB_10_0(10, 0),
JBOSS_WEB_11_0(11, 0),
JBOSS_WEB_12_0(12, 0),
JBOSS_WEB_12_1(12, 1),
JBOSS_WEB_13_0(13, 0),
JBOSS_WEB_14_0(14, 0),
JBOSS_WEB_14_1(14, 1),
JBOSS_WEB_15_0(15, 0),
public enum Version implements org.jboss.metadata.parser.util.Version<Version> {
JBOSS_WEB_3_0(org.jboss.metadata.parser.servlet.Version.SERVLET_3_0, 3, 0, "http://www.jboss.org/j2ee/dtd/jboss-web_%d_%d.dtd"),
JBOSS_WEB_3_2(org.jboss.metadata.parser.servlet.Version.SERVLET_3_0, 3, 2, "http://www.jboss.org/j2ee/dtd/jboss-web_%d_%d.dtd"),
JBOSS_WEB_4_0(org.jboss.metadata.parser.servlet.Version.SERVLET_3_0, 4, 0, "http://www.jboss.org/j2ee/dtd/jboss-web_%d_%d.dtd"),
JBOSS_WEB_4_2(org.jboss.metadata.parser.servlet.Version.SERVLET_3_0, 4, 2, "http://www.jboss.org/j2ee/dtd/jboss-web_%d_%d.dtd"),
JBOSS_WEB_5_0(org.jboss.metadata.parser.servlet.Version.SERVLET_3_0, 5, 0, "http://www.jboss.org/j2ee/dtd/jboss-web_%d_%d.dtd"),
JBOSS_WEB_5_1(org.jboss.metadata.parser.servlet.Version.SERVLET_3_0, 5, 1),
JBOSS_WEB_6_0(org.jboss.metadata.parser.servlet.Version.SERVLET_3_0, 6, 0),
JBOSS_WEB_7_0(org.jboss.metadata.parser.servlet.Version.SERVLET_3_0, 7, 0),
JBOSS_WEB_7_1(org.jboss.metadata.parser.servlet.Version.SERVLET_3_0, 7, 1),
JBOSS_WEB_7_2(org.jboss.metadata.parser.servlet.Version.SERVLET_3_0, 7, 2),
JBOSS_WEB_8_0(org.jboss.metadata.parser.servlet.Version.SERVLET_3_1, 8, 0),
JBOSS_WEB_10_0(org.jboss.metadata.parser.servlet.Version.SERVLET_3_1, 10, 0),
JBOSS_WEB_11_0(org.jboss.metadata.parser.servlet.Version.SERVLET_3_1, 11, 0),
JBOSS_WEB_12_0(org.jboss.metadata.parser.servlet.Version.SERVLET_3_1, 12, 0),
JBOSS_WEB_12_1(org.jboss.metadata.parser.servlet.Version.SERVLET_3_1, 12, 1),
JBOSS_WEB_13_0(org.jboss.metadata.parser.servlet.Version.SERVLET_4_0, 13, 0),
JBOSS_WEB_14_0(org.jboss.metadata.parser.servlet.Version.SERVLET_4_0, 14, 0),
JBOSS_WEB_14_1(org.jboss.metadata.parser.servlet.Version.SERVLET_4_0, 14, 1),
JBOSS_WEB_15_0(org.jboss.metadata.parser.servlet.Version.SERVLET_6_0, 15, 0),
;
// The corresponding servlet version
private final org.jboss.metadata.parser.servlet.Version servletVersion;
private final int major;
private final int minor;
private final String location;

Version(int major, int minor) {
this(String.format("https://www.jboss.org/j2ee/schema/jboss-web_%d_%d.xsd", major, minor));
Version(org.jboss.metadata.parser.servlet.Version version, int major, int minor) {
this(version, major, minor, "https://www.jboss.org/j2ee/schema/jboss-web_%d_%d.xsd");
}

Version(String location) {
this.location = location;
Version(org.jboss.metadata.parser.servlet.Version servletVersion, int major, int minor, String locationPattern) {
this.servletVersion = servletVersion;
this.major = major;
this.minor = minor;
this.location = String.format(locationPattern, major, minor);
}

private static final Map<String, Version> versions = new HashMap<>();
static {
for (Version version : EnumSet.allOf(Version.class)) {
versions.put(version.location, version);
}
public org.jboss.metadata.parser.servlet.Version getServletVersion() {
return this.servletVersion;
}

public static Version findVersion(String location) {
return versions.get(location);
@Override
public int major() {
return this.major;
}

@Override
public int minor() {
return this.minor;
}

String getLocation() {
return this.location;
}

/**
* @deprecated Use {@link #toString()} instead.
*/
@Deprecated(forRemoval = true)
public String versionString() {
return this.toString();
}

@Override
public String toString() {
return this.major + "." + this.minor;
}

private static final Map<String, Version> VERSIONS = EnumSet.allOf(Version.class).stream().collect(Collectors.toMap(Version::toString, Function.identity()));
private static final Map<String, Version> LOCATIONS = EnumSet.allOf(Version.class).stream().collect(Collectors.toMap(Version::getLocation, Function.identity()));

public static Version fromLocation(String location) {
return LOCATIONS.get(location);
}

public static Version fromString(String version) {
return VERSIONS.get(version);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The JBoss Metadata Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.jboss.metadata.parser.servlet;

import org.jboss.metadata.parser.util.MetaDataElementParser;
import org.jboss.metadata.parser.util.VersionedMetaDataParser;

/**
* Abstract meta data parser.
* @author Paul Ferraro
*/
public abstract class AbstractVersionedMetaDataParser<M> extends MetaDataElementParser implements VersionedMetaDataParser<M, Version> {

private final Version version;

AbstractVersionedMetaDataParser(Version version) {
this.version = version;
}

@Override
public Version getVersion() {
return this.version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@
import javax.xml.stream.XMLStreamReader;

import org.jboss.metadata.parser.ee.IdMetaDataParser;
import org.jboss.metadata.parser.util.MetaDataElementParser;
import org.jboss.metadata.property.PropertyReplacer;
import org.jboss.metadata.web.spec.CookieConfigMetaData;

/**
* @author Remy Maucherat
*/
public class CookieConfigMetaDataParser extends MetaDataElementParser {
public class CookieConfigMetaDataParser extends AbstractVersionedMetaDataParser<CookieConfigMetaData> {

public static CookieConfigMetaData parse(XMLStreamReader reader, PropertyReplacer propertyReplacer) throws XMLStreamException {
CookieConfigMetaDataParser(Version version) {
super(version);
}

@Override
public CookieConfigMetaData parse(XMLStreamReader reader, PropertyReplacer propertyReplacer) throws XMLStreamException {
CookieConfigMetaData cookieConfig = new CookieConfigMetaData();

IdMetaDataParser.parseAttributes(reader, cookieConfig);
Expand Down Expand Up @@ -67,5 +71,4 @@ public static CookieConfigMetaData parse(XMLStreamReader reader, PropertyReplace

return cookieConfig;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@

package org.jboss.metadata.parser.servlet;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.jboss.metadata.parser.ee.IdMetaDataParser;
import org.jboss.metadata.parser.util.MetaDataElementParser;
import org.jboss.metadata.property.PropertyReplacer;
import org.jboss.metadata.web.spec.SessionConfigMetaData;
import org.jboss.metadata.web.spec.SessionTrackingModeType;

/**
* @author Remy Maucherat
*/
public class SessionConfigMetaDataParser extends MetaDataElementParser {
public class SessionConfigMetaDataParser extends AbstractVersionedMetaDataParser<SessionConfigMetaData> {

public static SessionConfigMetaData parse(XMLStreamReader reader, PropertyReplacer propertyReplacer) throws XMLStreamException {
public SessionConfigMetaDataParser(Version version) {
super(version);
}

@Override
public SessionConfigMetaData parse(XMLStreamReader reader, PropertyReplacer propertyReplacer) throws XMLStreamException {
SessionConfigMetaData sessionConfig = new SessionConfigMetaData();

IdMetaDataParser.parseAttributes(reader, sessionConfig);
Expand All @@ -39,12 +43,12 @@ public static SessionConfigMetaData parse(XMLStreamReader reader, PropertyReplac
}
break;
case COOKIE_CONFIG:
sessionConfig.setCookieConfig(CookieConfigMetaDataParser.parse(reader, propertyReplacer));
sessionConfig.setCookieConfig(new CookieConfigMetaDataParser(this.getVersion()).parse(reader, propertyReplacer));
break;
case TRACKING_MODE:
List<SessionTrackingModeType> trackingModes = sessionConfig.getSessionTrackingModes();
if (trackingModes == null) {
trackingModes = new ArrayList<SessionTrackingModeType>();
trackingModes = new LinkedList<>();
sessionConfig.setSessionTrackingModes(trackingModes);
}
try {
Expand All @@ -60,5 +64,4 @@ public static SessionConfigMetaData parse(XMLStreamReader reader, PropertyReplac

return sessionConfig;
}

}
86 changes: 59 additions & 27 deletions web/src/main/java/org/jboss/metadata/parser/servlet/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,80 @@

package org.jboss.metadata.parser.servlet;

import java.util.HashMap;
import java.util.EnumSet;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public enum Version {
public enum Version implements org.jboss.metadata.parser.util.Version<Version> {

SERVLET_2_2("2.2"), SERVLET_2_3("2.3"), SERVLET_2_4("2.4"), SERVLET_2_5("2.5"), SERVLET_3_0("3.0"), SERVLET_3_1("3.1"), SERVLET_4_0("4.0"), SERVLET_5_0("5.0"), SERVLET_6_0("6.0");
SERVLET_2_2(2, 2, "http://java.sun.com/j2ee/dtds/web-app_%d_%d.dtd"),
SERVLET_2_3(2, 3, "http://java.sun.com/dtd/web-app_%d_%d.dtd"),
SERVLET_2_4(2, 4, "http://java.sun.com/xml/ns/j2ee/web-app_%d_%d.xsd"),
SERVLET_2_5(2, 5, "http://java.sun.com/xml/ns/javaee/web-app_%d_%d.xsd"),
SERVLET_3_0(3, 0, "http://java.sun.com/xml/ns/javaee/web-app_%d_%d.xsd"),
SERVLET_3_1(3, 1, "http://xmlns.jcp.org/xml/ns/javaee/web-app_%d_%d.xsd"),
SERVLET_4_0(4, 0, "http://xmlns.jcp.org/xml/ns/javaee/web-app_%d_%d.xsd"),
SERVLET_5_0(5, 0),
SERVLET_6_0(6, 0),
;
public static final Version LATEST = Version.SERVLET_6_0;
private static final Map<String, Version> VERSIONS = EnumSet.allOf(Version.class).stream().collect(Collectors.toMap(Version::toString, Function.identity()));
private static final Map<String, Version> SYSTEM_IDS = EnumSet.allOf(Version.class).stream().collect(Collectors.toMap(Version::getSystemId, Function.identity()));

private static final HashMap<String, Version> systemIDmap = new HashMap<String, Version>();
private static final Map<String, Version> PUBLIC_IDS = EnumSet.of(Version.SERVLET_2_2, Version.SERVLET_2_3).stream()
.collect(Collectors.toMap(version -> String.format("-//Sun Microsystems, Inc.//DTD Web Application %d.%d//EN", version.major(), version.minor()), Function.identity()));

static {
systemIDmap.put("http://java.sun.com/j2ee/dtds/web-app_2_2.dtd", Version.SERVLET_2_2);
systemIDmap.put("http://java.sun.com/dtd/web-app_2_3.dtd", Version.SERVLET_2_3);
systemIDmap.put("http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd", Version.SERVLET_2_4);
systemIDmap.put("http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd", Version.SERVLET_2_5);
systemIDmap.put("http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd", Version.SERVLET_3_0);
systemIDmap.put("http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd", Version.SERVLET_3_1);
systemIDmap.put("http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd", Version.SERVLET_4_0);
systemIDmap.put("https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd", Version.SERVLET_5_0);
systemIDmap.put("https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd", Version.SERVLET_6_0);
}
private final int major;
private final int minor;
private final String systemId;

private static final HashMap<String, Version> publicIDmap = new HashMap<String, Version>();
Version(int major, int minor) {
this(major, minor, "https://jakarta.ee/xml/ns/jakartaee/web-app_%d_%d.xsd");
}

static {
publicIDmap.put("-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN", Version.SERVLET_2_2);
publicIDmap.put("-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN", Version.SERVLET_2_3);
Version(int major, int minor, String systemIdFormat) {
this.major = major;
this.minor = minor;
this.systemId = String.format(systemIdFormat, major, minor);
}

Version(String version) {
this.version = version;
@Override
public int major() {
return this.major;
}

public static Version fromSystemID(String systemID) {
return systemIDmap.get(systemID);
@Override
public int minor() {
return this.minor;
}

public static Version fromPublicID(String publicID) {
return publicIDmap.get(publicID);
@Override
public String toString() {
return this.major + "." + this.minor;
}

private final String version;
public String getSystemId() {
return this.systemId;
}

/**
* @deprecated Use {@link #toString()} instead.
*/
@Deprecated(forRemoval = true)
public String versionString() {
return version;
return this.toString();
}

public static Version fromString(String version) {
return VERSIONS.get(version);
}

public static Version fromSystemID(String systemID) {
return SYSTEM_IDS.get(systemID);
}

public static Version fromPublicID(String publicID) {
return PUBLIC_IDS.get(publicID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*/
public class WebCommonMetaDataParser extends MetaDataElementParser {

public static boolean parse(XMLStreamReader reader, WebCommonMetaData wmd, PropertyReplacer propertyReplacer) throws XMLStreamException {
public static boolean parse(XMLStreamReader reader, Version version, WebCommonMetaData wmd, PropertyReplacer propertyReplacer) throws XMLStreamException {
// Only look at the current element, no iteration
final Element element = Element.forName(reader.getLocalName());
switch (element) {
Expand Down Expand Up @@ -95,7 +95,7 @@ public static boolean parse(XMLStreamReader reader, WebCommonMetaData wmd, Prope
case SESSION_CONFIG:
if (wmd.getSessionConfig() != null)
throw new XMLStreamException("Multiple session-config elements detected", reader.getLocation());
wmd.setSessionConfig(SessionConfigMetaDataParser.parse(reader, propertyReplacer));
wmd.setSessionConfig(new SessionConfigMetaDataParser(version).parse(reader, propertyReplacer));
break;
case MIME_MAPPING:
List<MimeMappingMetaData> mimeMappings = wmd.getMimeMappings();
Expand Down
Loading

0 comments on commit 2582e4b

Please sign in to comment.