Jump to: navigation, search

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

Line 131: Line 131:
 
=== Attributes ===
 
=== Attributes ===
 
; name : (required), the name of the setter method to call, or the field to set. If the name given is <var>xxx</var>, then a set<var>Xxx</var> method is used. If the set<var>Xxx</var> method cannot be found, then the <var>xxx</var> field is used.
 
; name : (required), the name of the setter method to call, or the field to set. If the name given is <var>xxx</var>, then a set<var>Xxx</var> method is used. If the set<var>Xxx</var> method cannot be found, then the <var>xxx</var> field is used.
; type : (optional), the declared type of the argument.
+
; type : (optional), the declared [[#Coercing Arguments to a Type|type]] of the argument.
 
; class :  (optional), if present, then this Set is treated as a static set method invocation
 
; class :  (optional), if present, then this Set is treated as a static set method invocation
  
Line 219: Line 219:
 
=== Attributes ===
 
=== Attributes ===
 
; name : (required), used as the put key
 
; name : (required), used as the put key
; type : (optional), forces the type of the value
+
; type : (optional), forces the [[#Coercing Arguments to a Type|type]] of the value
  
 
=== Can Contain ===
 
=== Can Contain ===
Line 298: Line 298:
  
 
=== Attributes ===
 
=== Attributes ===
; type : (optional), force the type of the argument
+
; type : (optional), force the [[#Coercing Arguments to a Type|type]] of the argument
  
 
=== Can Contain ===
 
=== Can Contain ===
Line 500: Line 500:
 
</Ref>
 
</Ref>
 
</source>
 
</source>
 +
 
== <Array> ==
 
== <Array> ==
 +
An Array element allows the creation of a new array. The type attribute determines the type of the new array, and contained Item elements are used for each element of the array.
 +
 
=== Attributes ===
 
=== Attributes ===
 +
; 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
 +
 
=== Can Contain ===
 
=== Can Contain ===
 +
[[#<Item>|<Item>]]
 +
 
=== Examples ===
 
=== Examples ===
 +
==== Basic example ====
 +
<source lang="xml">
 +
<Array type="java.lang.String">
 +
  <Item>value0</Item>
 +
  <Item><New class="java.lang.String"><Arg>value1</Arg></New></Item>
 +
</Array>
 +
</source>
 +
This is equivalent to:
 +
<source lang="java">
 +
String[] a = new String[] { "value0", new String("value1") };
 +
</source>
  
 
== <Map> ==
 
== <Map> ==

Revision as of 02:41, 8 July 2009

{{Jetty Reference |introduction = The Jetty XML syntax is a straightforward mapping of XML elements to the Java API. Any getter, setter, or method can be invoked in the XML configuration files.

Basic Example

Here is an example of a file which uses the Jetty XML syntax (the example is extracted from etc/jetty.xml, available from your distribution, so it may look familiar):

  <?xml version="1.0"?>
  <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
  <!-- root element -->
  <Configure id="Server" class="org.eclipse.jetty.server.Server">
    <!-- using a setter on the server class -->
    <Set name="ThreadPool">
      <!-- creating a new object, and customizing it after creation -->
      <New class="org.eclipse.jetty.util.thread.QueuedThreadPool">
        <Set name="minThreads">10</Set>
      </New>
    </Set>
 
    <!-- calling a non-setter/non-getter class, with arguments -->
    <Call name="addConnector">
      <Arg>
          <New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
            <Set name="host"><SystemProperty name="jetty.host" /></Set>
             ....
          </New>
      </Arg>
    </Call>
  </Configure>

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:

<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

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:

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 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 -->
<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. The type attribute determines the type of the new array, and contained Item elements are used for each element of the 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 on

Can Contain

<Item>

Examples

Basic 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") };

<Map>

Attributes

Can Contain

Examples

<Item>

Attributes

Can Contain

Examples

<SystemProperty>

Attributes

Can Contain

Examples

<Property>

Attributes

Can Contain

Examples

| more =

}}



Cite error: <ref> tags exist, but no <references/> tag was found