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/02/10 19:55:56 UTC

cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/util NetworkClassLoader.java URLResourceReader.java

costin      00/02/10 10:55:56

  Modified:    src/share/org/apache/tomcat/context DefaultCMSetter.java
               src/share/org/apache/tomcat/core Context.java
                        ServletWrapper.java
               src/share/org/apache/tomcat/loader
                        AdaptiveServletLoader.java
                        ServletClassLoaderImpl.java
               src/share/org/apache/tomcat/util NetworkClassLoader.java
                        URLResourceReader.java
  Removed:     src/share/org/apache/tomcat/core ServletClassLoader.java
  Log:
  - Removed ServletClassLoader and changed everything to use ServletLoader.
  - removed all "class path" methods from context, the call getServletLoader()
  to access the class path.
  
  ServletClassLoader was added as part of the clean up ( in 3.1M1 ) to allow the
  move of the loader in a separate package and make it plug-able.
  ServletLoader is the second attempt - it's still not final, we  need to
  implement servlet reloading and that will probably affect the interface too.
  
  Revision  Changes    Path
  1.7       +14 -29    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- DefaultCMSetter.java	2000/02/09 21:43:53	1.6
  +++ DefaultCMSetter.java	2000/02/10 18:55:54	1.7
  @@ -140,17 +140,10 @@
   	    ctx.addContextInterceptor(new LoadOnStartupInterceptor());
   	}
   	
  -	ctx.addClassPath("WEB-INF/classes");
  -	ctx.addLibPath("WEB-INF/lib");
  -
   	// XXX Loader properties - need to be set on loader!!
  -	if(ctx.getLoader() == null) {
  -	    // ctx.setLoader( new org.apache.tomcat.loader.ServletClassLoaderImpl(ctx));
  -	    ctx.setServletLoader( new org.apache.tomcat.loader.AdaptiveServletLoader());
  -	    initURLs( ctx );
  -	}
  -
  -	
  +	ctx.setServletLoader( new org.apache.tomcat.loader.ServletClassLoaderImpl());
  +	//	    ctx.setServletLoader( new org.apache.tomcat.loader.AdaptiveServletLoader());
  +	initURLs( ctx );
   
   	return 0;
       }
  @@ -158,28 +151,20 @@
       private void initURLs(Context context) {
   	ServletLoader loader=context.getServletLoader();
   	if( loader==null) return;
  -	
  -	String base = context.getDocBase();
   
  -        // The classes directory...
  -        for(Enumeration e = context.getClassPaths();
  -            e.hasMoreElements(); ) {
  -            String cpath = (String) e.nextElement();
  -	    File dir = new File(base + "/" + cpath + "/");
  -	    loader.addRepository( dir );
  -        }
  +	// Add "WEB-INF/classes"
  +
  +	String base = context.getDocBase();
  +	File dir = new File(base + "/WEB-INF/classes");
  +	loader.addRepository( dir );
   
  -        for(Enumeration e = context.getLibPaths();
  -            e.hasMoreElements(); ) {
  -            String libpath = (String) e.nextElement();
  -            File f =  new File(base + "/" + libpath + "/");
  -            Vector jars = new Vector();
  -            getJars(jars, f);
  +	File f =  new File(base + "/WEB-INF/lib");
  +	Vector jars = new Vector();
  +	getJars(jars, f);
               
  -            for(int i=0; i < jars.size(); ++i) {
  -		String jarfile = (String) jars.elementAt(i);
  -		loader.addRepository( new File( base + "/" + libpath + "/" +jarfile ));
  -	    }
  +	for(int i=0; i < jars.size(); ++i) {
  +	    String jarfile = (String) jars.elementAt(i);
  +	    loader.addRepository( new File( base + "/WEB-INF/" +jarfile ));
   	}
       }
   
  
  
  
  1.45      +57 -53    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.44
  retrieving revision 1.45
  diff -u -r1.44 -r1.45
  --- Context.java	2000/02/09 23:43:13	1.44
  +++ Context.java	2000/02/10 18:55:54	1.45
  @@ -121,12 +121,12 @@
       private boolean isWARExpanded = false;
       private boolean isWARValidated = false;
   
  -    // Class Loading
  -    private String classPath = ""; // classpath used by the classloader.
  -    private Vector classPaths = new Vector();
  -    private Vector libPaths = new Vector();
  -    // XXX deprecated
  -    private ServletClassLoader servletLoader;
  +//     // Class Loading
  +//     private String classPath = ""; // classpath used by the classloader.
  +//     private Vector classPaths = new Vector();
  +//     private Vector libPaths = new Vector();
  +//     // XXX deprecated
  +//     private ServletClassLoader servletLoader;
       private ServletLoader servletL;
   
       // Interceptors
  @@ -620,10 +620,14 @@
       }
   
       public Object getAttribute(String name) {
  -        if (name.equals("org.apache.tomcat.jsp_classpath"))
  -	  return getClassPath();
  +        if (name.equals("org.apache.tomcat.jsp_classpath")) {
  +	    //return getServletLoader().getClassPath();
  +	    String cp= getServletLoader().getClassPath();
  +	    //	    System.out.println("CP: " + cp);
  +	    return cp;
  +	}
   	else if(name.equals("org.apache.tomcat.classloader")) {
  -	  return this.getLoader();
  +	  return this.getServletLoader();
           }else {
               Object o = attributes.get(name);
               return attributes.get(name);
  @@ -998,52 +1002,52 @@
       public ServletLoader getServletLoader() {
   	return servletL;
       }
  -
  -    /** @deprecated
  -     */
  -    public void setLoader(ServletClassLoader loader ) {
  -	this.servletLoader=loader;
  -    }
  -
  -    /** @deprecated
  -     */
  -    public ServletClassLoader getLoader() {
  -	return servletLoader;
  -    }
   
  -    public Enumeration getClassPaths() {
  -        return this.classPaths.elements();
  -    }
  -
  -    public void addClassPath(String path) {
  -        this.classPaths.addElement(path);
  -    }
  +//     /** @deprecated
  +//      */
  +//     public void setLoader(ServletClassLoader loader ) {
  +// 	this.servletLoader=loader;
  +//     }
  +
  +//     /** @deprecated
  +//      */
  +//     public ServletClassLoader getLoader() {
  +// 	return servletLoader;
  +//     }
  +
  +//     public Enumeration getClassPaths() {
  +//         return this.classPaths.elements();
  +//     }
  +
  +//     public void addClassPath(String path) {
  +//         this.classPaths.addElement(path);
  +//     }
  +
  +//     public Enumeration getLibPaths() {
  +//         return this.libPaths.elements();
  +//     }
  +
  +//     public void addLibPath(String path) {
  +//         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 =
  +//             this.getLoader().getClassPath();
  +
  +//         if (servletLoaderClassPath != null &&
  +//             servletLoaderClassPath.trim().length() > 0) {
  +//             cp += ((cp.length() > 0) ? File.pathSeparator : "") +
  +//                 servletLoaderClassPath;
  +//         }
   
  -    public Enumeration getLibPaths() {
  -        return this.libPaths.elements();
  -    }
  -
  -    public void addLibPath(String path) {
  -        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 =
  -            this.getLoader().getClassPath();
  -
  -        if (servletLoaderClassPath != null &&
  -            servletLoaderClassPath.trim().length() > 0) {
  -            cp += ((cp.length() > 0) ? File.pathSeparator : "") +
  -                servletLoaderClassPath;
  -        }
  -
  -        return cp;
  -    }
  +//         return cp;
  +//     }
   
       /* -------------------- Utils  -------------------- */
       public void setDebug( int level ) {
  
  
  
  1.20      +4 -12     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.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- ServletWrapper.java	2000/02/09 23:26:28	1.19
  +++ ServletWrapper.java	2000/02/10 18:55:54	1.20
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/ServletWrapper.java,v 1.19 2000/02/09 23:26:28 costin Exp $
  - * $Revision: 1.19 $
  - * $Date: 2000/02/09 23:26:28 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/ServletWrapper.java,v 1.20 2000/02/10 18:55:54 costin Exp $
  + * $Revision: 1.20 $
  + * $Date: 2000/02/10 18:55:54 $
    *
    * ====================================================================
    *
  @@ -233,15 +233,7 @@
   	    if (servletClassName == null) 
   		throw new IllegalStateException(sm.getString("wrapper.load.noclassname"));
   
  -	    ServletLoader loader=context.getServletLoader();
  -	    if( loader==null) {
  -		// XXX old code
  -		servletClass = context.getLoader().loadServlet(this,
  -							       servletClassName);
  -	    } else {
  -		servletClass=loader.loadClass( servletClassName);
  -	    }
  -		
  +	    servletClass=context.getServletLoader().loadClass( servletClassName);
   	}
   	
   	servlet = (Servlet)servletClass.newInstance();
  
  
  
  1.2       +5 -1      jakarta-tomcat/src/share/org/apache/tomcat/loader/AdaptiveServletLoader.java
  
  Index: AdaptiveServletLoader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/loader/AdaptiveServletLoader.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AdaptiveServletLoader.java	2000/02/09 21:43:54	1.1
  +++ AdaptiveServletLoader.java	2000/02/10 18:55:55	1.2
  @@ -151,7 +151,11 @@
           for(Enumeration e = classP.elements() ; e.hasMoreElements(); ) {
               File f = (File) e.nextElement();
               if (cpath.length()>0) cpath += separator;
  -            cpath += FileUtil.patch(f.getName());
  +	    try {
  +		cpath += FileUtil.patch(f.getCanonicalPath());
  +	    } catch( IOException ex) {
  +		ex.printStackTrace();
  +	    }
           }
   
           return cpath;
  
  
  
  1.6       +86 -119   jakarta-tomcat/src/share/org/apache/tomcat/loader/ServletClassLoaderImpl.java
  
  Index: ServletClassLoaderImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/loader/ServletClassLoaderImpl.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ServletClassLoaderImpl.java	2000/02/09 20:36:11	1.5
  +++ ServletClassLoaderImpl.java	2000/02/10 18:55:55	1.6
  @@ -71,144 +71,111 @@
    * class loader. This should speed up performance compared
    * to the earlier class loader.
    *
  + * Changed to use ServletLoader interface ( costin )
  + *
    * @author Harish Prabandham
    */
  -//
  -// WARNING: Some of the APIs in this class are used by J2EE. 
  -// Please talk to harishp@eng.sun.com before making any changes.
  -//
  -public class ServletClassLoaderImpl extends NetworkClassLoader implements ServletClassLoader {
  -    private Context  context;
  +public class ServletClassLoaderImpl extends NetworkClassLoader implements ServletLoader  {
  +    Vector classP;
  +
  +    public ServletClassLoaderImpl() {
  +	super(null); // Who is the parent ??
  +	// this class will not be used as a class loader, it's just a trick for
  +	// protected loadClass()
  +	classP=new Vector();
  +    }
  +
       
  -    public ServletClassLoaderImpl(Context context) {
  -        super(null);
  -        // XXX Hangs up: super((ClassLoader)context.getLoader());
  -	this.context = context;
  -        initURLs(); 
  -    }
  -
  -    private void initURLs() {
  -        URL baseURL = context.getDocumentBase();
  -        String protocol = baseURL.getProtocol();
  -        int port = baseURL.getPort();
  -        String hostname = baseURL.getHost();
  -        
  -        String basepath = baseURL.getFile();
  -
  -        // The classes directory...
  -        for(Enumeration e = context.getClassPaths();
  -            e.hasMoreElements(); ) {
  -            String cpath = (String) e.nextElement();
  -            try {
  -                URL classesURL = new URL(protocol,hostname,port,
  -                                         basepath + "/" + cpath + "/");
  -                addURL(classesURL);
  -            }catch(MalformedURLException mue) {
  -            }
  -        }
  +    /** Check if we need to reload one particular class.
  +     *  No check is done for dependent classes.
  +     *  The final decision about reloading is left to the caller.
  +     */
  +    public boolean shouldReload( String className ) {
  +	return false;
  +    }
   
  -        // The jars in the lib directory...
  -        // This will not work if the URL is not a file URL.
  -        // An alternate way of figuring out the jar files should
  -        // be specified in the spec. Probably in the deployment
  -        // descriptor's web.xml ???
  -        for(Enumeration e = context.getLibPaths();
  -            e.hasMoreElements(); ) {
  -            String libpath = (String) e.nextElement();
  -            File f =  new File(basepath + "/" + libpath + "/");
  -            Vector jars = new Vector();
  -            getJars(jars, f);
  -            
  -            for(int i=0; i < jars.size(); ++i) {
  -                try {
  -                    String jarfile = (String) jars.elementAt(i);
  -                    URL jarURL = new URL(protocol,hostname,port,
  -                                         basepath + "/" + libpath + "/" + jarfile);
  -                    addURL(jarURL);
  -                }catch(MalformedURLException mue) {
  -                }
  -            }
  -        }
  +    
  +    /** Check if we need to reload. All loaded classes are
  +     *  checked.
  +     *  The final decision about reloading is left to the caller.
  +     */
  +    public boolean shouldReload() {
  +	return false;
       }
  +
  +    
   
  -    public synchronized Class loadServlet(ServletWrapper wrapper, String name)
  -        throws ClassNotFoundException {
  -	Class clazz = loadClass(name, true);
  -	// do whatever marking we need to do
  -	return clazz;
  +    /** Reset the class loader. The caller should take all actions
  +     *  required by this step ( free resources for GC, etc)
  +     */
  +    public void reload() {
  +    }
  +		
  +
  +    /** Return a real class loader
  +     */
  +    public ClassLoader getClassLoader() {
  +	return this;
       }
   
  -    protected synchronized Class loadClass(String name, boolean resolve)
  -        throws ClassNotFoundException {
  -        // This is a bad idea. Unfortunately the class loader may
  -        // be set on the context at any point.
  -        setParent(null); // XXX that hangs up tomcat: (ClassLoader)context.getLoader());
  -        return super.loadClass(name, resolve);
  +    
  +    /** Handle servlet loading. Same as getClassLoader().loadClass(name, true); 
  +     */
  +    public Class loadClass( String name)
  +	throws ClassNotFoundException
  +    {
  +	return loadClass(name, true);
       }
   
  +
  +    /** Return the class loader view of the class path
  +     */
       public String getClassPath() {
           String separator = System.getProperty("path.separator", ":");
           String cpath = "";
   
  -        for(Enumeration e = getURLs(); e.hasMoreElements(); ) {
  -            URL url = (URL) e.nextElement();
  +        for(Enumeration e = classP.elements() ; e.hasMoreElements(); ) {
  +            File f = (File) e.nextElement();
               if (cpath.length()>0) cpath += separator;
  -            cpath += FileUtil.patch(url.getFile());
  +	    try {
  +		cpath += FileUtil.patch(f.getCanonicalPath());
  +	    } catch( IOException ex) {
  +		ex.printStackTrace();
  +	    }
           }
   
           return cpath;
       }
   
  -    private void getJars(Vector v, File f) {
  -        FilenameFilter jarfilter = new JarFileFilter();
  -        FilenameFilter dirfilter = new DirectoryFilter();
  -        
  -        if(f.exists() && f.isDirectory() && f.isAbsolute()) {
  -            String[] jarlist = f.list(jarfilter);
  -
  -            for(int i=0; (jarlist != null) && (i < jarlist.length); ++i) {
  -                v.addElement(jarlist[i]);
  -            }
  -
  -            String[] dirlist = f.list(dirfilter);
  -
  -            for(int i=0; (dirlist != null) && (i < dirlist.length); ++i) {
  -                File dir = new File(f, dirlist[i]);
  -                getJars(v, dir);
  -            }
  -        }
  -    }
  -}
  -
  -
  -class JarFileFilter implements FilenameFilter {
  -
  -    public boolean accept(File dir, String fname) {
  -        if(fname.endsWith(".jar"))
  -            return true;
  -
  -        return false;
  -    }
  -}
  -
  -class DirectoryFilter implements FilenameFilter {
  -
  -    public boolean accept(File dir, String fname) {
  -        File f = new File(dir, fname);
  -        if(f.isDirectory())
  -            return true;
   
  -        return false;
  -    }
  +    /** Add a new directory or jar to the class loader.
  +     *  Not all loaders can add resources dynamically -
  +     *  that may require a reload.
  +     */
  +    public void addRepository( File f ) {
  +	classP.addElement( f );
  +	try {
  +	    String path=f.getCanonicalPath();
  +	    // NetworkClassLoader will use the last char to
  +	    // decide if it's a directory or a jar.
  +	    // X  Can we change that ?
  +	    if( ! path.endsWith("/") && f.isDirectory() )
  +		path=path+"/";
  +	    
  +	    URL url=new URL( "file", "", path);
  +	    //	    System.out.println("Adding " + url );
  +	    addURL( url );
  +	} catch( MalformedURLException ex) {
  +	    ex.printStackTrace();
  +	} catch( IOException ex1) {
  +	    ex1.printStackTrace();
  +	}
  +    }
  +
  +    /** Add a new remote repository. Not all class loader will
  +     *  support remote resources, use File if it's a local resource.
  +     */
  +    public void addRepository( URL url ) {
  +	return;// no support for URLs in AdaptiveClassLoader
  +    }    
   }
  -
  -
  -
  -
  -
  -
  -
  -
  -
  -
  -
  
  
  
  1.5       +4 -3      jakarta-tomcat/src/share/org/apache/tomcat/util/NetworkClassLoader.java
  
  Index: NetworkClassLoader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/NetworkClassLoader.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- NetworkClassLoader.java	1999/11/28 23:52:31	1.4
  +++ NetworkClassLoader.java	2000/02/10 18:55:55	1.5
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/NetworkClassLoader.java,v 1.4 1999/11/28 23:52:31 harishp Exp $
  - * $Revision: 1.4 $
  - * $Date: 1999/11/28 23:52:31 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/NetworkClassLoader.java,v 1.5 2000/02/10 18:55:55 costin Exp $
  + * $Revision: 1.5 $
  + * $Date: 2000/02/10 18:55:55 $
    *
    * ====================================================================
    *
  @@ -150,6 +150,7 @@
       private byte[] loadResource(URL url, String resourceName)
           throws IOException {
           URLResourceReader urr = (URLResourceReader) urlset.get(url);
  +	//	System.out.println("Loading from " + urr + " " + resourceName);
           if(urr != null) {
               return urr.getResource(resourceName);
           }
  
  
  
  1.3       +7 -3      jakarta-tomcat/src/share/org/apache/tomcat/util/URLResourceReader.java
  
  Index: URLResourceReader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/URLResourceReader.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- URLResourceReader.java	1999/12/03 18:07:07	1.2
  +++ URLResourceReader.java	2000/02/10 18:55:55	1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/URLResourceReader.java,v 1.2 1999/12/03 18:07:07 harishp Exp $
  - * $Revision: 1.2 $
  - * $Date: 1999/12/03 18:07:07 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/URLResourceReader.java,v 1.3 2000/02/10 18:55:55 costin Exp $
  + * $Revision: 1.3 $
  + * $Date: 2000/02/10 18:55:55 $
    *
    * ====================================================================
    *
  @@ -199,6 +199,10 @@
       public void close() {
           resourceCache.clear();
           resourceCache = null;
  +    }
  +
  +    public String toString() {
  +	return url.toString();
       }
   }