package moreservlets.listeners;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/** Listener that monitors changes in the company
 *  name (which is stored in the companyName attribute
 *  of the servlet context).
 *  <P>
 *  Taken from More Servlets and JavaServer Pages
 *  from Prentice Hall and Sun Microsystems Press,
 *  http://www.moreservlets.com/.
 *  &copy; 2002 Marty Hall; may be freely used or adapted.
 */

public class ChangedCompanyNameListener
    implements ServletContextAttributeListener {

  /** When the companyName attribute changes, put
   *  the previous value into the formerCompanyName
   *  attribute.
   */
  
  public void attributeReplaced
                   (ServletContextAttributeEvent event) {
    if (event.getName().equals("companyName")) {
      String oldName = (String)event.getValue();
      ServletContext context = event.getServletContext();
      context.setAttribute("formerCompanyName", oldName);
    }
  }

  public void attributeAdded
                   (ServletContextAttributeEvent event) {}
  
  public void attributeRemoved
                   (ServletContextAttributeEvent event) {}
}
