tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cos...@locus.apache.org
Subject cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/servlets InvokerServlet.java
Date Sun, 13 Feb 2000 01:16:19 GMT
costin      00/02/12 17:16:19

  Modified:    .        build.xml
               src/j2ee/org/apache/tomcat/deployment
                        WebApplicationReader.java
               src/j2ee/org/apache/tomcat/shell Startup.java
               src/share/org/apache/tomcat/context DefaultCMSetter.java
                        WebXmlReader.java
               src/share/org/apache/tomcat/core Context.java
                        ContextManager.java
               src/share/org/apache/tomcat/request SimpleMapper.java
               src/share/org/apache/tomcat/servlets InvokerServlet.java
  Log:
  Change in SimpleMapper and Context:  the mapper(s) are now in charge of the
  full mapping process, including creating of internal representations.
  
  Context will maintain a list with all declared mappings, but it will no longer
  have internal "byPrefix" or "byExtension" tables - it's up to the mapper to
  use whatever it need ( like tree or sorted arrays ).
  
  Implemented the notifications for servlet  add/remove, context add/remove
  and mapping add/remove - we might be able to support dynamic configuration
  changes ( for the admin tool). ( J2EE does support that by restarting the
  server ).
  
  Dynamic add of mappings also means that JSP or Invoker can declare new
  path->servlet mappings, and optimize future invocations ( by removing one
  step ).
  
  That will allow implementations of security authorization by reusing the
  mappings in SimpleMapper ( in next checkin )
  
  Also:
  - added "throws TomcatException" to various methods that might do that
  
  - removed one variant of addServlet, now only addServlet( ServletWrapper ) is
  used.
  
  Revision  Changes    Path
  1.31      +14 -1     jakarta-tomcat/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/build.xml,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- build.xml	2000/02/10 22:28:21	1.30
  +++ build.xml	2000/02/13 01:16:16	1.31
  @@ -96,8 +96,22 @@
            basedir="${tomcat.home}/classes"
            items="org/apache/jasper"/>
   
  +    <!-- Add Tomcat internal javadoc -->
  +    <mkdir dir="${tomcat.home}/webapps/ROOT/javadoc" />
  +    <javadoc packagenames="org.apache.tomcat.core"
  +             sourcepath="src/share"
  +             destdir="${tomcat.home}/webapps/ROOT/javadoc"
  +             author="true"
  +             version="true"
  +             use="true"
  +             windowtitle="Tomcat internal API"
  +             doctitle="Tomcat internal"
  +             bottom="Copyright &#169; 2000 Apache Software Foundation. All Rights Reserved."
  +    />
  +
       <deltree dir="${tomcat.home}/classes"/>
   
  +
       <!-- create webapp WARS -->
       <jar   jarfile="${tomcat.home}/webapps/examples.war"
              basedir="${tomcat.home}/webapps/examples"
  @@ -113,7 +127,6 @@
              basedir="${tomcat.home}/webapps/test"
              items="." /> 
       <deltree dir="${tomcat.home}/webapps/test"/>
  -
   
       <!-- Change permissions for unix -->
       <chmod perm="+x" src="${tomcat.home}/bin/tomcat.sh"/>
  
  
  
  1.2       +7 -3      jakarta-tomcat/src/j2ee/org/apache/tomcat/deployment/WebApplicationReader.java
  
  Index: WebApplicationReader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/j2ee/org/apache/tomcat/deployment/WebApplicationReader.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WebApplicationReader.java	2000/02/11 00:22:28	1.1
  +++ WebApplicationReader.java	2000/02/13 01:16:16	1.2
  @@ -761,7 +761,7 @@
   	processErrorPages(ctx, webDescriptor.getErrorPageDescriptors());
       }
   
  -    private void processServlets(Context ctx, Enumeration servlets) {
  +    private void processServlets(Context ctx, Enumeration servlets) throws Exception {
           // XXX
           // oh my ... this has suddenly turned rather ugly
           // perhaps the reader should do this normalization work
  @@ -788,7 +788,11 @@
   		    ctx.removeServletByName(name);
   		}
   
  -		ctx.addServlet(name, resourceName, description);
  +		ServletWrapper sw=new ServletWrapper();
  +		sw.setContext( ctx );
  +		sw.setServletName( name );
  +		sw.setServletClass( resourceName );
  +		ctx.addServlet(sw);
   	    } else if (webComponentDescriptor instanceof JspDescriptor) {
   		resourceName =
   		    ((JspDescriptor)webComponentDescriptor).getJspFileName();
  @@ -858,7 +862,7 @@
   			ctx.removeMapping(mapping);
   		    }
   
  -                    ctx.addMapping(name, mapping);
  +                    ctx.addServletMapping( mapping, name);
   		} else {
   // 		    String msg = sm.getString("context.dd.ignoreMapping",
   // 		        mapping);
  
  
  
  1.3       +13 -7     jakarta-tomcat/src/j2ee/org/apache/tomcat/shell/Startup.java
  
  Index: Startup.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/j2ee/org/apache/tomcat/shell/Startup.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Startup.java	2000/02/12 03:38:48	1.2
  +++ Startup.java	2000/02/13 01:16:17	1.3
  @@ -143,13 +143,19 @@
   		context.setSessionTimeOut(
   		    contextConfig.getDefaultSessionTimeOut());
   
  -		// this is the semantic of disable invoker.
  -		if( ! contextConfig.isInvokerEnabled() ) {
  -		    context.addServlet(org.apache.tomcat.core.Constants.INVOKER_SERVLET_NAME,
  -				       "org.apache.tomcat.core.NoInvokerServlet", null);
  -		    context.addMapping(org.apache.tomcat.core.Constants.INVOKER_SERVLET_NAME,
  -				       "/servlet");
  -		}
  +		// You can disable the invoker in web.xml
  +		// XXX is anyone using this ? J2EE has it's own disable
  +		// 		// this is the semantic of disable invoker.
  +		// 		if( ! contextConfig.isInvokerEnabled() ) {
  +		// 		    try {
  +		// 			context.addServlet(org.apache.tomcat.core.Constants.INVOKER_SERVLET_NAME,
  +		// 					   "org.apache.tomcat.core.NoInvokerServlet", null);
  +		// 			context.addMapping(org.apache.tomcat.core.Constants.INVOKER_SERVLET_NAME,
  +		// 					   "/servlet");
  +		// 		    } catch( TomcatException ex ) {
  +		// 			ex.printStackTrace();
  +		// 		    }
  +		// 		}
   
   		context.setIsWARExpanded(contextConfig.isWARExpanded());
   		context.setIsWARValidated(contextConfig.isWARValidated());
  
  
  
  1.11      +2 -2      jakarta-tomcat/src/share/org/apache/tomcat/context/DefaultCMSetter.java
  
  Index: DefaultCMSetter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/context/DefaultCMSetter.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- DefaultCMSetter.java	2000/02/12 03:38:49	1.10
  +++ DefaultCMSetter.java	2000/02/13 01:16:17	1.11
  @@ -87,8 +87,8 @@
   	    cm.addServerConnector(  new org.apache.tomcat.service.http.HttpAdapter() );
   	}
   	
  - 	Enumeration riE=cm.getRequestInterceptors();
  -	if( ! riE.hasMoreElements() ) {
  + 	RequestInterceptor rI[]=cm.getRequestInterceptors();
  +	if( rI.length ==0  ) {
   	    // nothing set up by starter, add default ones
   	    if(cm.getDebug()>0) cm.log("Setting default interceptors ");
   
  
  
  
  1.7       +3 -3      jakarta-tomcat/src/share/org/apache/tomcat/context/WebXmlReader.java
  
  Index: WebXmlReader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/context/WebXmlReader.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- WebXmlReader.java	2000/02/12 03:38:49	1.6
  +++ WebXmlReader.java	2000/02/13 01:16:17	1.7
  @@ -55,9 +55,9 @@
   	    xh.addRule("web-app/icon/small-icon", xh.methodSetter("setIcon", 0) ); 
   	    xh.addRule("web-app/distributable", xh.methodSetter("setDistributable", 0) );
   
  -	    xh.addRule("web-app/servlet-mapping", xh.methodSetter("addMapping", 2) ); 
  -	    xh.addRule("web-app/servlet-mapping/servlet-name", xh.methodParam(0) ); 
  -	    xh.addRule("web-app/servlet-mapping/url-pattern", xh.methodParam(1) );
  +	    xh.addRule("web-app/servlet-mapping", xh.methodSetter("addServletMapping", 2) ); 
  +	    xh.addRule("web-app/servlet-mapping/servlet-name", xh.methodParam(1) ); 
  +	    xh.addRule("web-app/servlet-mapping/url-pattern", xh.methodParam(0) );
   	    
   	    xh.addRule("web-app/taglib", xh.methodSetter("addTaglib", 2) ); 
   	    xh.addRule("web-app/taglib/taglib-uri", xh.methodParam(0) ); 
  
  
  
  1.51      +74 -203   jakarta-tomcat/src/share/org/apache/tomcat/core/Context.java
  
  Index: Context.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/Context.java,v
  retrieving revision 1.50
  retrieving revision 1.51
  diff -u -r1.50 -r1.51
  --- Context.java	2000/02/12 03:38:50	1.50
  +++ Context.java	2000/02/13 01:16:18	1.51
  @@ -68,11 +68,6 @@
   import javax.servlet.http.*;
   import javax.servlet.*;
   
  -//
  -// WARNING: Some of the APIs in this class are used by J2EE. 
  -// Please talk to harishp@eng.sun.com before making any changes.
  -//
  -
   /**
    * Context represent a Web Application as specified by Servlet Specs.
    * The implementation is a repository for all the properties
  @@ -158,12 +153,6 @@
       // Maps specified in web.xml ( String url -> ServletWrapper  )
       private Hashtable mappings = new Hashtable();
       
  -    // XXX deprecated, will be removed.
  -    //Maps specified in web.xml ( String->ServletWrapper )
  -    private Hashtable prefixMappedServlets = new Hashtable();
  -    private Hashtable extensionMappedServlets = new Hashtable();
  -    private Hashtable pathMappedServlets = new Hashtable();
  -
       // Authentication properties
       String authMethod;
       String realmName;
  @@ -282,34 +271,6 @@
   	return this.rsProvider;
       }
   
  -    /** Will return an URL that can be used to read the resource pointed by
  -     * req, using the context base and the mapped path
  -     */
  -    public URL getResourceURL(Request req)
  -	throws MalformedURLException
  -    {
  -	String mappedPath = req.getMappedPath();
  -	if( debug>0 ) log( "getResourceURL: " + mappedPath + " " + req.getPathInfo());
  -	if( mappedPath == null ) {
  -	    mappedPath=req.getPathInfo();
  -	}
  -	if(mappedPath == null )
  -	    mappedPath=req.getLookupPath();
  -	
  -        URL docBase = getDocumentBase();
  -
  -	// again, the special case of serving from wars
  -	// XXX Need an architecture to deal with other cases, like database-stored files,
  -	// etc.
  -	if (docBase.getProtocol().equalsIgnoreCase("war")) {
  -	    return WARUtil.createURL( this, mappedPath );
  -	}
  -	URL url=new URL(docBase.getProtocol(), docBase.getHost(),
  -		       docBase.getPort(), docBase.getFile() + mappedPath);
  -	if( debug>0) log( "getResourceURL=" + url + " request=" + req );
  -	return url;
  -    }
  -
       public RequestDispatcher getRequestDispatcher(String path) {
   	if ( path == null  || ! path.startsWith("/")) {
   	    return null; // spec say "return null if we can't return a dispather
  @@ -337,31 +298,39 @@
   
       /** Implements getResource() - use a sub-request to let interceptors do the job.
        */
  -    public URL getResource(String rpath)	throws MalformedURLException {
  +    public URL getResource(String rpath) throws MalformedURLException {
           URL url = null;
   
   	if ("".equals(rpath)) 
   	    return getDocumentBase();
   	
  -	// deal with exceptional cases
  -        if (rpath == null) 
  -            throw new MalformedURLException(sm.getString("scfacade.getresource.npe"));
  -        else if ( ! rpath.startsWith("/")) {
  -	    // XXX fix - it shouldn't be a special case, MapperInterceptor
  -	    // should deal with this ( workaround for bug in MapperInterceptor)
  -	    //	    System.out.println("rpath=" + rpath + " " + path);
  -	    if( "/".equals(path) ) // default context
  -		rpath="/" + rpath;
  -	    else
  -		throw new MalformedURLException(sm.getString("scfacade.getresource.iae", rpath));
  +        if (rpath == null)
  +	    return null;
  +
  +	if ( ! rpath.startsWith("/")) {
  +	    rpath="/" + rpath;
   	}
   
   	// Create a Sub-Request, do the request processing stage
   	// that will take care of aliasing and set the paths
   	Request lr=contextM.createRequest( this, rpath );
   	getContextManager().processRequest(lr);
  +
  +	String mappedPath = lr.getMappedPath();
   
  -	return getResourceURL( lr );
  +	// XXX workaround for mapper bugs
  +	if( mappedPath == null ) {
  +	    mappedPath=lr.getPathInfo();
  +	}
  +	if(mappedPath == null )
  +	    mappedPath=lr.getLookupPath();
  +	
  +        URL docBase = getDocumentBase();
  +
  +	url=new URL(docBase.getProtocol(), docBase.getHost(),
  +		       docBase.getPort(), docBase.getFile() + mappedPath);
  +	if( debug>0) log( "getResourceURL=" + url + " request=" + lr );
  +	return url;
       }
   
       
  @@ -437,18 +406,20 @@
        * from the webapp directory in the docbase.
        *
        * <p>This method may only be called once and must be called
  -     * before any requests are handled by this context.
  +     * before any requests are handled by this context and after setContextManager()
  +     * is called.
        */
       public synchronized void init() throws TomcatException {
  +	// XXX Context is a mostly a "data" object, it contain all
  +	// context properties. We should use ContextManager.initContext() instead.
   	if (this.initialized) {
   	    String msg = sm.getString("context.init.alreadyinit");
   	    throw new IllegalStateException(msg);
   	}
   	this.initialized = true;
  -
  -	for( int i=0; i< contextInterceptors.size(); i++ ) {
  -	    ((ContextInterceptor)contextInterceptors.elementAt(i)).contextInit( this );
  -	}
  +	
  +	// All interceptor logic is in ContextManager.
  +	contextM.initContext( this );
       }
   
       public void addContextInterceptor( ContextInterceptor ci) {
  @@ -776,22 +747,28 @@
       // XXX use external iterator 
       /** Remove all servlets with a specific class name
        */
  -    void removeServletByClassName(String className) {
  +    void removeServletByClassName(String className)
  +	throws TomcatException
  +    {
   	Enumeration enum = servlets.keys();
   	while (enum.hasMoreElements()) {
   	    String key = (String)enum.nextElement();
   	    ServletWrapper sw = (ServletWrapper)servlets.get(key);
               if (className.equals(sw.getServletClass()))
  -		removeServlet( sw );
  +		servlets.remove(sw.getServletName());
  +		contextM.removeServlet( this, sw );
   	}
       }
   
       /** Remove the servlet with a specific name
        */
  -    public void removeServletByName(String servletName) {
  +    public void removeServletByName(String servletName)
  +	throws TomcatException
  +    {
   	ServletWrapper wrapper=(ServletWrapper)servlets.get(servletName);
   	if( wrapper != null ) {
  -	    removeServlet( wrapper );
  +	    servlets.remove( servletName );
  +	    contextM.removeServlet( this, wrapper );
   	}
       }
   
  @@ -817,14 +794,18 @@
        *  XXX Find out if we really need that - it can be avoided!
        * @deprecated Use removeServlet and findServletByPath or ByName
        */
  -    public void removeJSP(String path) {
  +    public void removeJSP(String path)
  +	throws TomcatException
  +    {
   	Enumeration enum = servlets.keys();
   	while (enum.hasMoreElements()) {
   	    String key = (String)enum.nextElement();
   	    ServletWrapper sw = (ServletWrapper)servlets.get(key);
   	    //	    if( (sw instanceof JspWrapper ) &&
  -	    if(path.equals( (sw).getPath()))
  -	        removeServlet( sw );
  +	    if(path.equals( (sw).getPath())) {
  +		servlets.remove( sw.getServletName() );
  +		contextM.removeServlet( this, sw );
  +	    }
   	}
       }
   
  @@ -851,15 +832,34 @@
        *    default servlet
        *
        */
  -    public void addServletMapping(String servletName, String path) {
  +    public void addServletMapping(String path, String servletName)
  +	throws TomcatException
  +    {
           ServletWrapper sw = (ServletWrapper)servlets.get(servletName);
   
   	if (sw == null) {
  -	    log("Servlet not registered " + servletName );
  -	    return;
  +	    //	    System.out.println("Servlet not registered " + servletName );
  +	    // Workaround for frequent "bug" in web.xmls
  +	    // Declare a mapping for a JSP or servlet that is not
  +	    // declared as servlet. 
  +	    
  +	    sw = new ServletWrapper(this);
  +
  +	    sw.setServletName(servletName);
  +	    if ( servletName.startsWith("/")) {
  +	        sw.setPath(servletName);
  +	    } else {
  +		sw.setServletClass(servletName);
  +	    }
  +	    addServlet( sw );
  +
   	    // or throw an exception !
   	}
  +	if( "/".equals(path) )
  +	    defaultServlet = sw;
  +
   	mappings.put( path, sw );
  +	contextM.addMapping( this, path, sw );
       }
   
       public Enumeration getServletMappings() {
  @@ -870,7 +870,7 @@
   	return (ServletWrapper)mappings.get(path);
       }
   
  -    public void removeMappingNew( String path ) {
  +    public void removeMapping( String path ) {
   	log( "Removing " + path + " -> " + mappings.get(path) );
   	mappings.remove( path );
       }
  @@ -890,55 +890,6 @@
       
       // -------------------- deprecated code
       
  -    // XXX only one mapping, the mapper should do it's own optimizations
  -    /**
  -     * Maps a named servlet to a particular path or extension.
  -     * If the named servlet is unregistered, it will be added
  -     * and subsequently mapped.
  -     *
  -     * Note that the order of resolution to handle a request is:
  -     *
  -     *    exact mapped servlet (eg /catalog)
  -     *    prefix mapped servlets (eg /foo/bar/*)
  -     *    extension mapped servlets (eg *jsp)
  -     *    default servlet
  -     *
  -     */
  -    public void addMapping(String servletName, String path) {
  -        ServletWrapper sw = (ServletWrapper)servlets.get(servletName);
  -
  -	if (sw == null) {
  -	    System.out.println("Servlet not registered " + servletName );
  -	    // XXX
  -	    // this might be a bit aggressive
  -
  -	    if ( servletName.startsWith("/")) {
  -	        addJSP(servletName, servletName, null);
  -	    } else {
  -	        addServlet(servletName, null, servletName);
  -	    }
  -
  -	    sw = (ServletWrapper)servlets.get(servletName);
  -	}
  -
  -	path = path.trim();
  -
  -	if (sw != null &&
  -	    (path.length() > 0)) {
  -	    if (path.startsWith("/") &&
  -                path.endsWith("/*")){
  -	        prefixMappedServlets.put(path, sw);
  -		//		System.out.println("Map " + path + " -> " + sw );
  -	    } else if (path.startsWith("*.")) {
  -	        extensionMappedServlets.put(path, sw);
  -	    } else if (! path.equals("/")) {
  -	        pathMappedServlets.put(path, sw);
  -	    } else {
  -	        defaultServlet = sw;
  -	    }
  -	}
  -    }
  -
       public ServletWrapper getDefaultServlet() {
   	if( defaultServlet==null)
   	    defaultServlet=getServletByName(Constants.DEFAULT_SERVLET_NAME );
  @@ -948,35 +899,11 @@
   	return defaultServlet;
       }
       
  -    public Hashtable getPathMap() {
  -	return pathMappedServlets;
  -    }
  -
  -    public Hashtable getPrefixMap() {
  -	return prefixMappedServlets;
  -    }
  -
  -    public Hashtable getExtensionMap() {
  -	return extensionMappedServlets;
  -    }
  -    
       public boolean containsMapping(String mapping) {
           mapping = mapping.trim();
  -
  -        return (prefixMappedServlets.containsKey(mapping) ||
  -	    extensionMappedServlets.containsKey(mapping) ||
  -	    pathMappedServlets.containsKey(mapping));
  +	return mappings.containsKey( mapping );
       }
   
  -    public void removeMapping(String mapping) {
  -        mapping = mapping.trim();
  -
  -	prefixMappedServlets.remove(mapping);
  -	extensionMappedServlets.remove(mapping);
  -	pathMappedServlets.remove(mapping);
  -    }
  -
  -    // XXX replace with getServlet()
       public ServletWrapper getServletByName(String servletName) {
   	return (ServletWrapper)servlets.get(servletName);
       }
  @@ -987,77 +914,21 @@
        * and instantiated using the given class name.
        *
        * Called to add a new servlet from web.xml
  -     * @deprecated use addServlet(ServletWrapper)
        */
  -    public void addServlet(String name, String className,
  -			   String description) {
  -	// assert className!=null
  -
  -        // check for duplicates
  -        if (servlets.get(name) != null) {
  -            removeServletByClassName(name); // XXX XXX why?
  -            removeServletByName(name);
  -        }
  -
  -        ServletWrapper wrapper = new ServletWrapper(this);
  -	wrapper.setServletName(name);
  -	wrapper.setServletDescription(description);
  -	wrapper.setServletClass(className);
  -
  -	servlets.put(name, wrapper);
  -    }
  -
  -    public void addServlet(ServletWrapper wrapper) {
  +    public void addServlet(ServletWrapper wrapper)
  +    	throws TomcatException
  +    {
   	String name=wrapper.getServletName();
   	//	System.out.println("Adding servlet " + name  + " " + wrapper);
  +
           // check for duplicates
           if (servlets.get(name) != null) {
  -            removeServletByClassName(name); // XXX XXX why?
  +	    log("Removing duplicate servlet " + name  + " " + wrapper);
               removeServletByName(name);
           }
   	servlets.put(name, wrapper);
       }
   
  -    private void removeServlet(ServletWrapper sw) {
  -	if (prefixMappedServlets.contains(sw)) {
  -	    Enumeration enum = prefixMappedServlets.keys();
  -	    
  -	    while (enum.hasMoreElements()) {
  -		String key = (String)enum.nextElement();
  -		
  -		if (prefixMappedServlets.get(key).equals(sw)) {
  -		    prefixMappedServlets.remove(key);
  -		}
  -	    }
  -	}
  -	
  -	if (extensionMappedServlets.contains(sw)) {
  -	    Enumeration enum = extensionMappedServlets.keys();
  -	    
  -	    while (enum.hasMoreElements()) {
  -		String key = (String)enum.nextElement();
  -
  -		if (extensionMappedServlets.get(key).equals(sw)) {
  -		    extensionMappedServlets.remove(key);
  -		}
  -	    }
  -	}
  -	
  -	if (pathMappedServlets.contains(sw)) {
  -	    Enumeration enum = pathMappedServlets.keys();
  -	    
  -	    while (enum.hasMoreElements()) {
  -		String key = (String)enum.nextElement();
  -
  -		if (pathMappedServlets.get(key).equals(sw)) {
  -		    pathMappedServlets.remove(key);
  -		}
  -	    }
  -	}
  -	
  -	servlets.remove(sw.getServletName());
  -    }
  -    
       public Enumeration getServletNames() {
   	return servlets.keys();
       }
  
  
  
  1.37      +112 -4    jakarta-tomcat/src/share/org/apache/tomcat/core/ContextManager.java
  
  Index: ContextManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/ContextManager.java,v
  retrieving revision 1.36
  retrieving revision 1.37
  diff -u -r1.36 -r1.37
  --- ContextManager.java	2000/02/12 03:38:50	1.36
  +++ ContextManager.java	2000/02/13 01:16:18	1.37
  @@ -90,7 +90,12 @@
       int debug=0;
       
       private Vector requestInterceptors = new Vector();
  +    private Vector contextInterceptors = new Vector();
       
  +    // cache - faster access
  +    ContextInterceptor cInterceptors[];
  +    RequestInterceptor rInterceptors[];
  +    
       /**
        * The set of Contexts associated with this ContextManager,
        * keyed by context paths.
  @@ -135,6 +140,9 @@
           return contexts.keys();
       }
   
  +    /** Init() is called after the context manager is set up
  +     *  and configured
  +     */
       public void init()  throws TomcatException {
   	long time=System.currentTimeMillis();
   
  @@ -158,6 +166,23 @@
   	org.apache.tomcat.task.ApacheConfig apacheConfig=new  org.apache.tomcat.task.ApacheConfig();
   	apacheConfig.execute( this );     
       }
  +
  +    /** Initialize a context. This method is will call all "global"
  +     * interceptors ( set on CM level ) and then context specific
  +     * interceptors.
  +     */
  +    public void initContext( Context ctx ) throws TomcatException {
  +	//
  +	ContextInterceptor cI[]=getContextInterceptors();
  +	for( int i=0; i< cI.length; i++ ) {
  +	    cI[i].contextInit( ctx );
  +	}
  +	
  +	cI=ctx.getContextInterceptors();
  +	for( int i=0; i< cI.length; i++ ) {
  +	    cI[i].contextInit( ctx );
  +	}
  +    }
       
       /** Will start the connectors and begin serving requests
        */
  @@ -182,7 +207,7 @@
       }
   
       public void destroy() throws Exception {//TomcatException {
  -
  +	
   	Enumeration enum = getContextNames();
   	while (enum.hasMoreElements()) {
   	    Context context =
  @@ -223,6 +248,12 @@
   
   	// Set defaults for the context
   	new DefaultCMSetter().addContext( this, ctx );
  +
  +	ContextInterceptor cI[]=getContextInterceptors();
  +	for( int i=0; i< cI.length; i++ ) {
  +	    cI[i].addContext( this, ctx );
  +	}
  +
   	
   	if(debug>0) log(" adding " + ctx + " " + ctx.getPath() + " " +  ctx.getDocBase());
   
  @@ -241,13 +272,55 @@
   
   	Context context = (Context)contexts.get(name);
   
  +	ContextInterceptor cI[]=getContextInterceptors();
  +	for( int i=0; i< cI.length; i++ ) {
  +	    cI[i].removeContext( this, context );
  +	}
  +
   	if(context != null) {
   	    context.shutdown();
   	    contexts.remove(name);
   	}
       }
   
  +    public void removeServlet( Context ctx, ServletWrapper sw )
  +	throws TomcatException
  +    {
  +	ContextInterceptor cI[]=getContextInterceptors();
  +	for( int i=0; i< cI.length; i++ ) {
  +	    cI[i].removeServlet( ctx, sw );
  +	}
  +
  +    }
   
  +    public void addServlet( Context ctx, ServletWrapper sw )
  +	throws TomcatException
  +    {
  +	ContextInterceptor cI[]=getContextInterceptors();
  +	for( int i=0; i< cI.length; i++ ) {
  +	    cI[i].addServlet( ctx, sw );
  +	}
  +    }
  +
  +    public void addMapping( Context ctx ,String path, ServletWrapper sw )
  +    	throws TomcatException
  +    {
  +	ContextInterceptor cI[]=getContextInterceptors();
  +	for( int i=0; i< cI.length; i++ ) {
  +	    cI[i].addMapping( ctx, path, sw );
  +	}
  +    }
  +
  +    public void removeMapping( Context ctx, String path )
  +    	throws TomcatException
  +    {
  +	ContextInterceptor cI[]=getContextInterceptors();
  +	for( int i=0; i< cI.length; i++ ) {
  +	    cI[i].removeMapping( ctx, path );
  +	}
  +    }
  +
  +
       // -------------------- Connectors and Interceptors --------------------
   
       /**
  @@ -268,15 +341,50 @@
       public void addRequestInterceptor( RequestInterceptor ri ) {
   	if(debug>0) log(" adding request intereptor " + ri.getClass().getName());
   	requestInterceptors.addElement( ri );
  +	if( ri instanceof ContextInterceptor )
  +	    contextInterceptors.addElement( ri );
   	// XXX XXX use getMethods() to find what notifications are needed by interceptor
   	// ( instead of calling all interceptors )
   	// No API change - can be done later.
       }
   
  -    public Enumeration getRequestInterceptors() {
  -	return requestInterceptors.elements();
  +    /** Return the context interceptors as an array.
  +	For performance reasons we use an array instead of
  +	returning the vector - the interceptors will not change at
  +	runtime and array access is faster and easier than vector
  +	access
  +    */
  +    public RequestInterceptor[] getRequestInterceptors() {
  +	if( rInterceptors == null || rInterceptors.length != requestInterceptors.size()) {
  +	    rInterceptors=new RequestInterceptor[requestInterceptors.size()];
  +	    for( int i=0; i<rInterceptors.length; i++ ) {
  +		rInterceptors[i]=(RequestInterceptor)requestInterceptors.elementAt(i);
  +	    }
  +	}
  +	return rInterceptors;
       }
  -    
  +
  +    public void addContextInterceptor( ContextInterceptor ci) {
  +	contextInterceptors.addElement( ci );
  +    }
  +
  +
  +    /** Return the context interceptors as an array.
  +	For performance reasons we use an array instead of
  +	returning the vector - the interceptors will not change at
  +	runtime and array access is faster and easier than vector
  +	access
  +    */
  +    public ContextInterceptor[] getContextInterceptors() {
  +	if( cInterceptors == null || cInterceptors.length != contextInterceptors.size()) {
  +	    cInterceptors=new ContextInterceptor[contextInterceptors.size()];
  +	    for( int i=0; i<cInterceptors.length; i++ ) {
  +		cInterceptors[i]=(ContextInterceptor)contextInterceptors.elementAt(i);
  +	    }
  +	}
  +	return cInterceptors;
  +    }
  +
       // -------------------- Defaults for all contexts --------------------
       /** The root directory of tomcat
        */
  
  
  
  1.9       +202 -21   jakarta-tomcat/src/share/org/apache/tomcat/request/SimpleMapper.java
  
  Index: SimpleMapper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/request/SimpleMapper.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- SimpleMapper.java	2000/02/10 22:28:23	1.8
  +++ SimpleMapper.java	2000/02/13 01:16:18	1.9
  @@ -63,7 +63,7 @@
   import org.apache.tomcat.core.*;
   import org.apache.tomcat.core.Constants;
   import org.apache.tomcat.util.*;
  -import java.util.Hashtable;
  +import java.util.*;
   
   /** Parse request URI and find ContextPath, ServletPath, PathInfo and QueryString
    *  Use a simple alghoritm - no optimizations or tricks.
  @@ -72,7 +72,7 @@
    *  For "production" environment you should use either an optimized version
    *  or a real web server parser.
    */
  -public class SimpleMapper extends  BaseInterceptor implements RequestInterceptor {
  +public class SimpleMapper extends  BaseInterceptor  {
       int debug=0;
       ContextManager cm;
       
  @@ -83,14 +83,33 @@
   
       public void setContextManager( ContextManager cm ) {
   	this.cm=cm;
  +	// Add all context that are set in CM
  +	Enumeration enum=cm.getContextNames();
  +	while( enum.hasMoreElements() ) {
  +	    String name=(String) enum.nextElement();
  +	    try {
  +		Context ctx=cm.getContext( name );
  +		if(debug>0) ctx.log("Adding existing context " + name );
  +		addContext( cm, ctx );
  +	    } catch (TomcatException ex ) {
  +		ex.printStackTrace();
  +	    }
  +	}
       }
   
       public void setDebug( int level ) {
   	debug=level;
       }
   
  +    public void setDebug( String level ) {
  +	debug=new Integer( level ).intValue();
  +    }
  +
       void log( String msg ) {
  -	System.out.println("SimpleMapper: " + msg );
  +	if( cm==null) 
  +	    System.out.println("SimpleMapper: " + msg );
  +	else
  +	    cm.getContext("").log( msg );
       }
   
       /** First step of request porcessing is finding the Context.
  @@ -138,20 +157,23 @@
   	Context context=req.getContext();
   	String path=req.getLookupPath();
           ServletWrapper wrapper = null;
  +
  +	String ctxP=context.getPath();
  +	Mappings m=(Mappings)contextPaths.get(ctxP);
   
  -	if(debug>0) log( "Mapping: " + req );
  +	if(debug>0) context.log( "Mapping: " + req );
   	//	/*XXX*/ try {throw new Exception(); } catch(Exception ex) {ex.printStackTrace();}
   
   	// try an exact match
  -        wrapper = getPathMatch(context, path, req);
  +        wrapper = getPathMatch(m, context, path, req);
   
   	// try a prefix match
   	if( wrapper == null ) 
  -	    wrapper = getPrefixMatch(context, path, req);
  +	    wrapper = getPrefixMatch(m, context, path, req);
   
   	// try an extension match
   	if (wrapper == null) 
  -	    wrapper = getExtensionMatch(context, path, req);
  +	    wrapper = getExtensionMatch(m, context, path, req);
   
   	// set default wrapper, return
   	if (wrapper == null) {
  @@ -159,31 +181,186 @@
   	    req.setWrapper( wrapper );
   	    req.setServletPath( "" );
   	    req.setPathInfo( path);
  -	    if(debug>0) log("Default mapper " + "\n    " + req);
  +	    if(debug>0) context.log("Default mapper " + "\n    " + req);
   	    return OK;
   	} 
   
   	req.setWrapper( wrapper );
   
  -	if(debug>0) log("Found wrapper using getMapPath " + "\n    " + req);
  +	if(debug>0) context.log("Found wrapper using getMapPath " + "\n    " + req);
   
   	return OK;
       }
  +
  +
  +    // -------------------- Internal representation of mappings --------------------
  +    /* Implementation:
  +       We will create an internal representation of mappings ( context path and internal
mappings
  +       and security mappings). Advanced ( optimized ) mappers will sort the list and will
do
  +       efficient char[] matching ( instead of creating a lot of String garbage ).
  +
  +    */
  +
  +    // String prefix -> Mappings context maps
  +    Hashtable contextPaths=new Hashtable();
  +    
  +    class Mappings {
  +	Context ctx;
  +	Hashtable prefixMappedServlets;
  +	Hashtable extensionMappedServlets;
  +	Hashtable pathMappedServlets;
  +    }
  +
  +
  +    /** Called when a context is added to a CM
  +     */
  +    public void addContext( ContextManager cm, Context ctx )
  +	throws TomcatException
  +    {
  +	Mappings m=new Mappings();
  +	m.ctx=ctx;
  +	m.prefixMappedServlets=new Hashtable();
  +	m.extensionMappedServlets=new Hashtable();
  +	m.pathMappedServlets=new Hashtable();
  +	contextPaths.put( ctx.getPath(), m );
  +
  +	if(debug>0) ctx.log("New context added to maps. ");
  +	// add all existing mappings
  +	Enumeration enum=ctx.getServletMappings();
  +	while( enum.hasMoreElements() ) {
  +	    String path=(String) enum.nextElement();
  +	    ServletWrapper sw=ctx.getServletMapping( path );
  +	    if(debug>0) ctx.log("Adding existing " + path );
  +	    addMapping( ctx, path, sw );
  +	}
  +
  +    }
  +
  +    /** Called when a context is removed from a CM
  +     */
  +    public void removeContext( ContextManager cm, Context ctx ) throws TomcatException
  +    {
  +	String ctxP=ctx.getPath();
  +	Mappings m=(Mappings)contextPaths.get(ctxP);
  +
  +	if(debug>0) ctx.log( "Removed from maps ");
  +	contextPaths.remove( ctxP );
  +	// m will be GC ( we may want to set all to null and clean the
  +	// Hashtable to help a bit)
  +    }
  +
  +
  +    /**
  +     * Maps a named servlet to a particular path or extension.
  +     * If the named servlet is unregistered, it will be added
  +     * and subsequently mapped.
  +     *
  +     * Note that the order of resolution to handle a request is:
  +     *
  +     *    exact mapped servlet (eg /catalog)
  +     *    prefix mapped servlets (eg /foo/bar/*)
  +     *    extension mapped servlets (eg *jsp)
  +     *    default servlet
  +     *
  +     */
  +    public void addMapping( Context ctx, String path, ServletWrapper sw)
  +	throws TomcatException
  +    {
  +	String ctxP=ctx.getPath();
  +	Mappings m=(Mappings)contextPaths.get(ctxP);
  +	if(debug>0) ctx.log( "Add mapping " + path + " " + sw + " " + m );
  +	
  +	path = path.trim();
  +
  +	if ((path.length() == 0))
  +	    return;
  +	if (path.startsWith("/") &&
  +	    path.endsWith("/*")){
  +	    m.prefixMappedServlets.put(path, sw);
  +	    //	    System.out.println("Map " + path + " -> " + sw );
  +	} else if (path.startsWith("*.")) {
  +	    m.extensionMappedServlets.put(path, sw);
  +	} else if (! path.equals("/")) {
  +	    m.pathMappedServlets.put(path, sw);
  +	} 
  +    }
   
  -    public int beforeBody( Request request, Response response) {
  -	return 0;
  +    /** Notify when a mapping is deleted  from  a context
  +     */
  +    public void removeMapping( Context ctx, String mapping )
  +	throws TomcatException
  +    {
  +	String ctxP=ctx.getPath();
  +	Mappings m=(Mappings)contextPaths.get(ctxP);
  +	
  +	if(debug>0) ctx.log( "Remove mapping " + mapping );
  +	
  +        mapping = mapping.trim();
  +	
  +	m.prefixMappedServlets.remove(mapping);
  +	m.extensionMappedServlets.remove(mapping);
  +	m.pathMappedServlets.remove(mapping);
       }
   
  +    
  +    public void removeServlet(Context ctx, ServletWrapper sw) {
  +
  +	// find the mappings for ctx
  +	if( debug > 0 ) ctx.log( "Remove servlet " + sw );
  +	String path=ctx.getPath();
  +	Mappings m=(Mappings)contextPaths.get(path);
  +	
  +	if (m.prefixMappedServlets.contains(sw)) {
  +	    Enumeration enum = m.prefixMappedServlets.keys();
  +	    
  +	    while (enum.hasMoreElements()) {
  +		String key = (String)enum.nextElement();
  +		
  +		if (m.prefixMappedServlets.get(key).equals(sw)) {
  +		    m.prefixMappedServlets.remove(key);
  +		}
  +	    }
  +	}
  +	
  +	if (m.extensionMappedServlets.contains(sw)) {
  +	    Enumeration enum = m.extensionMappedServlets.keys();
  +	    
  +	    while (enum.hasMoreElements()) {
  +		String key = (String)enum.nextElement();
  +
  +		if (m.extensionMappedServlets.get(key).equals(sw)) {
  +		    m.extensionMappedServlets.remove(key);
  +		}
  +	    }
  +	}
  +	
  +	if (m.pathMappedServlets.contains(sw)) {
  +	    Enumeration enum = m.pathMappedServlets.keys();
  +	    
  +	    while (enum.hasMoreElements()) {
  +		String key = (String)enum.nextElement();
  +
  +		if (m.pathMappedServlets.get(key).equals(sw)) {
  +		    m.pathMappedServlets.remove(key);
  +		}
  +	    }
  +	}
  +	
  +    }
  +    
  +
  +
  +    // -------------------- Implementation --------------------
       /** Get an exact match ( /catalog ) - rule 1 in 10.1
        */
  -    private ServletWrapper getPathMatch(Context context, String path, Request req) {
  +    private ServletWrapper getPathMatch(Mappings m, Context context, String path, Request
req) {
           ServletWrapper wrapper = null;
  -	wrapper = (ServletWrapper)context.getPathMap().get(path);
  +	wrapper = (ServletWrapper)m.pathMappedServlets.get(path);
   
   	if (wrapper != null) {
   	    req.setServletPath( path );
   	    // No path info - it's an exact match
  -	    if(debug>1) log("path match " + path );
  +	    if(debug>1) context.log("path match " + path );
   	}
           return wrapper;
       }
  @@ -191,7 +368,7 @@
   
       /** Match a prefix rule - /foo/bar/index.html/abc
        */
  -    private ServletWrapper getPrefixMatch(Context context, String path, Request req) {
  +    private ServletWrapper getPrefixMatch(Mappings m, Context context, String path, Request
req) {
   	ServletWrapper wrapper = null;
           String s = path;
   
  @@ -202,7 +379,12 @@
   	while (s.length() > 0) {
   	    // XXX we can remove /* in prefix map when we add it, so no need
   	    // for another string creation
  -	    wrapper = (ServletWrapper)context.getPrefixMap().get(s + "/*" );
  +	    if(debug>2) context.log( "Prefix: " + s  );
  +	    wrapper = (ServletWrapper)m.prefixMappedServlets.get(s + "/*" );
  +	    // 	    Enumeration en=m.prefixMappedServlets.keys();
  +	    // 	    while( en.hasMoreElements() ) {
  +	    // 		System.out.println("XXX: " + en.nextElement());
  +	    // 	    }
   	    
   	    if (wrapper == null)
   		s=removeLast( s );
  @@ -217,7 +399,7 @@
   	    String pathI = path.substring(s.length(), path.length());
   	    if( ! "".equals(pathI) ) 
   		req.setPathInfo(pathI);
  -	    if(debug>0) log("prefix match " + path );
  +	    if(debug>0) context.log("prefix match " + path );
   	}
   	return wrapper;
       }
  @@ -225,14 +407,13 @@
       // It looks like it's broken: try /foo/bar.jsp/test/a.baz -> will not match it
       // as baz, but neither as .jsp, which is wrong.
       // XXX Fix this code - I don't think evolution will work in this class.
  -    private ServletWrapper getExtensionMatch(Context context, String path, Request req)
{
  +    private ServletWrapper getExtensionMatch(Mappings m, Context context, String path,
Request req) {
   	String extension=getExtension( path );
   	if( extension == null ) return null;
   
   	// XXX need to store the extensions without *, to avoid extra
   	// string creation
  -	ServletWrapper wrapper= (ServletWrapper)context.getExtensionMap().get("*" + extension);
  -
  +	ServletWrapper wrapper= (ServletWrapper)m.extensionMappedServlets.get("*" + extension);
   	if (wrapper == null)
   	    return null;
   
  @@ -251,7 +432,7 @@
   	    req.setServletPath( path );
   	}
   		
  -	if(debug>0) log("extension match " + path );
  +	if(debug>0) context.log("extension match " + path );
   	return wrapper; 
       }
   
  
  
  
  1.5       +6 -2      jakarta-tomcat/src/share/org/apache/tomcat/servlets/InvokerServlet.java
  
  Index: InvokerServlet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/servlets/InvokerServlet.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- InvokerServlet.java	2000/02/09 21:43:54	1.4
  +++ InvokerServlet.java	2000/02/13 01:16:18	1.5
  @@ -181,8 +181,12 @@
   	    wrapper.setContext(context);
   	    wrapper.setServletClass(servletName);
   	    wrapper.setServletName(servletName); // XXX it can create a conflict !
  -	    
  -            context.addServlet( wrapper );
  +
  +	    try {
  +		context.addServlet( wrapper );
  +	    } catch(TomcatException ex ) {
  +		ex.printStackTrace();
  +	    }
   
   	    // XXX add mapping - if the engine supports dynamic changes in mappings,
   	    // we'll avoid the extra parsing in Invoker !!!
  
  
  

Mime
View raw message