package de.torisan.servlet;

import java.io.File;
import java.io.Serializable;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

import org.apache.log4j.Logger;


/** Example for using of using lifecyle support of a servlet container.
 * <p />
 * To use lifecycle support any classes wishing to do so must implement HttpSessionBindingListener.
 * <p />
 * The contract of this interface is to have two methods explicitly reacting to the binding and the unbinding
 * event.
 * <p />
 * Each time an instance of a class implementing the interface is bound to a HttpSession (for instance 
 * instantiated as a session bean for the first time) the method valueBound ist being called. 
 * <br />
 * When the instance is unbound from the HttpSession (HttpSession#destroy - either initiated by the user 
 * programmatically or through session manager of container) the valueUnbound method is called.
 * <p /> 
 * Using these lifecacle events for one's own purposes is done through implementing the body of the two 
 * methods appropriately. 
 * 
 * @author Toralf Richter
 * @version $Id$
 */
public class SessionLiveCycleSupport implements HttpSessionBindingListener, Serializable{

 private static final transient Logger logger = Logger.getLogger( SessionLiveCycleSupport.class ); 
 
 /* (non-Javadoc)
  * @see javax.servlet.http.HttpSessionBindingListener#valueBound(javax.servlet.http.HttpSessionBindingEvent)
  */
 public void valueBound(HttpSessionBindingEvent be) {
  logger.info( "Object bound to session, we are not taking any action at this time. BindingEvent information: *" + be.getName() + "*, " + be.getValue() );

  /* Measures here might be initialisation of the instance with values from a database, 
   * serialisation store ... (as far as those have not already been taken by the constructor)
   */
 }

 /* (non-Javadoc)
  * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(javax.servlet.http.HttpSessionBindingEvent)
  */
 public void valueUnbound(HttpSessionBindingEvent be) {
  logger.info("Object unbound from session. BindingEvent information*" + be.getName() + "*, " + be.getValue() );

  /* Sample code - delete some file this instance has been holding. 
   * This file would become obsolete after expiry of the HttpSession and litter the filesystem. 
   * To avoid having obsolete files in the system this file should be deleted on session exit */
  if(this.someStoreFileToBeDeletedOnSessionExit!=null ) {
   if(this.someStoreFileToBeDeletedOnSessionExit.exists()) {
   this.someStoreFileToBeDeletedOnSessionExit.delete();
   logger.info("Delete obsolete file " + this.someStoreFileToBeDeletedOnSessionExit.getAbsolutePath() + " before exiting HttpSession." );
   }
  } else {
   logger.info("Expected file " + this.someStoreFileToBeDeletedOnSessionExit.getAbsolutePath() + " was not found in the filesystem. No further actions taken on session exit." );   
  }
 } 
 
 
 /** Gets the value of "some dummy instance field for demonstration purposes"
  * @return - File
  */
 public File getSomeStoreFileToBeDeletedOnSessionExit() {
  return this.someStoreFileToBeDeletedOnSessionExit;
 }
 
 /** Sets the value of "some dummy instance field for demonstration purposes"
  * @param someStoreFileToBeDeletedOnSessionExit - File
  */
 public void setSomeStoreFileToBeDeletedOnSessionExit(File someStoreFileToBeDeletedOnSessionExit) {
  this.someStoreFileToBeDeletedOnSessionExit = someStoreFileToBeDeletedOnSessionExit;
 }
 
 /** Some dummy instance field for demonstration purposes */
 private File someStoreFileToBeDeletedOnSessionExit = null;
 
 
}

