[Httpunit-commit] CVS: httpunit/src/com/meterware/servletunit ServletUnitServletConfig.java,1.6,1.7
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2003-02-19 19:18:05
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/servletunit
In directory sc8-pr-cvs1:/tmp/cvs-serv2393/src/com/meterware/servletunit
Modified Files:
ServletUnitServletConfig.java WebApplication.java
Log Message:
Share ServletContext, autoload servlets as required
Index: ServletUnitServletConfig.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitServletConfig.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ServletUnitServletConfig.java 19 Jun 2002 13:47:19 -0000 1.6
+++ ServletUnitServletConfig.java 19 Feb 2003 19:17:57 -0000 1.7
@@ -2,7 +2,7 @@
/********************************************************************************************************************
* $Id$
*
-* Copyright (c) 2000-2002, Russell Gold
+* Copyright (c) 2000-2003, 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
@@ -19,7 +19,6 @@
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
-import java.io.File;
import java.util.Enumeration;
import java.util.Hashtable;
@@ -37,7 +36,7 @@
ServletUnitServletConfig( Servlet servlet, WebApplication application, Hashtable initParams ) {
_name = servlet.getClass().getName();
_initParameters = initParams;
- _context = new ServletUnitServletContext( application );
+ _context = application.getServletContext();
}
Index: WebApplication.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/WebApplication.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- WebApplication.java 13 Feb 2003 20:25:56 -0000 1.17
+++ WebApplication.java 19 Feb 2003 19:17:58 -0000 1.18
@@ -30,6 +30,7 @@
import javax.servlet.Servlet;
import javax.servlet.ServletException;
+import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -50,6 +51,35 @@
**/
class WebApplication {
+ private final static SecurityConstraint NULL_SECURITY_CONSTRAINT = new NullSecurityConstraint();
+
+ private final ServletConfiguration SECURITY_CHECK_CONFIGURATION = new ServletConfiguration( SecurityCheckServlet.class.getName() );
+
+ private final ServletMapping SECURITY_CHECK_MAPPING = new ServletMapping( SECURITY_CHECK_CONFIGURATION );
+
+ /** A mapping of resource names to servlet class names. **/
+ private ServletMap _servletMapping = new ServletMap();
+
+ private ArrayList _securityConstraints = new ArrayList();
+
+ private boolean _useBasicAuthentication;
+
+ private boolean _useFormAuthentication;
+
+ private String _authenticationRealm = "";
+
+ private URL _loginURL;
+
+ private URL _errorURL;
+
+ private Hashtable _contextParameters = new Hashtable();
+
+ private File _contextDir = null;
+
+ private String _contextPath = null;
+
+ private ServletUnitServletContext _servletContext;
+
/**
* Constructs a default application spec with no information.
@@ -85,6 +115,7 @@
extractSecurityConstraints( document );
extractContextParameters( document );
extractLoginConfiguration( document );
+ _servletMapping.autoLoadServlets();
}
@@ -101,6 +132,14 @@
}
+ ServletContext getServletContext() {
+ if (_servletContext == null) {
+ _servletContext = new ServletUnitServletContext( this );
+ }
+ return _servletContext;
+ }
+
+
/**
* Registers a servlet class to be run.
**/
@@ -226,34 +265,6 @@
//------------------------------------------------ private members ---------------------------------------------
- private final static SecurityConstraint NULL_SECURITY_CONSTRAINT = new NullSecurityConstraint();
-
- private final ServletConfiguration SECURITY_CHECK_CONFIGURATION = new ServletConfiguration( SecurityCheckServlet.class.getName() );
-
- private final ServletMapping SECURITY_CHECK_MAPPING = new ServletMapping( SECURITY_CHECK_CONFIGURATION );
-
- /** A mapping of resource names to servlet class names. **/
- private ServletMap _servletMapping = new ServletMap();
-
- private ArrayList _securityConstraints = new ArrayList();
-
- private boolean _useBasicAuthentication;
-
- private boolean _useFormAuthentication;
-
- private String _authenticationRealm = "";
-
- private URL _loginURL;
-
- private URL _errorURL;
-
- private Hashtable _contextParameters = new Hashtable();
-
- private File _contextDir = null;
-
- private String _contextPath = null;
-
-
private void extractLoginConfiguration( Document document ) throws MalformedURLException, SAXException {
NodeList nl = document.getElementsByTagName( "login-config" );
if (nl.getLength() == 1) {
@@ -360,9 +371,16 @@
}
//============================================= ServletConfiguration class =============================================
+ final static int DONT_AUTOLOAD = Integer.MIN_VALUE;
+ final static int ANY_LOAD_ORDER = Integer.MAX_VALUE;
class ServletConfiguration {
+ private Servlet _servlet;
+ private String _className;
+ private Hashtable _initParams = new Hashtable();
+ private int _loadOrder = DONT_AUTOLOAD;
+
ServletConfiguration( String className ) {
_className = className;
}
@@ -381,6 +399,15 @@
_initParams.put( getChildNodeValue( (Element) initParams.item( i ), "param-name" ),
getChildNodeValue( (Element) initParams.item( i ), "param-value" ) );
}
+ final NodeList loadOrder = servletElement.getElementsByTagName( "load-on-startup" );
+ for (int i = 0; i < loadOrder.getLength(); i++) {
+ String order = getTextValue( loadOrder.item(i) );
+ try {
+ _loadOrder = Integer.parseInt( order );
+ } catch (NumberFormatException e) {
+ _loadOrder = ANY_LOAD_ORDER;
+ }
+ }
}
@@ -410,9 +437,14 @@
}
- private Servlet _servlet;
- private String _className;
- private Hashtable _initParams = new Hashtable();
+ boolean isLoadOnStartup() {
+ return _loadOrder != DONT_AUTOLOAD;
+ }
+
+
+ public int getLoadOrder() {
+ return _loadOrder;
+ }
}
@@ -674,10 +706,55 @@
}
- private void destroyServlets( Map exactMatches ) {
- for (Iterator iterator = exactMatches.values().iterator(); iterator.hasNext();) {
- ServletMapping servletMapping = (ServletMapping) iterator.next();
- servletMapping.destroyServlet();
+ private void destroyServlets( Map map ) {
+ for (Iterator iterator = map.values().iterator(); iterator.hasNext();) {
+ Object o = iterator.next();
+ if (o instanceof ServletMapping) {
+ ServletMapping servletMapping = (ServletMapping) o;
+ servletMapping.destroyServlet();
+ } else {
+ destroyServlets( (Map) o );
+ }
+ }
+ }
+
+
+ void autoLoadServlets() {
+ ArrayList autoLoadable = new ArrayList();
+ if (_defaultMapping != null && _defaultMapping.getConfiguration().isLoadOnStartup()) autoLoadable.add( _defaultMapping.getConfiguration() );
+ collectAutoLoadableServlets( autoLoadable, _exactMatches );
+ collectAutoLoadableServlets( autoLoadable, _extensions );
+ collectAutoLoadableServlets( autoLoadable, _urlTree );
+ if (autoLoadable.isEmpty()) return;
+
+ Collections.sort( autoLoadable, new Comparator() {
+ public int compare( Object o1, Object o2 ) {
+ ServletConfiguration sc1 = (ServletConfiguration) o1;
+ ServletConfiguration sc2 = (ServletConfiguration) o2;
+ return (sc1.getLoadOrder() <= sc2.getLoadOrder()) ? -1 : +1;
+ }
+ });
+ for (Iterator iterator = autoLoadable.iterator(); iterator.hasNext();) {
+ ServletConfiguration servletConfiguration = (ServletConfiguration) iterator.next();
+ try {
+ servletConfiguration.getServlet();
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new RuntimeException( "Unable to autoload servlet: " + servletConfiguration.getClassName() + ": " + e );
+ }
+ }
+ }
+
+
+ private void collectAutoLoadableServlets( Collection collection, Map map ) {
+ for (Iterator iterator = map.values().iterator(); iterator.hasNext();) {
+ Object o = iterator.next();
+ if (o instanceof ServletMapping) {
+ ServletMapping servletMapping = (ServletMapping) o;
+ if (servletMapping.getConfiguration().isLoadOnStartup()) collection.add( servletMapping.getConfiguration() );
+ } else {
+ collectAutoLoadableServlets( collection, (Map) o );
+ }
}
}
|