XML-based configuration utilities for Eclipse Jetty providing IoC mechanism for component configuration.
—
Extensible configuration processing system supporting custom configuration formats and processors. The library provides service provider interfaces that allow alternative configuration formats (e.g., Spring XML, YAML, JSON) to be plugged in instead of or alongside the default Jetty XML format.
Interface for implementing custom configuration processors that can handle alternative configuration formats.
/**
* Interface for alternative configuration processors
* Allows non-XML configuration formats to be used instead of Jetty XML
*/
interface ConfigurationProcessor {
/**
* Initialize processor with configuration context
* @param resource configuration resource being processed
* @param root root node of parsed document (may be null for non-XML formats)
* @param configuration XmlConfiguration instance for context
* @throws Exception if initialization fails
*/
void init(Resource resource, XmlParser.Node root, XmlConfiguration configuration) throws Exception;
/**
* Configure existing object
* @param obj object to configure
* @return configured object
* @throws Exception if configuration fails
*/
Object configure(Object obj) throws Exception;
/**
* Create and configure new object
* @return configured object instance
* @throws Exception if configuration fails
*/
Object configure() throws Exception;
}Service provider interface for creating configuration processors based on document characteristics.
/**
* Factory interface for creating configuration processors
* Implementations are discovered via ServiceLoader
*/
interface ConfigurationProcessorFactory {
/**
* Get processor for specific DTD and root tag combination
* @param dtd DTD identifier (may be null if validation disabled)
* @param tag root element tag name
* @return configuration processor instance, or null if not supported
*/
ConfigurationProcessor getConfigurationProcessor(String dtd, String tag);
}Custom XML catalog implementation that supports runtime determination of base URI for entity resolution.
/**
* XML catalog where xml:base is defined externally
* Allows runtime determination of base URI for entity resolution
*/
class BaseClassCatalog implements Catalog, EntityResolver {
/**
* Factory method to load catalog from XML file
* @param uriToCatalogXml URI to catalog XML file
* @param baseClass base class for relative URI resolution
* @return loaded catalog instance
* @throws IllegalArgumentException if catalog cannot be loaded
*/
static BaseClassCatalog load(URI uriToCatalogXml, Class<?> baseClass);
}Standard XML catalog operations for entity resolution.
/**
* Return stream of alternative catalogs (always empty for BaseClassCatalog)
* @return empty stream
*/
Stream<Catalog> catalogs();
/**
* Match public ID to system URI
* @param publicId public identifier to match
* @return system URI or null if no match
*/
String matchPublic(String publicId);
/**
* Match system ID to URI
* @param systemId system identifier to match
* @return resolved URI or null if no match
*/
String matchSystem(String systemId);
/**
* Match URI to identifier
* @param uri URI to match
* @return matched identifier or null if no match
*/
String matchURI(String uri);SAX EntityResolver implementation for custom entity resolution.
/**
* Resolve external entity references
* @param publicId public identifier
* @param systemId system identifier
* @return InputSource for resolved entity, or null for default behavior
* @throws SAXException if resolution fails
* @throws IOException if I/O error occurs
*/
InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException;Builder for creating isolated runtime environments with custom classpaths and module paths.
/**
* Builder for Environment instances used in XML configuration
* Supports custom classpaths and module paths
*/
class EnvironmentBuilder {
/**
* Create builder with environment name
* @param name environment name
*/
EnvironmentBuilder(@Name("name") String name);
}Configure environment with custom paths and modules.
/**
* Add paths to classpath
* @param classPaths classpath entries to add
*/
void addClassPath(String... classPaths);
/**
* Add path to module path
* @param modulePath module path entry to add
*/
void addModulePath(String modulePath);
/**
* Create configured Environment instance
* @return built environment
*/
Environment build();import org.eclipse.jetty.xml.ConfigurationProcessor;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.eclipse.jetty.xml.XmlParser;
import org.eclipse.jetty.util.resource.Resource;
public class JsonConfigurationProcessor implements ConfigurationProcessor {
private Resource resource;
private XmlConfiguration xmlConfig;
private JsonNode configRoot;
@Override
public void init(Resource resource, XmlParser.Node root, XmlConfiguration configuration)
throws Exception {
this.resource = resource;
this.xmlConfig = configuration;
// Parse JSON configuration
ObjectMapper mapper = new ObjectMapper();
try (InputStream in = resource.newInputStream()) {
this.configRoot = mapper.readTree(in);
}
}
@Override
public Object configure() throws Exception {
// Create object from JSON configuration
String className = configRoot.get("class").asText();
Class<?> clazz = Class.forName(className);
Object instance = clazz.getDeclaredConstructor().newInstance();
return configure(instance);
}
@Override
public Object configure(Object obj) throws Exception {
// Apply JSON configuration to object
JsonNode properties = configRoot.get("properties");
if (properties != null) {
properties.fields().forEachRemaining(entry -> {
try {
String name = entry.getKey();
String value = entry.getValue().asText();
// Use reflection to set property
setProperty(obj, name, value);
} catch (Exception e) {
throw new RuntimeException("Failed to set property: " + entry.getKey(), e);
}
});
}
return obj;
}
private void setProperty(Object obj, String name, String value) throws Exception {
// Reflection-based property setting logic
String setterName = "set" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
Method setter = obj.getClass().getMethod(setterName, String.class);
setter.invoke(obj, value);
}
}import org.eclipse.jetty.xml.ConfigurationProcessorFactory;
import org.eclipse.jetty.xml.ConfigurationProcessor;
public class JsonConfigurationProcessorFactory implements ConfigurationProcessorFactory {
@Override
public ConfigurationProcessor getConfigurationProcessor(String dtd, String tag) {
// Support JSON configuration files
if (tag != null && tag.equals("json-config")) {
return new JsonConfigurationProcessor();
}
// Support Spring-style XML
if (dtd != null && dtd.contains("springframework")) {
return new SpringConfigurationProcessor();
}
return null; // Not supported
}
}Create META-INF/services/org.eclipse.jetty.xml.ConfigurationProcessorFactory file:
com.example.JsonConfigurationProcessorFactory
com.example.SpringConfigurationProcessorFactoryimport org.eclipse.jetty.xml.BaseClassCatalog;
import org.eclipse.jetty.xml.XmlParser;
import java.net.URI;
// Load custom catalog
BaseClassCatalog catalog = BaseClassCatalog.load(
URI.create("file:///path/to/custom-catalog.xml"),
MyClass.class
);
// Use catalog with parser
XmlParser parser = new XmlParser();
parser.addCatalog(catalog.getURI(), MyClass.class);
// Parse with custom entity resolution
XmlParser.Node root = parser.parse("config-with-entities.xml");import org.eclipse.jetty.xml.EnvironmentBuilder;
import org.eclipse.jetty.util.component.Environment;
// Create isolated environment
EnvironmentBuilder builder = new EnvironmentBuilder("test-env");
builder.addClassPath("/path/to/additional/classes");
builder.addClassPath("/path/to/lib/*.jar");
builder.addModulePath("/path/to/modules");
Environment env = builder.build();
// Use environment for configuration
// (Environment usage depends on broader Jetty context)Example JSON configuration file (server.json):
{
"class": "com.example.Server",
"properties": {
"port": "8080",
"host": "localhost",
"maxThreads": "200"
},
"handlers": [
{
"class": "com.example.WebAppHandler",
"properties": {
"contextPath": "/",
"resourceBase": "./webapps"
}
}
]
}XML wrapper to invoke JSON processor:
<?xml version="1.0" encoding="UTF-8"?>
<json-config file="server.json"/>Custom catalog file (custom-catalog.xml):
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<public publicId="-//MyCompany//DTD Config//EN"
uri="config.dtd"/>
<system systemId="http://example.com/schemas/config.xsd"
uri="schemas/config.xsd"/>
</catalog>Support alternative configuration formats by implementing:
ConfigurationProcessor for format-specific processing logicConfigurationProcessorFactory for format detection and processor creationCustomize XML entity resolution by:
Catalog implementationsBaseClassCatalog for runtime base URI determinationXmlParser instancesCreate isolated runtime environments by:
EnvironmentBuilder to configure custom classpathsThe extension system uses Java's ServiceLoader mechanism:
ConfigurationProcessorFactoryMETA-INF/services/The XmlConfiguration class automatically discovers and uses registered processors based on DTD and root element matching.
Install with Tessl CLI
npx tessl i tessl/maven-org-eclipse-jetty--jetty-xml