~ubuntu-branches/ubuntu/maverick/tomcat6/maverick

« back to all changes in this revision

Viewing changes to .pc/0005-change-default-DBCP-factory-class.patch/webapps/docs/jndi-resources-howto.xml

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2010-06-28 21:41:31 UTC
  • mfrom: (2.2.15 sid)
  • Revision ID: james.westby@ubuntu.com-20100628214131-3wktukpb3lgdf83h
Tags: 6.0.26-5
* Convert patches to dep3 format.
* Backport security fix from trunk to fix CVE-2010-1157. (Closes: #587447)
* Set urgency to medium due to the security fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0"?>
 
2
<!--
 
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
 
9
 
 
10
      http://www.apache.org/licenses/LICENSE-2.0
 
11
 
 
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.
 
17
-->
 
18
<!DOCTYPE document [
 
19
  <!ENTITY project SYSTEM "project.xml">
 
20
]>
 
21
<document url="jndi-resources-howto.html">
 
22
 
 
23
    &project;
 
24
 
 
25
    <properties>
 
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>
 
29
    </properties>
 
30
 
 
31
<body>
 
32
 
 
33
<section name="Table of Contents">
 
34
<toc/>
 
35
</section>
 
36
 
 
37
<section name="Introduction">
 
38
 
 
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>
 
45
 
 
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>
 
49
<ul>
 
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>
 
54
</ul>
 
55
 
 
56
</section>
 
57
 
 
58
<section name="web.xml configuration" >
 
59
 
 
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
 
62
resources:</p>
 
63
<ul>
 
64
<li><code><strong>&lt;env-entry&gt;</strong></code> - Environment entry, a
 
65
    single-value parameter that can be used to configure how the application
 
66
    will operate.</li>
 
67
<li><code><strong>&lt;resource-ref&gt;</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>&lt;resource-env-ref&gt;</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>
 
75
</ul>
 
76
 
 
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>
 
81
</section>
 
82
 
 
83
<section name="context.xml configuration">
 
84
 
 
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>&lt;Context&gt;</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>
 
93
 
 
94
<p>Tomcat specific resource configuration is performed using the following
 
95
elements in the <a href="config/context.html"><code>&lt;Context&gt;</code></a>
 
96
element:</p>
 
97
 
 
98
<ul>
 
99
<li><a href="config/context.html#Environment Entries">&lt;Environment&gt;</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>&lt;env-entry&gt;</code> element in the web application
 
104
    deployment descriptor).</li>
 
105
<li><a href="config/context.html#Resource Definitions">&lt;Resource&gt;</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>&lt;resource-ref&gt;</code> element in the web application
 
109
    deployment descriptor).</li>
 
110
<li><a href="config/context.html#Resource Links">&lt;ResourceLink&gt;</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">&lt;GlobalNamingResources&gt;</a>
 
114
    child element of the <a href="config/server.html">&lt;Server&gt;</a>
 
115
    element.</li>
 
116
<li><a href="config/context.html#Transaction">&lt;Transaction&gt;</a> -
 
117
    Add a resource factory for instantiating the UserTransaction object 
 
118
    instance that is available at <code>java:comp/UserTransaction</code>.</li>
 
119
 
 
120
</ul>
 
121
 
 
122
<p>Any number of these elements may be nested inside a
 
123
<a href="config/context.html"><code>&lt;Context&gt;</code></a> element and will
 
124
be associated only with that particular web application.</p>
 
125
 
 
126
<p>If a resource has been defined in a
 
127
<a href="config/context.html"><code>&lt;Context&gt;</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>
 
131
 
 
132
<p>Where the same resource name has been defined for a
 
133
<code>&lt;env-entry&gt;</code> element included in the web application
 
134
deployment descriptor (<code>/WEB-INF/web.xml</code>) and in an
 
135
<code>&lt;Environment&gt;</code> element as part of the
 
136
<a href="config/context.html"><code>&lt;Context&gt;</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>&lt;Environment&gt;</code> element (by setting the <code>override</code>
 
140
attribute to "true").</p>
 
141
 
 
142
</section>
 
143
 
 
144
<section name="Global configuration">
 
145
 
 
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>&lt;GlobalNameingResources&gt;</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">&lt;ResourceLink&gt;</a> to
 
153
include it in the per-web-application context.</p>
 
154
 
 
155
<p>If a resource has been defined using a 
 
156
<a href="config/context.html#Resource Links">&lt;ResourceLink&gt;</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>
 
160
 
 
161
</section>
 
162
 
 
163
<section name="Using resources">
 
164
 
 
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>
 
171
 
 
172
<source>
 
173
// Obtain our environment naming context
 
174
Context initCtx = new InitialContext();
 
175
Context envCtx = (Context) initCtx.lookup("java:comp/env");
 
176
 
 
177
// Look up our data source
 
178
DataSource ds = (DataSource)
 
179
  envCtx.lookup("jdbc/EmployeeDB");
 
180
 
 
181
// Allocate and use a connection from the pool
 
182
Connection conn = ds.getConnection();
 
183
... use this connection to access the database ...
 
184
conn.close();
 
185
</source>
 
186
 
 
187
</section>
 
188
 
 
189
<section name="Tomcat Standard Resource Factories">
 
190
 
 
191
  <p>Tomcat 6 includes a series of standard resource factories that can
 
192
  provide services to your web applications, but give you configuration
 
193
  flexibility (via the
 
194
  <a href="config/context.html"><code>&lt;Context&gt;</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
 
197
  factories.</p>
 
198
 
 
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
 
202
  Tomcat 6.</p>
 
203
 
 
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>
 
211
 
 
212
  <subsection name="Generic JavaBean Resources">
 
213
 
 
214
    <h3>0.  Introduction</h3>
 
215
 
 
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>
 
222
 
 
223
    <p>The steps required to use this facility are described below.</p>
 
224
 
 
225
    <h3>1.  Create Your JavaBean Class</h3>
 
226
 
 
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
 
230
    like this:</p>
 
231
 
 
232
<source>
 
233
package com.mycompany;
 
234
 
 
235
public class MyBean {
 
236
 
 
237
  private String foo = "Default Foo";
 
238
 
 
239
  public String getFoo() {
 
240
    return (this.foo);
 
241
  }
 
242
 
 
243
  public void setFoo(String foo) {
 
244
    this.foo = foo;
 
245
  }
 
246
 
 
247
  private int bar = 0;
 
248
 
 
249
  public int getBar() {
 
250
    return (this.bar);
 
251
  }
 
252
 
 
253
  public void setBar(int bar) {
 
254
    this.bar = bar;
 
255
  }
 
256
 
 
257
 
 
258
}
 
259
</source>
 
260
 
 
261
  <h3>2.  Declare Your Resource Requirements</h3>
 
262
 
 
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>&lt;resource-env-ref&gt;</code> element, like this:</p>
 
267
 
 
268
<source>
 
269
&lt;resource-env-ref&gt;
 
270
  &lt;description&gt;
 
271
    Object factory for MyBean instances.
 
272
  &lt;/description&gt;
 
273
  &lt;resource-env-ref-name&gt;
 
274
    bean/MyBeanFactory
 
275
  &lt;/resource-env-ref-name&gt;
 
276
  &lt;resource-env-ref-type&gt;
 
277
    com.mycompany.MyBean
 
278
  &lt;/resource-env-ref-type&gt;
 
279
&lt;/resource-env-ref&gt;
 
280
</source>
 
281
 
 
282
    <p><strong>WARNING</strong> - Be sure you respect the element ordering
 
283
    that is required by the DTD for web application deployment descriptors!
 
284
    See the
 
285
    <a href="http://java.sun.com/products/servlet/download.html">Servlet
 
286
    Specification</a> for details.</p>
 
287
 
 
288
  <h3>3.  Code Your Application's Use Of This Resource</h3>
 
289
 
 
290
  <p>A typical use of this resource environment reference might look
 
291
  like this:</p>
 
292
 
 
293
<source>
 
294
Context initCtx = new InitialContext();
 
295
Context envCtx = (Context) initCtx.lookup("java:comp/env");
 
296
MyBean bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");
 
297
 
 
298
writer.println("foo = " + bean.getFoo() + ", bar = " +
 
299
               bean.getBar());
 
300
</source>
 
301
 
 
302
    <h3>4.  Configure Tomcat's Resource Factory</h3>
 
303
 
 
304
    <p>To configure Tomcat's resource factory, add an element like this to the
 
305
    <a href="config/context.html"><code>&lt;Context&gt;</code></a> element for
 
306
    this web application.</p>
 
307
 
 
308
<source>
 
309
&lt;Context ...&gt;
 
310
  ...
 
311
  &lt;Resource name="bean/MyBeanFactory" auth="Container"
 
312
            type="com.mycompany.MyBean"
 
313
            factory="org.apache.naming.factory.BeanFactory"
 
314
            bar="23"/&gt;
 
315
  ...
 
316
&lt;/Context&gt;
 
317
</source>
 
318
 
 
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>
 
326
 
 
327
  </subsection>
 
328
 
 
329
 
 
330
  <subsection name="UserDatabase Resources">
 
331
 
 
332
    <h3>0.  Introduction</h3>
 
333
 
 
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>
 
338
 
 
339
    <p>The steps required to set up a global UserDatabase resource are described
 
340
    below.</p>
 
341
 
 
342
    <h3>1. Create/edit the XML file</h3>
 
343
 
 
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
 
348
    look like:</p>
 
349
 
 
350
<source>
 
351
&lt;?xml version='1.0' encoding='utf-8'?&gt;
 
352
&lt;tomcat-users&gt;
 
353
  &lt;role rolename="tomcat"/&gt;
 
354
  &lt;role rolename="role1"/&gt;
 
355
  &lt;user username="tomcat" password="tomcat" roles="tomcat"/&gt;
 
356
  &lt;user username="both" password="tomcat" roles="tomcat,role1"/&gt;
 
357
  &lt;user username="role1" password="tomcat" roles="role1"/&gt;
 
358
&lt;/tomcat-users&gt;
 
359
</source>
 
360
 
 
361
    <h3>2.  Declare Your Resource</h3>
 
362
 
 
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
 
365
    this:</p>
 
366
 
 
367
<source>
 
368
&lt;Resource name="UserDatabase"
 
369
          auth="Container"
 
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" /&gt;
 
375
</source>
 
376
 
 
377
    <p>The <code>pathname</code> attribute can be absolute or relative. If
 
378
    relative, it is relative to <code>$CATALINA_BASE</code>.</p>
 
379
    
 
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>
 
386
 
 
387
    <h3>3.  Configure the Realm</h3>
 
388
 
 
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>
 
391
 
 
392
  </subsection>
 
393
 
 
394
 
 
395
  <subsection name="JavaMail Sessions">
 
396
 
 
397
    <h3>0.  Introduction</h3>
 
398
 
 
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>
 
405
 
 
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>
 
412
 
 
413
    <p>The steps required for this are outlined below.</p>
 
414
 
 
415
    <h3>1.  Declare Your Resource Requirements</h3>
 
416
 
 
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>
 
424
<source>
 
425
&lt;resource-ref&gt;
 
426
  &lt;description&gt;
 
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
 
430
    SMTP server.
 
431
  &lt;/description&gt;
 
432
  &lt;res-ref-name&gt;
 
433
    mail/Session
 
434
  &lt;/res-ref-name&gt;
 
435
  &lt;res-type&gt;
 
436
    javax.mail.Session
 
437
  &lt;/res-type&gt;
 
438
  &lt;res-auth&gt;
 
439
    Container
 
440
  &lt;/res-auth&gt;
 
441
&lt;/resource-ref&gt;
 
442
</source>
 
443
 
 
444
    <p><strong>WARNING</strong> - Be sure you respect the element ordering
 
445
    that is required by the DTD for web application deployment descriptors!
 
446
    See the
 
447
    <a href="http://java.sun.com/products/servlet/download.html">Servlet
 
448
    Specification</a> for details.</p>
 
449
 
 
450
    <h3>2.  Code Your Application's Use Of This Resource</h3>
 
451
 
 
452
    <p>A typical use of this resource reference might look like this:</p>
 
453
<source>
 
454
Context initCtx = new InitialContext();
 
455
Context envCtx = (Context) initCtx.lookup("java:comp/env");
 
456
Session session = (Session) envCtx.lookup("mail/Session");
 
457
 
 
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);
 
466
</source>
 
467
 
 
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>&lt;Context&gt;</code></a> element
 
472
    for the web application as described below.</p>
 
473
 
 
474
    <h3>3.  Configure Tomcat's Resource Factory</h3>
 
475
 
 
476
    <p>To configure Tomcat's resource factory, add an elements like this to the
 
477
    <a href="config/context.html"><code>&lt;Context&gt;</code></a> element for
 
478
    this web application.</p>
 
479
 
 
480
<source>
 
481
&lt;Context ...&gt;
 
482
  ...
 
483
  &lt;Resource name="mail/Session" auth="Container"
 
484
            type="javax.mail.Session"
 
485
            mail.smtp.host="localhost"/&gt;
 
486
  ...
 
487
&lt;/Context&gt;
 
488
</source>
 
489
 
 
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>
 
494
 
 
495
    <h3>4.  Install the JavaMail libraries</h3>
 
496
 
 
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>.
 
501
    </p>
 
502
 
 
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.
 
510
    </p>
 
511
 
 
512
    <h3>Example Application</h3>
 
513
 
 
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>
 
519
 
 
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>&lt;Context&gt;</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
 
526
    on your network.</p>
 
527
 
 
528
  </subsection>
 
529
 
 
530
  <subsection name="JDBC Data Sources">
 
531
 
 
532
    <h3>0.  Introduction</h3>
 
533
 
 
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>
 
541
 
 
542
    <p>For information about JDBC, you should consult the following:</p>
 
543
    <ul>
 
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>
 
555
    </ul>
 
556
 
 
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>
 
564
 
 
565
    <h3>1.  Install Your JDBC Driver</h3>
 
566
 
 
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>
 
573
 
 
574
    <h3>2.  Declare Your Resource Requirements</h3>
 
575
 
 
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>
 
583
<source>
 
584
&lt;resource-ref&gt;
 
585
  &lt;description&gt;
 
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 &lt;Context&gt;
 
589
    configurartion for the web application.
 
590
  &lt;/description&gt;
 
591
  &lt;res-ref-name&gt;
 
592
    jdbc/EmployeeDB
 
593
  &lt;/res-ref-name&gt;
 
594
  &lt;res-type&gt;
 
595
    javax.sql.DataSource
 
596
  &lt;/res-type&gt;
 
597
  &lt;res-auth&gt;
 
598
    Container
 
599
  &lt;/res-auth&gt;
 
600
&lt;/resource-ref&gt;
 
601
</source>
 
602
 
 
603
    <p><strong>WARNING</strong> - Be sure you respect the element ordering
 
604
    that is required by the DTD for web application deployment descriptors!
 
605
    See the
 
606
    <a href="http://java.sun.com/products/servlet/download.html">Servlet
 
607
    Specification</a> for details.</p>
 
608
 
 
609
    <h3>3.  Code Your Application's Use Of This Resource</h3>
 
610
 
 
611
    <p>A typical use of this resource reference might look like this:</p>
 
612
<source>
 
613
Context initCtx = new InitialContext();
 
614
Context envCtx = (Context) initCtx.lookup("java:comp/env");
 
615
DataSource ds = (DataSource)
 
616
  envCtx.lookup("jdbc/EmployeeDB");
 
617
 
 
618
Connection conn = ds.getConnection();
 
619
... use this connection to access the database ...
 
620
conn.close();
 
621
</source>
 
622
 
 
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>&lt;Context&gt;</code></a> element for
 
627
    the web application as described below.</p>
 
628
 
 
629
    <h3>4.  Configure Tomcat's Resource Factory</h3>
 
630
 
 
631
    <p>To configure Tomcat's resource factory, add an element like this to the
 
632
    <a href="config/context.html"><code>&lt;Context&gt;</code></a> element for
 
633
    the web application.</p>
 
634
 
 
635
<source>
 
636
&lt;Context ...&gt;
 
637
  ...
 
638
  &lt;Resource name="jdbc/EmployeeDB"
 
639
            auth="Container"
 
640
            type="javax.sql.DataSource"
 
641
            username="dbusername"
 
642
            password="dbpassword"
 
643
            driverClassName="org.hsql.jdbcDriver"
 
644
            url="jdbc:HypersonicSQL:database"
 
645
            maxActive="8"
 
646
            maxIdle="4"/&gt;
 
647
  ...
 
648
&lt;/Context&gt;
 
649
</source>
 
650
 
 
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>
 
653
 
 
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>
 
658
 
 
659
    <p>The configuration properties for Tomcat's standard data source
 
660
    resource factory
 
661
    (<code>org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory</code>) are
 
662
    as follows:</p>
 
663
    <ul>
 
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
 
674
        JDBC driver.</li>
 
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
 
679
        JDBC driver.</li>
 
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>
 
684
    </ul>
 
685
    <p>For more details, please refer to the commons-dbcp documentation.</p>
 
686
 
 
687
  </subsection>
 
688
 
 
689
</section>
 
690
 
 
691
 
 
692
<section name="Adding Custom Resource Factories">
 
693
 
 
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>&lt;Context&gt;</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
 
701
  above.</p>
 
702
 
 
703
  <h3>1.  Write A Resource Factory Class</h3>
 
704
 
 
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>
 
710
  <ul>
 
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>&lt;Context&gt;</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
 
720
      is specified.</li>
 
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>
 
727
  </ul>
 
728
 
 
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>
 
731
 
 
732
<source>
 
733
package com.mycompany;
 
734
 
 
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;
 
743
 
 
744
public class MyBeanFactory implements ObjectFactory {
 
745
 
 
746
  public Object getObjectInstance(Object obj,
 
747
      Name name, Context nameCtx, Hashtable environment)
 
748
      throws NamingException {
 
749
 
 
750
      // Acquire an instance of our specified bean class
 
751
      MyBean bean = new MyBean();
 
752
 
 
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")) {
 
761
              bean.setFoo(value);
 
762
          } else if (name.equals("bar")) {
 
763
              try {
 
764
                  bean.setBar(Integer.parseInt(value));
 
765
              } catch (NumberFormatException e) {
 
766
                  throw new NamingException("Invalid 'bar' value " + value);
 
767
              }
 
768
          }
 
769
      }
 
770
 
 
771
      // Return the customized instance
 
772
      return (bean);
 
773
 
 
774
  }
 
775
 
 
776
}
 
777
</source>
 
778
 
 
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>&lt;ResourceParams&gt;</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>
 
787
 
 
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>
 
791
 
 
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
 
798
  application.</p>
 
799
 
 
800
  <h3>2.  Declare Your Resource Requirements</h3>
 
801
 
 
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>&lt;resource-env-ref&gt;</code> element, like this:</p>
 
806
 
 
807
<source>
 
808
&lt;resource-env-ref&gt;
 
809
  &lt;description&gt;
 
810
    Object factory for MyBean instances.
 
811
  &lt;/description&gt;
 
812
  &lt;resource-env-ref-name&gt;
 
813
    bean/MyBeanFactory
 
814
  &lt;/resource-env-ref-name&gt;
 
815
  &lt;resource-env-ref-type&gt;
 
816
    com.mycompany.MyBean
 
817
  &lt;/resource-env-ref-type&gt;
 
818
&lt;resource-env-ref&gt;
 
819
</source>
 
820
 
 
821
    <p><strong>WARNING</strong> - Be sure you respect the element ordering
 
822
    that is required by the DTD for web application deployment descriptors!
 
823
    See the
 
824
    <a href="http://java.sun.com/products/servlet/download.html">Servlet
 
825
    Specification</a> for details.</p>
 
826
 
 
827
  <h3>3.  Code Your Application's Use Of This Resource</h3>
 
828
 
 
829
  <p>A typical use of this resource environment reference might look
 
830
  like this:</p>
 
831
 
 
832
<source>
 
833
Context initCtx = new InitialContext();
 
834
Context envCtx = (Context) initCtx.lookup("java:comp/env");
 
835
MyBean bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");
 
836
 
 
837
writer.println("foo = " + bean.getFoo() + ", bar = " +
 
838
               bean.getBar());
 
839
</source>
 
840
 
 
841
    <h3>4.  Configure Tomcat's Resource Factory</h3>
 
842
 
 
843
    <p>To configure Tomcat's resource factory, add an elements like this to the
 
844
    <a href="config/context.html"><code>&lt;Context&gt;</code></a> element for
 
845
    this web application.</p>
 
846
 
 
847
<source>
 
848
&lt;Context ...&gt;
 
849
  ...
 
850
  &lt;Resource name="bean/MyBeanFactory" auth="Container"
 
851
            type="com.mycompany.MyBean"
 
852
            factory="com.mycompany.MyBeanFactory"
 
853
            bar="23"/&gt;
 
854
  ...
 
855
&lt;/Context&gt;
 
856
</source>
 
857
 
 
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>
 
865
 
 
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
 
873
    compatible APIs.</p>
 
874
 
 
875
</section>
 
876
 
 
877
</body>
 
878
 
 
879
</document>