Jump to: navigation, search

Difference between revisions of "Jetty/Feature/JNDI"

< Jetty‎ | Feature
Line 1: Line 1:
 
{{Jetty Feature  
 
{{Jetty Feature  
 
| introduction =  
 
| introduction =  
Jetty supports java:comp/env lookups in webapps. This is an optional feature, and as such some setup needs to be done. However, if you're using the [[http://dist.codehaus.org/jetty Hightide]] distribution of jetty, then this feature is already fully enabled for you, so you can skip any setup steps, and read the section on how to put objects into jetty's JNDI so that you can retrieve them at runtime.
+
Jetty supports <code>java:comp/env</code> lookups in webapps. This is an optional feature, and as such some setup needs to be done. However, if you're using the [[http://dist.codehaus.org/jetty Hightide]] distribution of jetty, then this feature is already fully enabled for you, so you can skip any setup steps, and read the section on how to put objects into jetty's JNDI so that you can retrieve them at runtime.
  
 
| body =
 
| body =
Line 44: Line 44:
 
</source>
 
</source>
  
Alternatively, we could apply these configurations to every webapp that is deployed. To do that, we edit the $JETTY_HOME/etc/jetty.xml file (or create a new new file and put it on the runline or put it in start.ini) to add:
+
Alternatively, we could apply these configurations to every webapp that is deployed. To do that, we edit the <code>$JETTY_HOME/etc/jetty.xml</code> file (or create a new new file and put it on the runline or put it in <code>start.ini</code>) to add:
  
 
<source lang="xml">
 
<source lang="xml">
Line 66: Line 66:
 
</Configure>
 
</Configure>
 
</source>
 
</source>
 +
{{tip|Tip:|
 +
We have included the <code>etc/jetty-plus.xml</code> configuration file that configures a WebAppDeployer to deploy all webapps in the <code>webapps-plus</code> directory with JNDI. You can modify this file as desired, or merge it with your <code>etc/jetty.xml</code> file.}}
  
 
=== Classpath jars ===
 
=== Classpath jars ===
Line 75: Line 77:
 
</source>
 
</source>
  
If you prefer, you can edit the start.ini file and add "plus" to the default OPTIONS to lessen the verbosity of the runline.
+
{{tip|Tip:|If you prefer, you can edit the <code>start.ini</code> file and add "plus" to the default OPTIONS to lessen the verbosity of the runline.}}
 +
You may now configure naming resources that can be referenced in a web.xml file and accessed from within the <code>java:comp/env</code> naming environment of the webapp during execution. Specifically, you may configure support for the following web.xml elements:
  
 +
<source lang="xml">
 +
<env-entry/>
 +
<resource-ref/>
 +
<resource-env-ref/>
 +
</source>
 +
 +
[[#Configuring_env-entrys]] shows you how to set up overrides for <code><env-entry></code> elements in web.xml. [Configuring resource-refs and resource-env-refs|#res-ref|How to configure a resource] discusses how to configure support resources such as <code>javax.sql.DataSource</code>.
 +
 +
Furthermore, it is possible to plug a JTA <code>javax.transaction.UserTransaction</code> implementation into Jetty so that webapps can lookup <code>java:comp/UserTransaction</code> to obtain a distributed transaction manager. See [Configuring XA Transactions|#tx|How to setup JTA].
 +
 +
You can define your naming resources with 3 scopes:
 +
 +
# jvm scope - the name is unique within the jvm
 +
# server scope - the name is unique to the Server instance
 +
# webapp scope - the name is unique to the WebAppContext instance
 +
 +
The section [[#Global_or_scoped_to_a_webapp]] explains what scoping is, and shows you how to use it. Essentially, scoping ensures that JNDI bindings from one webapp do not interfere with the JNDI bindings of another - unless of course you wish them to.
 +
 +
Before we go any further, lets take a look at what kind of things can be bound into JNDI with Jetty.
 +
 +
=== What can be bound and general overview ===
 +
 +
There are 4 types of objects that can be bound into Jetty's JNDI:
 +
 +
* an ordinary POJO instance
 +
* a [http://java.sun.com/j2se/1.5.0/docs/api/javax/naming/Reference.html java.naming.Reference] instance
 +
* an object instance that implements the [http://java.sun.com/j2se/1.5.0/docs/api/javax/naming/Referenceable.html java.naming.Referenceable] interface
 +
* a linkage between a name as referenced in web.xml and as referenced in the environment
 +
 +
The binding for all of these object types generally follows the same pattern:
 +
 +
<source lang="xml">
 +
<New class=type of naming entry>
 +
  <Arg>scope</Arg>
 +
  <Arg>name to bind as</Arg>
 +
  <Arg>the object to bind</Arg>
 +
</New>
 +
</source>
 +
 +
The <code>type of naming entry</code> can be:
 +
 +
* <code>"org.mortbay.jetty.plus.naming.EnvEntry"</code> for <env-entry>s
 +
* <code>"org.mortbay.naming.plus.Resource"</code> for all other type of resources
 +
* <code>"org.mortbay.plus.naming.Transaction"</code> for a JTA manager. We'll take a closer look at this in the [[#Configuring_XA_Transactions]] section.
 +
* <code>"org.mortbay.plus.naming.Link"</code> for link between a web.xml resource name and a NamingEntry. See [#Configuring_Links] for more info.
 +
 +
There are 3 places in which you can define naming entries:
 +
 +
# <code>jetty.xml</code>
 +
# <code>WEB-INF/jetty-env.xml</code>
 +
# context xml file
 +
 +
Naming entries defined in a <code>jetty.xml</code> file will generally be scoped at either the jvm level or the Server level. Naming entries in a [jetty-env.xml|jetty-env.xml] file will generally be scoped to the webapp in which the file resides, although you are able to enter jvm or Server scopes if you wish, that is not really recommended. In most cases you will define all naming entries that you want visible to a particular Server instance, or to the jvm as a whole in a jetty.xml file. Entries in a [context |ContextDeployer] xml file will generally be scoped at the level of the webapp to which it applies, although once again, you can supply a less strict scoping level of Server or jvm if you want.
 +
 +
=== Configuring env-entrys ===
 +
Sometimes it is useful to be able to pass configuration information to a webapp at runtime that either cannot be or is not convenient to be coded into a web.xml <code><env-entry></code>. In this case, you can use <code>org.mortbay.jetty.plus.naming.EnvEntry</code> and even configure them to override an entry of the same name in web.xml.
 +
 +
<source lang="xml">
 +
<New class="org.mortbay.jetty.plus.naming.EnvEntry">
 +
  <Arg></Arg>
 +
  <Arg>mySpecialValue</Arg>
 +
  <Arg type="java.lang.Integer">4000</Arg>
 +
  <Arg type="boolean">true</Arg>
 +
</New>
 +
</source>
 +
 +
This example will define a virtual <code><env-entry></code> called <code>mySpecialValue</code> with value <code>4000</code> that is unique within the whole jvm. It will be put into JNDI at <code>java:comp/env/mySpecialValue</code> for _every_ webapp deployed. Moreover, the boolean argument indicates that this value should override an <code>env-entry</code> of the same name in web.xml. If you don't want to override, then omit this argument or set it to <code>false</code>.
 +
 +
See [Global or scoped to a webapp|#global|Global or scoped to a webapp] for more information on other scopes.
 +
 +
Note that the Servlet Specification only allows the following types of object to be bound to an <code>env-entry</code>:
 +
 +
* java.lang.String
 +
* java.lang.Integer
 +
* java.lang.Float
 +
* java.lang.Double
 +
* java.lang.Long
 +
* java.lang.Short
 +
* java.lang.Character
 +
* java.lang.Byte
 +
* java.lang.Boolean
 +
 +
However, Jetty is a little more flexible and will also allow you to bind custom POJOs, [javax.naming.References|http://java.sun.com/j2se/1.5.0/docs/api/javax/naming/Reference.html] and [javax.naming.Referenceables|http://java.sun.com/j2se/1.5.0/docs/api/javax/naming/Referenceable.html]. Be aware if you take advantage of this feature that your web application will *not be portable*.
 +
 +
To use the <code>EnvEntry</code> configured above, use code in your servlet/filter/etc such as:
 +
 +
<source lang="java">
 +
import javax.naming.InitialContext;
 +
 +
InitialContext ic = new InitialContext();
 +
Integer mySpecialValue = (Integer)ic.lookup("java:comp/env/mySpecialValue");
 +
</source>
 +
 +
=== Configuring resource-refs and resource-env-refs ===
 +
 +
Any type of resource that you want to refer to in a web.xml file as a <code><resource-ref></code> or <code><resource-env-ref></code> can be configured using the <code>org.mortbay.naming.plus.Resource</code> type of naming entry. You provide the scope, the name of the object (relative to <code>java:comp/env</code>) and a POJO instance or a  javax.naming.Reference instance or javax.naming.Referenceable instance.
 +
 +
The [J2EE Specification|http://jcp.org/aboutJava/communityprocess/pr/jsr244/index.html] recommends that DataSources are stored in <code>java:comp/env/jdbc</code>, JMS connection factories under <code>java:comp/env/jms</code>, JavaMail connection factories under <code>java:comp/env/mail</code> and URL connection factories under <code>java:comp/env/url</code>. For example:
 +
 +
 +
|| Resource Type || Name in jetty.xml || Environment Lookup ||
 +
| javax.sql.DataSource | jdbc/myDB | java:comp/env/jdbc/myDB |
 +
| javax.jms.QueueConnectionFactory | jms/myQueue | java:comp/env/jms/myQueue |
 +
| javax.mail.Session | mail/myMailService | java:comp/env/mail/myMailService |
 +
 +
h3. Configuring DataSources
 +
 +
Lets look at an example of configuring a javax.sql.DataSource. Jetty can use any DataSource implementation available on it's classpath. In our example, we'll use a DataSource from the [Derby|http://db.apache.org/derby] relational database, but you can use any implementation of a <code>javax.sql.DataSource</code>. In this example, we'll configure it as scoped to a webapp with the id of 'wac':
 +
 +
<source lang="xml">
 +
<Configure id='wac' class="org.mortbay.jetty.webapp.WebAppContext">
 +
...
 +
<New id="myds" class="org.mortbay.jetty.plus.naming.Resource">
 +
  <Arg><Ref id="wac"/></Arg>
 +
  <Arg>jdbc/myds</Arg>
 +
  <Arg>
 +
    <New class="org.apache.derby.jdbc.EmbeddedDataSource">
 +
      <Set name="DatabaseName">test</Set>
 +
      <Set name="createDatabase">create</Set>
 +
    </New>
 +
  </Arg>
 +
</New>
 +
</Configure>
 +
</source>
 +
 +
The above would create an instance of <code>org.apache.derby.jdbc.EmbeddedDataSource</code>, call the two setter methods <code>setDatabaseName("test");</code> and <code>setCreateDatabase("create");</code> and bind it into the JNDI scope for the webapp. If you have the appropriate <resource-ref> setup in your web.xml, then it will be available from application lookups as <code>java:comp/env/jdbc/myds</code>.
 +
 +
To lookup your DataSource in your servlet/filter/etc do:
 +
 +
<source lang="java">
 +
import javax.naming.InitialContext;
 +
import javax.sql.DataSource;
 +
 +
InitialContext ic = new InitialContext();
 +
DataSource myDS = (DataSource)ic.lookup("java:comp/env/jdbc/myds");
 +
</source>
 +
 +
{note:title=Careful!}
 +
When configuring Resources, you need to ensure that the type of object you configure matches the type of object you expect to lookup in <code>java:comp/env</code>. For database connection factories, this means that the object you register as a Resource *must* implement the <code>javax.sql.DataSource</code> interface.
 +
{note}
 +
 +
There are [more examples|DataSource Examples] of DataSources for various databases [here|DataSource Examples].
 +
 +
h3. Configuring JMS Queues, Topics and ConnectionFactories
 +
 +
Jetty is able to bind any implementation of the JMS destinations and connection factories. You just need to ensure the implementation jars are available on Jetty's classpath.
 +
 +
Here's an example of binding an [ActiveMQ|http://www.activemq.org] in-JVM connection factory:
 +
 +
<source lang="xml">
 +
<Configure id='wac' class="org.mortbay.jetty.webapp.WebAppContext">
 +
...
 +
<New id="cf" class="org.mortbay.jetty.plus.naming.Resource">
 +
  <Arg><Ref id='wac'/></Arg>
 +
  <Arg>jms/connectionFactory</Arg>
 +
  <Arg>
 +
    <New class="org.apache.activemq.ActiveMQConnectionFactory">
 +
      <Arg>vm://localhost?broker.persistent=false</Arg>
 +
    </New>
 +
  </Arg>
 +
</New>
 +
</Configure>
 +
</source>
 +
 +
There is more information about [ActiveMQ|http://www.activemq.org] and Jetty [here|Integrating with ActiveMQ].
 +
 +
h3. Configuring Mail
 +
 +
Jetty also provides infrastructure for providing access to javax.mail.Sessions from within an application:
 +
 +
<source lang="xml">
 +
<Configure id='wac' class="org.mortbay.jetty.webapp.WebAppContext">
 +
...
 +
<New id="mail" class="org.mortbay.jetty.plus.naming.Resource">
 +
    <Arg><Ref id="wac"/></Arg>
 +
    <Arg>mail/Session</Arg>
 +
    <Arg>
 +
      <New class="org.mortbay.naming.factories.MailSessionReference">
 +
        <Set name="user">fred</Set>
 +
        <Set name="password">OBF:1xmk1w261z0f1w1c1xmq</Set>
 +
        <Set name="properties">
 +
          <New class="java.util.Properties">
 +
            <Put name="mail.smtp.host">XXX</Put>
 +
            <Put name="mail.from">me@me</Put>
 +
            <Put name="mail.debug">true</Put>
 +
          </New>
 +
          </Set>
 +
      </New>
 +
    </Arg>
 +
</New>
 +
</Configure>
 +
</source>
 +
 +
The setup above creates an instance of the <code>org.mortbay.naming.factories.MailSessionReference</code> class, calls it's setter methods <code>setUser("fred");</code>, <code>setPassword("OBF:1xmk1w261z0f1w1c1xmq");</code> to set up the authentication for the mail system, then populates a set of Properties, setting them on the MailSessionReference instance. The result of this is that an application can lookup <code>java:comp/env/mail/Session</code> at runtime and obtain access to a <code>javax.mail.Session</code> that has the necessary configuration to permit it to send email via SMTP.
 +
 +
{tip}
 +
You can set the password to be plain text, or use Jetty's [password obfuscation|http://docs.codehaus.org/display/JETTY/Securing+Passwords] mechanism to make the config file more secure from prying eyes. Note that the other Jetty encryption mechanisms of MD5 and Crypt cannot be used as the original password cannot be recovered, which is necessary for the mail system.
 +
{tip}
 +
 +
We will be adding more examples of configuring database datasources (eg using [XAPool|http://xapool.experlog.com] and [DBCP|http://jakarta.apache.org/commons/dbcp]) and jms connection factories, so check back regularly. Contributions are also welcome.
 +
 +
=== Configuring XA Transactions ===
 +
 +
If you want to be able to perform distributed transactions with your resources, you will need a transaction manager that supports the JTA interfaces that you can lookup as <code>java:comp/UserTransaction</code> in your webapp. Jetty does not ship with one, rather you may plug in the one of your preference. You can configure the one of your choice using the <code>org.mortbay.jetty.plus.naming.Transaction</code> object in a jetty config file. In the following example, we will configure the [Atomikos|http://www.atomikos.com] transaction manager:
 +
 +
<source lang="xml">
 +
<New id="tx" class="org.mortbay.jetty.plus.naming.Transaction">
 +
  <Arg>
 +
    <New class="com.atomikos.icatch.jta.J2eeUserTransaction"/>
 +
  </Arg>
 +
</New>
 +
</source>
 +
 +
{tip:title=Hint} In order to use the Atomikos transaction manager, you will need to download it and install it. There are instructions [here|Atomikos] on how to configure it for jetty6.
 +
{tip}
 +
 +
See also the instructions for how to configure [JOTM]. Contributions of instructions for other transaction managers are welcome.
 +
 +
=== Configuring Links ===
 +
 +
Usually, the name you configure for your NamingEntry should be the same as the name you refer to it as in you web.xml. For example:
 +
 +
<source lang="xml">
 +
In a context xml file:
 +
 +
<Configure id='wac' class="org.mortbay.jetty.webapp.WebAppContext">
 +
...
 +
<New id="myds" class="org.mortbay.jetty.plus.naming.Resource">
 +
  <Arg><Ref id="wac"/></Arg>
 +
  <Arg>jdbc/mydatasource</Arg>
 +
  <Arg>
 +
    <New class="org.apache.derby.jdbc.EmbeddedDataSource">
 +
      <Set name="DatabaseName">test</Set>
 +
      <Set name="createDatabase">create</Set>
 +
    </New>
 +
  </Arg>
 +
</New>
 +
</Configure>
 +
</source>
 +
 +
and in web.xml:
 +
 +
<source lang="xml">
 +
  <resource-ref>
 +
    <res-ref-name>jdbc/mydatasource</res-ref-name>
 +
    <res-type>javax.sql.DataSource</res-type>
 +
    <res-auth>Container</res-auth>
 +
    <injection-target>
 +
      <injection-target-class>com.acme.JNDITest</injection-target-class>
 +
      <injection-target-name>myDatasource</injection-target-name>
 +
    </injection-target>
 +
  </resource-ref>
 +
</source>
 +
 +
If you wish, you can refer to it in web.xml by a different name, and link it to the name in your org.mortbay.jetty.plus.naming.Resource by using an org.mortbay.jetty.plus.naming.Link type of NamingEntry. For the example above, we could refer to the <code>jdbc/mydatasource</code> resource as <code>jdbc/mydatasource1}
 +
by doing:
 +
 +
In a context xml file:
 +
 +
<source lang="xml">
 +
<Configure id='wac' class="org.mortbay.jetty.webapp.WebAppContext">
 +
...
 +
<New id="myds" class="org.mortbay.jetty.plus.naming.Resource">
 +
  <Arg><Ref id="wac"/></Arg>
 +
  <Arg>jdbc/mydatasource</Arg>
 +
  <Arg>
 +
    <New class="org.apache.derby.jdbc.EmbeddedDataSource">
 +
      <Set name="DatabaseName">test</Set>
 +
      <Set name="createDatabase">create</Set>
 +
    </New>
 +
  </Arg>
 +
</New>
 +
</Configure>
 +
 +
 +
in a jetty-env.xml file:
 +
<source lang="xml">
 +
  <New id="map1" class="org.mortbay.jetty.plus.naming.Link">
 +
    <Arg><Ref id='wac'/></Arg>
 +
    <Arg>jdbc/mydatasource1</Arg> <!-- name in web.xml -->
 +
    <Arg>jdbc/mydatasource</Arg>  <!-- name in container environment -->
 +
  </New>
 +
</source>
 +
 +
and in web.xml:
 +
<source lang="xml">
 +
  <resource-ref>
 +
    <res-ref-name>jdbc/mydatasource1</res-ref-name>
 +
    <res-type>javax.sql.DataSource</res-type>
 +
    <res-auth>Container</res-auth>
 +
    <injection-target>
 +
      <injection-target-class>com.acme.JNDITest</injection-target-class>
 +
      <injection-target-name>myDatasource</injection-target-name>
 +
    </injection-target>
 +
  </resource-ref>
 +
</source>
 +
 +
This can be useful when you cannot change web.xml but need to link it to a resource in your deployment environment.
 +
 +
=== Global or scoped to a webapp ===
 +
As we said before, you can control the visibility of your JNDI naming entries within your jvm, Server and WebAppContext instances. Naming entries at the _jvm scope_ are visible by any application code, and are available to be bound to java:comp/env. Naming entries at the _Server scope_ will not interfere with entries of the same name in a different Server instance, and are avilable to be bound to java:comp/env of any webapps deployed to that Server instance. Finally, the most specific scope are entries at the _webapp scope_. These are only available to be bound to java:comp/env of the webapp in which it is defined.
 +
 +
The scope is controlled by the 1st parameter to the NamingEntry.
 +
 +
The jvm scope is represented by a null parameter:
 +
<source lang="xml">
 +
<New id="cf" class="org.mortbay.jetty.plus.naming.Resource">
 +
  <Arg></Arg>
 +
  <Arg>jms/connectionFactory</Arg>
 +
  <Arg>
 +
    <New class="org.apache.activemq.ActiveMQConnectionFactory">
 +
      <Arg>vm://localhost?broker.persistent=false</Arg>
 +
    </New>
 +
  </Arg>
 +
</New>
 +
</source>
 +
 +
The Server scope is represented by referencing the related Server object:
 +
<source lang="xml">
 +
<Configure id="Server" class="org.mortbay.jetty.Server">
 +
...
 +
<New id="cf" class="org.mortbay.jetty.plus.naming.Resource">
 +
  <Arg><Ref id="Server"/></Arg>
 +
  <Arg>jms/connectionFactory</Arg>
 +
  <Arg>
 +
    <New class="org.apache.activemq.ActiveMQConnectionFactory">
 +
      <Arg>vm://localhost?broker.persistent=false</Arg>
 +
    </New>
 +
  </Arg>
 +
</New>
 +
</Configure>
 +
</source>
 +
 +
The webapp scope is represented by referencing the related WebAppContext object:
 +
<source lang="xml">
 +
<Configure id='wac' class="org.mortbay.jetty.webapp.WebAppContext">
 +
...
 +
<New id="cf" class="org.mortbay.jetty.plus.naming.Resource">
 +
  <Arg><Ref id='wac'/></Arg>
 +
  <Arg>jms/connectionFactory</Arg>
 +
  <Arg>
 +
    <New class="org.apache.activemq.ActiveMQConnectionFactory">
 +
      <Arg>vm://localhost?broker.persistent=false</Arg>
 +
    </New>
 +
  </Arg>
 +
</New>
 +
</Configure>
 +
</source>
 +
 +
As you can see, the most natural config files in which to declare naming entries of each scope are:
 +
* <code>etc/jetty.xml</code> - jvm or Server scope
 +
* <code>WEB-INF/jetty-env.xml</code> or a context xml file - webapp scope
 +
 +
 +
=== Demo Web Application ===
 +
 +
There is a demonstration webapp which sets up examples of all of the JNDI resources we've discussed so far.
 +
 +
In order to run this demonstration, you will need to download the transaction manager of your choice and [Derby|http://db.apache.org/derby] . At the time of writing, the webapp has been tested with both [JOTM|http://jotm.objectweb.org] and with [Atomikos|http://www.atomikos.com] transaction managers.
 +
 +
h3. Building the Demo
 +
 +
As the demo webapp is not pre-built with the distribution, you first have to build it. It is located in <code>examples/test-jndi-webapp</code>. There is a <code>README.txt</code> file in there which explains how to build it, and how to add support for different transaction managers.
 +
 +
* run <code>mvn clean install</code> to build it
 +
* then edit <code>contexts/test-jndi.xml</code> and uncomment one of the transaction manager setups
 +
* then edit <code>contexts/test-jndi.d/WEB-INF/jetty-env.xml</code> and uncomment one of the transaction manager setups
 +
* copy a <code>derby.jar</code> to the jetty <code>lib/</code> directory, as well as copy all the necessary jars for the flavour of transaction manager you are using. There are instructions for some of the popular transaction managers on the wiki at [JNDI]
 +
 +
You run the demo like so:
 +
 +
For jetty 6.x:
 +
<source lang="bash">
 +
java -jar start.jar
 +
</source>
 +
 +
For jetty 7.x:
 +
<source lang="bash">
 +
java -DOPTIONS=plus,ext,default -jar start.jar
 +
</source>
 +
 +
The URL for the demonstration is at:
 +
 +
<code>http://localhost:8080/test-jndi</code>
 +
 +
 +
h4. Adding Support for a Different Transaction Manager
 +
 +
# Edit the <code>src/etc/templates/filter.properties</code> file and add a new set of token and replacement strings following the pattern established for ATOMIKOS and JOTM.
 +
# Edit the <code>src/etc/templates/jetty-env.xml</code> file and add configuration for new transaction manager following the pattern established for the other transaction managers.
 +
# Edit the <code>src/etc/templates/jetty-test-jndi.xml</code> file and add configuration for the new transaction manager following the pattern established for the other transaction managers.
 
}}
 
}}

Revision as of 14:40, 8 June 2010



Introduction

Jetty supports java:comp/env lookups in webapps. This is an optional feature, and as such some setup needs to be done. However, if you're using the [Hightide] distribution of jetty, then this feature is already fully enabled for you, so you can skip any setup steps, and read the section on how to put objects into jetty's JNDI so that you can retrieve them at runtime.

Feature

Setup

Deployment-time configuration

Skip this step if you are using the Hightide distribution of jetty as JNDI is automatically enabled for you. For non-Hightide distributions, you may enable JNDI either for a particular web app or you can enable it by default for all webapps. In either case, we need to re-define the list of configurations that can be applied to a WebAppContext on deployment:

<Array id="plusConfig" type="java.lang.String">
  <Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
  <Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
  <Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
  <Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
  <Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item> <!-- add -->
  <Item>org.eclipse.jetty.plus.webapp.Configuration</Item>    <!-- add -->
  <Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
  <Item>org.eclipse.jetty.webapp.TagLibConfiguration</Item>
</Array>

Now, to apply this to a single webapp, we create a context xml file that describes the setup of that particular webapp and instruct it to apply these special configurations on deployment:

<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
 
  <Array id="plusConfig" type="java.lang.String">
    <Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
    <Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
    <Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
    <Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
    <Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item> <!-- add for JNDI -->
    <Item>org.eclipse.jetty.plus.webapp.Configuration</Item>    <!-- add for JNDI -->
    <Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
    <Item>org.eclipse.jetty.webapp.TagLibConfiguration</Item>
  </Array>
 
  <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/my-cool-webapp</Set>
  <Set name="configurationClasses"><Ref id="plusConfig"/></Set>
 
</Configure>

Alternatively, we could apply these configurations to every webapp that is deployed. To do that, we edit the $JETTY_HOME/etc/jetty.xml file (or create a new new file and put it on the runline or put it in start.ini) to add:

<Configure id="Server" class="org.eclipse.jetty.server.Server">
 
    <Call name="setAttribute">
      <Arg>org.eclipse.jetty.webapp.configuration</Arg>
      <Arg>
          <Array type="java.lang.String">
              <Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
              <Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
              <Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
              <Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
              <Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
              <Item>org.eclipse.jetty.plus.webapp.Configuration</Item>
              <Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
              <Item>org.eclipse.jetty.webapp.TagLibConfiguration</Item>
          </Array>
      </Arg>
    </Call>
</Configure>
Idea.png
Tip:
We have included the etc/jetty-plus.xml configuration file that configures a WebAppDeployer to deploy all webapps in the webapps-plus directory with JNDI. You can modify this file as desired, or merge it with your etc/jetty.xml file.


Classpath jars

Now that we have the JNDI configuration for the webapp(s) set up, we need to ensure that the JNDI implementation jars are on jetty's classpath. These jars are optional, so won't be there by default. We add these into the classpath by using startup time OPTIONS:

java -jar start.jar OPTIONS=plus
Idea.png
Tip:
If you prefer, you can edit the start.ini file and add "plus" to the default OPTIONS to lessen the verbosity of the runline.

You may now configure naming resources that can be referenced in a web.xml file and accessed from within the java:comp/env naming environment of the webapp during execution. Specifically, you may configure support for the following web.xml elements:

<env-entry/>
<resource-ref/>
<resource-env-ref/>

#Configuring_env-entrys shows you how to set up overrides for <env-entry> elements in web.xml. [Configuring resource-refs and resource-env-refs