You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by co...@locus.apache.org on 2000/01/09 03:57:41 UTC

cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/core Context.java ServletWrapper.java

costin      00/01/08 18:57:41

  Modified:    src/share/org/apache/tomcat/core Context.java
                        ServletWrapper.java
  Added:       src/share/org/apache/tomcat/context
                        LoadOnStartupInterceptor.java
                        LocalStrings.properties
  Log:
  Moved load on startup to interceptor.
  
  Revision  Changes    Path
  1.1                  jakarta-tomcat/src/share/org/apache/tomcat/context/LoadOnStartupInterceptor.java
  
  Index: LoadOnStartupInterceptor.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  
  
  package org.apache.tomcat.context;
  
  import org.apache.tomcat.core.*;
  import org.apache.tomcat.core.Constants;
  import org.apache.tomcat.util.*;
  import org.apache.tomcat.deployment.*;
  import java.io.*;
  import java.net.*;
  import java.util.*;
  import javax.servlet.http.*;
  
  
  /**
   * Interceptor that loads the "load-on-startup" servlets
   *
   * @author costin@dnt.ro
   */
  public class LoadOnStartupInterceptor {
      private static StringManager sm =StringManager.getManager("org.apache.tomcat.context");
      
      public LoadOnStartupInterceptor() {
      }
  	
      public int handleContextInit(Context ctx) {
  	Vector orderedKeys = new Vector();
  	Enumeration e=ctx.getInitLevels();
  		
  	// order keys
  	while (e.hasMoreElements()) {
  	    Integer key = (Integer)e.nextElement();
  	    int slot = -1;
  	    for (int i = 0; i < orderedKeys.size(); i++) {
  	        if (key.intValue() <
  		    ((Integer)(orderedKeys.elementAt(i))).intValue()) {
  		    slot = i;
  		    break;
  		}
  	    }
  	    if (slot > -1) {
  	        orderedKeys.insertElementAt(key, slot);
  	    } else {
  	        orderedKeys.addElement(key);
  	    }
  	}
  
  	// loaded ordered servlets
  
  	// Priorities IMO, should start with 0.
  	// Only System Servlets should be at 0 and rest of the
  	// servlets should be +ve integers.
  	// WARNING: Please do not change this without talking to:
  	// harishp@eng.sun.com (J2EE impact)
  
  	for (int i = 0; i < orderedKeys.size(); i ++) {
  	    Integer key = (Integer)orderedKeys.elementAt(i);
  
  	    Enumeration sOnLevel = ctx.getLoadableServlets( key );
  
  	    while (sOnLevel.hasMoreElements()) {
  		String servletName = (String)sOnLevel.nextElement();
  		ServletWrapper  result = ctx.getServletByName(servletName);
  		
  		if(result==null)
  		    System.out.println("Warning: we try to load an undefined servlet " + servletName);
  		else {
  		    try {
  			result.loadServlet();
  		    } catch (Exception ee) {
  			String msg = sm.getString("context.loadServlet.e",
  						  servletName);
  			System.out.println(msg);
  		    } 
  		}
  	    }
  	}
  	return 0;
      }
  }
  
  
  
  1.1                  jakarta-tomcat/src/share/org/apache/tomcat/context/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  context.props.iae=no class for servlet name: {0}
  
  
  
  1.20      +44 -126   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.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- Context.java	2000/01/08 22:55:45	1.19
  +++ Context.java	2000/01/09 02:57:40	1.20
  @@ -380,21 +380,19 @@
   
   	// Read context's web.xml
   	new WebXmlInterceptor().handleContextInit( this );
  -	
  +
   	if (! this.isInvokerEnabled) {
   	    // Put in a special "no invoker" that handles
   	    // /servlet requests and explains why no servlet
   	    // is being invoked
   	    this.addServlet(Constants.Servlet.NoInvoker.Name,
  -	        Constants.Servlet.NoInvoker.Class);
  +			    Constants.Servlet.NoInvoker.Class, null);
   	    this.addMapping(Constants.Servlet.NoInvoker.Name,
  -	        Constants.Servlet.NoInvoker.Map);
  +			    Constants.Servlet.NoInvoker.Map);
   	}
   
  -	// load-on-startup
  -        if (! loadableServlets.isEmpty()) {
  -	    loadServlets();
  -        }
  +	// load initial servlets
  +	new LoadOnStartupInterceptor().handleContextInit( this );
       }
   
       public SessionManager getSessionManager() {
  @@ -546,7 +544,7 @@
       }
   
   
  -    public Enumeration getInitLevles() {
  +    public Enumeration getInitLevels() {
   	return loadableServlets.keys();
       }
   
  @@ -569,65 +567,7 @@
   	loadableServlets.put(level, v);
       }
       
  -    private void loadServlets() {
  -	Vector orderedKeys = new Vector();
  -	Enumeration e = loadableServlets.keys();
  -	
  -	// order keys
  -
  -	while (e.hasMoreElements()) {
  -	    Integer key = (Integer)e.nextElement();
  -	    int slot = -1;
  -
  -	    for (int i = 0; i < orderedKeys.size(); i++) {
  -	        if (key.intValue() <
  -		    ((Integer)(orderedKeys.elementAt(i))).intValue()) {
  -		    slot = i;
   
  -		    break;
  -		}
  -	    }
  -
  -	    if (slot > -1) {
  -	        orderedKeys.insertElementAt(key, slot);
  -	    } else {
  -	        orderedKeys.addElement(key);
  -	    }
  -	}
  -
  -	// loaded ordered servlets
  -
  -	// Priorities IMO, should start with 0.
  -	// Only System Servlets should be at 0 and rest of the
  -	// servlets should be +ve integers.
  -	// WARNING: Please do not change this without talking to:
  -	// harishp@eng.sun.com (J2EE impact)
  -
  -	for (int i = 0; i < orderedKeys.size(); i ++) {
  -	    Integer key = (Integer)orderedKeys.elementAt(i);
  -	    e = ((Vector)(loadableServlets.get(key))).elements();
  -
  -	    while (e.hasMoreElements()) {
  -		String servletName = (String)e.nextElement();
  -		ServletWrapper  result = getServletByName(servletName);
  -		
  -		if(result==null)
  -		    System.out.println("Warning: we try to load an undefined servlet " + servletName);
  -		
  -		try {
  -		    if(result!=null)
  -			result.loadServlet();
  -		} catch (Exception ee) {
  -		    String msg = sm.getString("context.loadServlet.e",
  -		        servletName);
  -
  -		    System.out.println(msg);
  -		} 
  -	    }
  -	}
  -    }
  -
  -
       // -------------------- From Container
   
       public URL getServletBase() {
  @@ -638,30 +578,8 @@
           this.servletBase = servletBase;
       }
   
  -    /**
  -     * Add a servlet with the given name to the container. The
  -     * servlet will be loaded by the container's class loader
  -     * and instantiated using the given class name.
  -     */
  -    
  -    public void addServlet(String name, String className) {
  -        addServlet(name, null, className, null);
  -    }
  - 
  -    public void addServlet(String name, String className,
  -        String description) {
  -        addServlet(name, description, className, null);
  -    }
  -
  -    public void addServlet(String name, Class clazz) {
  -        addServlet(name, null, null, clazz);
  -    }
   
  -    public void addServlet(String name, Class clazz,
  -	String description) {
  -        addServlet(name, description, null, clazz);
  -    }
  -
  +    // -------------------- 
       public void addJSP(String name, String path) {
           addJSP(name, null, path);
       }
  @@ -682,10 +600,14 @@
       /** True if we have a servlet with className.
        */
       public boolean containsServlet(String className) {
  -        ServletWrapper[] sw = getServlets(className);
  -
  -        return (sw != null &&
  -	    sw.length > 0);
  +	Enumeration enum = servlets.keys();
  +	while (enum.hasMoreElements()) {
  +	    String key = (String)enum.nextElement();
  +	    ServletWrapper sw = (ServletWrapper)servlets.get(key);
  +            if (className.equals(sw.getServletClass()))
  +	        return true;
  +	}
  +	return false;
       }
   
       /** Check if we have a servlet with the specified name
  @@ -696,8 +618,14 @@
   
       /** Remove all servlets with a specific class name
        */
  -    void removeServlet(String className) {
  -        removeServlets(getServlets(className));
  +    void removeServletByClassName(String className) {
  +	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 );
  +	}
       }
   
       /** Remove the servlet with a specific name
  @@ -718,21 +646,16 @@
   
       public void removeJSP(String path) {
   	Enumeration enum = servlets.keys();
  -
   	while (enum.hasMoreElements()) {
   	    String key = (String)enum.nextElement();
   	    ServletWrapper sw = (ServletWrapper)servlets.get(key);
  -
  -	    if (sw.getPath() != null &&
  -	        sw.getPath().equals(path)) {
  +	    if (path.equals(sw.getPath()))
   	        removeServlet( sw );
  -	    }
   	}
       }
   
       public void setServletInitParams(String name, Hashtable initParams) {
   	ServletWrapper wrapper = (ServletWrapper)servlets.get(name);
  -
   	if (wrapper != null) {
   	    wrapper.setInitArgs(initParams);
   	}
  @@ -751,7 +674,6 @@
        *    default servlet
        *
        */
  -
       public void addMapping(String servletName, String path) {
           ServletWrapper sw = (ServletWrapper)servlets.get(servletName);
   
  @@ -760,7 +682,7 @@
   	    // this might be a bit aggressive
   
   	    if (! servletName.startsWith("/")) {
  -	        addServlet(servletName, null, servletName, null);
  +	        addServlet(servletName, null, servletName);
   	    } else {
   	        addJSP(servletName, servletName);
   	    }
  @@ -863,29 +785,29 @@
           return wrapper;
       }
   
  -    private void addServlet(String name, String description,
  -        String className, Class clazz) {
  -        // XXX
  -        // check for duplicates!
  +    /**
  +     * Add a servlet with the given name to the container. The
  +     * servlet will be loaded by the container's class loader
  +     * and instantiated using the given class name.
  +     *
  +     * Called to add a new servlet from web.xml
  +     *
  +     */
  +    public void addServlet(String name, String className,
  +			   String description) {
  +	// assert className!=null
   
  +        // check for duplicates
           if (servlets.get(name) != null) {
  -            removeServlet(name);
  +            removeServletByClassName(name); // XXX XXX why?
               removeServletByName(name);
           }
   
           ServletWrapper wrapper = new ServletWrapper(this);
  -
   	wrapper.setServletName(name);
   	wrapper.setServletDescription(description);
  +	wrapper.setServletClass(className);
   
  -	if (className != null) {
  -	    wrapper.setServletClass(className);
  -	}
  -
  -	if (clazz != null) {
  -	    wrapper.setServletClass(clazz);
  -	}
  -
   	servlets.put(name, wrapper);
       }
   
  @@ -939,7 +861,7 @@
   
       /** Return servlets with a specified class name
        */
  -    private ServletWrapper[] getServlets(String name) {
  +    private ServletWrapper[] getServletsByClassName(String name) {
           Vector servletWrappers = new Vector();
   	Enumeration enum = servlets.keys();
   
  @@ -1024,6 +946,10 @@
           this.libPaths.addElement(path);
       }
   
  +    // XXX XXX XXX ugly, need rewrite ( servletLoader will call getClassPaths and getLibPaths
  +    // and will concatenate the "file" part of them ).
  +    /** Returns the classpath as a string
  +     */
       public String getClassPath() {
           String cp = this.classPath.trim();
           String servletLoaderClassPath =
  @@ -1036,13 +962,5 @@
           }
   
           return cp;
  -    }
  -    
  -    public void setClassPath(String classPath) {
  -        if (this.classPath.trim().length() > 0) {
  -	    this.classPath += File.pathSeparator;
  -	}
  -
  -        this.classPath += classPath;
       }
   }
  
  
  
  1.8       +4 -4      jakarta-tomcat/src/share/org/apache/tomcat/core/ServletWrapper.java
  
  Index: ServletWrapper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/ServletWrapper.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ServletWrapper.java	2000/01/08 15:09:26	1.7
  +++ ServletWrapper.java	2000/01/09 02:57:40	1.8
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/ServletWrapper.java,v 1.7 2000/01/08 15:09:26 costin Exp $
  - * $Revision: 1.7 $
  - * $Date: 2000/01/08 15:09:26 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/ServletWrapper.java,v 1.8 2000/01/09 02:57:40 costin Exp $
  + * $Revision: 1.8 $
  + * $Date: 2000/01/09 02:57:40 $
    *
    * ====================================================================
    *
  @@ -198,7 +198,7 @@
   	}
       }
       
  -    void loadServlet()
  +    public void loadServlet()
       throws ClassNotFoundException, InstantiationException,
           IllegalAccessException, ServletException {
           // Check if this is a JSP, they get special treatment