<?xml version="1.0"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
-->

<document>
  <properties>
    <title>Torque Tutorial - Step 4: Configuring the Torque Runtime</title>
    <author email="pete-apache-dev@kazmier.com">Pete Kazmier</author>
    <author email="seade@backstagetech.com.au">Scott Eade</author>
    <author email="fischer@seitenbau.de">Thomas Fischer</author>
  </properties>
  <body>

<section name="Step 4: Configuring the Torque Runtime">

<p>
  Before we can start to write a Torque application, we have to
  configure the runtime environment:
  <ul>
    <li>
      We have to make sure that the generated
      object model classes have access to the Torque
      runtime and associated libraries.
    </li>
    <li>
      The Torque runtime needs a configuration file
      in order to retrieve the data which is necessary
      to connect to the database.
    </li>
  </ul>
  These two steps will be covered in the following sections.
</p>

</section>

<section name="Setting up the classpath">

<p>
  The libraries which the generated classes depend on
  are specified in the file <em>project.xml</em>
  in the top level directory of the project.
  You already created this file in step 3 of the
  tutorial; now it needs to be extended a bit.
  Assuming that you use JDK 1.4+, you need to add
  the following entries to the &lt;dependencies&gt;
  section of your <em>project.xml</em>
</p>
<source><![CDATA[
    <dependency>
      <artifactId>torque</artifactId>
      <groupId>torque</groupId>
      <version>3.3</version>
    </dependency>

    <dependency>
      <groupId>avalon-framework</groupId>
      <artifactId>avalon-framework-api</artifactId>
      <version>4.3</version>
      <url>http://avalon.apache.org/</url>
    </dependency>

    <dependency>
        <groupId>avalon-logkit</groupId>
        <artifactId>avalon-logkit</artifactId>
        <version>2.1</version>
        <type>jar</type>
    </dependency>

    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils-core</artifactId>
      <version>1.7.0</version>
      <url>http://commons.apache.org/beanutils/</url>
    </dependency>

    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.2</version>
      <url>http://commons.apache.org/collections/</url>
    </dependency>

    <dependency>
      <groupId>commons-configuration</groupId>
      <artifactId>commons-configuration</artifactId>
      <version>1.4</version>
      <url>http://commons.apache.org/configuration/</url>
    </dependency>

    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.2.2</version>
      <url>http://commons.apache.org/dbcp/</url>
    </dependency>

    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.3</version>
      <url>http://commons.apache.org/lang/</url>
    </dependency>

    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1</version>
      <url>http://commons.apache.org/logging/</url>
    </dependency>

    <dependency>
      <groupId>commons-pool</groupId>
      <artifactId>commons-pool</artifactId>
      <version>1.3</version>
      <url>http://commons.apache.org/pool/</url>
    </dependency>

    <dependency>
      <groupId>jcs</groupId>
      <artifactId>jcs</artifactId>
      <version>1.3</version>
      <url>http://jakarta.apache.org/jcs/</url>
    </dependency>

    <dependency>
      <artifactId>village</artifactId>
      <groupId>torque</groupId>
      <version>3.3</version>
    </dependency>

    <dependency>
      <groupId>xerces</groupId>
      <artifactId>xercesImpl</artifactId>
      <version>2.6.2</version>
      <url>http://xerces.apache.org/xerces2-j/</url>
    </dependency>

    <dependency>
      <groupId>xml-apis</groupId>
      <artifactId>xml-apis</artifactId>
      <version>2.0.2</version>
      <url>http://xml.apache.org/commons/</url>
    </dependency>

]]></source>

<p>
  You don't need to download any of these libraries -
  Maven will download them automatically when you
  build your project.
</p>

<p>
  <b>
    Note: There is no need to include the torque-gen jar file in your project
    classpath, including it may adversly affect the logging configuration of
    your application.
  </b>
</p>

</section>

<section name="Torque Runtime Properties">

<p>
  The second step in the configuration of the
  Torque Runtime are the
  Torque run-time properties.  As the name suggests,
  these properties are used when your application is
  executing the object model code generated by Torque.
  The run-time properties control database
  parameters such as drivers, usernames, and
  passwords.  These properties can be saved in any
  file because your application must explicitly
  initialize Torque (as you'll see later in this
  tutorial).
</p>

<p>
  We will save our runtime properties in the
  a file called <em>torque.properties</em>.
  Create a subdirectory src/conf in the
  top-level directory of your project, and create
  a new file called <em>torque.properties</em>
  in it. Add the following lines to this file:
</p>

<source><![CDATA[
torque.database.default = bookstore
torque.database.bookstore.adapter = mysql

#Using commons-dbcp
torque.dsfactory.bookstore.factory = org.apache.torque.dsfactory.SharedPoolDataSourceFactory
torque.dsfactory.bookstore.connection.driver = org.gjt.mm.mysql.Driver
torque.dsfactory.bookstore.connection.url = jdbc:mysql://localhost:3306/bookstore
torque.dsfactory.bookstore.connection.user = user
torque.dsfactory.bookstore.connection.password = password
  ]]></source>

  <p>
    Change the adapter, driver, url, user and password parameters
    to match the parameters for your database.
    In the following table, the parameters used in the sample
    configuration are described. For further information, see the
    <a href="../runtime/documentation/initialisation-configuration.html#Configuration">Runtime Configuration Reference</a>.
  </p>

  <table>
    <tr> <th>Property</th> <th>Description</th> </tr>
    <tr>
      <td>torque.database.default</td>
      <td>
        Torque has the ability to use multiple
        databases.  This property specifies which
        database is to be used as the default.
      </td>
    </tr>
    <tr>
      <td>torque.database.XXX.adapter</td>
      <td>
        Torque has the ability to deal with multiple database systems.
        This property specifies the database adapter to use.
      </td>
    </tr>
    <tr>
      <td>torque.dsfactory.XXX.factory</td>
      <td>
        The factory class that will be used to provide database connections.
      </td>
    </tr>
    <tr>
      <td>torque.dsfactory.XXX.connection.driver</td>
      <td>
        The JDBC database driver to use when
        connecting to your database.
      </td>
    </tr>
    <tr>
      <td>torque.database.XXX.connection.url</td>
      <td>
        The URL that will be used to access your
        database.  Torque's generated object model
        will perform all database operations using
        this URL.  This value should reflect the
        database name specified in your database
        schema file (see the <em>database</em>
        element's <em>name</em> attribute).
      </td>
    </tr>
    <tr>
      <td>torque.database.XXX.connection.username</td>
      <td>
        The username that has sufficient privileges
        to access your database.  This user does not
        require privileges to create and drop
        tables, unlike the username that was
        specified in <em>project.properties</em>.
      </td>
    </tr>
    <tr>
      <td>torque.database.XXX.connection.password</td>
      <td>
        The password for the specified username.
      </td>
    </tr>
  </table>

  <p>
    It is worth re-iterating that these run-time
    properties are not used by Torque when generating
    your object model and creating your database.  They
    are used only by the application utilizing the
    Torque-generated object model classes at run-time.
  </p>

</section>

<section name="Logging configuration">

  <p>
    Torque uses
    <a href="http://commons.apache.org/logging/">
    commons-logging</a> as a logging interface.
    To enable logging in your application, read the
    <a href="http://commons.apache.org/logging/commons-logging-1.0.3/usersguide.html">
    commons-logging user guide</a>.
  </p>

  <p>
    If you have no trouble running the tutorial,
    you do not need to configure logging now. If, however,
    you experience runtime errors later on
    and do not find the reason immediately,
    it might be a good idea to configure
    logging and look at the log messages. Also, for a
    &quot;serious&quot; application, logging is
    indispensible in order to track down any
    unexpected errors.
  </p>

</section>

<section name="Where to next">

  <p>
    Now you have finished configuring the Torque runtime.
    You are now ready to use the generated classes to access
    the database.
  </p>
  <p>
    Next we will look <a href="step5.html">Writing a Sample Application</a>.
  </p>

</section>

<section name="User Comments">
  <a href="http://wiki.apache.org/db-torque/TutorialUserCommentsStep4">User comments</a>
  for this step
</section>

</body>
</document>
