/* * LogHttpSessionBindingEvent - Verify the Bound/Unbound of SessionBinding * event (bug 4817526) */ package event_listeners; import java.sql.*; import javax.sql.*; import javax.naming.*; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class LogHttpSessionBindingEvent implements HttpSessionBindingListener { private static File baseDir = null; private PrintWriter logFileOut = null; private static final String logFileName = "LogHttpSessionBindingEvent.txt"; public void valueBound(HttpSessionBindingEvent event) { HttpSession session = event.getSession(); try { if (baseDir == null) { ServletContext context = session.getServletContext(); baseDir = (File) context.getAttribute( "javax.servlet.context.tempdir"); logFileOut = new PrintWriter(new FileWriter( new File(baseDir, logFileName)), true); } Context context = (Context) new InitialContext().lookup("java:comp/env"); logFileOut.println("[LogHttpSessionBindingEvent.valueBound()]: " + "InitialContext.lookup(\"java:comp/env\") OK"); DataSource pool = (DataSource) context.lookup("jdbc/jdbc-simple"); logFileOut.println("[LogHttpSessionBindingEvent.valueBound()]: " + "context.lookup(\"jdbc/jdbc-simple\") OK"); Connection connection = pool.getConnection(); logFileOut.println("[LogHttpSessionBindingEvent.valueBound()]: " + "pool.getConnection() OK"); PreparedStatement statement = connection.prepareStatement("INSERT INTO LOGGING (namevalue, sessid) values (?,?)"); statement.setString(1, "Bound"); statement.setString(2, (String) session.getId()); statement.executeUpdate(); logFileOut.println("[LogHttpSessionBindingEvent.valueBound()]: " + "statement.executeUpdate() OK"); connection.close(); logFileOut.close(); baseDir = null; } catch (Exception e) { System.out.println("[LogHttpSessionBindingEvent.valueBound()]: " + "*** EXCEPTION -> " + e); e.printStackTrace(System.out); } } public void valueUnbound(HttpSessionBindingEvent event) { HttpSession session = event.getSession(); try { if (baseDir == null) { ServletContext context = session.getServletContext(); baseDir = (File) context.getAttribute( "javax.servlet.context.tempdir"); logFileOut = new PrintWriter(new FileWriter( new File(baseDir, logFileName)), true); } Context context = (Context) new InitialContext().lookup("java:comp/env"); logFileOut.println("[LogHttpSessionBindingEvent.valueUnbound()]: " + "InitialContext.lookup(\"java:comp/env\") " + "OK"); DataSource pool = (DataSource) context.lookup("jdbc/jdbc-simple"); logFileOut.println("[LogHttpSessionBindingEvent.valueUnbound()]: " + "context.lookup(\"jdbc/jdbc-simple\") OK"); Connection connection = pool.getConnection(); logFileOut.println("[LogHttpSessionBindingEvent.valueUnbound()]: " + "pool.getConnection() OK"); PreparedStatement statement = connection.prepareStatement("UPDATE LOGGING SET namevalue = ? WHERE sessid = ?"); statement.setString(1, "Unbound"); statement.setString(2, (String) session.getId()); statement.executeUpdate(); logFileOut.println("[LogHttpSessionBindingEvent.valueBound()]: " + "statement.executeUpdate() OK"); connection.close(); logFileOut.close(); baseDir = null; } catch (Exception e) { System.out.println("[LogHttpSessionBindingEvent.valueUnbound()]: " + "*** EXCEPTION -> " + e); e.printStackTrace(System.out); } } }