3
Licensed to the Apache Software Foundation (ASF) under one or more
4
contributor license agreements. See the NOTICE file distributed with
5
this work for additional information regarding copyright ownership.
6
The ASF licenses this file to You under the Apache License, Version 2.0
7
(the "License"); you may not use this file except in compliance with
8
the License. You may obtain a copy of the License at
10
http://www.apache.org/licenses/LICENSE-2.0
12
Unless required by applicable law or agreed to in writing, software
13
distributed under the License is distributed on an "AS IS" BASIS,
14
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
See the License for the specific language governing permissions and
16
limitations under the License.
19
<!ENTITY project SYSTEM "project.xml">
21
<document url="jndi-resources-howto.html">
26
<author email="craigmcc@apache.org">Craig R. McClanahan</author>
27
<author email="yoavs@apache.org">Yoav Shapira</author>
28
<title>JNDI Resources HOW-TO</title>
33
<section name="Table of Contents">
37
<section name="Introduction">
39
<p>Tomcat 6 provides a JNDI <strong>InitialContext</strong> implementation
40
instance for each web application running under it, in a manner that is
41
compatible with those provided by a
42
<a href="http://java.sun.com/j2ee">Java2 Enterprise Edition</a> application
43
server. The J2EE standard provides a standard set of elements in the
44
<code>/WEB-INF/web.xml</code> file to reference/define resources.</p>
46
<p>See the following Specifications for more information about programming APIs
47
for JNDI, and for the features supported by Java2 Enterprise Edition (J2EE)
48
servers, which Tomcat emulates for the services that it provides:</p>
50
<li><a href="http://java.sun.com/products/jndi">Java Naming and Directory
51
Interface</a> (included in JDK 1.4 onwards)</li>
52
<li><a href="http://java.sun.com/j2ee/download.html">J2EE Platform
53
Specification</a> (in particular, see Chapter 5 on <em>Naming</em>)</li>
58
<section name="web.xml configuration" >
60
<p>The following elements may be used in the web application deployment
61
descriptor (<code>/WEB-INF/web.xml</code>) of your web application to define
64
<li><code><strong><env-entry></strong></code> - Environment entry, a
65
single-value parameter that can be used to configure how the application
67
<li><code><strong><resource-ref></strong></code> - Resource reference,
68
which is typically to an object factory for resources such as a JDBC
69
<code>DataSource</code>, a JavaMail <code>Session</code>, or custom
70
object factories configured into Tomcat 6.</li>
71
<li><code><strong><resource-env-ref></strong></code> - Resource
72
environment reference, a new variation of <code>resource-ref</code>
73
added in Servlet 2.4 that is simpler to configure for resources
74
that do not require authentication information.</li>
77
<p>Providing that Tomcat is able to identify an appropriate resource factory to
78
use to create the resource and that no further configuration information is
79
required, Tomcat will use the information in <code>/WEB-INF/web.xml</code> to
80
create the resource.</p>
83
<section name="context.xml configuration">
85
<p>If Tomcat is unable to identify the appropriate resource factory and/or
86
additional configuration information is required, additional Tomcat specific
87
configuration must be specified before Tomcat can create the resource.
88
Tomcat specific resource configuration is entered in
89
the <a href="config/context.html"><code><Context></code></a> elements that
90
can be specified in either <code>$CATALINA_BASE/conf/server.xml</code> or,
91
preferably, the per-web-application context XML file
92
(<code>META-INF/context.xml</code>).</p>
94
<p>Tomcat specific resource configuration is performed using the following
95
elements in the <a href="config/context.html"><code><Context></code></a>
99
<li><a href="config/context.html#Environment Entries"><Environment></a> -
100
Configure names and values for scalar environment entries that will be
101
exposed to the web application through the JNDI
102
<code>InitialContext</code> (equivalent to the inclusion of an
103
<code><env-entry></code> element in the web application
104
deployment descriptor).</li>
105
<li><a href="config/context.html#Resource Definitions"><Resource></a> -
106
Configure the name and data type of a resource made available to the
107
application (equivalent to the inclusion of a
108
<code><resource-ref></code> element in the web application
109
deployment descriptor).</li>
110
<li><a href="config/context.html#Resource Links"><ResourceLink></a> -
111
Add a link to a resource defined in the global JNDI context. Use resource
112
links to give a web application access to a resource defined in
113
the<a href="config/globalresources.html"><GlobalNamingResources></a>
114
child element of the <a href="config/server.html"><Server></a>
116
<li><a href="config/context.html#Transaction"><Transaction></a> -
117
Add a resource factory for instantiating the UserTransaction object
118
instance that is available at <code>java:comp/UserTransaction</code>.</li>
122
<p>Any number of these elements may be nested inside a
123
<a href="config/context.html"><code><Context></code></a> element and will
124
be associated only with that particular web application.</p>
126
<p>If a resource has been defined in a
127
<a href="config/context.html"><code><Context></code></a> element it is not
128
necessary for that resource to be defined in <code>/WEB-INF/web.xml</code>.
129
However, it is recommended to keep the entry in <code>/WEB-INF/web.xml</code>
130
to document the resource requirements for the web application.</p>
132
<p>Where the same resource name has been defined for a
133
<code><env-entry></code> element included in the web application
134
deployment descriptor (<code>/WEB-INF/web.xml</code>) and in an
135
<code><Environment></code> element as part of the
136
<a href="config/context.html"><code><Context></code></a> element for the
137
web application, the values in the deployment descriptor will take precedence
138
<strong>only</strong> if allowed by the corresponding
139
<code><Environment></code> element (by setting the <code>override</code>
140
attribute to "true").</p>
144
<section name="Global configuration">
146
<p>Tomcat 6 maintains a separate namespace of global resources for the
147
entire server. These are configured in the
148
<a href="config/globalresources.html">
149
<code><strong><GlobalNameingResources></strong></code></a> element of
150
<code>$CATALINA_BASE/conf/server.xml</code>. You may expose these resources to
151
web applications by using a
152
<a href="config/context.html#Resource Links"><ResourceLink></a> to
153
include it in the per-web-application context.</p>
155
<p>If a resource has been defined using a
156
<a href="config/context.html#Resource Links"><ResourceLink></a>, it is not
157
necessary for that resource to be defined in <code>/WEB-INF/web.xml</code>.
158
However, it is recommended to keep the entry in <code>/WEB-INF/web.xml</code>
159
to document the resource requirements for the web application.</p>
163
<section name="Using resources">
165
<p>The <code>InitialContext</code> is configured as a web application is
166
initially deployed, and is made available to web application components (for
167
read-only access). All configured entries and resources are placed in
168
the <code>java:comp/env</code> portion of the JNDI namespace, so a typical
169
access to a resource - in this case, to a JDBC <code>DataSource</code> -
170
would look something like this:</p>
173
// Obtain our environment naming context
174
Context initCtx = new InitialContext();
175
Context envCtx = (Context) initCtx.lookup("java:comp/env");
177
// Look up our data source
178
DataSource ds = (DataSource)
179
envCtx.lookup("jdbc/EmployeeDB");
181
// Allocate and use a connection from the pool
182
Connection conn = ds.getConnection();
183
... use this connection to access the database ...
189
<section name="Tomcat Standard Resource Factories">
191
<p>Tomcat 6 includes a series of standard resource factories that can
192
provide services to your web applications, but give you configuration
194
<a href="config/context.html"><code><Context></code></a> element)
195
without modifying the web application or the deployment descriptor. Each
196
subsection below details the configuration and usage of the standard resource
199
<p>See <a href="#Adding Custom Resource Factories">Adding Custom
200
Resource Factories</a> for information about how to create, install,
201
configure, and use your own custom resource factory classes with
204
<p><em>NOTE</em> - Of the standard resource factories, only the
205
"JDBC Data Source" and "User Transaction" factories are mandated to
206
be available on other platforms, and then they are required only if
207
the platform implements the Java2 Enterprise Edition (J2EE) specs.
208
All other standard resource factories, plus custom resource factories
209
that you write yourself, are specific to Tomcat and cannot be assumed
210
to be available on other containers.</p>
212
<subsection name="Generic JavaBean Resources">
214
<h3>0. Introduction</h3>
216
<p>This resource factory can be used to create objects of <em>any</em>
217
Java class that conforms to standard JavaBeans naming conventions (i.e.
218
it has a zero-arguments constructor, and has property setters that
219
conform to the setFoo() naming pattern. The resource factory will
220
create a new instance of the appropriate bean class every time a
221
<code>lookup()</code> for this entry is made.</p>
223
<p>The steps required to use this facility are described below.</p>
225
<h3>1. Create Your JavaBean Class</h3>
227
<p>Create the JavaBean class which will be instantiated each time
228
that the resource factory is looked up. For this example, assume
229
you create a class <code>com.mycompany.MyBean</code>, which looks
233
package com.mycompany;
235
public class MyBean {
237
private String foo = "Default Foo";
239
public String getFoo() {
243
public void setFoo(String foo) {
249
public int getBar() {
253
public void setBar(int bar) {
261
<h3>2. Declare Your Resource Requirements</h3>
263
<p>Next, modify your web application deployment descriptor
264
(<code>/WEB-INF/web.xml</code>) to declare the JNDI name under which
265
you will request new instances of this bean. The simplest approach is
266
to use a <code><resource-env-ref></code> element, like this:</p>
269
<resource-env-ref>
271
Object factory for MyBean instances.
273
<resource-env-ref-name>
275
</resource-env-ref-name>
276
<resource-env-ref-type>
278
</resource-env-ref-type>
279
</resource-env-ref>
282
<p><strong>WARNING</strong> - Be sure you respect the element ordering
283
that is required by the DTD for web application deployment descriptors!
285
<a href="http://java.sun.com/products/servlet/download.html">Servlet
286
Specification</a> for details.</p>
288
<h3>3. Code Your Application's Use Of This Resource</h3>
290
<p>A typical use of this resource environment reference might look
294
Context initCtx = new InitialContext();
295
Context envCtx = (Context) initCtx.lookup("java:comp/env");
296
MyBean bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");
298
writer.println("foo = " + bean.getFoo() + ", bar = " +
302
<h3>4. Configure Tomcat's Resource Factory</h3>
304
<p>To configure Tomcat's resource factory, add an element like this to the
305
<a href="config/context.html"><code><Context></code></a> element for
306
this web application.</p>
311
<Resource name="bean/MyBeanFactory" auth="Container"
312
type="com.mycompany.MyBean"
313
factory="org.apache.naming.factory.BeanFactory"
319
<p>Note that the resource name (here, <code>bean/MyBeanFactory</code>
320
must match the value specified in the web application deployment
321
descriptor. We are also initializing the value of the <code>bar</code>
322
property, which will cause <code>setBar(23)</code> to be called before
323
the new bean is returned. Because we are not initializing the
324
<code>foo</code> property (although we could have), the bean will
325
contain whatever default value is set up by its constructor.</p>
330
<subsection name="UserDatabase Resources">
332
<h3>0. Introduction</h3>
334
<p>UserDatabase resources are typically configured as global resources for
335
use by a UserDatabase realm. Tomcat includes a UserDatabaseFactoory that
336
creates UserDatabase resources backed by an XML file - usually
337
<code>tomcat-users.xml</code></p>
339
<p>The steps required to set up a global UserDatabase resource are described
342
<h3>1. Create/edit the XML file</h3>
344
<p>The XMl file is typically located at
345
<code>$CATALINA_BASE/conf/tomcat-users.xml</code> however, you are free to
346
locate the file anywhere on the file system. It is recommended that the XML
347
files are placed in <code>$CATALINA_BASE/conf</code>. A typical XML would
351
<?xml version='1.0' encoding='utf-8'?>
353
<role rolename="tomcat"/>
354
<role rolename="role1"/>
355
<user username="tomcat" password="tomcat" roles="tomcat"/>
356
<user username="both" password="tomcat" roles="tomcat,role1"/>
357
<user username="role1" password="tomcat" roles="role1"/>
358
</tomcat-users>
361
<h3>2. Declare Your Resource</h3>
363
<p>Next, modify <code>$CATALINA_BASE/conf/server.xml</code> to create the
364
UserDatabase resource based on your XMl file. It should look something like
368
<Resource name="UserDatabase"
370
type="org.apache.catalina.UserDatabase"
371
description="User database that can be updated and saved"
372
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
373
pathname="conf/tomcat-users.xml"
374
readonly="false" />
377
<p>The <code>pathname</code> attribute can be absolute or relative. If
378
relative, it is relative to <code>$CATALINA_BASE</code>.</p>
380
<p>The <code>readonly</code> attribute is optional and defaults to
381
<code>false</code> if not supplied. If the XML is writeable then it will be
382
written to when Tomcat starts. <strong>WARNING:</strong> When the file is
383
written it will inherit the default file permissions for the user Tomcat
384
is running as. Ensure that these are appropriate to maintain the security
385
of your installation.</p>
387
<h3>3. Configure the Realm</h3>
389
<p>Configure a UserDatabase Realm to use this resource as described in the
390
<a href="config/realm.html">Realm configuration documentation</a>.</p>
395
<subsection name="JavaMail Sessions">
397
<h3>0. Introduction</h3>
399
<p>In many web applications, sending electronic mail messages is a
400
required part of the system's functionality. The
401
<a href="http://java.sun.com/products/javamail">Java Mail</a> API
402
makes this process relatively straightforward, but requires many
403
configuration details that the client application must be aware of
404
(including the name of the SMTP host to be used for message sending).</p>
406
<p>Tomcat 6 includes a standard resource factory that will create
407
<code>javax.mail.Session</code> session instances for you, already
408
configured to connect to an SMTP server.
409
In this way, the application is totally insulated from changes in the
410
email server configuration environment - it simply asks for, and receives,
411
a preconfigured session whenever needed.</p>
413
<p>The steps required for this are outlined below.</p>
415
<h3>1. Declare Your Resource Requirements</h3>
417
<p>The first thing you should do is modify the web application deployment
418
descriptor (<code>/WEB-INF/web.xml</code>) to declare the JNDI name under
419
which you will look up preconfigured sessions. By convention, all such
420
names should resolve to the <code>mail</code> subcontext (relative to the
421
standard <code>java:comp/env</code> naming context that is the root of
422
all provided resource factories. A typical <code>web.xml</code> entry
423
might look like this:</p>
427
Resource reference to a factory for javax.mail.Session
428
instances that may be used for sending electronic mail
429
messages, preconfigured to connect to the appropriate
434
</res-ref-name>
441
</resource-ref>
444
<p><strong>WARNING</strong> - Be sure you respect the element ordering
445
that is required by the DTD for web application deployment descriptors!
447
<a href="http://java.sun.com/products/servlet/download.html">Servlet
448
Specification</a> for details.</p>
450
<h3>2. Code Your Application's Use Of This Resource</h3>
452
<p>A typical use of this resource reference might look like this:</p>
454
Context initCtx = new InitialContext();
455
Context envCtx = (Context) initCtx.lookup("java:comp/env");
456
Session session = (Session) envCtx.lookup("mail/Session");
458
Message message = new MimeMessage(session);
459
message.setFrom(new InternetAddress(request.getParameter("from"));
460
InternetAddress to[] = new InternetAddress[1];
461
to[0] = new InternetAddress(request.getParameter("to"));
462
message.setRecipients(Message.RecipientType.TO, to);
463
message.setSubject(request.getParameter("subject"));
464
message.setContent(request.getParameter("content"), "text/plain");
465
Transport.send(message);
468
<p>Note that the application uses the same resource reference name
469
that was declared in the web application deployment descriptor. This
470
is matched up against the resource factory that is configured in the
471
<a href="config/context.html"><code><Context></code></a> element
472
for the web application as described below.</p>
474
<h3>3. Configure Tomcat's Resource Factory</h3>
476
<p>To configure Tomcat's resource factory, add an elements like this to the
477
<a href="config/context.html"><code><Context></code></a> element for
478
this web application.</p>
483
<Resource name="mail/Session" auth="Container"
484
type="javax.mail.Session"
485
mail.smtp.host="localhost"/>
490
<p>Note that the resource name (here, <code>mail/Session</code>) must
491
match the value specified in the web application deployment descriptor.
492
Customize the value of the <code>mail.smtp.host</code> parameter to
493
point at the server that provides SMTP service for your network.</p>
495
<h3>4. Install the JavaMail libraries</h3>
497
<p><a href="http://java.sun.com/products/javamail/downloads/index.html" target="_blank">
498
Download the JavaMail API</a>. The JavaMail API requires the Java Activation
499
Framework (JAF) API as well. The Java Activation Framework can be downloaded
500
from <a href="http://java.sun.com/products/javabeans/glasgow/jaf.html">Sun's site</a>.
503
<p>This download includes 2 vital libraries for the configuration;
504
activation.jar and mail.jar. Unpackage both distributions and place
505
them into $CATALINA_HOME/lib so that they are available to
506
Tomcat during the initialization of the mail Session Resource.
507
<strong>Note:</strong> placing these jars in both $CATALINA_HOME/lib and a
508
web application's lib folder will cause an error, so ensure you have
509
them in the $CATALINA_HOME/lib location only.
512
<h3>Example Application</h3>
514
<p>The <code>/examples</code> application included with Tomcat contains
515
an example of utilizing this resource factory. It is accessed via the
516
"JSP Examples" link. The source code for the servlet that actually
517
sends the mail message is in
518
<code>/WEB-INF/classes/SendMailServlet.java</code>.</p>
520
<p><strong>WARNING</strong> - The default configuration assumes that there
521
is an SMTP server listing on port 25 on <code>localhost</code>. If this is
522
not the case, edit the
523
<a href="config/context.html"><code><Context></code></a> element for
524
this web application and modify the parameter value for the
525
<code>mail.smtp.host</code> parameter to be the host name of an SMTP server
530
<subsection name="JDBC Data Sources">
532
<h3>0. Introduction</h3>
534
<p>Many web applications need to access a database via a JDBC driver,
535
to support the functionality required by that application. The J2EE
536
Platform Specification requires J2EE Application Servers to make
537
available a <em>DataSource</em> implementation (that is, a connection
538
pool for JDBC connections) for this purpose. Tomcat 6 offers exactly
539
the same support, so that database-based applications you develop on
540
Tomcat using this service will run unchanged on any J2EE server.</p>
542
<p>For information about JDBC, you should consult the following:</p>
544
<li><a href="http://java.sun.com/products/jdbc/">http://java.sun.com/products/jdbc/</a> -
545
Home page for information about Java Database Connectivity.</li>
546
<li><a href="http://java.sun.com/j2se/1.3/docs/guide/jdbc/spec2/jdbc2.1.frame.html">http://java.sun.com/j2se/1.3/docs/guide/jdbc/spec2/jdbc2.1.frame.html</a> -
547
The JDBC 2.1 API Specification.</li>
548
<li><a href="http://java.sun.com/products/jdbc/jdbc20.stdext.pdf">http://java.sun.com/products/jdbc/jdbc20.stdext.pdf</a> -
549
The JDBC 2.0 Standard Extension API (including the
550
<code>javax.sql.DataSource</code> API). This package is now known
551
as the "JDBC Optional Package".</li>
552
<li><a href="http://java.sun.com/j2ee/download.html">http://java.sun.com/j2ee/download.html</a> -
553
The J2EE Platform Specification (covers the JDBC facilities that
554
all J2EE platforms must provide to applications).</li>
557
<p><strong>NOTE</strong> - The default data source support in Tomcat
558
is based on the <strong>DBCP</strong> connection pool from the
559
<a href="http://commons.apache.org/">Commons</a>
560
project. However, it is possible to use any other connection pool
561
that implements <code>javax.sql.DataSource</code>, by writing your
562
own custom resource factory, as described
563
<a href="#Adding Custom Resource Factories">below</a>.</p>
565
<h3>1. Install Your JDBC Driver</h3>
567
<p>Use of the <em>JDBC Data Sources</em> JNDI Resource Factory requires
568
that you make an appropriate JDBC driver available to both Tomcat internal
569
classes and to your web application. This is most easily accomplished by
570
installing the driver's JAR file(s) into the
571
<code>$CATALINA_HOME/lib</code> directory, which makes the driver
572
available both to the resource factory and to your application.</p>
574
<h3>2. Declare Your Resource Requirements</h3>
576
<p>Next, modify the web application deployment descriptor
577
(<code>/WEB-INF/web.xml</code>) to declare the JNDI name under
578
which you will look up preconfigured data source. By convention, all such
579
names should resolve to the <code>jdbc</code> subcontext (relative to the
580
standard <code>java:comp/env</code> naming context that is the root of
581
all provided resource factories. A typical <code>web.xml</code> entry
582
might look like this:</p>
586
Resource reference to a factory for java.sql.Connection
587
instances that may be used for talking to a particular
588
database that is configured in the <Context>
589
configurartion for the web application.
593
</res-ref-name>
600
</resource-ref>
603
<p><strong>WARNING</strong> - Be sure you respect the element ordering
604
that is required by the DTD for web application deployment descriptors!
606
<a href="http://java.sun.com/products/servlet/download.html">Servlet
607
Specification</a> for details.</p>
609
<h3>3. Code Your Application's Use Of This Resource</h3>
611
<p>A typical use of this resource reference might look like this:</p>
613
Context initCtx = new InitialContext();
614
Context envCtx = (Context) initCtx.lookup("java:comp/env");
615
DataSource ds = (DataSource)
616
envCtx.lookup("jdbc/EmployeeDB");
618
Connection conn = ds.getConnection();
619
... use this connection to access the database ...
623
<p>Note that the application uses the same resource reference name that was
624
declared in the web application deployment descriptor. This is matched up
625
against the resource factory that is configured in the
626
<a href="config/context.html"><code><Context></code></a> element for
627
the web application as described below.</p>
629
<h3>4. Configure Tomcat's Resource Factory</h3>
631
<p>To configure Tomcat's resource factory, add an element like this to the
632
<a href="config/context.html"><code><Context></code></a> element for
633
the web application.</p>
638
<Resource name="jdbc/EmployeeDB"
640
type="javax.sql.DataSource"
641
username="dbusername"
642
password="dbpassword"
643
driverClassName="org.hsql.jdbcDriver"
644
url="jdbc:HypersonicSQL:database"
651
<p>Note that the resource name (here, <code>jdbc/EmployeeDB</code>) must
652
match the value specified in the web application deployment descriptor.</p>
654
<p>This example assumes that you are using the HypersonicSQL database
655
JDBC driver. Customize the <code>driverClassName</code> and
656
<code>driverName</code> parameters to match your actual database's
657
JDBC driver and connection URL.</p>
659
<p>The configuration properties for Tomcat's standard data source
661
(<code>org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory</code>) are
664
<li><strong>driverClassName</strong> - Fully qualified Java class name
665
of the JDBC driver to be used.</li>
666
<li><strong>maxActive</strong> - The maximum number of active instances
667
that can be allocated from this pool at the same time.</li>
668
<li><strong>maxIdle</strong> - The maximum number of connections that
669
can sit idle in this pool at the same time.</li>
670
<li><strong>maxWait</strong> - The maximum number of milliseconds that the
671
pool will wait (when there are no available connections) for a
672
connection to be returned before throwing an exception.</li>
673
<li><strong>password</strong> - Database password to be passed to our
675
<li><strong>url</strong> - Connection URL to be passed to our JDBC driver.
676
(For backwards compatibility, the property <code>driverName</code>
677
is also recognized.)</li>
678
<li><strong>username</strong> - Database username to be passed to our
680
<li><strong>validationQuery</strong> - SQL query that can be used by the
681
pool to validate connections before they are returned to the
682
application. If specified, this query MUST be an SQL SELECT
683
statement that returns at least one row.</li>
685
<p>For more details, please refer to the commons-dbcp documentation.</p>
692
<section name="Adding Custom Resource Factories">
694
<p>If none of the standard resource factories meet your needs, you can write
695
your own factory and integrate it into Tomcat 6, and then configure the use
696
of this factory in the
697
<a href="config/context.html"><code><Context></code></a> element for
698
the web application. In the example below, we will create a factory that only
699
knows how to create <code>com.mycompany.MyBean</code> beans from the
700
<a href="#Generic JavaBean Resources">Generic JavaBean Resources</a> example
703
<h3>1. Write A Resource Factory Class</h3>
705
<p>You must write a class that implements the JNDI service provider
706
<code>javax.naming.spi.ObjectFactory</code> inteface. Every time your
707
web application calls <code>lookup()</code> on a context entry that is
708
bound to this factory, the <code>getObjectInstance()</code> method is
709
called, with the following arguments:</p>
711
<li><strong>Object obj</strong> - The (possibly null) object containing
712
location or reference information that can be used in creating an object.
713
For Tomcat, this will always be an object of type
714
<code>javax.naming.Reference</code>, which contains the class name of
715
this factory class, as well as the configuration properties (from the
716
<a href="config/context.html"><code><Context></code></a> for the
717
web application) to use in creating objects to be returned.</li>
718
<li><strong>Name name</strong> - The name to which this factory is bound
719
relative to <code>nameCtx</code>, or <code>null</code> if no name
721
<li><strong>Context nameCtx</strong> - The context relative to which the
722
<code>name</code> parameter is specified, or <code>null</code> if
723
<code>name</code> is relative to the default initial context.</li>
724
<li><strong>Hashtable environment</strong> - The (possibly null)
725
environment that is used in creating this object. This is generally
726
ignored in Tomcat object factories.</li>
729
<p>To create a resource factory that knows how to produce <code>MyBean</code>
730
instances, you might create a class like this:</p>
733
package com.mycompany;
735
import java.util.Enumeration;
736
import java.util.Hashtable;
737
import javax.naming.Context;
738
import javax.naming.Name;
739
import javax.naming.NamingException;
740
import javax.naming.RefAddr;
741
import javax.naming.Reference;
742
import javax.naming.spi.ObjectFactory;
744
public class MyBeanFactory implements ObjectFactory {
746
public Object getObjectInstance(Object obj,
747
Name name, Context nameCtx, Hashtable environment)
748
throws NamingException {
750
// Acquire an instance of our specified bean class
751
MyBean bean = new MyBean();
753
// Customize the bean properties from our attributes
754
Reference ref = (Reference) obj;
755
Enumeration addrs = ref.getAll();
756
while (addrs.hasMoreElements()) {
757
RefAddr addr = (RefAddr) addrs.nextElement();
758
String name = addr.getType();
759
String value = (String) addr.getContent();
760
if (name.equals("foo")) {
762
} else if (name.equals("bar")) {
764
bean.setBar(Integer.parseInt(value));
765
} catch (NumberFormatException e) {
766
throw new NamingException("Invalid 'bar' value " + value);
771
// Return the customized instance
779
<p>In this example, we are unconditionally creating a new instance of
780
the <code>com.mycompany.MyBean</code> class, and populating its properties
781
based on the parameters included in the <code><ResourceParams></code>
782
element that configures this factory (see below). You should note that any
783
parameter named <code>factory</code> should be skipped - that parameter is
784
used to specify the name of the factory class itself (in this case,
785
<code>com.mycompany.MyBeanFactory</code>) rather than a property of the
786
bean being configured.</p>
788
<p>For more information about <code>ObjectFactory</code>, see the
789
<a href="http://java.sun.com/products/jndi/docs.html">JNDI 1.2 Service
790
Provider Interface (SPI) Specification</a>.</p>
792
<p>You will need to compile this class against a class path that includes
793
all of the JAR files in the <code>$CATALINA_HOME/lib</code> directory. When you are through,
794
place the factory class (and the corresponding bean class) unpacked under
795
<code>$CATALINA_HOME/lib</code>, or in a JAR file inside
796
<code>$CATALINA_HOME/lib</code>. In this way, the required class
797
files are visible to both Catalina internal resources and your web
800
<h3>2. Declare Your Resource Requirements</h3>
802
<p>Next, modify your web application deployment descriptor
803
(<code>/WEB-INF/web.xml</code>) to declare the JNDI name under which
804
you will request new instances of this bean. The simplest approach is
805
to use a <code><resource-env-ref></code> element, like this:</p>
808
<resource-env-ref>
810
Object factory for MyBean instances.
812
<resource-env-ref-name>
814
</resource-env-ref-name>
815
<resource-env-ref-type>
817
</resource-env-ref-type>
818
<resource-env-ref>
821
<p><strong>WARNING</strong> - Be sure you respect the element ordering
822
that is required by the DTD for web application deployment descriptors!
824
<a href="http://java.sun.com/products/servlet/download.html">Servlet
825
Specification</a> for details.</p>
827
<h3>3. Code Your Application's Use Of This Resource</h3>
829
<p>A typical use of this resource environment reference might look
833
Context initCtx = new InitialContext();
834
Context envCtx = (Context) initCtx.lookup("java:comp/env");
835
MyBean bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");
837
writer.println("foo = " + bean.getFoo() + ", bar = " +
841
<h3>4. Configure Tomcat's Resource Factory</h3>
843
<p>To configure Tomcat's resource factory, add an elements like this to the
844
<a href="config/context.html"><code><Context></code></a> element for
845
this web application.</p>
850
<Resource name="bean/MyBeanFactory" auth="Container"
851
type="com.mycompany.MyBean"
852
factory="com.mycompany.MyBeanFactory"
858
<p>Note that the resource name (here, <code>bean/MyBeanFactory</code>
859
must match the value specified in the web application deployment
860
descriptor. We are also initializing the value of the <code>bar</code>
861
property, which will cause <code>setBar(23)</code> to be called before
862
the new bean is returned. Because we are not initializing the
863
<code>foo</code> property (although we could have), the bean will
864
contain whatever default value is set up by its constructor.</p>
866
<p>You will also note that, from the application developer's perspective,
867
the declaration of the resource environment reference, and the programming
868
used to request new instances, is identical to the approach used for the
869
<em>Generic JavaBean Resources</em> example. This illustrates one of the
870
advantages of using JNDI resources to encapsulate functionality - you can
871
change the underlying implementation without necessarily having to
872
modify applications using the resources, as long as you maintain