<?xml version="1.0" encoding="Shift_JIS" ?>
<!DOCTYPE document [
  <!ENTITY project SYSTEM "project.xml">
]>
<document url="jndi-resources-howto.html">

    &project;

    <properties>
        <author email="craigmcc@apache.org">Craig R. McClanahan</author>
        <primary><title>JNDI Resources HOW-TO</title></primary>
        <title>JNDI リソース HOW-TO</title>
        <translator>鰈崎 義之</translator>
        <editor>高橋 達男</editor>
    </properties>

<body>


<primary>
<section name="Introduction"/>
</primary>
<section name="はじめに">

<primary>
<p>Tomcat 5 provides a JNDI <strong>InitialContext</strong> implementation
instance to web applications running under it, in a manner that is compatible
with those provided by a <a href="http://java.sun.com/j2ee">Java2 Enterprise
Edition</a> application server.  Entries in this <code>InitialContext</code>
are configured in the <code>$CATALINA_HOME/conf/server.xml</code> file, and
may be referenced by the following elements in the web application deployment
descriptor (<code>/WEB-INF/web.xml</code>) of your web application:</p>
</primary>
<p>
Tomcat 5 は、その下で動作する Web アプリケーションに対して JNDI
<strong>InitialContext</strong> の実装を提供します。この実装は、
<a href="http://java.sun.com/j2ee">Java2 Enterprise Edition</a>
アプリケーションサーバで提供される JNDI の実装と互換性があります。
この <code>InitialContext</code> 内のエントリは 
<code>$CATALINA_HOME/conf/server.xml</code> ファイルで設定され、
アプリケーションの Web アプリケーション配備記述子
(<code>/WEB-INF/web.xml</code>)の以下の要素で参照できます。
</p>
<primary>
<ul>
<li><code><strong>&lt;env-entry&gt;</strong></code> - Environment entry, a
    single-value parameter that can be used to configure how the application
    will operate.</li>
<li><code><strong>&lt;resource-ref&gt;</strong></code> - Resource reference,
    which is typically to an object factory for resources such as a JDBC
    <code>DataSource</code>, a JavaMail <code>Session</code>, or custom
    object factories configured into Tomcat 5.</li>
<li><code><strong>&lt;resource-env-ref&gt;</strong></code> - Resource
    environment reference, a new variation of <code>resource-ref</code>
    added in Servlet 2.4 that is simpler to configure for resources
    that do not require authentication information.</li>
</ul>
</primary>
<ul>
<li><code><strong>&lt;env-entry&gt;</strong></code> - 環境エントリ。
    アプリケーションがどのように動作するかを設定するのに使用できる、
    単独の値を持つパラメータ。</li>
<li><code><strong>&lt;resource-ref&gt;</strong></code> - リソース参照。
    主として JDBC <code>DataSource</code> や JavaMail <code>Session</code> など、
Tomcat 5 内に設定されたカスタムオブジェクトファクトリに対するリソース参照。</li>
<li><code><strong>&lt;resource-env-ref&gt;</strong></code> - リソース環境参照。
    Servlet 2.4 で追加された <code>resource-ref</code> の新たなバリエーション。
    認証情報が不要なリソースの設定が簡単にできます。</li>
</ul>

<primary>
<p>The <code>InitialContext</code> is configured as a web application is
initially deployed, and is made available to web application components (for
read-only access).  All configured entries and resources will be placed in
the <code>java:comp/env</code> portion of the JNDI namespace, so a typical
access to a resource - in this case, to a JDBC <code>DataSource</code> -
would look something like this:</p>
</primary>
<p>
<code>InitialContext</code> は、Web アプリケーションが最初に配備されたときに設定され、
Web アプリケーションコンポーネントから (読み込み専用で) 利用できるようになります。
すべての設定されたエントリとリソースは、JNDI 名前空間の 
<code>java:comp/env</code> の下に置かれます。したがってリソース - この場合 
JDBC <code>DataSource</code> - への典型的なアクセスは以下のようになります。
</p>
<primary>
<source>
// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");

// Look up our data source
DataSource ds = (DataSource)
  envCtx.lookup("jdbc/EmployeeDB");

// Allocate and use a connection from the pool
Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();
</source>
</primary>
<source>
// 環境ネーミングコンテキストを取得
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");

// データソースを検索
DataSource ds = (DataSource)
  envCtx.lookup("jdbc/EmployeeDB");

// 接続をプールから割り当てて使用する
Connection conn = ds.getConnection();
... この接続をデータベースアクセスに使用 ...
conn.close();
</source>

<primary>
<p>See the following Specifications for more information about programming APIs
for JNDI, and for the features supported by Java2 Enterprise Edition (J2EE)
servers, which Tomcat emulates for the services that it provides:</p>
</primary>
<p>
JNDI のプログラミング API や、サービスを提供するために Tomcat 
がエミュレートしている Java2 Enterprise Edition (J2EE) 
サーバでサポートする機能に関するより詳しい情報は、以下の仕様を参照してください。
</p>
<primary>
<ul>
<li><a href="http://java.sun.com/products/jndi/#download">Java Naming and
    Directory Interface</a> (included in JDK 1.4, available separately for
    prior JDK versions)</li>
<li><a href="http://java.sun.com/j2ee/download.html">J2EE Platform
    Specification</a> (in particular, see Chapter 5 on <em>Naming</em>)</li>
</ul>
</primary>
<ul>
<li><a href="http://java.sun.com/products/jndi/#download">Java Naming and Directory Interface(JNDI)</a>
    (JDK 1.4 に含まれている。それ以前の JDK のバージョンでは別途提供)</li>
<li><a href="http://java.sun.com/j2ee/download.html">J2EE プラットフォーム仕様</a>
    (特に 5 章の <em>Naming</em> を参照)</li>
</ul>

</section>


<primary>
<section name="Configuring JNDI Resources"/>
</primary>
<section name="JNDI リソースの設定">

<primary>
<p>Each available JNDI Resource is configured based on inclusion of the
following elements in the <code>$CATALINA_HOME/conf/server.xml</code>
file:</p>
</primary>
<p>
利用可能な各 JNDI リソースは、<code>$CATALINA_HOME/conf/server.xml</code>
ファイルに以下の要素を含めることで設定できます。
</p>
<primary>
<ul>
<li><a href="config/context.html#Environment Entries">&lt;Environment&gt;</a> -
    Configure names and values for scalar environment entries that will be
    exposed to the web application through the JNDI
    <code>InitialContext</code> (equivalent to the inclusion of an
    <code>&lt;env-entry&gt;</code> element in the web application
    deployment descriptor).</li>
<li><a href="config/context.html#Resource Definitions">&lt;Resource&gt;</a> -
    Configure the name and data type of a resource made available to the
    application (equivalent to the inclusion of a
    <code>&lt;resource-ref&gt;</code> element in the web application
    deployment descriptor).</li>
<li><a href="config/context.html#Resource Parameters">&lt;ResourceParams&gt;</a> -
    Configure the Java class name of the resource factory implementation to be
    used, as well as JavaBeans properties used to configure that resource
    factory.</li>
<li><a href="config/context.html#Resource Links">&lt;ResourceLink&gt;</a> -
    Add a link to a resource defined in the global JNDI context.</li>
</ul>
</primary>
<ul>
<li><a href="config/context.html#Environment Entries">&lt;Environment&gt;</a> -
    JNDI <code>InitialContext</code> を通じて Web アプリケーションに公開するスカラ値の環境エントリの名前と値を設定します
    (Web アプリケーション配備記述子に含める <code>&lt;env-entry&gt;</code>
    要素と同等)。</li>
<li><a href="config/context.html#Resource Definitions">&lt;Resource&gt;</a> -
    アプリケーションで利用できるようにリソースの名前とデータ型を設定します
    (Web アプリケーション配備記述子に含める <code>&lt;resource-ref&gt;</code> 
    要素と同等)。</li>
<li><a href="config/context.html#Resource Parameters">&lt;ResourceParams&gt;</a> -
    リソースファクトリの実装に使われる Java クラス名と、
    リソースファクトリの設定に使われる JavaBean のプロパティを設定します。</li>
<li><a href="config/context.html#Resource Links">&lt;ResourceLink&gt;</a> -
    グローバル JNDI コンテキストで定義されたリソースへのリンクを追加します。</li>
</ul>

<primary>
<p>Any number of these elements may be nested inside a
<a href="config/context.html">&lt;Context&gt;</a> element (to be associated
only with that particular web application) or inside a
<a href="config/defaultcontext.html">&lt;DefaultContext&gt;</a> element
(used to set the default configuration characteristics for automatically
deloyed applications).</p>
</primary>
<p>
これらの要素は、
<a href="config/context.html">&lt;Context&gt;</a> 要素 
(特定の Web アプリケーションだけに関連付けられる)内や、
<a href="config/defaultcontext.html">&lt;DefaultContext&gt;</a> 要素(自動的に配備されるアプリケーションのデフォルト設定として使われる) 内の入れ子として、
いくつ入っていてもかまいません。
</p>

<primary>
<p>In addition, the names and values of all <code>&lt;env-entry&gt;</code>
elements included in the web application deployment descriptor
(<code>/WEB-INF/web.xml</code>) are configured into the initial context as
well, overriding corresponding values from <code>conf/server.xml</code>
<strong>only</strong> if allowed by the corresponding
<code>&lt;Environment&gt;</code> element (by setting the
<code>override</code> attribute to "true").</p>
</primary>
<p>
さらに、Web アプリケーション配備記述子 (<code>/WEB-INF/web.xml</code>) の中に含まれているすべての  <code>&lt;env-entry&gt;</code>
要素の名前と値は初期コンテキスト内で同様に設定されます。
その際、<code>conf/server.xml</code> で対応する値は、対応する 
<code>&lt;Environment&gt;</code> 要素で (<code>override</code> 属性を "true" 
にセットすることによって) 許可されている時 <strong>だけ</strong> 上書きされます。
</p>

<primary>
<p>Global resources can be defined in the server-wide JNDI context, by adding
the resource elements described above to the
<a href="config/globalresources.html">&lt;GlobalNamingResources&gt;</a>
child element of the <a href="config/server.html">&lt;Server&gt;</a>
element.</p>
</primary>
<p>
グローバルリソースは、サーバ全体の JNDI コンテキストの中で定義されます。
<a href="config/server.html">&lt;Server&gt;</a> 要素の子要素である
<a href="config/globalresources.html">&lt;GlobalNamingResources&gt;</a>
に、上記の resource 要素を追加することによって定義されます。 
</p>

</section>


<primary>
<section name="Tomcat Standard Resource Factories"/>
</primary>
<section name="Tomcat 標準リソースファクトリ">

  <primary>
  <p>Tomcat 5 includes a series of standard resource factories that can
  provide services to your web applications, but give you configuration
  flexibility (in <code>$CATALINA_HOME/conf/server.xml</code>) without
  modifying the web application or the deployment descriptor.  Each
  subsection below details the configuration and usage of the standard
  resource factories.</p>
  </primary>
  <p>
  Tomcat 5 には一連の標準リソースファクトリがあり、
  Web アプリケーションへのサービスの提供も可能ですが、
  それ以上に、 Web アプリケーションや配備記述子の修正なしに、
  (<code>$CATALINA_HOME/conf/server.xml</code> での) 柔軟な設定を実現します。
  標準リソースファクトリの設定と使い方については、以下の各節で詳しく述べます。
  </p>

  <primary>
  <p>See <a href="#Adding Custom Resource Factories">Adding Custom
  Resource Factories</a> for information about how to create, install,
  configure, and use your own custom resource factory classes with
  Tomcat 5.</p>
  </primary>
  <p>
  Tomcat 5 で独自のカスタムリソースファクトリのクラスを作成・インストール・設定・使用する方法に関する情報は
  <a href="#カスタムリソースファクトリの追加">カスタムリソースファクトリの追加</a> を参照してください。
  </p>

  <primary>
  <p><em>NOTE</em> - Of the standard resource factories, only the
  "JDBC Data Source" and "User Transaction" factories are mandated to
  be available on other platforms, and then they are required only if
  the platform implements the Java2 Enterprise Edition (J2EE) specs.
  All other standard resource factories, plus custom resource factories
  that you write yourself, are specific to Tomcat and cannot be assumed
  to be available on other containers.</p>
  </primary>
  <p>
  <em>注意</em> - 標準リソースファクトリのうち、
  他のプラットフォームでも利用可能であることが義務づけられているのは、
  「JDBC データソース」と「ユーザトランザクション」のファクトリだけで、
  しかもこれらが必要なのは、プラットフォームが Java2 Enterprise Edition (J2EE)
  仕様を実装する場合のみです。他のすべての標準リソースファクトリ、
  および独自に作成したカスタムリソースファクトリは Tomcat 固有のものであり、
  他のコンテナで使えるかどうかはわかりません。
  </p>

  <primary>
  <subsection name="Generic JavaBean Resources"/>
  </primary>
  <subsection name="汎用 JavaBean リソース">

    <primary>
    <h3>0.  Introduction</h3>
    </primary>
    <h3>0.  はじめに</h3>

    <primary>
    <p>This resource factory can be used to create objects of <em>any</em>
    Java class that conforms to standard JavaBeans naming conventions (i.e.
    it has a zero-arguments constructor, and has property setters that
    conform to the setFoo() naming pattern.  The resource factory will
    create a new instance of the appropriate bean class every time a
    <code>lookup()</code> for this entry is made.</p>
    </primary>
    <p>
    このリソースファクトリは、標準 JavaBeans 命名規約
    (つまり引数のないコンストラクタがあり、setFoo() 
    形式の命名パターンに沿ったプロパティセッタがある) に従った <em>あらゆる</em>
    Java クラスのオブジェクトの作成に使用できます。
    リソースファクトリは、エントリの <code>lookup()</code>
    が行われるたびに適切な bean クラスの新しいインスタンスを作成します。
    </p>

    <primary>
    <p>The steps required to use this facility are described below.</p>
    </primary>
    <p>
    この機能を利用するために必要な手順を以下に示します。
    </p>

    <primary>
    <h3>1.  Create Your JavaBean Class</h3>
    </primary>
    <h3>1.  JavaBean クラスの作成</h3>

    <primary>
    <p>Create the JavaBean class which will be instantiated each time
    that the resource factory is looked up.  For this example, assume
    you create a class <code>com.mycompany.MyBean</code>, which looks
    like this:</p>
    </primary>
    <p>
    リソースファクトリが検索されるたびにインスタンス化される
    JavaBean クラスを作成します。この例では、<code>com.mycompany.MyBean</code>
    クラスを以下のように作成すると仮定します。
    </p>

<source>
package com.mycompany;

public class MyBean {

  private String foo = "Default Foo";

  public String getFoo() {
    return (this.foo);
  }

  public void setFoo(String foo) {
    this.foo = foo;
  }

  private int bar = 0;

  public int getBar() {
    return (this.bar);
  }

  public void setBar(int bar) {
    this.bar = bar;
  }


}
</source>

  <primary>
  <h3>2.  Declare Your Resource Requirements</h3>
  </primary>
  <h3>2.  リソースに必要なものの定義</h3>

  <primary>
  <p>Next, modify your web application deployment descriptor
  (<code>/WEB-INF/web.xml</code>) to declare the JNDI name under which
  you will request new instances of this bean.  The simplest approach is
  to use a <code>&lt;resource-env-ref&gt;</code> element, like this:</p>
  </primary>
  <p>
  次に、この bean の新しいインスタンスをリクエストする JNDI 名を定義するために、
  Web アプリケーション配備記述子 (<code>/WEB-INF/web.xml</code>) を修正します。
  一番簡単なのは、<code>&lt;resource-env-ref&gt;</code> 要素を使う方法で、
  以下のように行います。
  </p>

<primary>
<source>
&lt;resource-env-ref&gt;
  &lt;description&gt;
    Object factory for MyBean instances.
  &lt;/description&gt;
  &lt;resource-env-ref-name&gt;
    bean/MyBeanFactory
  &lt;/resource-env-ref-name&gt;
  &lt;resource-env-ref-type&gt;
    com.mycompany.MyBean
  &lt;/resource-env-ref-type&gt;
&lt;/resource-env-ref&gt;
</source>
</primary>
<source>
&lt;resource-env-ref&gt;
  &lt;description&gt;
    MyBean インスタンスのためのオブジェクトファクトリ
  &lt;/description&gt;
  &lt;resource-env-ref-name&gt;
    bean/MyBeanFactory
  &lt;/resource-env-ref-name&gt;
  &lt;resource-env-ref-type&gt;
    com.mycompany.MyBean
  &lt;/resource-env-ref-type&gt;
&lt;/resource-env-ref&gt;
</source>

    <primary>
    <p><strong>WARNING</strong> - Be sure you respect the element ordering
    that is required by the DTD for web application deployment descriptors!
    See the
    <a href="http://java.sun.com/products/servlet/download.html">Servlet
    Specification</a> for details.</p>
    </primary>
    <p>
    <strong>警告</strong> - 要素の順序が Web アプリケーション配備記述子の DTD
    で要求されたとおりであることを確認してください! 詳細は、
    <a href="http://java.sun.com/products/servlet/download.html">Servlet
    仕様</a> を参照してください。
    </p>

  <primary>
  <h3>3.  Code Your Application's Use Of This Resource</h3>
  </primary>
  <h3>3.  このリソースを使うコードをアプリケーションに記述</h3>

  <primary>
  <p>A typical use of this resource environment reference might look
  like this:</p>
  </primary>
  <p>
  このリソース環境参照の典型的な使い方は、以下のようになるでしょう。
  </p>

<source>
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
MyBean bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");

writer.println("foo = " + bean.getFoo() + ", bar = " +
               bean.getBar());
</source>

    <primary>
    <h3>4.  Configure Tomcat's Resource Factory</h3>
    </primary>
    <h3>4.  Tomcat のリソースファクトリの設定</h3>

    <primary>
    <p>To configure Tomcat's resource factory, add an elements like this to the
    <code>$CATALINA_HOME/conf/server.xml</code> file, nested inside the
    <code>Context</code> element for this web application (or nested inside
    a <code>DefaultContext</code> element for the surrounding
    <code>&lt;Host&gt;</code> or <code>&lt;Engine&gt;</code> element.</p>
    </primary>
    <p>
    Tomcat のリソースファクトリを設定するには、この Web アプリケーションの
    <code>Context</code> 要素内で入れ子になった
    (あるいは <code>&lt;Host&gt;</code> または <code>&lt;Engine&gt;</code>
    要素内の<code>DefaultContext</code> 要素内で入れ子になった)、
    以下のような要素を <code>$CATALINA_HOME/conf/server.xml</code>
    ファイルに追加します。
    </p>
<source>
&lt;Context ...&gt;
  ...
  &lt;Resource name="bean/MyBeanFactory" auth="Container"
            type="com.mycompany.MyBean"/&gt;
  &lt;ResourceParams name="bean/MyBeanFactory"&gt;
    &lt;parameter&gt;
      &lt;name&gt;factory&lt;/name&gt;
      &lt;value&gt;org.apache.naming.factory.BeanFactory&lt;/value&gt;
    &lt;/parameter&gt;
    &lt;parameter&gt;
      &lt;name&gt;bar&lt;/name&gt;
      &lt;value&gt;23&lt;/value&gt;
    &lt;/parameter&gt;
  &lt;/ResourceParams&gt;
  ...
&lt;/Context&gt;
</source>

    <primary>
    <p>Note that the resource name (here, <code>bean/MyBeanFactory</code>
    must match the value specified in the web application deployment
    descriptor.  We are also initializing the value of the <code>bar</code>
    property, which will cause <code>setBar(23)</code> to be called before
    the new bean is returned.  Because we are not initializing the
    <code>foo</code> property (although we could have), the bean will
    contain whatever default value is set up by its constructor.</p>
    </primary>
    <p>
    リソース名 (ここでは <code>bean/MyBeanFactory</code>) が Web
    アプリケーション配備記述子で指定された値と一致していなければならないことに注意してください。
    また、<code>bar</code> プロパティの値の初期化も行っており、それにより
    新しい bean が返される前に <code>setBar(23)</code> が呼び出されます。
    <code>foo</code> プロパティは (初期化可能でしたが) 初期化していないので、
    bean ではコンストラクタでセットアップする何らかのデフォルト値が入ることでしょう。
    </p>

  </subsection>


  <primary>
  <subsection name="JavaMail Sessions"/>
  </primary>
  <subsection name="JavaMail セッション">

    <primary>
    <h3>0.  Introduction</h3>
    </primary>
    <h3>0.  はじめに</h3>

    <primary>
    <p>In many web applications, sending electronic mail messages is a
    required part of the system's functionality.  The
    <a href="http://java.sun.com/products/javamail">Java Mail</a> API
    makes this process relatively straightforward, but requires many
    configuration details that the client application must be aware of
    (including the name of the SMTP host to be used for message sending).</p>
    </primary>
    <p>
    多くの Web アプリケーションにおいて、
    システムの機能として電子メールメッセージの送信が必要となります。
    <a href="http://java.sun.com/products/javamail">Java Mail</a> API は、
    比較的簡単にこういった処理を実現しますが、設定の詳細の多くを
    クライアントアプリケーションが知っている必要があります
    (メッセージ送信に使う SMTP ホスト名もその一つです)。
    </p>

    <primary>
    <p>Tomcat 5 includes a standard resource factory that will create
    <code>javax.mail.Session</code> session instances for you, already
    connected to the SMTP server that is configured in <code>server.xml</code>.
    In this way, the application is totally insulated from changes in the
    email server configuration environment - it simply asks for, and receives,
    a preconfigured session whenever needed.</p>
    </primary>
    <p>
    Tomcat 5 には、 <code>javax.mail.Session</code>
    のセッションインスタンスを作成する標準リソースファクトリがあります。
    このセッションインスタンスは <code>server.xml</code> で設定された SMTP
    サーバに既に接続されています。この方法で、
    アプリケーションは電子メールサーバ環境設定の変更から完全に分離されます
    - アプリケーションはいつでも必要なときに、
    あらかじめ設定されたセッションを要求して受け取るだけです。
    </p>

    <primary>
    <p>The steps required for this are outlined below.</p>
    </primary>
    <p>必要な手順の概要を以下で説明します。</p>

    <primary>
    <h3>1.  Declare Your Resource Requirements</h3>
    </primary>
    <h3>1.  リソースに必要なものを定義</h3>

    <primary>
    <p>The first thing you should do is modify the web application deployment
    descriptor (<code>/WEB-INF/web.xml</code>) to declare the JNDI name under
    which you will look up preconfigured sessions.  By convention, all such
    names should resolve to the <code>mail</code> subcontext (relative to the
    standard <code>java:comp/env</code> naming context that is the root of
    all provided resource factories.  A typical <code>web.xml</code> entry
    might look like this:</p>
    </primary>
    <p>
    まず最初にしなければいけないのは、あらかじめ設定されたセッションを検索する
    JNDI 名を定義するために、Web アプリケーション配備記述子
    (<code>/WEB-INF/web.xml</code>) を修正することです。
    <note>is modify → is to modify</note>
    慣例に従えば、こうした名前はすべて、<code>mail</code> サブコンテキスト
    (提供されるすべてのリソースファクトリのルートである標準の
    <code>java:comp/env</code> ネーミングコンテキストの下位にある) に属すべきです。
    典型的な <code>web.xml</code> のエントリは以下のようになるでしょう。
    </p>
<primary>
<source>
&lt;resource-ref&gt;
  &lt;description&gt;
    Resource reference to a factory for javax.mail.Session
    instances that may be used for sending electronic mail
    messages, preconfigured to connect to the appropriate
    SMTP server.
  &lt;/description&gt;
  &lt;res-ref-name&gt;
    mail/Session
  &lt;/res-ref-name&gt;
  &lt;res-type&gt;
    javax.mail.Session
  &lt;/res-type&gt;
  &lt;res-auth&gt;
    Container
  &lt;/res-auth&gt;
&lt;/resource-ref&gt;
</source>
</primary>
<source>
&lt;resource-ref&gt;
  &lt;description&gt;
    電子メールメッセージを送信するのに使われる javax.mail.Session
    インスタンスのファクトリへのリソース参照。
    適切な SMTP サーバへ接続するようにあらかじめ設定されています。
  &lt;/description&gt;
  &lt;res-ref-name&gt;
    mail/Session
  &lt;/res-ref-name&gt;
  &lt;res-type&gt;
    javax.mail.Session
  &lt;/res-type&gt;
  &lt;res-auth&gt;
    Container
  &lt;/res-auth&gt;
&lt;/resource-ref&gt;
</source>

    <primary>
    <p><strong>WARNING</strong> - Be sure you respect the element ordering
    that is required by the DTD for web application deployment descriptors!
    See the
    <a href="http://java.sun.com/products/servlet/download.html">Servlet
    Specification</a> for details.</p>
    </primary>
    <p>
    <strong>警告</strong> - 要素の順序が Web アプリケーション配備記述子の DTD
    で要求されたとおりであることを確認してください! 詳細は、
    <a href="http://java.sun.com/products/servlet/download.html">Servlet
    仕様</a> を参照してください。
    </p>

    <primary>
    <h3>2.  Code Your Application's Use Of This Resource</h3>
    </primary>
    <h3>2.  このリソースを使うコードをアプリケーションに記述</h3>

    <primary>
    <p>A typical use of this resource reference might look like this:</p>
    </primary>
    <p>
    このリソース環境参照の典型的な使い方は、以下のようになるでしょう。
    </p>
<source>
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
Session session = (Session) envCtx.lookup("mail/Session");

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(request.getParameter("from"));
InternetAddress to[] = new InternetAddress[1];
to[0] = new InternetAddress(request.getParameter("to"));
message.setRecipients(Message.RecipientType.TO, to);
message.setSubject(request.getParameter("subject"));
message.setContent(request.getParameter("content"), "text/plain");
Transport.send(message);
</source>

    <primary>
    <p>Note that the application uses the same resource reference name
    that was declared in the web application deployment descriptor.  This
    is matched up against the resource factory that is configured in
    <code>$CATALINA_HOME/conf/server.xml</code>, as described below.</p>
    </primary>
    <p>
    アプリケーションでは、Web アプリケーション配備記述子で定義されたものと同じリソース参照名を使うことに注意してください。
    これは、以下で示す <code>$CATALINA_HOME/conf/server.xml</code>
    で定義されるリソースファクトリと一致してなければなりません。
    </p>

    <primary>
    <h3>3.  Configure Tomcat's Resource Factory</h3>
    </primary>
    <h3>3.  Tomcat のリソースファクトリの設定</h3>

    <primary>
    <p>To configure Tomcat's resource factory, add an elements like this to the
    <code>$CATALINA_HOME/conf/server.xml</code> file, nested inside the
    <code>Context</code> element for this web application (or nested inside
    a <code>DefaultContext</code> element for the surrounding
    <code>&lt;Host&gt;</code> or <code>&lt;Engine&gt;</code> element.</p>
    </primary>
    <p>
    Tomcat のリソースファクトリを設定するには、この Web アプリケーションの
    <code>Context</code> 要素内で入れ子になった
    (あるいは <code>&lt;Host&gt;</code> または <code>&lt;Engine&gt;</code>
    要素内の<code>DefaultContext</code> 要素内で入れ子になった)、
    以下のような要素を <code>$CATALINA_HOME/conf/server.xml</code>
    ファイルに追加します。
    </p>
<source>
&lt;Context ...&gt;
  ...
  &lt;Resource name="mail/Session" auth="Container"
            type="javax.mail.Session"/&gt;
  &lt;ResourceParams name="mail/Session"&gt;
    &lt;parameter&gt;
      &lt;name&gt;mail.smtp.host&lt;/name&gt;
      &lt;value&gt;localhost&lt;/value&gt;
    &lt;/parameter&gt;
  &lt;/ResourceParams&gt;
  ...
&lt;/Context&gt;
</source>

    <primary>
    <p>Note that the resource name (here, <code>mail/Session</code>) must
    match the value specified in the web application deployment descriptor.
    Customize the value of the <code>mail.smtp.host</code> parameter to
    point at the server that provides SMTP service for your network.</p>
    </primary>
    <p>
    リソース名 (ここでは <code>mail/Session</code>) が Web 
    アプリケーション配備記述子で指定された値と一致していなければならないことに注意してください。
    <code>mail.smtp.host</code> パラメータの値は、実際のネットワークで SMTP 
    サービスを提供しているサーバを指すようにカスタマイズしてください。
    </p>

    <primary>
    <h3>Example Application</h3>
    </primary>
    <h3>サンプルアプリケーション</h3>

    <primary>
    <p>The <code>/examples</code> application included with Tomcat contains
    an example of utilizing this resource factory.  It is accessed via the
    "JSP Examples" link.  The source code for the servlet that actually
    sends the mail message is in
    <code>/WEB-INF/classes/SendMailServlet.java</code>.</p>
    </primary>
    <p>
    Tomcat に付属している <code>/examples</code> アプリケーションには、
    このリソースファクトリを利用しているサンプルがあります。
    このアプリケーションには "JSP Examples" リンクからアクセスできます。
    実際にメールメッセージを送信しているサーブレットのソースコードは、
    <code>/WEB-INF/classes/SendMailServlet.java</code> にあります。
    </p>

    <primary>
    <p><strong>WARNING</strong> - The default configuration assumes that
    there is an SMTP server listing on port 25 on <code>localhost</code>.
    If this is not the case, edit the
    <code>$CATALINA_HOME/conf/server.xml</code> file, and modify the
    parameter value for the <code>mail.smtp.host</code> parameter to be
    the host name of an SMTP server on your network.</p>
    </primary>
    <p>
    <strong>警告</strong> - デフォルトの設定では、<code>localhost</code>
    の 25 番ポートで SMTP サーバが接続要求を受付けていることを想定しています。
    このケースに当てはまらない場合は、<code>$CATALINA_HOME/conf/server.xml</code> 
    ファイルを編集し、<code>mail.smtp.host</code>
    パラメータの値を実際のネットワークの SMTP サーバのホスト名に修正してください。
    <note>listing→listening</note>
    </p>

  </subsection>

  <primary>
  <subsection name="JDBC Data Sources"/>
  </primary>
  <subsection name="JDBC データソース">

    <primary>
    <h3>0.  Introduction</h3>
    </primary>
    <h3>0.  はじめに</h3>

    <primary>
    <p>Many web applications need to access a database via a JDBC driver,
    to support the functionality required by that application.  The J2EE
    Platform Specification requires J2EE Application Servers to make
    available a <em>DataSource</em> implementation (that is, a connection
    pool for JDBC connections) for this purpose.  Tomcat 5 offers exactly
    the same support, so that database-based applications you develop on
    Tomcat using this service will run unchanged on any J2EE server.</p>
    </primary>
    <p>
    アプリケーションで必要な機能をサポートするために、多くの Web 
    アプリケーションは JDBC ドライバ経由でデータベースにアクセスする必要があります。
    そのため、J2EE プラットフォーム仕様では、J2EE
    アプリケーションサーバは、<em>DataSource</em> 実装 
    (すなわち JDBC 接続の接続プール)
    を使えるようにする必要があります。Tomcat 5
    はまさしく同様のサポートを提供するので、このサービスを使って Tomcat 
    で開発した、データベースを利用するアプリケーションは、変更なしにどの
    J2EE サーバでも動かすことができるでしょう。
    </p>

    <primary>
    <p>For information about JDBC, you should consult the following:</p>
    </primary>
    <p>JDBC についての情報は、以下を参考にして下さい。</p>
    <primary>
    <ul>
    <li><a href="http://java.sun.com/products/jdbc/">http://java.sun.com/products/jdbc/</a> -
        Home page for information about Java Database Connectivity.</li>
    <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> -
        The JDBC 2.1 API Specification.</li>
    <li><a href="http://java.sun.com/products/jdbc/jdbc20.stdext.pdf">http://java.sun.com/products/jdbc/jdbc20.stdext.pdf</a> -
        The JDBC 2.0 Standard Extension API (including the
        <code>javax.sql.DataSource</code> API).  This package is now known
        as the "JDBC Optional Package".</li>
    <li><a href="http://java.sun.com/j2ee/download.html">http://java.sun.com/j2ee/download.html</a> -
        The J2EE Platform Specification (covers the JDBC facilities that
        all J2EE platforms must provide to applications).</li>
    </ul>
    </primary>
    <ul>
    <li><a href="http://java.sun.com/products/jdbc/">http://java.sun.com/products/jdbc/</a> -
        Java Database Connectivity(JDBC)に関する情報のホームページ。</li>
    <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> -
        JDBC 2.1 API 仕様。</li>
    <li><a href="http://java.sun.com/products/jdbc/jdbc20.stdext.pdf">http://java.sun.com/products/jdbc/jdbc20.stdext.pdf</a> -
        JDBC 2.0 標準拡張 API (<code>javax.sql.DataSource</code>
        はここにあります)。このパッケージは、今では "JDBCオプショナルパッケージ"
        として知られています。</li>
    <li><a href="http://java.sun.com/j2ee/download.html">http://java.sun.com/j2ee/download.html</a> -
        J2EE プラットフォーム仕様 (すべての J2EE 
        プラットフォームがアプリケーションに提供しなければならない 
        JDBC の機能をカバーしています)。</li>
    </ul>

    <primary>
    <p><strong>NOTE</strong> - The default data source support in Tomcat
    is based on the <strong>DBCP</strong> connection pool from the
    <a href="http://jakarta.apache.org/commons">Jakarta Commons</a>
    subproject.  However, it is possible to use any other connection pool
    that implements <code>javax.sql.DataSource</code>, by writing your
    own custom resource factory, as described
    <a href="#Adding Custom Resource Factories">below</a>.</p>
    </primary>
    <p>
    <strong>注意</strong> - Tomcat でのデフォルトのデータソースサポートは、
    <a href="http://jakarta.apache.org/commons">Jakarta Commons</a>
    サブプロジェクトの <strong>DBCP</strong> 接続プールに基づいています。
    けれども、<a href="#カスタムリソースファクトリの追加">後ほど</a>
    説明するカスタムリソースファクトリを自分で作成することにより、
    <code>javax.sql.DataSource</code> を実装する他の接続プールも利用できます。
    </p>

    <primary>
    <h3>1.  Install Your JDBC Driver</h3>
    </primary>
    <h3>1.  JDBC ドライバのインストール</h3>

    <primary>
    <p>Use of the <em>JDBC Data Sources</em> JNDI Resource Factory requires
    that you make an appropriate JDBC driver available to both Tomcat internal
    classes and to your web application.  This is most easily accomplished by
    installing the driver's JAR file(s) into the
    <code>$CATALINA_HOME/common/lib</code> directory, which makes the driver
    available both to the resource factory and to your application.</p>
    </primary>
    <p>
    <em>JDBC データソース</em> JNDI リソースファクトリを使うには、
    適切な JDBC ドライバを Tomcat 内部クラスと Web 
    アプリケーションの両方で利用可能にする必要があります。
    これを実現するには、ドライバの JAR ファイル (複数の場合もあり) を 
    <code>$CATALINA_HOME/common/lib</code>
    ディレクトリ以下にインストールするのが一番簡単です。そうすればドライバは、
    リソースファクトリとアプリケーションの両方から利用可能となります。
    </p>

    <primary>
    <h3>2.  Declare Your Resource Requirements</h3>
    </primary>
    <h3>2.  リソースに必要なものを定義</h3>

    <primary>
    <p>Next, modify the web application deployment descriptor
    (<code>/WEB-INF/web.xml</code>) to declare the JNDI name under
    which you will look up preconfigured data source.  By convention, all such
    names should resolve to the <code>jdbc</code> subcontext (relative to the
    standard <code>java:comp/env</code> naming context that is the root of
    all provided resource factories.  A typical <code>web.xml</code> entry
    might look like this:</p>
    </primary>
    <p>
    次に、あらかじめ設定されたデータソースを検索する
    JNDI 名を定義するために、Web アプリケーション配備記述子
    (<code>/WEB-INF/web.xml</code>) を修正します。
    慣例に従えば、こうした名前はすべて、<code>jdbc</code> サブコンテキスト
    (提供されるすべてのリソースファクトリのルートである標準の
    <code>java:comp/env</code> ネーミングコンテキストの下位)に属すべきです。
    典型的な <code>web.xml</code> のエントリは以下のようになるでしょう。
    </p>
<primary>
<source>
&lt;resource-ref&gt;
  &lt;description&gt;
    Resource reference to a factory for java.sql.Connection
    instances that may be used for talking to a particular
    database that is configured in the server.xml file.
  &lt;/description&gt;
  &lt;res-ref-name&gt;
    jdbc/EmployeeDB
  &lt;/res-ref-name&gt;
  &lt;res-type&gt;
    javax.sql.DataSource
  &lt;/res-type&gt;
  &lt;res-auth&gt;
    Container
  &lt;/res-auth&gt;
&lt;/resource-ref&gt;
</source>
</primary>
<source>
&lt;resource-ref&gt;
  &lt;description&gt;
    java.sql.Connection インスタンスのためのファクトリへの
    リソース参照。server.xml ファイルで設定された、特定の
    データベースと接続するのに使われます。
  &lt;/description&gt;
  &lt;res-ref-name&gt;
    jdbc/EmployeeDB
  &lt;/res-ref-name&gt;
  &lt;res-type&gt;
    javax.sql.DataSource
  &lt;/res-type&gt;
  &lt;res-auth&gt;
    Container
  &lt;/res-auth&gt;
&lt;/resource-ref&gt;
</source>

    <primary>
    <p><strong>WARNING</strong> - Be sure you respect the element ordering
    that is required by the DTD for web application deployment descriptors!
    See the
    <a href="http://java.sun.com/products/servlet/download.html">Servlet
    Specification</a> for details.</p>
    </primary>
    <p>
    <strong>警告</strong> - 要素の順序が Web アプリケーション配備記述子の DTD
    で要求されたとおりであることを確認してください! 詳細は、
    <a href="http://java.sun.com/products/servlet/download.html">Servlet
    仕様</a> を参照してください。
    </p>

    <primary>
    <h3>3.  Code Your Application's Use Of This Resource</h3>
    </primary>
    <h3>3.  このリソースを使うコードをアプリケーションに記述</h3>

    <primary>
    <p>A typical use of this resource reference might look like this:</p>
    </primary>
    <p>
    このリソース環境参照の典型的な使い方は、以下のようになるでしょう。
    </p>

<primary>
<source>
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
  envCtx.lookup("jdbc/EmployeeDB");

Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();
</source>
</primary>
<source>
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
  envCtx.lookup("jdbc/EmployeeDB");

Connection conn = ds.getConnection();
... データベースアクセスでこの接続を使用 ...
conn.close();
</source>

    <primary>
    <p>Note that the application uses the same resource reference name
    that was declared in the web application deployment descriptor.  This
    is matched up against the resource factory that is configured in
    <code>$CATALINA_HOME/conf/server.xml</code>, as described below.</p>
    </primary>
    <p>
    アプリケーションでは、Web アプリケーション配備記述子で定義されたものと同じリソース参照名を使うことに注意してください。
    これは、以下で示す <code>$CATALINA_HOME/conf/server.xml</code>
    で定義されるリソースファクトリと一致してなければなりません。
    </p>

    <primary>
    <h3>4.  Configure Tomcat's Resource Factory</h3>
    </primary>
    <h3>4.  Tomcat のリソースファクトリの設定</h3>

    <primary>
    <p>To configure Tomcat's resource factory, add an elements like this to the
    <code>$CATALINA_HOME/conf/server.xml</code> file, nested inside the
    <code>Context</code> element for this web application (or nested inside
    a <code>DefaultContext</code> element for the surrounding
    <code>&lt;Host&gt;</code> or <code>&lt;Engine&gt;</code> element.</p>
    </primary>
    <p>
    Tomcat のリソースファクトリを設定するには、この Web アプリケーションの
    <code>Context</code> 要素内で入れ子になった
    (あるいは <code>&lt;Host&gt;</code> または <code>&lt;Engine&gt;</code>
    要素内の<code>DefaultContext</code> 要素内で入れ子になった)、
    以下のような要素を <code>$CATALINA_HOME/conf/server.xml</code>
    ファイルに追加します。
    </p>
<source>
&lt;Context ...&gt;
  ...
  &lt;Resource name="jdbc/EmployeeDB" auth="Container"
            type="javax.sql.DataSource"/&gt;
  &lt;ResourceParams name="jdbc/EmployeeDB"&gt;
    &lt;parameter&gt;
      &lt;name&gt;username&lt;/name&gt;
      &lt;value&gt;dbusername&lt;/value&gt;
    &lt;/parameter&gt;
    &lt;parameter&gt;
      &lt;name&gt;password&lt;/name&gt;
      &lt;value&gt;dbpassword&lt;/value&gt;
    &lt;/parameter&gt;
    &lt;parameter&gt;
      &lt;name&gt;driverClassName&lt;/name&gt;
      &lt;value&gt;org.hsql.jdbcDriver&lt;/value&gt;
    &lt;/parameter&gt;
    &lt;parameter&gt;
      &lt;name&gt;url&lt;/name&gt;
      &lt;value&gt;jdbc:HypersonicSQL:database&lt;/value&gt;
    &lt;/parameter&gt;
    &lt;parameter&gt;
      &lt;name&gt;maxActive&lt;/name&gt;
      &lt;value&gt;8&lt;/value&gt;
    &lt;/parameter&gt;
    &lt;parameter&gt;
      &lt;name&gt;maxIdle&lt;/name&gt;
      &lt;value&gt;4&lt;/value&gt;
    &lt;/parameter&gt;
  &lt;/ResourceParams&gt;
  ...
&lt;/Context&gt;
</source>

    <primary>
    <p>Note that the resource name (here, <code>jdbc/EmployeeDB</code>) must
    match the value specified in the web application deployment descriptor.</p>
    </primary>
    <p>
    リソース名 (ここでは <code>jdbc/EmployeeDB</code>) が Web 
    アプリケーション配備記述子で指定した値と一致していなければならないことに注意してください。
    </p>

    <primary>
    <p>This example assumes that you are using the HypersonicSQL database
    JDBC driver.  Customize the <code>driverClassName</code> and
    <code>driverName</code> parameters to match your actual database's
    JDBC driver and connection URL.</p>
    </primary>
    <p>
    この例では、HypersonicSQL データベースの JDBC 
    ドライバを使用していると仮定しています。
    <code>driverClassName</code> と <code>driverName</code> 
    パラメータをそれぞれ実際に使用するデータベースの JDBC ドライバと接続 URL 
    に合わせてカスタマイズしてください。
    </p>

    <primary>
    <p>The configuration properties for Tomcat's standard data source
    resource factory
    (<code>org.apache.naming.factory.DbcpDataSourceFactory</code>) are
    as follows:</p>
    </primary>
    <p>
    Tomcat の標準データソースリソースファクトリ
    (<code>org.apache.naming.factory.DbcpDataSourceFactory</code>)
    の設定プロパティは、以下のようになります。
    </p>
    <primary>
    <ul>
    <li><strong>driverClassName</strong> - Fully qualified Java class name
        of the JDBC driver to be used.</li>
    <li><strong>maxActive</strong> - The maximum number of active instances
        that can be allocated from this pool at the same time.</li>
    <li><strong>maxIdle</strong> - The maximum number of connections that
        can sit idle in this pool at the same time.</li>
    <li><strong>maxWait</strong> - The maximum number of milliseconds that the
        pool will wait (when there are no available connections) for a
        connection to be returned before throwing an exception.</li>
    <li><strong>password</strong> - Database password to be passed to our
        JDBC driver.</li>
    <li><strong>url</strong> - Connection URL to be passed to our JDBC driver.
        (For backwards compatibility, the property <code>driverName</code>
        is also recognized.)</li>
    <li><strong>user</strong> - Database username to be passed to our
        JDBC driver.</li>
    <li><strong>validationQuery</strong> - SQL query that can be used by the
        pool to validate connections before they are returned to the
        application.  If specified, this query MUST be an SQL SELECT
        statement that returns at least one row.</li>
    </ul>
    </primary>
    <ul>
    <li><strong>driverClassName</strong> - 使用される JDBC ドライバの完全修飾
    Java クラス名。</li>
    <li><strong>maxActive</strong> - 
    プールから同時に割り当てられるアクティブインスタンスの最大数。</li>
    <li><strong>maxIdle</strong> - 
    プール内で同時にアイドルにしておける接続の最大数。</li>
    <li><strong>maxWait</strong> - プールが例外を投げるまでに、
    (利用可能な接続がない場合に) 接続を返されるのを待つ最大ミリ秒。</li>
    <li><strong>password</strong> - 
    JDBC ドライバに渡されるデータベースのパスワード。</li>
    <li><strong>url</strong> - JDBC ドライバに渡される接続 URL。
    (下位互換性のために、<code>driverName</code> プロパティも認識します。)</li>
    <li><strong>user</strong> - JDBC ドライバに渡されるデータベースのユーザ名。</li>
    <li><strong>validationQuery</strong> - アプリケーションに返される前に、
    接続が有効かどうか確認するのに、プールが使用する SQL クエリ。
    指定されている場合、このクエリは少なくとも 1 行を返す SQL SELECT
    文でなければなりません。</li>
    </ul>

  </subsection>

</section>


<primary>
<section name="Adding Custom Resource Factories"/>
</primary>
<section name="カスタムリソースファクトリの追加">

  <primary>
  <p>If none of the standard resource factories meet your needs, you can
  write your own factory and integrate it into Tomcat 5, and then configure
  the use of this factory in the <code>conf/server.xml</code> configuration
  file.  In the example below, we will create a factory that only knows how
  to create <code>com.mycompany.MyBean</code> beans, from the
  <a href="#Generic JavaBean Resources">Generic JavaBean Resources</a>
  example, above.</p>
  </primary>
  <p>
  もし標準リソースファクトリに要件を満たすものがなければ、
  独自のファクトリを実装し Tomcat 5 に組み込み、<code>conf/server.xml</code> 
  設定ファイルでそのファクトリの利用方法を設定できます。
  以下の例においては、前述の <a href="#汎用 JavaBean リソース">汎用 
  JavaBean リソース</a>の例から、<code>com.mycompany.MyBean</code> Bean 
  の作り方だけを知っているファクトリを作成します。
  </p>

  <primary>
  <h3>1.  Write A Resource Factory Class</h3>
  </primary>
  <h3>1.  リソースファクトリクラスの実装</h3>

  <primary>
  <p>You must write a class that implements the JNDI service provider
  <code>javax.naming.spi.ObjectFactory</code> inteface.  Every time your
  web application calls <code>lookup()</code> on a context entry that is
  bound to this factory, the <code>getObjectInstance()</code> method is
  called, with the following arguments:</p>
  </primary>
  <p>
  JNDI サービスプロバイダである <code>javax.naming.spi.ObjectFactory</code> 
  インタフェースを実装するクラスを作成しなければいけません。
  Web アプリケーションがこのファクトリと紐づいたコンテキストエントリへの 
  <code>lookup()</code> を呼出すたびに、以下の引数で 
  <code>getObjectInstance()</code> メソッドが呼出されます。
  </p>
  <primary>
  <ul>
  <li><strong>Object obj</strong> - The (possibly null) object containing
      location or reference information that can be used in creating an
      object.  For Tomcat, this will always be an object of type
      <code>javax.naming.Reference</code>, which contains the class name
      of this factory class, as well as the configuration properties
      (from <code>conf/server.xml</code>) to use in creating objects
      to be returned.</li>
  <li><strong>Name name</strong> - The name to which this factory is bound
      relative to <code>nameCtx</code>, or <code>null</code> if no name
      is specified.</li>
  <li><strong>Context nameCtx</strong> - The context relative to which the
      <code>name</code> parameter is specified, or <code>null</code> if
      <code>name</code> is relative to the default initial context.</li>
  <li><strong>Hashtable environment</strong> - The (possibly null)
      environment that is used in creating this object.  This is generally
      ignored in Tomcat object factories.</li>
  </ul>
  </primary>
  <ul>
  <li><strong>Object obj</strong> - オブジェクト作成時に使われる、
      場所および参照の情報を含むオブジェクト (null の可能性あり)。
      Tomcat では、これは常に <code>javax.naming.Reference</code> 
      型のオブジェクトで、このファクトリクラスのクラス名や、
      返値のオブジェクトを作成するのに使う (<code>conf/server.xml</code>
      からの) 設定プロパティが含まれています。</li>
  <li><strong>Name name</strong> - このファクトリが紐づく名前で、
      <code>nameCtx</code> をベースとします。
      名前を指定しないときは <code>null</code>。</li>
  <li><strong>Context nameCtx</strong> - 指定された <code>name</code> 
      パラメータのベースとなるコンテキスト。<code>name</code> 
      がデフォルトの初期コンテキストをベースとする場合は 
      <code>null</code>。</li>
  <li><strong>Hashtable environment</strong> - 
      このオブジェクトを作成するのに使われる環境 (null の可能性あり)。
      Tomcat オブジェクトファクトリでは通常は無視される。</li>
  </ul>

  <primary>
  <p>To create a resource factory that knows how to produce <code>MyBean</code>
  instances, you might create a class like this:</p>
  </primary>
  <p>
  <code>MyBean</code> インスタンスの生成方法を知るリソースファクトリを作成するには、
  以下のようなクラスを作ればいいでしょう。
  </p>

<primary>
<source>
package com.mycompany;

import java.util.Enumeration;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;

public class MyBeanFactory implements ObjectFactory {

  public Object getObjectInstance(Object obj,
      Name name, Context nameCtx, Hashtable environment)
      throws NamingException {

      // Acquire an instance of our specified bean class
      MyBean bean = new MyBean();

      // Customize the bean properties from our attributes
      Reference ref = (Reference) obj;
      Enumeration addrs = ref.getAll();
      while (addrs.hasMoreElements()) {
          RefAddr addr = (RefAddr) addrs.nextElement();
          String name = addr.getType();
          String value = (String) addr.getContent();
          if (name.equals("foo")) {
              bean.setFoo(value);
          } else if (name.equals("bar")) {
              try {
                  bean.setBar(Integer.parseInt(value));
              } catch (NumberFormatException e) {
                  throw new NamingException("Invalid 'bar' value " + value);
              }
          }
      }

      // Return the customized instance
      return (bean);

  }

}
</source>
</primary>
<source>
package com.mycompany;

import java.util.Enumeration;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;

public class MyBeanFactory implements ObjectFactory {

  public Object getObjectInstance(Object obj,
      Name name, Context nameCtx, Hashtable environment)
      throws NamingException {

      // 指定の bean クラスのインスタンスを取得
      MyBean bean = new MyBean();

      // 属性にしたがい bean のプロパティをカスタマイズ
      Reference ref = (Reference) obj;
      Enumeration addrs = ref.getAll();
      while (addrs.hasMoreElements()) {
          RefAddr addr = (RefAddr) addrs.nextElement();
          String name = addr.getType();
          String value = (String) addr.getContent();
          if (name.equals("foo")) {
              bean.setFoo(value);
          } else if (name.equals("bar")) {
              try {
                  bean.setBar(Integer.parseInt(value));
              } catch (NumberFormatException e) {
                  throw new NamingException("Invalid 'bar' value " + value);
              }
          }
      }

      // カスタマイズしたインスタンスを返す
      return (bean);

  }

}
</source>

  <primary>
  <p>In this example, we are unconditionally creating a new instance of
  the <code>com.mycompany.MyBean</code> class, and populating its properties
  based on the parameters included in the <code>&lt;ResourceParams&gt;</code>
  element that configures this factory (see below).  You should note that any
  parameter named <code>factory</code> should be skipped - that parameter is
  used to specify the name of the factory class itself (in this case,
  <code>com.mycompany.MyBeanFactory</code>) rather than a property of the
  bean being configured.</p>
  </primary>
  <p>
  この例では、無条件に <code>com.mycompany.MyBean</code> 
  クラスの新しいインスタンスを作成し、このファクトリを設定する 
  <code>&lt;ResourceParams&gt;</code> 要素 (以下を参照) 
  に含まれているパラメータに基づいてプロパティに設定を行います。
  <code>factory</code> という名前のパラメータはすべてスキップされることに注意して下さい。
  このパラメータは、bean に設定されるパラメータではなく、
  ファクトリクラスそのものの名前 (この場合は
  <code>com.mycompany.MyBeanFactory</code>) を指定するのに使います。
  </p>

  <primary>
  <p>For more information about <code>ObjectFactory</code>, see the
  <a href="http://java.sun.com/products/jndi/docs.html">JNDI 1.2 Service
  Provider Interface (SPI) Specification</a>.</p>
  </primary>
  <p>
  <code>ObjectFactory</code> に関するより詳しい情報は、
  <a href="http://java.sun.com/products/jndi/docs.html">JNDI 1.2 Service
  Provider Interface (SPI) 仕様</a> を参照してください。
  </p>

  <primary>
  <p>You will need to compile this class against a class path that includes
  all of the JAR files in the <code>$CATALINA_HOME/common/lib</code> and
  <code>$CATALINA_HOME/server/lib</code> directories.  When you are through,
  place the factory class (and the corresponding bean class) unpacked under
  <code>$CATALINA_HOME/common/classes</code>, or in a JAR file inside
  <code>$CATALINA_HOME/common/lib</code>.  In this way, the required class
  files are visible to both Catalina internal resources and your web
  application.</p>
  </primary>
  <p>
  このクラスをコンパイルするには、クラスパスに <code>$CATALINA_HOME/common/lib</code> 
  と <code>$CATALINA_HOME/server/lib</code> ディレクトリ内のすべての JAR 
  ファイルが含まれている必要があります。それが完了している場合は、
  ファクトリクラス (と対応する bean クラス) を展開した状態で
  <code>$CATALINA_HOME/common/classes</code> 以下に置くか、もしくは JAR 
  ファイルとして <code>$CATALINA_HOME/common/lib</code> 内に置いてください。
  こうすれば、必要なクラスファイルが Catalina 内部リソースと Web 
  アプリケーションの両方から見えるようになります。
  </p>

  <primary>
  <h3>2.  Declare Your Resource Requirements</h3>
  </primary>
  <h3>2.  リソースに必要なものを定義</h3>

  <primary>
  <p>Next, modify your web application deployment descriptor
  (<code>/WEB-INF/web.xml</code>) to declare the JNDI name under which
  you will request new instances of this bean.  The simplest approach is
  to use a <code>&lt;resource-env-ref&gt;</code> element, like this:</p>
  </primary>
  <p>
  次に、この bean の新しいインスタンスをリクエストする JNDI 名を定義するために 
  Web アプリケーション配備記述子 (<code>/WEB-INF/web.xml</code>) を修正します。
  一番簡単なのは以下のように <code>&lt;resource-env-ref&gt;</code> 
  要素を使う方法です。
  </p>

<primary>
<source>
&lt;resource-env-ref&gt;
  &lt;description&gt;
    Object factory for MyBean instances.
  &lt;/description&gt;
  &lt;resource-env-ref-name&gt;
    bean/MyBeanFactory
  &lt;/resource-env-ref-name&gt;
  &lt;resource-env-ref-type&gt;
    com.mycompany.MyBean
  &lt;/resource-env-ref-type&gt;
&lt;resource-env-ref&gt;
</source>
</primary>
<source>
&lt;resource-env-ref&gt;
  &lt;description&gt;
    MyBean インスタンスのためのオブジェクトファクトリ。
  &lt;/description&gt;
  &lt;resource-env-ref-name&gt;
    bean/MyBeanFactory
  &lt;/resource-env-ref-name&gt;
  &lt;resource-env-ref-type&gt;
    com.mycompany.MyBean
  &lt;/resource-env-ref-type&gt;
&lt;resource-env-ref&gt;
</source>

    <primary>
    <p><strong>WARNING</strong> - Be sure you respect the element ordering
    that is required by the DTD for web application deployment descriptors!
    See the
    <a href="http://java.sun.com/products/servlet/download.html">Servlet
    Specification</a> for details.</p>
    </primary>
    <p>
    <strong>警告</strong> - 要素の順序が Web アプリケーション配備記述子の DTD
    で要求されたとおりであることを確認してください! 詳細は、
    <a href="http://java.sun.com/products/servlet/download.html">Servlet
    仕様</a> を参照してください。
    </p>

  <primary>
  <h3>3.  Code Your Application's Use Of This Resource</h3>
  </primary>
  <h3>3.  このリソースを使うコードをアプリケーションに記述</h3>

  <primary>
  <p>A typical use of this resource environment reference might look
  like this:</p>
  </primary>
  <p>
  このリソース環境参照の典型的な使い方は、以下のようになるでしょう。
  </p>

<source>
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
MyBean bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");

writer.println("foo = " + bean.getFoo() + ", bar = " +
               bean.getBar());
</source>

    <primary>
    <h3>4.  Configure Tomcat's Resource Factory</h3>
    </primary>
    <h3>4.  Tomcat でのリソースファクトリの定義</h3>

    <primary>
    <p>To configure Tomcat's resource factory, add an elements like this to the
    <code>$CATALINA_HOME/conf/server.xml</code> file, nested inside the
    <code>Context</code> element for this web application (or nested inside
    a <code>DefaultContext</code> element for the surrounding
    <code>&lt;Host&gt;</code> or <code>&lt;Engine&gt;</code> element.</p>
    </primary>
    <p>
    Tomcat のリソースファクトリを設定するには、この Web アプリケーションの
    <code>Context</code> 要素内で入れ子になった
    (あるいは <code>&lt;Host&gt;</code> または <code>&lt;Engine&gt;</code>
    要素内の <code>DefaultContext</code> 要素内で入れ子になった)、
    以下のような要素を <code>$CATALINA_HOME/conf/server.xml</code>
    ファイルに追加します。
    </p>
<source>
&lt;Context ...&gt;
  ...
  &lt;Resource name="bean/MyBeanFactory" auth="Container"
            type="com.mycompany.MyBean"/&gt;
  &lt;ResourceParams name="bean/MyBeanFactory"&gt;
    &lt;parameter&gt;
      &lt;name&gt;factory&lt;/name&gt;
      &lt;value&gt;com.mycompany.MyBeanFactory&lt;/value&gt;
    &lt;/parameter&gt;
    &lt;parameter&gt;
      &lt;name&gt;bar&lt;/name&gt;
      &lt;value&gt;23&lt;/value&gt;
    &lt;/parameter&gt;
  &lt;/ResourceParams&gt;
  ...
&lt;/Context&gt;
</source>

    <primary>
    <p>Note that the resource name (here, <code>bean/MyBeanFactory</code>
    must match the value specified in the web application deployment
    descriptor.  We are also initializing the value of the <code>bar</code>
    property, which will cause <code>setBar(23)</code> to be called before
    the new bean is returned.  Because we are not initializing the
    <code>foo</code> property (although we could have), the bean will
    contain whatever default value is set up by its constructor.</p>
    </primary>
    <p>
    リソース名 (ここでは <code>bean/MyBeanFactory</code>) が Web 
    アプリケーション配備記述子で指定した値と一致していなければならないことに注意してください。
    また、<code>bar</code> プロパティの値の初期化も行っており、それにより
    新しい bean が返される前に <code>setBar(23)</code> が呼び出されます。
    <code>foo</code> プロパティは (初期化可能でしたが) 初期化していないので、
    bean ではコンストラクタでセットアップする何らかのデフォルト値が入ることでしょう。
    </p>

    <primary>
    <p>You will also note that, from the application developer's perspective,
    the declaration of the resource environment reference, and the programming
    used to request new instances, is identical to the approach used for the
    <em>Generic JavaBean Resources</em> example.  This illustrates one of the
    advantages of using JNDI resources to encapsulate functionality - you can
    change the underlying implementation without necessarily having to
    modify applications using the resources, as long as you maintain
    compatible APIs.</p>
    </primary>
    <p>
    また、アプリケーション開発者としての観点で言えば、リソース環境参照の定義や、
    新しいインスタンスを要求するのに使われているプログラミングは、
    <em>汎用 JavaBean リソース</em> の例で使われている方法と同じであることにも注意して下さい。
    これは、機能性をカプセル化するために JNDI リソースを使う利点の一つを具体的に示しています。
    つまり 互換性のある API を維持している限り、
    リソースを使ってアプリケーションを修正しなくても、
    内部の実装を変更できます。
    </p>

</section>

</body>

</document>
