Syntax
| Tip |
|---|
IOC = inversion of control |
Jetty.xml IOC/DI has long been available even before spring sprung
(springframework)
Almost anything you can do programmatically can be done on jetty.xml, so long as it
does not require loops.
The entire functionality is covered by two classes:
1. org.mortbay.xml.XmlConfiguration
2. org.mortbay.xml.XmlParser
Attributes
id
This simply means that you want to reference an object for later use.
Jetty persists the object in a java.util.HashMap<String,Object>
Most common use cases:
| Code Block |
|---|
<New id="foo" class="com.acme.Foo"/> // equivalent to: // com.acme.Foo foo = new com.acme.Foo(); // instantiates with the default constructor then persists the object <Ref id="foo"/> // references that object <Call id="bar" class="com.acme.MyStaticObjectFactory" name="createObject"/> // equivalent to: // com.acme.MyStaticObjectFactory.createObject(); // call the static method and the returned object is persisted <Configure id="Server" class="org.mortbay.jetty.Server"> // .. // configure this object // tags like Set, Get, and Call are addressed to this object </Configure> /** The id is persisted in a hashmap so that more than one jetty.xml can configure the same object. e.g java -jar start.jar etc/jetty.xml etc/jetty-ssl.xml etc/jetty-plus.xml */ |
class
The value is the fully qualified class name of an object.
Used in three tags:
1. #Configure
2. #New
3. #Call
name
Generally used to specify the name of the property/method of an object.
Also used to specify the name of a System property
Used on the ff tags:
1. #Set
2. #Get
3. #Call
4. #SystemProperty
type
Used to specify the type of an #Array tag.
The value is the fully qualified class name of an object.
Used only in one tag:
1. #Array
default
Used to specify the default value of a System property if it does not exist.
Used only in one tag:
1. #SystemProperty
...
configure.dtd
This is the document type descriptor which specifies a straightforward mapping of xml elements to the java api.
...
Jetty xml files are parsed by the org.mortbay.util.XmlConfiguration class using the configure.dtd descriptor.
| Anchor | ||
|---|---|---|
|
<Configure> Element
This is the root element that specifies the class of object that is to be configured:
...
The Configure element can contain the New, Set, _Put, Call or Ref elements.
| Anchor | ||
|---|---|---|
|
<Set> Element
This element maps to a call to a set method on the current object. The name and optional type attributes are used to select the set method. If the name given is xxx, then a setXxx method is used, or the xxx field is used of setXxx cannot be found.
...
| Code Block | ||
|---|---|---|
| ||
<Set name="xyz"> . . . </Set> |
| Anchor | ||
|---|---|---|
|
<Get> Element
This element maps to a call to a get method of field on the current object. The name attribute is used to select the get method. If the name given is xxx, then a getXxx method is used, or the xxx field is used of setXxx cannot be found.
...
A Get with a class attribute is treated as a static get method or field.
| Anchor | ||
|---|---|---|
|
<Put> Element
This element maps to a call to a put method on the current object, which must implement the Map interface. The name attribute is used as the put key and the optional type attribute can force the type of the value.
A Put element can contain value text and/or the value elements Call, New and SystemProperty. If no value type is specified, the white space is trimmed out of the value. If it contains multiple value elements, they are added as strings before being converted to any specified type.
| Code Block | ||
|---|---|---|
| ||
<Put name="admin">admin</Put>
|
| Anchor | ||
|---|---|---|
|
<Call> Element
This element maps to an arbitrary call to a method on the current object. The name attribute and Arg elements are used to select the method.
...
| Panel | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Is equivalent to:
|
| Anchor | ||
|---|---|---|
|
<Arg> Element
This element defines a positional argument for the Call element. The optional type attribute can force the type of the value.
...
The Ref tag here points to a previously created object with an id parameter.
| Anchor | ||
|---|---|---|
|
<New> Element
This element allows the creation of a new object as part of a value of a Set, Put or Arg element. The class attribute determines the type of the new object and the contained Arg elements are used to select the constructor for the new object. A New element may have an id parameter which gives a unique name to the object which can be referenced later by Ref elements.
...
| Panel | |||||||
|---|---|---|---|---|---|---|---|
This is equivalent to:
|
| Anchor | ||
|---|---|---|
|
<Ref> Element
This element allows a previously created object to be referenced by unique name.
...
Alternatively, the Ref element can be used as the value of an Arg or Set element:
| Code Block | ||
|---|---|---|
| ||
<Set name="someMethod"><Ref id="myobj"/></Set>
|
| Anchor | ||
|---|---|---|
|
<Array> Element
This element allows the creation of a new array as part of a value of a Set, Put or Arg element. The type attribute determines the type of the new array and the contained Item elements are used for each element of the array.
| Panel | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
This is equivalent to
|
| Anchor | ||
|---|---|---|
|
<Map> Element
This element allows the creation of a new HashMap and to populate it with (key,value) pairs.
| Panel | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
This is equivalent to
|
| Anchor | ||
|---|---|---|
|
<Item> Element
This element defines an entry for the Array or Map Entry elements. The optional type attribute can force the type of the value.
...
| Code Block | ||
|---|---|---|
| ||
<Item>
<New class="org.mortbay.jetty.nio.SelectChannelConnector">
<Set name="port">8080</Set>
<Set name="maxIdleTime">30000</Set>
<Set name="Acceptors">10</Set>
</New>
</Item>
|
| Anchor | ||
|---|---|---|
|
<SystemProperty> Element
This element allows JVM System properties to be retrieved as part of the value of a Set, Put or Arg element. The name attribute specifies the property name and the optional default argument provides a default value.
...