package coreservlets.listeners;

import javax.servlet.*;
import coreservlets.beans.*;

/** ServletContextListener to load JDBC connection information
 *  into the servlet context. For each desired connection, a
 *  ConnectionInfoBean is created (stores the connection
 *  name, description, driver class, url, username, and password)
 *  and placed into the servlet context. The key for obtaining
 *  the ConnectionInfoBean is the connection name (in upper case).
 *  <P>
 *  Hard-code the connection in the loadConnectionInfo method,
 *  as shown, or use a ConnectionPropertiesListener to load
 *  the data from a Properties file.
 *  <P>
 *  Taken from Core Servlets and JavaServer Pages
 *  from Prentice Hall and Sun Microsystems Press,
 *  http://www.coreservlets.com/.
 *  &copy; 2004 Marty Hall and Larry Brown;
 *  may be freely used or adapted.
 */

public class ConnectionServletContextListener
    implements ServletContextListener {
  // ConnectionInfoBean for Northwind database on MS Access.
  public static final String CIB_MSACCESS_NORTHWIND =
    "CIB_MSACCESS_NORTHWIND";
  // ConnectionInfoBean for CSAJSP database on MySQL.
  public static final String CIB_MYSQL_CSAJSP =
    "CIB_MYSQL_CSAJSP";
  protected ServletContext context;

  public void contextInitialized(ServletContextEvent event) {
    context = event.getServletContext();
    loadConnectionInfo();
  }

  /** Build a ConnectionInfoBean for each connection and
   *  store in the servlet context.
   */

  public void loadConnectionInfo() {
    String connectionName, description, driverClass, url,
           username, password;
    ConnectionInfoBean info = null;

    // Hard-coded information for a connection.
    // Use ConnectionPropertiesListener to load the data
    // from a Properties file.

    // Set up ConnectionInfoBean to Northwind database on
    // Microsoft Access.
    connectionName = CIB_MSACCESS_NORTHWIND;
    description = "MS Access 4.0";
    driverClass = "sun.jdbc.odbc.JdbcOdbcDriver";
    url = "jdbc:odbc:Northwind";
    username = "";
    password = "";
    info = new ConnectionInfoBean(connectionName, description,
                                  driverClass, url,
                                  username, password);
    addConnectionInfo(info);

    // Set up ConnectionInfoBean to CSAJSP database on MySQL.
    connectionName = CIB_MYSQL_CSAJSP;
    description = "MySQL Connector/J 3.0";
    driverClass = "com.mysql.jdbc.Driver";
    url = "jdbc:mysql://localhost:3306/csajsp";
    username = "brown";
    password = "larry";
    info = new ConnectionInfoBean(connectionName, description,
                                  driverClass, url,
                                  username, password);
    addConnectionInfo(info);

    // Add your connection information here ...
  }

  /** Store the ConnectionInfoBean in the servlet context,
   *  using the connection name as the key. The stored
   *  ConnectionInfoBean contains the necessary information
   *  to create a JDBC connection (driver class, URL, username,
   *  and password).
   */

  public void addConnectionInfo(ConnectionInfoBean info) {
    String connectionName = info.getConnectionName();
    connectionName = connectionName.toUpperCase();
    context.setAttribute(connectionName, info);
    System.out.println("ConnectionInfoBean added: " +
                       connectionName);
  }

  public void contextDestroyed(ServletContextEvent event) { }
}