[Httpunit-commit] CVS: httpunit/src/com/meterware/servletunit SessionListenerDispatcher.java,NONE,1.
Brought to you by:
russgold
Update of /cvsroot/httpunit/httpunit/src/com/meterware/servletunit In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11966/src/com/meterware/servletunit Modified Files: ServletRunner.java ServletUnitContext.java ServletUnitHttpSession.java WebApplication.java Added Files: SessionListenerDispatcher.java Log Message: Add support for HttpSessionListener ***** Error reading new file[Errno 2] No such file or directory: 'SessionListenerDispatcher.java' Index: ServletRunner.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletRunner.java,v retrieving revision 1.23 retrieving revision 1.24 diff -u -r1.23 -r1.24 --- ServletRunner.java 13 Feb 2004 03:17:46 -0000 1.23 +++ ServletRunner.java 13 Feb 2004 05:45:19 -0000 1.24 @@ -50,7 +50,7 @@ } /** - * Constructor which expects the full path to the web.xml for the + * Constructor which expects the full path to the web.xml for the * application. * * @param webXMLFileSpec the full path to the web.xml file @@ -62,7 +62,7 @@ /** - * Constructor which expects the full path to the web.xml for the + * Constructor which expects the full path to the web.xml for the * application and a context path under which to mount it. * * @param webXMLFileSpec the full path to the web.xml file @@ -100,7 +100,7 @@ private void completeInitialization( String contextPath ) { - _context = new ServletUnitContext( contextPath ); + _context = new ServletUnitContext( contextPath, _application ); _application.registerServlet( "*.jsp", _jspServletDescriptor.getClassName(), _jspServletDescriptor.getInitializationParameters( null, null ) ); } Index: ServletUnitContext.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitContext.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- ServletUnitContext.java 17 Mar 2003 01:35:44 -0000 1.5 +++ ServletUnitContext.java 13 Feb 2004 05:45:19 -0000 1.6 @@ -2,7 +2,7 @@ /******************************************************************************************************************** * $Id$ * -* Copyright (c) 2000-2003, Russell Gold +* Copyright (c) 2000-2004, Russell Gold * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated * documentation files (the "Software"), to deal in the Software without restriction, including without limitation @@ -21,24 +21,34 @@ *******************************************************************************************************************/ +import javax.servlet.http.HttpSession; import java.util.*; import java.util.Hashtable; class ServletUnitContext { + private SessionListenerDispatcher _listenerDispatcher; + + ServletUnitContext() { - this( null ); + this( null, new SessionListenerDispatcher() { + public void sendSessionCreated( HttpSession session ) {} + public void sendSessionDestroyed( HttpSession session ) {} + } ); } - ServletUnitContext( String contextPath ) { + + ServletUnitContext( String contextPath, SessionListenerDispatcher dispatcher ) { _contextPath = (contextPath != null ? contextPath : ""); + _listenerDispatcher = dispatcher; } Set getSessionIDs() { return _sessions.keySet(); } - + + /** * Returns the session with the specified ID, if any. **/ @@ -51,8 +61,9 @@ * Creates a new session with a unique ID. **/ ServletUnitHttpSession newSession() { - ServletUnitHttpSession result = new ServletUnitHttpSession(); + ServletUnitHttpSession result = new ServletUnitHttpSession( _listenerDispatcher ); _sessions.put( result.getId(), result ); + _listenerDispatcher.sendSessionCreated( result ); return result; } Index: ServletUnitHttpSession.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitHttpSession.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- ServletUnitHttpSession.java 25 Feb 2003 17:17:29 -0000 1.8 +++ ServletUnitHttpSession.java 13 Feb 2004 05:45:19 -0000 1.9 @@ -2,7 +2,7 @@ /******************************************************************************************************************** * $Id$ * -* Copyright (c) 2000-2003 Russell Gold +* Copyright (c) 2000-2004 Russell Gold * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated * documentation files (the "Software"), to deal in the Software without restriction, including without limitation @@ -35,6 +35,14 @@ final static public String SESSION_COOKIE_NAME = "JSESSION"; + private SessionListenerDispatcher _listenerDispatcher; + + + public ServletUnitHttpSession( SessionListenerDispatcher listenerDispatcher ) { + _listenerDispatcher = listenerDispatcher; + } + + /** * Returns the maximum time interval, in seconds, that the servlet engine will keep this session open * between client requests. You can set the maximum time interval with the setMaxInactiveInterval method. @@ -104,6 +112,7 @@ * Invalidates this session and unbinds any objects bound to it. **/ public void invalidate() { + _listenerDispatcher.sendSessionDestroyed( this ); _invalid = true; _values.clear(); } Index: WebApplication.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/WebApplication.java,v retrieving revision 1.20 retrieving revision 1.21 diff -u -r1.20 -r1.21 --- WebApplication.java 13 Feb 2004 03:17:46 -0000 1.20 +++ WebApplication.java 13 Feb 2004 05:45:19 -0000 1.21 @@ -29,9 +29,7 @@ import java.util.*; import javax.servlet.*; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.*; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -48,7 +46,7 @@ * @author <a href="ba...@we...">Donald Ball</a> * @author <a href="jay...@us...">Jay Dunning</a> **/ -class WebApplication { +class WebApplication implements SessionListenerDispatcher { private final static SecurityConstraint NULL_SECURITY_CONSTRAINT = new NullSecurityConstraint(); @@ -63,6 +61,8 @@ private ArrayList _contextListeners = new ArrayList(); + private ArrayList _sessionListeners = new ArrayList(); + private boolean _useBasicAuthentication; private boolean _useFormAuthentication; @@ -117,25 +117,24 @@ extractSecurityConstraints( document ); extractContextParameters( document ); extractLoginConfiguration( document ); - extractContextListeners( document ); + extractListeners( document ); notifyContextInitialized(); _servletMapping.autoLoadServlets(); } - private void extractContextListeners( Document document ) throws SAXException { - ServletContextListener listener; + private void extractListeners( Document document ) throws SAXException { NodeList nl = document.getElementsByTagName( "listener" ); for (int i = 0; i < nl.getLength(); i++) { String listenerName = getChildNodeValue((Element) nl.item(i), "listener-class").trim(); try { - listener = (ServletContextListener) Class.forName(listenerName).newInstance(); - } catch (Exception x) { - throw new RuntimeException("Unable to load context listener " + listenerName, x); - } catch (Error x) { + Object listener = Class.forName( listenerName ).newInstance(); + + if (listener instanceof ServletContextListener) _contextListeners.add( listener ); + if (listener instanceof HttpSessionListener) _sessionListeners.add( listener ); + } catch (Throwable x) { throw new RuntimeException("Unable to load context listener " + listenerName, x); } - _contextListeners.add(listener); } } @@ -307,8 +306,30 @@ return _contextParameters; } +//---------------------------------------- SessionListenerDispatcher methods ------------------------------------------- + + + public void sendSessionCreated( HttpSession session ) { + HttpSessionEvent event = new HttpSessionEvent( session ); + + for (Iterator i = _sessionListeners.iterator(); i.hasNext();) { + HttpSessionListener listener = (HttpSessionListener) i.next(); + listener.sessionCreated( event ); + } + } + + + public void sendSessionDestroyed( HttpSession session ) { + HttpSessionEvent event = new HttpSessionEvent( session ); + + for (Iterator i = _sessionListeners.iterator(); i.hasNext();) { + HttpSessionListener listener = (HttpSessionListener) i.next(); + listener.sessionDestroyed( event ); + } + } + -//------------------------------------------------ private members --------------------------------------------- +//--------------------------------------------------- private members -------------------------------------------------- private void extractLoginConfiguration( Document document ) throws MalformedURLException, SAXException { |