/*
 * DatabaseHttpServlet.java
 *
 * Created on 14 de Novembro de 2000, 15:15
 * Modified on 19 de Fevereiro de 2001
 */

package com.newtradebr.servlets;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * This class extends the basic HttpServlet by adding a database connection.
 * The connection is made using the connection URL and driver parameter passed
 * through the servlet context. It is important that the drivers for the
 * database connection be present in the <I>/lib</I> directory of the WEB-INF
 * folder. <P>
 * 
 * For the class to function properly, you must set the following servlet 
 * context parameters in the web.xml file:<BR>
 * <B>dbConnectionUrl</B> - the connection URL for the database connection.<BR>
 * <B>dbDriver</B> - the driver that must be loaded. <P>
 * 
 * This class has been tested with the following drivers:<BR>
 * MySQL - mm.mysql-2.0.4-bin.jar
 * 
 * @author  Christian Rauh
 * @company Newtrade
 * @version 1.0
 */
public class DatabaseHttpServlet extends SingleMethodServlet {

    // Holds the connection to the database.
    public Connection dbConnection;

    public void init(ServletConfig config) throws ServletException {
        
        // Call the superclass´s init() for the servlet to work properly
        super.init(config);
        
        // Get the servlet context parameters
        String dbDriver = config.getServletContext().getInitParameter("dbDriver");
        String dbConnectionUrl = config.getServletContext().getInitParameter("dbConnectionUrl");
        String dbUser = config.getServletContext().getInitParameter("dbUser");
        String dbPassword = config.getServletContext().getInitParameter("dbPassword");

        // Print some information to know if things are going well.
        System.out.println("Initializing database connection...");
        System.out.println("Database Driver: " + dbDriver); 
        System.out.println("Connection URL: " + dbConnectionUrl);

        try {

            dbConnection = connectToDatabase(dbDriver, dbConnectionUrl, dbUser, dbPassword);

        } catch(ClassNotFoundException cnfe) {

            // The driver class was not found.
            System.out.println("Database driver class not found.");
            System.out.println("Terminating.");
            throw new ServletException();

        } catch(SQLException sqle) {

            // A connection to the server could not be done
            System.out.println("Could not connect to server.");
            System.out.println("Terminating.");
            throw new ServletException();

        } catch(Exception e) {
            
            // A connection to the server could not be done
            System.out.println("Unknown Exception found.");
            System.out.println("Check if the database server is up.");
            System.out.println(e);
            System.out.println("Terminating.");
            throw new ServletException();
            
        }

        System.out.println("Initialization done.");
    }

    /**
     * Loads the database driver and returns a connection to the database.
     */
    private Connection connectToDatabase(String dbDriver, String dbConnectionUrl, String dbUser, String dbPassword)
    throws ClassNotFoundException, SQLException {

        // Load drivers
        Class.forName(dbDriver);
        System.out.println("Driver loaded.");

        // Create a connection
        Connection c = DriverManager.getConnection(dbConnectionUrl, dbUser, dbPassword);
        System.out.println("Connection made.");

        return(c);
    }

}