<?xml version="1.0" encoding="UTF-8"?>
<!--
  Copyright 2002-2004 The Apache Software Foundation or its licensors,
  as applicable.

  Licensed 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.
-->
<!DOCTYPE document PUBLIC "-//WAMBLEE.ORG//ENTITIES Include Extension for Xdoc V1//EN"
"includeExtension-v1.dtd">
<document> 
  <header> 
    <title>Installation</title> 
  </header> 
  <body> 
    <section id="introduction">
      <title>Introduction</title>
      <p> PhotoXChange is a standard J2EE web application. Configuration settings are required
        for both the web application itself, and for the application server. 
      </p>
    </section>
    
    <section id="conf-web-app">
      <title>Web application configuration</title>
      <p>
        To configure the web application, the following configuration settings must be done:  
      </p>
      
      <table>
        <tr>
          <th>Where</th>
          <th>Parameter Name</th>
          <th>Meaning</th><th>Default
          value</th></tr>
        <tr>
          <td><code>WEB-INF/classes/ org.wamblee.photos.properties</code></td>
          <td>org.wamblee.photos.path</td>
          <td>Full path name of the directory where photos are stored. 
            This directory must be created beforehand.</td>
          <td>A path that works on some developer's system but most likely not on yours.</td>
        </tr>
        <tr>
          <td></td>
          <td>org.wamblee.photos.realm</td>
          <td>The security domain used. This must match with the JNDI name specified
              in <code>WEB-INF/jboss-web.xml</code> and it must also correspond with
              a security domain configured in the application server.</td>
          <td>PhotoXChange</td>
        </tr>
        <tr>
          <td></td>
          <td>org.wamblee.photos.security.userprincipal</td>
          <td>The classname of the principal class representing the user. </td>
          <td>org.jboss.security.SimplePrincipal</td>
        </tr>
        <tr>
          <td></td>
          <td>org.wamblee.photos.admingroup</td>
          <td>The name of the administrator group.</td>
          <td>administrators</td>
        </tr>
      </table>
      
    </section>
    
    <section>
      <title>Database Setup</title>
      <p>The only currently supported database is MySQL. </p>
      
      <section>
        <title>MySQL Setup</title>
        
        <p>
          The aim of the MySQL setup is to setup a database with the following
          properties:
        </p>       

        <ul>
          <li>The InnoDB storage engine is used</li>
          <li>Transaction isolation level is repeatable read</li>
          <li>A single user is created with all rights</li>
        </ul>
        
        <section>
          <title>Selecting the storage engine</title>
          <p>
            First of all, MySQL must be configured to use the InnoDB storage engine for the photos 
            database. This can be done in two ways: 
          </p>
          <ul>
            <li>Configuration setting; Edit the <code>my.cnf</code> configuration file (usually
              located in <code>/etc</code> on *nix systems and add the following lines to the 
              <code>[mysqld]</code> section:
              <source>default-table-type = INNODB
              </source>
              <p>After this, restart mysql. </p>
            </li>
            <li>
              After creating the database tables: For every database table execute the command
              <source>alter table TABLE_NAME engine = INNODB</source>
              to change the storage engine for that table to INNODB. 
            </li>
          </ul>
        </section>
        
        <section>
          <title>Create the database</title>
          
          <p>
            Now create a MySQL database
          </p>
          <source>
            mysqladmin -u root -p create photoxchange
          </source>
          <p>
            Where <code>root</code> is the user name of the administrator and <code>photoxchange</code>
            is the name of the database. 
          </p>
          <p>
            Create a user on the database by logging in as administrator 
          </p>
          <source>
            mysql -u root -p photoxchange
          </source>
          <p>
            and creating a user that has access to the database
          </p>
          <source>
            grant all on * to username identified by 'password'; 
          </source>
          <p>
            This creates a user that can access the database from the same host the 
            database is running on. If MySQL is running on a different machine than
            the application server, consult the MySQL manuals for information on how
            to configure access for that user. 
          </p>
        </section>
        
        <section>
          <title>Transactions</title>
          <p>
            After creating the database check the transaction settings by logging on to
            the photoxchange database as administrator and executing the command
          </p>
          <source>
            show variables
          </source>
          <p>
            The value of the <code>tx_isolation</code> variable should be 
            <code>REPEATABLE-READ</code>. If this not the case, set the transaction
            level globally for all databases by specifying 
          </p>
          <source>
            transaction-isolation = REPEATABLE-READ
          </source>
          <p>
            In the <code>[mysqld]</code> section of the <code>my.cnf</code> file. 
            It is also possible to set the transaction isolation level for one database only
            using 
          </p>
          <source>
            SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ
          </source>
          <p>
            See the MySQL manuals for more details. 
          </p>
        </section>
        
        <section>
          <title>Creating the database schema</title>
          <p>
            Populate the database by executing the sql scripts provided with the 
            delivery on the database. On *nix systems:
          </p>
          <source>
            mysql -u username -p photoxchange &lt; setup.sql
          </source>
          <p>
            After this, execute the command <code>show table status;</code> to check that the tables
            indeed use the InnoDB storage engine. 
          </p>
        </section>
        
        <section>
          <title>Populating the database</title>
          <p>
            The photos application will perform the following initializations automatically
            at startup. 
          </p>
          <table>
            <tr>
              <th>Initialization performed</th>
              <th>When?</th>
            </tr>
            <tr>
              <td>Initialization of users with one user 'erik' and one administrator
                'admin', both having password 'abc123'.</td>
              <td>When no users have been defined.</td>
            </tr>
            <tr>
              <td>Initialization of authorization rules. </td>
              <td>When no authorization rules exist.</td>
            </tr>
          </table>
        </section>
        
      </section>
      
    </section>
    
    <section>
      <title>Application Server Setup</title>
      <p>
        For each supported application server, a separate section is provided.
      </p>
      <section id="jboss-4">
        <title>JBoss 4</title>
        
        <p>
          For JBoss, configuration amounts to configuring the datasource and 
          the security domain, 
          The name of the security domain must match that specified in 
          <a href="#conf-web-app">configuration settings</a>.
        </p>
        
        <section>
          <title>Datasource Setup</title>
          
          <p>
            First of all, the database driver for MySQL must be installed by 
            copying the driver jar <code>mysql-connector-java-3.x.y-bin.jar</code>
            from the <a href="site:mysql">MySQL web site</a> to the <code>server/default/lib</code>
            directory of JBoss. 
          </p>
          
          <p>
            Next, the datasource must be configured by copying a 
              <a href="/raw/documentation/photos-ds.xml">photos-ds.xml</a> file to the
            <code>server/default/deploy</code> directory of the application server and
            choosing the correct username and password. 
          </p>
          
        </section>
        
        <section>
          <title>Security Setup</title>
         
          <p>
            To configure security, add the following security domain configuration 
            to <code>login-config.xml</code> in the <code>server/default/conf</code>
            directory of the server: 
          </p>
          
          <source>
   <![CDATA[         <application-policy name="PhotoXChange">
              <authentication>
                <login-module code="org.jboss.security.auth.spi.DatabaseServerLoginModule" flag="required">
                  <module-option name="unauthenticatedIdentity">guest</module-option>
                  <module-option name="dsJndiName">java:/PhotoXChange</module-option>
                  <module-option name="principalsQuery">
                    SELECT PASSWORD FROM USERS WHERE NAME = ?;
                  </module-option>
                  <module-option name="rolesQuery">
                    select g.name, 'Roles' from USERS u, USER_GROUPS ug, GROUPS g 
                    where u.id = ug.user_id and ug.group_id = g.id and u.name = ?
                  </module-option>
                  <module-option name="hashAlgorithm">MD5</module-option>
                  <module-option name="hashEncoding">hex</module-option>
                </login-module>
              </authentication>
            </application-policy>   ]]>
          </source>
          <p>
            Note the module option <code>dsJndiName</code> which refers to the previously configured
            datasource. Also, note the module options <code>hashEncoding</code> and
            <code>hashAlgorithm</code> which specify that passwords should be stored as a base 16
            MD5 hash. 
          </p>
          
        </section>
        
        <section>
          <title>Managing users and groups</title>
          
          <p>
            This release of PhotoXChange provides a rudimentary user interface for managing users
            and groups. This interface can be accessed by logging in as administrator and clicking
            on 'Administration'. 
          </p>
          <p> 
            Alternatively, users and groups may be modified using direct SQL. 
            The table below shows some common scenarios
          </p>
          <table>
            <tr>
              <th>Scenario</th> <th>Howto</th>
            </tr>
            <tr>
              <td>Changing a password</td>
              <td><code>update USERS set password = md5('password') where user = 'username'</code>.
                The MD5 encoded password can also be generated using openssl: 
                <code>echo -n PASSWORD | openssl dgst -md5 -hex</code>
              </td>
            </tr>
            <tr>
              <td>Adding a user</td>
              <td>Add a new row to the USERS table and add a new row to the USER_GROUPS table to add
                the user to a group.</td>
            </tr>
            <tr>
              <td>Adding a group</td>
              <td>Add a new row to the GROUPS table.</td>
            </tr>
            <tr>
              <td>Adding a user to a group</td>
              <td>Add an entry to the USER_GROUPS table</td>
            </tr>
          </table>
        </section>
        
        
      </section>
      
    </section>
    
  </body>
</document>
