Jump to: navigation, search

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

Line 292: Line 292:
  
 
== <Arg> ==
 
== <Arg> ==
 +
 +
An Arg element can be an argument of either a method or a constructor. Use it within [[#<Call>|<Call>]] and [[#<New>|<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 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.
 +
 
=== Attributes ===
 
=== Attributes ===
 +
; type : (optional), force the type of the argument
 +
 
=== Can Contain ===
 
=== Can Contain ===
 +
{{Jetty DTD Value}}
 +
 
=== Examples ===
 
=== Examples ===
 +
==== Basic examples ====
 +
 +
<source lang="xml">
 +
<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 -->
 +
</source>
 +
 +
==== Coercing type ====
 +
This explicitly coerces the type to a boolean:
 +
<source lang="xml">
 +
<Arg type="boolean">False</Arg>
 +
</source>
 +
 +
==== As a parameter ====
 +
Here are a couple of examples of <code><Arg></code> being used as a parameter to methods and to constructors:
 +
 +
<source lang="xml">
 +
<Call class="com.acme.Environment" name="setFoo">
 +
  <Arg>
 +
    <New class="com.acme.Foo">
 +
      <Arg>bar</Arg>
 +
    </New>
 +
  </Arg>
 +
</Call>
 +
</source>
 +
 +
This is equivalent to:
 +
<source lang="java">
 +
com.acme.Environment.setFoo(new com.acme.Foo("bar"));
 +
</source>
 +
 +
 +
 +
<source lang="xml">
 +
<New class="com.acme.Baz">
 +
  <Arg>
 +
    <Call id="bar" class="com.acme.MyStaticObjectFactory" name="createObject">
 +
      <Arg>2</Arg>
 +
    </Call>
 +
  </Arg>
 +
</New>
 +
</source>
 +
 +
This is equivalent to:
 +
<source lang="java">
 +
new com.acme.Baz(com.acme.MyStaticObjectFactory.createObject(2));
 +
</source>
  
 
== <New> ==
 
== <New> ==

Revision as of 22:44, 7 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]


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