Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Jetty/Reference/jetty.xml syntax"

m
m
Line 2: Line 2:
 
| 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 predates all of them). Typically Jetty XML is used by [[Jetty/Reference/jetty.xml|jetty.xml]] to configure a [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/Server.html Jetty server] or by a [[Jetty/Reference/context.xml|context.xml]] file to configure a [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/handler/ContextHandler.html ContextHandler] or subclass, but you can also use the mechanism to configure arbitrary POJOs.
 
| 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 predates all of them). Typically Jetty XML is used by [[Jetty/Reference/jetty.xml|jetty.xml]] to configure a [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/Server.html Jetty server] or by a [[Jetty/Reference/context.xml|context.xml]] file to configure a [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/handler/ContextHandler.html ContextHandler] or subclass, but you can also use the mechanism to configure arbitrary POJOs.
  
This page describes the basic syntax of Jetty XML configuration. See [[Jetty/Reference/jetty.xml usage]] for information on how you can use and combine Jetty XML. See [[Jetty/Reference#Configuration_Files|Configuration Files]] for specific examples.
+
This page describes the basic syntax of Jetty XML configuration. See [[Jetty/Reference/jetty.xml usage|jetty.xml Usage]] for information on how you can use and combine Jetty XML. See [[Jetty/#Configuration_Files|Configuration Files]] for specific examples.
  
 
| body =
 
| body =
Line 9: Line 9:
 
== Basic Example ==
 
== Basic Example ==
  
The following XML configuration file creates some java objects and sets some attributes:
+
The following XML configuration file creates some Java objects and sets some attributes:
  
 
<source lang="xml">
 
<source lang="xml">
Line 57: Line 57:
 
=== Overview ===
 
=== Overview ===
  
==== DTD and Parsing ====
+
==== Understanding DTD and Parsing ====
The [http://www.eclipse.org/jetty/configure.dtd 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 [[Jetty/Reference|references]].
+
The [http://www.eclipse.org/jetty/configure.dtd document type descriptor (DTD)] describes all valid elements. 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 [[etty/#Configuration_Files|references]].
  
Jetty XML files are parsed by the org.eclipse.jetty.xml.XmlConfiguration class using the configure.dtd descriptor.
+
The org.eclipse.jetty.xml.XmlConfiguration class parses the Jetty XML files using the configure.dtd descriptor.
 +
 
 +
==== Using Method Calls ====
 +
 
 +
You configure Java objects with a sequence of <code><New>, <Set>, <Put> and <Call></code> elements:
  
==== Method Calls ====
 
Java objects are configured by a sequence of <code><New>, <Set>, <Put> and <Call></code> elements:
 
 
<div id="methods">
 
<div id="methods">
 
; <source lang="xml"><Set name="Test">value</Set></source>
 
; <source lang="xml"><Set name="Test">value</Set></source>
Line 76: Line 78:
  
 
==== Coercing Arguments to a Type ====
 
==== Coercing Arguments to a Type ====
Values are coerced to match method arguments on a best effort approach, but explicit types can also be specified with the <tt>type</tt> attribute. Supported values for type are:
+
Jetty coerces values to match method arguments on a best effort approach, but you can also specify explicit types with the <tt>type</tt> attribute. Supported values for type are:
  
 
String, Character, Short, Byte, Integer, Long, Boolean, Float, Double,
 
String, Character, Short, Byte, Integer, Long, Boolean, Float, Double,
Line 82: Line 84:
 
URL, InetAddress, InetAddrPort, void
 
URL, InetAddress, InetAddrPort, void
  
For Java classes, you can use either the fully qualified class name, or just the class name.
+
For Java classes, you can use either the fully-qualified class name, or just the class name.
  
 
==== Referring to a Class ====
 
==== Referring to a Class ====
If you do not specify the classname, Jetty will assume you are calling the method on <code>this</code>. Otherwise, use the <tt>class</tt> 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.)
+
If you do not specify the classname, Jetty assumes you are calling the method on <code>this</code>. Otherwise, use the <tt>class</tt> attribute to specify the fully-qualified class name of an object to configure. (You must always specify the class of the root Configure element.)
  
 
==== Referring to an Object ====
 
==== Referring to an Object ====
You can use the <tt>id</tt> attribute to store a reference to this object when first creating or referring to this object. You can then use the <code>[[#&lt;Ref>|&lt;Ref>]]</code> tag to reference the object later on. The id must be unique for each object created.
+
You can use the <tt>id</tt> attribute to store a reference to this object when first creating or referring to this object. You can then use the <code>[[#&lt;Ref>|&lt;Ref>]]</code> tag to reference the object later. The id must be unique for each object you create.
  
 
== &lt;Configure&gt; ==
 
== &lt;Configure&gt; ==
Line 102: Line 104:
  
 
=== Examples ===
 
=== Examples ===
==== Basic example ====
+
==== Basic Example ====
 
<source lang="xml">
 
<source lang="xml">
 
<Configure class="org.eclipse.jetty.server.Server">
 
<Configure class="org.eclipse.jetty.server.Server">
Line 126: Line 128:
 
<source lang="xml">
 
<source lang="xml">
 
<Configure id="Server" class="org.eclipse.jetty.server.Server">
 
<Configure id="Server" class="org.eclipse.jetty.server.Server">
   <!-- assumes that we have the basic server configuration set up; this files only contains additional configuration for logging -->
+
   <!-- assumes that you have the basic server configuration set up; this file only contains additional configuration for logging -->
 
</Configure>
 
</Configure>
 
</source>
 
</source>
Line 134: Line 136:
  
 
== &lt;Set&gt; ==
 
== &lt;Set&gt; ==
A Set element maps to a call to a setter method or field on the current object. It can contain text and/or elements such as Call, New, SystemProperty, etc, as values. The <tt>name</tt> and optional <tt>type</tt> attributes are used to select the setter method. If no value type is specified, then white space is trimmed out of the value.  If it contains multiple elements as values, they are added as strings before being converted to any specified type.
+
A Set element maps to a call to a setter method or field on the current object. It can contain text and/or elements such as Call, New, SystemProperty, etc., as values. The <tt>name</tt> and optional <tt>type</tt> attributes are used to select the setter method. If you do not specify a value type, white space is trimmed out of the value.  If it contains multiple elements as values, they are added as strings before being converted to any specified type.
  
 
=== Attributes ===
 
=== Attributes ===
Line 145: Line 147:
  
 
=== Examples ===
 
=== Examples ===
==== Basic example ====
+
==== Basic Example ====
 
<source lang="xml">
 
<source lang="xml">
 
<Configure id="server" class="org.eclipse.jetty.server.Server">
 
<Configure id="server" class="org.eclipse.jetty.server.Server">
Line 152: Line 154:
 
</source>
 
</source>
  
==== Set via a system property ====
+
==== Set via a System Property ====
 
<source lang="xml">
 
<source lang="xml">
 
<Configure id="server" class="org.eclipse.jetty.server.Server">
 
<Configure id="server" class="org.eclipse.jetty.server.Server">
Line 159: Line 161:
 
</source>
 
</source>
  
==== Create a new object and set it on the server ====
+
==== Creating a NewObject and Setting It on the Server ====
 
<source lang="xml">
 
<source lang="xml">
 
<Configure id="server" class="org.eclipse.jetty.server.Server">
 
<Configure id="server" class="org.eclipse.jetty.server.Server">
Line 182: Line 184:
 
</source>
 
</source>
  
==== Invoke a static setter ====
+
==== Invoking a Static Setter ====
 
<source lang="xml">
 
<source lang="xml">
 
<Configure id="server" class="org.eclipse.jetty.server.Server">
 
<Configure id="server" class="org.eclipse.jetty.server.Server">
Line 190: Line 192:
  
 
== &lt;Get&gt; ==
 
== &lt;Get&gt; ==
A Get element maps to a call to a getter method or field on the current object. It can contain nested elements such as [[#&lt;Set&gt;|Set]], [[#&lt;Put&gt;|Put]], [[#&lt;Call&gt;|Call]], etc; these act on the object returned by the get call.
+
A Get element maps to a call to a getter method or field on the current object. It can contain nested elements such as [[#&lt;Set&gt;|Set]], [[#&lt;Put&gt;|Put]], [[#&lt;Call&gt;|Call]], etc.; these act on the object returned by the Get call.
  
 
=== Attributes ===
 
=== Attributes ===
 
; name : (required), the name of the getter method to call, or the field to get. If the name given is <var>xxx</var>, then a get<var>Xxx</var> method is used. If the get<var>Xxx</var> method cannot be found, then the <var>xxx</var> field is used.
 
; name : (required), the name of the getter method to call, or the field to get. If the name given is <var>xxx</var>, then a get<var>Xxx</var> method is used. If the get<var>Xxx</var> method cannot be found, then the <var>xxx</var> field is used.
 
; class :  (optional), if present, then this Get is treated as a static getter or field.
 
; class :  (optional), if present, then this Get is treated as a static getter or field.
; id : (optional), if present, then you can use this id to refer to the returned object later on.
+
; id : (optional), if present, then you can use this id to refer to the returned object later.
  
 
=== Can Contain ===
 
=== Can Contain ===
Line 201: Line 203:
  
 
=== Examples ===
 
=== Examples ===
==== Basic example ====
+
==== Basic Example ====
This simple example doesn't do much on its own. We'd normally use this in conjunction with a <code><[[#&lt;Ref&gt;|Ref]] id="Logger" /></code>.
+
This simple example doesn't do much on its own. You would normally use this in conjunction with a <code><[[#&lt;Ref&gt;|Ref]] id="Logger" /></code>.
 
<source lang="xml">
 
<source lang="xml">
 
<Configure id="server" class="org.eclipse.jetty.server.Server">
 
<Configure id="server" class="org.eclipse.jetty.server.Server">
Line 209: Line 211:
 
</source>
 
</source>
  
==== Invoke a static getter and call methods on the returned object ====
+
==== Invoking a Static Getter and Call Methods on the Returned Object ====
 
<source lang="xml">
 
<source lang="xml">
 
<Configure id="server" class="org.eclipse.jetty.server.Server">
 
<Configure id="server" class="org.eclipse.jetty.server.Server">
Line 222: Line 224:
 
== &lt;Put&gt; ==
 
== &lt;Put&gt; ==
  
A Put element maps to a call to a put method on the current object, which must implement the Map interface. It can contain text and/or elements such as Call, New, SystemProperty, etc as values. If no value type is specified, then white space is trimmed out of the value.  If it contains multiple elements as values, they are added as strings before being converted to any specified type.
+
A Put element maps to a call to a put method on the current object, which must implement the Map interface. It can contain text and/or elements such as Call, New, SystemProperty, etc. as values. If you do not specify a no value type, white space is trimmed out of the value.  If it contains multiple elements as values, they are added as strings before being converted to any specified type.
  
 
=== Attributes ===
 
=== Attributes ===
Line 245: Line 247:
 
; name : (required), the name of the arbitrary method to call. The method called will use the exact name you provide it.
 
; name : (required), the name of the arbitrary method to call. The method called will use the exact name you provide it.
 
; class : (optional), if present, then this Call is treated as a static method.
 
; class : (optional), if present, then this Call is treated as a static method.
; id : (optional), if present, then you can use this id to refer to any object returned by the call, for later use
+
; id : (optional), if present, you can use this id to refer to any object returned by the call, for later use.
  
 
=== Can Contain ===
 
=== Can Contain ===
Line 266: Line 268:
 
</source>
 
</source>
  
==== Invoke a static method ====
+
==== Invoking a static method ====
 
<source lang="xml">
 
<source lang="xml">
 
<Call class="com.acme.Foo" name="setString">
 
<Call class="com.acme.Foo" name="setString">
Line 278: Line 280:
 
</source>
 
</source>
  
==== Invoke the actual method instead of relying on getter/setter magic ====
+
==== Invoking the Actual Method Instead of Relying on Getter/Setter Magic ====
 
<source lang="xml">
 
<source lang="xml">
 
<Configure id="Server" class="org.eclipse.jetty.server.Server">
 
<Configure id="Server" class="org.eclipse.jetty.server.Server">
Line 301: Line 303:
 
An Arg element can be an argument of either a method or a constructor. Use it within [[#&lt;Call&gt;|&lt;Call&gt;]] and [[#&lt;New&gt;|&lt;New&gt;]].  
 
An Arg element can be an argument of either a method or a constructor. Use it within [[#&lt;Call&gt;|&lt;Call&gt;]] and [[#&lt;New&gt;|&lt;New&gt;]].  
  
It can contain text and/or elements, such as Call, New, SystemProperty, etc, as values. The optional type attribute can force the type of the value. If no type is specified, then white space is trimmed out of the value. If it contains multiple elements as values, they are added as strings before being converted to any specified type.
+
It can contain text and/or elements, such as Call, New, SystemProperty, etc., as values. The optional type attribute can force the type of the value. If you don't specify a type, white space is trimmed out of the value. If it contains multiple elements as values, they are added as strings before being converted to any specified type.
  
 
=== Attributes ===
 
=== Attributes ===
Line 319: Line 321:
 
</source>
 
</source>
  
==== Coercing type ====
+
==== Coercing Type ====
 
This explicitly coerces the type to a boolean:
 
This explicitly coerces the type to a boolean:
 
<source lang="xml">
 
<source lang="xml">
Line 325: Line 327:
 
</source>
 
</source>
  
==== As a parameter ====
+
==== As a Parameter ====
 
Here are a couple of examples of <code><Arg></code> being used as a parameter to methods and to constructors:
 
Here are a couple of examples of <code><Arg></code> being used as a parameter to methods and to constructors:
  
Line 361: Line 363:
  
 
== &lt;New&gt; ==
 
== &lt;New&gt; ==
Instantiates an object. Equivalent to <code>new</code> in Java, and allows the creation of a new object. A New element can contain a sequence of <code><Arg></code>s, followed by a sequence of configuration elements (Set, Put, etc). <code><Arg></code>s are used to select a constructor for the object to be created. The sequence of configuration elements then acts on the newly-created object
+
Instantiates an object. Equivalent to <code>new</code> in Java, and allows the creation of a new object. A New element can contain a sequence of <code><Arg></code>s, followed by a sequence of configuration elements (Set, Put, etc). <code><Arg></code>s are used to select a constructor for the object to be created. The sequence of configuration elements then acts on the newly-created object.
  
 
=== Attributes ===
 
=== Attributes ===
Line 383: Line 385:
 
</source>
 
</source>
  
==== Instantiate with the default constructor ====
+
==== Instantiate with the Default Constructor ====
 
<source lang="xml">
 
<source lang="xml">
 
<New class="com.acme.Foo" />
 
<New class="com.acme.Foo" />
Line 393: Line 395:
 
</source>
 
</source>
  
==== Instantiate with multiple arguments, then further configure ====
+
==== Instantiate with Multiple Arguments, Then Configuring Further ====
 
<source lang="xml">
 
<source lang="xml">
 
  <New id="foo" class="com.acme.Foo">
 
  <New id="foo" class="com.acme.Foo">
Line 409: Line 411:
  
 
== &lt;Ref&gt; ==
 
== &lt;Ref&gt; ==
A Ref element allows a previously created object to be referenced by a unique id. It can contain a sequence of elements, such as Set, Put, etc which then act on the referenced object. It can also be used used as a value for other elements such as Set and Arg.
+
A Ref element allows a previously created object to be referenced by a unique id. It can contain a sequence of elements, such as Set, Put, etc. which then act on the referenced object. You can also use a Ref element as a value for other elements such as Set and Arg.
  
The Ref element is provided for convenience and to ease readability. The effect of the Ref element may usually be achieved by nesting elements (method calls), but this can get complicated very easily. The Ref element makes it possible to refer to the same object if you're using it as multiple times, or passing it into multiple methods. It also makes it possible to split up configuration across multiple files.
+
The Ref element provides convenience and eases readability. You can usually achieve the effect of the Ref by nesting elements (method calls), but this can get complicated very easily. The Ref element makes it possible to refer to the same object if you're using it multiple times, or passing it into multiple methods. It also makes it possible to split up configuration across multiple files.
  
 
=== Attributes ===
 
=== Attributes ===
Line 433: Line 435:
 
</source>
 
</source>
  
==== Manipulate the object returned by Ref ====
+
==== Manipulating the Object Returned by Ref ====
 
<source lang="xml">
 
<source lang="xml">
 
<Get id="foo" name="xFoo" />
 
<Get id="foo" name="xFoo" />
Line 447: Line 449:
 
</source>
 
</source>
  
==== Ref versus nested elements ====
+
==== Ref vs. Nested Elements ====
  
Here is an example of the difference in syntax between using the Ref element, and nesting method calls. Both are exactly equivalent:
+
Here is an example of the difference in syntax between using the Ref element, and nesting method calls. They are exactly equivalent:
  
 
<source lang="xml">
 
<source lang="xml">
Line 511: Line 513:
  
 
=== Attributes ===
 
=== Attributes ===
; type : (optional), specify what [[#Coercing Arguments to a Type|types]] of items the array can contain
+
; type : (optional), specify what [[#Coercing Arguments to a Type|types]] of items the array can contain.
; id : (optional), unique identifier you can use to refer to the array later on
+
; id : (optional), unique identifier you can use to refer to the array later.
  
 
=== Can Contain ===
 
=== Can Contain ===
Line 534: Line 536:
 
=== Attributes ===
 
=== Attributes ===
 
; type : (optional), force the [[#Coercing Arguments to a Type|types]] of value.
 
; type : (optional), force the [[#Coercing Arguments to a Type|types]] of value.
; id : (optional), unique identifier which can be used to refer to this item later on
+
; id : (optional), unique identifier which you can use to refer to the array later.
  
 
=== Can Contain ===
 
=== Can Contain ===
Line 543: Line 545:
  
 
=== Attributes ===
 
=== Attributes ===
; id : (optional), unique identifier you can use to refer to the map later on
+
; id : (optional), unique identifier you can use to refer to the map later.
  
 
=== Can Contain ===
 
=== Can Contain ===
Line 579: Line 581:
 
; name : (required), property name  
 
; name : (required), property name  
 
; default : (optional), a default value as a fallback
 
; default : (optional), a default value as a fallback
; id : (optional), unique identifier which can be used to refer to this system property later on
+
; id : (optional), unique identifier which you can use to refer to the array later.
  
 
=== Can Contain ===
 
=== Can Contain ===
Line 597: Line 599:
  
 
== &lt;Property&gt; ==
 
== &lt;Property&gt; ==
A Property element allows arbitrary properties to be retrieved by name. It can contain a sequence of elements, such as Set, Put, Call, etc, which act on the retrieved object.
+
A Property element allows arbitrary properties to be retrieved by name. It can contain a sequence of elements, such as Set, Put, Call, etc., which act on the retrieved object.
  
 
=== Attributes ===
 
=== Attributes ===
 
; name : (required), property name  
 
; name : (required), property name  
 
; default : (optional), a default value as a fallback
 
; default : (optional), a default value as a fallback
; id : (optional), unique identifier which can be used to refer to this property later on
+
; id : (optional), unique identifier which you can use to refer to the array later.
  
 
=== Can Contain ===
 
=== Can Contain ===
Line 618: Line 620:
 
== Additional Resources ==
 
== Additional Resources ==
 
* [http://www.eclipse.org/jetty/configure.dtd configure.dtd]
 
* [http://www.eclipse.org/jetty/configure.dtd configure.dtd]
* [[Jetty/Reference/jetty.xml usage|Jetty XML Usage]]
+
* [[Jetty/Reference/jetty.xml usage|jetty.xml Usage]]
 
* [[Jetty/Reference/jetty.xml| jetty.xml, server configuration file]]
 
* [[Jetty/Reference/jetty.xml| jetty.xml, server configuration file]]
 
* [[Jetty/Reference/jetty-web.xml| jetty-web.xml, webapp configuration file]]
 
* [[Jetty/Reference/jetty-web.xml| jetty-web.xml, webapp configuration file]]

Revision as of 17:39, 14 June 2011



Contents

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 predates all of them). Typically Jetty XML is used by jetty.xml to configure a Jetty server or by a context.xml file to configure a ContextHandler or subclass, but you can also use the mechanism to configure arbitrary POJOs.

This page describes the basic syntax of Jetty XML configuration. See jetty.xml Usage for information on how you can use and combine Jetty XML. See Configuration Files for specific examples.

Basic Example

The following XML configuration file creates some Java objects and sets some attributes:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="foo" class="com.acme.Foo">
  <Set name="name">demo</Set>
  <Set name="nested">
    <New id="bar" class="com.acme.Bar">
      <Arg>true</Arg>
      <Set name="wibble">10</Set>
      <Set name="wobble">xyz</Set>
      <Set name="parent"><Ref id="foo"/></Set>
      <Call name="init">
         <Arg>false</Arg>
      </Call>
    </New>
  </Set>
 
  <Ref id="bar">
    <Set name="wibble">20</Set>
    <Get name="parent">
      <Set name="name">demo2</Set>
    </Get>
  </Ref>
</Configure>

The XML above is equivalent to the following java code:

com.acme.Foo foo = new com.acme.Foo();
foo.setName("demo");
 
com.acme.Bar bar = new com.acme.Bar(true);
bar.setWibble(10);
bar.setWobble("xyz");
bar.setParent(foo);
bar.init(false);
 
foo.setNested(bar);
 
bar.setWibble(20);
bar.getParent().setName("demo2");


Overview

Understanding DTD and Parsing

The document type descriptor (DTD) describes all valid elements. 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.

The org.eclipse.jetty.xml.XmlConfiguration class parses the Jetty XML files using the configure.dtd descriptor.

Using Method Calls

You configure Java objects with a sequence of <New>, <Set>, <Put> and <Call> elements:

<Set name="Test">value</Set>
obj.setTest("value");
<Put  name="Test">value</Put>
obj.put("Test","value");
<Call name="test"><Arg>value</Arg></Call>
obj.test("value");
<New class="com.acme.MyStuff"><Arg/></New>
new com.acme.MyStuff();

Coercing Arguments to a Type

Jetty coerces values to match method arguments on a best effort approach, but you can also specify explicit types with the type attribute. Supported values for type are:

String, Character, Short, Byte, Integer, Long, Boolean, Float, Double, char, short, byte, int, long, boolean, float, double, URL, InetAddress, InetAddrPort, void

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

Referring to a Class

If you do not specify the classname, Jetty assumes you are calling the method on this. Otherwise, use the class attribute to specify the fully-qualified class name of an object to configure. (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 <Ref> tag to reference the object later. The id must be unique for each object you create.

<Configure>

This is the root element that specifies the class of object that is to be configured. It is usually either the Server, in jetty.xml , or a WebAppContext, in jetty-web.xml.

Attributes

id 
(optional), a reference to the object that was created. If you define multiple <Configure> tags with the same id, they will be treated as one object, even if they're in different files. You can use this to break up configuration of an object (such as the Server) across multiple files.
class 
(optional), the fully qualified classname of the object to be configured. Could be org.eclipse.jetty.server.Server, org.eclipse.jetty.webapp.WebAppContext, a handler, etc.

Can Contain

<Set>,<Get>, <Put>, <Call>, <New>, <Ref>, <Array>, <Map>, <Property>

Examples

Basic Example

<Configure class="org.eclipse.jetty.server.Server">
  <Set name="port">8080</Set>
</Configure>

This is equivalent to:

org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server();
server.setPort(8080);

Using id to break up configuration of one object across multiple files

(etc/jetty.xml)

<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <!-- basic configuration here -->
</Configure>

(etc/jetty-logging.xml)

<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <!-- assumes that you have the basic server configuration set up; this file only contains additional configuration for logging -->
</Configure>

Then run the combined configuration using:

java -jar start.jar etc/jetty.xml jetty-logging.xml

<Set>

A Set element maps to a call to a setter method or field on the current object. It can contain text and/or elements such as Call, New, SystemProperty, etc., as values. The name and optional type attributes are used to select the setter method. If you do not specify a value type, white space is trimmed out of the value. If it contains multiple elements as values, they are added as strings before being converted to any specified type.

Attributes

name 
(required), the name of the setter method to call, or the field to set. If the name given is xxx, then a setXxx method is used. If the setXxx method cannot be found, then the xxx field is used.
type 
(optional), the declared type of the argument.
class 
(optional), if present, then this Set is treated as a static set method invocation

Can Contain

value text, <Get>, <Call>, <New>, <Ref>, <Array>, <Map>, <SystemProperty>, <Property>

Examples

Basic Example

<Configure id="server" class="org.eclipse.jetty.server.Server">
  <Set name="port">8080</Set>
</Configure>

Set via a System Property

<Configure id="server" class="org.eclipse.jetty.server.Server">
  <Set name="port"><SystemProperty name="jetty.port" /></Set>
</Configure>

Creating a NewObject and Setting It on the Server

<Configure id="server" class="org.eclipse.jetty.server.Server">
  <Set name="threadPool">
    <New class="org.eclipse.jetty.util.thread.QueuedThreadPool">
      <Set name="minThreads">10</Set>
      <Set name="maxThreads">1000</Set>
    </New>
  </Set>
</Configure>

This is equivalent to:

org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server();
 
org.eclipse.jetty.util.thread.QueuedThreadPool threadPool = new org.eclipse.jetty.util.thread.QueuedThreadPool();
threadPool.setMinThreads(10);
threadPool.setMaxThreads(1000);
 
server.setThreadPool(threadPool);

Invoking a Static Setter

<Configure id="server" class="org.eclipse.jetty.server.Server">
  <Set class="org.eclipse.jetty.util.log.Log" name="logToParent">loggerName</Set>
</Configure">

<Get>

A Get element maps to a call to a getter method or field on the current object. It can contain nested elements such as Set, Put, Call, etc.; these act on the object returned by the Get call.

Attributes

name 
(required), the name of the getter method to call, or the field to get. If the name given is xxx, then a getXxx method is used. If the getXxx method cannot be found, then the xxx field is used.
class 
(optional), if present, then this Get is treated as a static getter or field.
id 
(optional), if present, then you can use this id to refer to the returned object later.

Can Contain

<Set>,<Get>, <Put>, <Call>, <New>, <Ref>, <Array>, <Map>, <Property>

Examples

Basic Example

This simple example doesn't do much on its own. You would normally use this in conjunction with a <Ref id="Logger" />.

<Configure id="server" class="org.eclipse.jetty.server.Server">
  <Get id="Logger" class="org.eclipse.jetty.util.log.Log" name="log"/>
</Configure>

Invoking a Static Getter and Call Methods on the Returned Object

<Configure id="server" class="org.eclipse.jetty.server.Server">
    <Get class="java.lang.System" name="out">
      <Call name="println">
        <Arg>Server version is: <Get class="org.eclipse.jetty.server.Server" name="version"/></Arg>
      </Call>
    </Get>
</Configure>

<Put>

A Put element maps to a call to a put method on the current object, which must implement the Map interface. It can contain text and/or elements such as Call, New, SystemProperty, etc. as values. If you do not specify a no value type, white space is trimmed out of the value. If it contains multiple elements as values, they are added as strings before being converted to any specified type.

Attributes

name 
(required), used as the put key
type 
(optional), forces the type of the value

Can Contain

value text, <Get>, <Call>, <New>, <Ref>, <Array>, <Map>, <SystemProperty>, <Property>

Example

<Get name="someKindOfMap">
   <Put name="keyName">objectValue</Put>
</Get>

<Call>

A Call element maps to an arbitrary call to a method on the current object. It can contain a sequence of Arg elements followed by a sequence of configuration elements, such as Set, Put, Call. The <Arg>s are passed as arguments to the method; the sequence of configuration elements act on the object returned by the original call.

Attributes

name 
(required), the name of the arbitrary method to call. The method called will use the exact name you provide it.
class 
(optional), if present, then this Call is treated as a static method.
id 
(optional), if present, you can use this id to refer to any object returned by the call, for later use.

Can Contain

<Arg>, <Set>,<Get>, <Put>, <Call>, <New>, <Ref>, <Array>, <Map>, <Property>

Examples

Basic example

<Call name="doFoo">
  <Arg>bar</Arg>
  <Set name="test">1, 2, 3</Set>
</Call>

This is equivalent to:

Object o2 = o1.doFoo("bar");
o2.setTest("1, 2, 3");

Invoking a static method

<Call class="com.acme.Foo" name="setString">
  <Arg>somestring</Arg>
</Call>

which is equivalent to:

com.acme.Foo.setString("somestring");

Invoking the Actual Method Instead of Relying on Getter/Setter Magic

<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <Call name="getPort" id="port" />
  <Call class="com.acme.Environment" name="setPort">
    <Arg>
      <Ref id="port"/>
    </Arg>
  </Call>
</Configure>

which is equivalent to:

org.mortbay.jetty.Server server = new org.mortbay.jetty.Server();
com.acme.Environment.setPort( server.getPort() );

<Arg>

An Arg element can be an argument of either a method or a constructor. Use it within <Call> and <New>.

It can contain text and/or elements, such as Call, New, SystemProperty, etc., as values. The optional type attribute can force the type of the value. If you don't specify a type, white space is trimmed out of the value. If it contains multiple elements as values, they are added as strings before being converted to any specified type.

Attributes

type 
(optional), force the type of the argument

Can Contain

value text, <Get>, <Call>, <New>, <Ref>, <Array>, <Map>, <SystemProperty>, <Property>

Examples

Basic examples

<Arg>foo</Arg> <!-- String -->
<Arg>true</Arg> <!-- Boolean -->
<Arg>1</Arg> <!-- int, long, short, float, double -->
<Arg><Ref id="foo" /></Arg>  <!-- any object; reference a previously created object with id "foo", and pass it as a parameter -->

Coercing Type

This explicitly coerces the type to a boolean:

<Arg type="boolean">False</Arg>

As a Parameter

Here are a couple of examples of <Arg> being used as a parameter to methods and to constructors:

<Call class="com.acme.Environment" name="setFoo">
  <Arg>
    <New class="com.acme.Foo">
      <Arg>bar</Arg>
    </New>
  </Arg>
</Call>

This is equivalent to:

com.acme.Environment.setFoo(new com.acme.Foo("bar"));


<New class="com.acme.Baz">
  <Arg>
    <Call id="bar" class="com.acme.MyStaticObjectFactory" name="createObject">
      <Arg>2</Arg>
    </Call>
  </Arg>
</New>

This is equivalent to:

new com.acme.Baz(com.acme.MyStaticObjectFactory.createObject(2));

<New>

Instantiates an object. Equivalent to new in Java, and allows the creation of a new object. A New element can contain a sequence of <Arg>s, followed by a sequence of configuration elements (Set, Put, etc). <Arg>s are used to select a constructor for the object to be created. The sequence of configuration elements then acts on the newly-created object.

Attributes

class 
(required), fully qualified classname, which determines the type of the new object that is instantiated.
id 
(optional), gives a unique name to the object which can be referenced later by Ref elements.

Can Contain

<Arg>, <Set>,<Get>, <Put>, <Call>, <New>, <Ref>, <Array>, <Map>, <Property>

Examples

Basic example

<New class="com.acme.Foo">
  <Arg>bar</Arg>
</New>

which is equivalent to:

com.acme.Foo foo = new com.acme.Foo("bar");

Instantiate with the Default Constructor

<New class="com.acme.Foo" />

which is equivalent to:

com.acme.Foo foo = new com.acme.Foo();

Instantiate with Multiple Arguments, Then Configuring Further

 <New id="foo" class="com.acme.Foo">
   <Arg>bar</Arg>
   <Arg>baz</Arg>
   <Set name="test">1, 2, 3</Set>
 </New>

which is equivalent to:

Object foo = new com.acme.Foo("bar", "baz");
foo.setTest("1, 2, 3");

<Ref>

A Ref element allows a previously created object to be referenced by a unique id. It can contain a sequence of elements, such as Set, Put, etc. which then act on the referenced object. You can also use a Ref element as a value for other elements such as Set and Arg.

The Ref element provides convenience and eases readability. You can usually achieve the effect of the Ref by nesting elements (method calls), but this can get complicated very easily. The Ref element makes it possible to refer to the same object if you're using it multiple times, or passing it into multiple methods. It also makes it possible to split up configuration across multiple files.

Attributes

id 
(required), the unique identifier used to name a previously created object.

Can Contain

<Set>,<Get>, <Put>, <Call>, <New>, <Ref>, <Array>, <Map>, <Property>

Examples

Basic example

Use the referenced object as an argument to a method call or constructor:

<Get id="foo" name="xFoo" />
<Set name="test"><Ref id="foo"/></Set>

This is equivalent to:

foo = getXFoo();
setSomeMethod(foo);

Manipulating the Object Returned by Ref

<Get id="foo" name="xFoo" />
<Ref id="foo">
  <Set name="test">1, 2, 3</Set>
</Ref>

This is equivalent to:

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

Ref vs. Nested Elements

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

<!-- using Ref in conjunction with Get -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <Get id="Logger" class="org.eclipse.jetty.util.log.Log" name="log"/>
  <Ref id="Logger">
    <Set name="debugEnabled">true</Set>
  </Ref>
</Configure>
<!-- calling the setter directly on the object returned by Get -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <Get class="org.eclipse.jetty.util.log.Log" name="log">
    <Set name="debugEnabled">true</Set>
  </Get>
</Configure>

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

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

<Array>

An Array element allows the creation of a new array.

Attributes

type 
(optional), specify what types of items the array can contain.
id 
(optional), unique identifier you can use to refer to the array later.

Can Contain

<Item>

Example

 <Array type="java.lang.String">
   <Item>value0</Item>
   <Item><New class="java.lang.String"><Arg>value1</Arg></New></Item>
 </Array>

This is equivalent to:

String[] a = new String[] { "value0", new String("value1") };

<Item>

An Item element defines an entry for Array and Map elements.

Attributes

type 
(optional), force the types of value.
id 
(optional), unique identifier which you can use to refer to the array later.

Can Contain

value text, <Get>, <Call>, <New>, <Ref>, <Array>, <Map>, <SystemProperty>, <Property>

<Map>

A Map element allows the creation of a new HashMap and to populate it with (key, value) pairs.

Attributes

id 
(optional), unique identifier you can use to refer to the map later.

Can Contain

<Entry>

Example

<Map>
  <Entry>
    <Item>keyName</Item>
    <Item><New class="java.lang.String"><Arg>value1</Arg></New></Item>
  </Entry>
</Map>

This is equivalent to:

Map m = new HashMap();
m.put("keyName", new String("value1"));

<Entry>

An Entry element contains a key-value <Item> pair for a Map.

Attributes

No attributes.

Can Contain

Item

<SystemProperty>

A SystemProperty element gets the value of a JVM system property. It can be used within elements which accept values, such as Set, Put, Arg, etc.

Attributes

name 
(required), property name
default 
(optional), a default value as a fallback
id 
(optional), unique identifier which you can use to refer to the array later.

Can Contain

Cannot contain anything.

Example

<SystemProperty name="jetty.port" default="8080"/>

That is equivalent to:

System.getProperty("jetty.port", "8080");

Both try to retrieve the value of jetty.port. If jetty.port is not set, then 8080 will be used.

<Property>

A Property element allows arbitrary properties to be retrieved by name. It can contain a sequence of elements, such as Set, Put, Call, etc., which act on the retrieved object.

Attributes

name 
(required), property name
default 
(optional), a default value as a fallback
id 
(optional), unique identifier which you can use to refer to the array later.

Can Contain

<Set>,<Get>, <Put>, <Call>, <New>, <Ref>, <Array>, <Map>, <Property>

Example

<Property name="Server">
  <Call id="jdbcIdMgr" name="getAttribute">
    <Arg>jdbcIdMgr</Arg>
  </Call>
</Property>

Additional Resources


Back to the top