Jetty/Reference/jetty.xml syntax

{{Jetty Reference
|introduction =
The Jetty XML syntax is a straightforward mapping of XML elements to a java API so that POJOs can be instantiated and getters, setters and methods called. It is very similar to Inversion Of Control (IOC) or Dependency Injection (DI) frameworks like spring or plexus (but it pre dates all of them).

Overview

DTD and Parsing

The document type descriptor (DTD) describes all valid elements that can be used. The first two lines must reference the DTD, and you must substitute the appropriate class for the object you are trying to configure. See the appropriate jetty-*.xml references.

Jetty XML files are parsed by the org.eclipse.jetty.xml.XmlConfiguration class using the configure.dtd descriptor.

Method Calls

Java objects are configured by a sequence of <New>, <Set>, <Put> and <Call> elements:

<Setname="Test">value</Set>

obj.setTest("value");

<Putname="Test">value</Put>

obj.put("Test","value");

<Callname="test"><Arg>value</Arg></Call>

obj.test("value");

<Newclass="com.acme.MyStuff"><Arg/></New>

new com.acme.MyStuff();

Coercing Arguments to a Type

Values are coerced to match method arguments on a best effort approach, but explicit types may also be specified with the type attribute. Supported values for type are:

For Java classes, you may use either the fully qualified class name, or just the class name.

Referring to a Class

If you do not specify the classname, Jetty will assume you are calling the method on this. Otherwise, use the class attribute to specify the fully-qualified class name of an object to be configured. (You must always specify the class of the root Configure element.)

Referring to an Object

You can use the id attribute to store a reference to this object when first creating or referring to this object. You can then use the [[#[1]
</source>

This is equivalent to:

foo = getXFoo();
foo.setTest("1, 2, 3");

Ref versus nested elements

Here is an example of the difference in syntax between using the Ref element, and nesting method calls. Both are exactly equivalent:

<!-- using Ref in conjunction with Get --><Configureid="Server"class="org.eclipse.jetty.server.Server"><Getid="Logger"class="org.eclipse.jetty.util.log.Log"name="log"/><Refid="Logger"><Setname="debugEnabled">true</Set></Ref></Configure>

<!-- calling the setter directly on the object returned by Get --><Configureid="Server"class="org.eclipse.jetty.server.Server"><Getclass="org.eclipse.jetty.util.log.Log"name="log"><Setname="debugEnabled">true</Set></Get></Configure>

Here is a more practical example, taken from the handler configuration section in etc/jetty.xml:

<Setname="handler"><Newid="Handlers"class="org.eclipse.jetty.server.handler.HandlerCollection"><Setname="handlers"><Arraytype="org.eclipse.jetty.server.Handler"><Item><!-- create a new instance of a ContextHandlerCollection named "Contexts" --><Newid="Contexts"class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/></Item><Item><Newid="DefaultHandler"class="org.eclipse.jetty.server.handler.DefaultHandler"/></Item><Item><!-- create a new instance of a RequestLogHandler named "RequestLog"---><Newid="RequestLog"class="org.eclipse.jetty.server.handler.RequestLogHandler"/></Item></Array></Set></New></Set><Callname="addBean"><Arg><Newclass="org.eclipse.jetty.deploy.ContextDeployer"><!-- pass in the ContextHandlerCollection object ("Contexts") that was created earlier, as an argument --><Setname="contexts"><Refid="Contexts"/></Set></New></Arg></Call><!-- configure the RequestLogHandler object ("RequestLog") that we created earlier --><Refid="RequestLog">
....
</Ref>