costin      01/06/08 20:35:07

  Modified:    src/share/org/apache/tomcat/startup Main.java
  Log:
  The cleanup of Main - the duplicated methods are moved in util, unused
  code removed, the other class loaders are saved ( instead of just abandoning
  them and using getClassLoader later )
  
  Revision  Changes    Path
  1.33      +69 -205   jakarta-tomcat/src/share/org/apache/tomcat/startup/Main.java
  
  Index: Main.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/startup/Main.java,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- Main.java 2001/05/21 04:44:24     1.32
  +++ Main.java 2001/06/09 03:35:06     1.33
  @@ -1,4 +1,4 @@
  -/* $Id: Main.java,v 1.32 2001/05/21 04:44:24 costin Exp $
  +/* $Id: Main.java,v 1.33 2001/06/09 03:35:06 costin Exp $
    * ====================================================================
    *
    * The Apache Software License, Version 1.1
  @@ -69,6 +69,8 @@
   import org.apache.tomcat.util.IntrospectionUtils;
   import org.apache.tomcat.util.compat.Jdk11Compat;
   
  +// The main idea is to have a starter with minimal class loader deps,
  +// and use it to create the initial environment
   
   /**
        Starter class for Tomcat.
  @@ -107,7 +109,7 @@
        @author Costin Manolache
        @author Ignacio J. Ortega
        @author Mel Martinez [EMAIL PROTECTED]
  -     @version $Revision: 1.32 $ $Date: 2001/05/21 04:44:24 $
  +     @version $Revision: 1.33 $ $Date: 2001/06/09 03:35:06 $
    */
   public class Main{
   
  @@ -199,74 +201,35 @@
   
       // -------------------- Utils --------------------
   
  -    public static String checkDir( String base ) {
  -        String r=null;
  -        try {
  -            File f = new File(base);
  -            r = f.getCanonicalPath();
  -            if( ! r.endsWith("/") && ! r.endsWith(File.separator) ) 
r+=File.separator;
  -        } catch (IOException ioe) {
  -            ioe.printStackTrace();
  -            r=base;
  -        }
  -        return r;
  +    public String getInstallDir() {
  +     if( installDir==null )
  +         installDir=".";
  +     return installDir;
       }
   
  -    public static URL getURL( String base, String file ) {
  -        try {
  -            File baseF = new File(base);
  -            File f = new File(baseF,file);
  -            String path = f.getCanonicalPath();
  -            if( f.isDirectory() ){
  -                    path +="/";
  -            }
  -            return new URL( "file", "", path );
  -        } catch (Exception ex) {
  -            ex.printStackTrace();
  -            return null;
  -        }
  -    }
  -
       public String getServerDir() {
  -        if( libBase!=null ){
  -            return libBase;
  +        if( libBase==null ){
  +         libBase=getInstallDir() + File.separator + "lib" +
  +             File.separator + "container" + File.separator;
           }
  -        if( installDir!=null ){
  -            libBase=checkDir( installDir + File.separator + "lib" +
  -                           File.separator + "container");
  -        }else{
  -            libBase=checkDir("." + File.separator + "lib" +
  -                          File.separator + "container");
  -        }
  -        return libBase;
  +     return libBase;
       }
   
       public String getAppsDir() {
  -        if( serverBase!=null ){
  -            return serverBase;
  -        }
  -        if( installDir!=null ){
  -            serverBase=checkDir( installDir + File.separator + "lib" +
  -                              File.separator + "apps");
  -        }else{
  -            serverBase=checkDir("." + File.separator + "lib" +
  -                             File.separator + "apps");
  +        if( serverBase==null ){
  +         serverBase=getInstallDir() + File.separator + "lib" +
  +             File.separator + "apps" + File.separator;
           }
  -        return serverBase;
  +     return serverBase;
       }
   
       public String getCommonDir() {
  -        if( commonBase!=null ){
  -            return commonBase;
  +        if( commonBase==null ){
  +         commonBase=getInstallDir() + File.separator + "lib" +
  +             File.separator+ "common" + File.separator;
  +                     
           }
  -        if( installDir!=null ){
  -            commonBase=checkDir( installDir + File.separator + "lib" +
  -                              File.separator+ "common");
  -        }else{
  -            commonBase=checkDir("." +File.separator + "lib" +
  -                             File.separator + "common");
  -        }
  -        return commonBase;
  +     return commonBase;
       }
   
   
  @@ -281,47 +244,49 @@
               homeDir = System.getProperty("tomcat.home");
   
               ClassLoader parentL=this.getClass().getClassLoader();
  +
  +            // the server classloader loads from classes dir too and
  +         // from tools.jar
   
  -            // the server classloader loads from classes dir too and from tools.jar
  +         // Create the common class loader --------------------
  +         Vector commonJars = new Vector();
  +         IntrospectionUtils.addToClassPath( commonJars,
  +                                            getCommonDir());
  +         IntrospectionUtils.addJarsFromClassPath(commonJars,
  +                                                 TOMCAT_COMMON_CLASSPATH);
  +
  +            URL[] commonClassPath=IntrospectionUtils.getClassPath(commonJars);
  +         //            displayClassPath( "common ", commonClassPath );
  +         ClassLoader commonCl=
  +                    jdk11Compat.newClassLoaderInstance(commonClassPath ,
  +                                                    parentL);
   
  +
  +         // Create the container class loader --------------------
               Vector serverJars=new Vector();
  -            //serverJars.addElement( getURL(  getServerDir() ,"../classes/" ));
  -            Vector serverUrlV =getClassPathV(getServerDir());
  -            for(int i=0; i < serverUrlV.size();i++){
  -                serverJars.addElement(serverUrlV.elementAt(i));
  -            }
  -            serverJars.addElement( new URL( "file", "" ,
  -                System.getProperty( "java.home" ) + "/../lib/tools.jar"));
  -
  -            Vector commonDirJars = getClassPathV(getCommonDir());
  -            Vector commonJars = getJarsFromClassPath(TOMCAT_COMMON_CLASSPATH);
  -            Enumeration jars = commonDirJars.elements();
  -            while(jars.hasMoreElements()){
  -                URL url = (URL)jars.nextElement();
  -                if(!commonJars.contains(url)){
  -                    commonJars.addElement(url);
  -                }
  -            }
  -            Vector appsDirJars = getClassPathV(getAppsDir());
  -            Vector appsJars = getJarsFromClassPath(TOMCAT_APPS_CLASSPATH);
  -            jars = appsDirJars.elements();
  -            while(jars.hasMoreElements()){
  -                URL url = (URL)jars.nextElement();
  -                if(!appsJars.contains(url)){
  -                    appsJars.addElement(url);
  -                }
  -            }
  -            URL[] commonClassPath=getURLs(commonJars);
  -            ClassLoader commonCl=
  -                    jdk11Compat.newClassLoaderInstance(commonClassPath ,parentL);
  -            URL[] appsClassPath=getURLs(appsJars);
  -            ClassLoader appsCl=
  -                    jdk11Compat.newClassLoaderInstance(appsClassPath ,commonCl);
  -            URL[] serverClassPath=getURLs(serverJars);
  +         IntrospectionUtils.addToClassPath( serverJars, getServerDir());
  +         IntrospectionUtils.addToolsJar( serverJars );
  +
  +
  +         URL[] serverClassPath=IntrospectionUtils.getClassPath(serverJars);
  +         //displayClassPath( "server ", serverClassPath );
               ClassLoader serverCl=
  -                    jdk11Compat.newClassLoaderInstance(serverClassPath ,commonCl);
  +                    jdk11Compat.newClassLoaderInstance(serverClassPath ,
  +                                                    commonCl);
   
  +         // Create the webapps class loader --------------------
  +            Vector appsJars = new Vector();
  +         IntrospectionUtils.addToClassPath(appsJars, getAppsDir());
  +         IntrospectionUtils.addJarsFromClassPath( appsJars, 
  +                                                  TOMCAT_APPS_CLASSPATH);
  +
  +         
  +            URL[] appsClassPath=IntrospectionUtils.getClassPath(appsJars);
  +            ClassLoader appsCl=
  +             jdk11Compat.newClassLoaderInstance(appsClassPath ,
  +                                                    commonCl);
   
  +         // Instantiate tomcat ( using container class loader )
               Class cls=serverCl.loadClass("org.apache.tomcat.startup.Tomcat");
               Object proxy=cls.newInstance();
   
  @@ -329,6 +294,12 @@
               IntrospectionUtils.setAttribute(proxy,"home", homeDir );
               IntrospectionUtils.setAttribute(proxy,"install", installDir );
               IntrospectionUtils.setAttribute(proxy,"parentClassLoader",appsCl);
  +         IntrospectionUtils.setAttribute(proxy,"commonClassLoader",
  +                                         commonCl);
  +         IntrospectionUtils.setAttribute(proxy,"containerClassLoader",
  +                                         serverCl);
  +         IntrospectionUtils.setAttribute(proxy,"appsClassLoader",
  +                                         appsCl);
               IntrospectionUtils.execute(  proxy, "execute" );
               return;
           } catch( Exception ex ) {
  @@ -337,120 +308,13 @@
           }
       }
   
  -    // -------------------- Command-line args processing --------------------
  -    /* Later
  -       static class Arg {
  -       String name;
  -       String aliases[];
  -       int args;
  -
  -       boolean task;
  -       }
  -    */
  -/*
  -    String args0[]= { "help", "stop", "g", "generateConfigs" };
  -    String args1[]= { "f", "config", "h", "home" };
  -
  -     Read command line arguments and set properties in proxy,
  -     using ant-like patterns
  -    void processArgs(Object proxy, String args[] )
  -     throws Exception
  -    {
  -
  -     for( int i=0; i< args.length; i++ ) {
  -         String arg=args[i];
  -         if( arg.startsWith("-"))
  -             arg=arg.substring(1);
  -
  -         for( int j=0; j< args0.length ; j++ ) {
  -             if( args0[j].equalsIgnoreCase( arg )) {
  -                 IntrospectionUtils.setAttribute( proxy, args0[j], "true");
  -                 break;
  -             }
  -         }
  -         for( int j=0; j< args1.length ; j++ ) {
  -             if( args1[j].equalsIgnoreCase( arg )) {
  -                 i++;
  -                 if( i < args.length )
  -                     IntrospectionUtils.setAttribute( proxy,
  -                                                      args1[j], args[i]);
  -                 break;
  -             }
  -         }
  +    public void displayClassPath( String msg, URL[] cp ) {
  +     System.out.println(msg);
  +     for( int i=0; i<cp.length; i++ ) {
  +         System.out.println( cp[i].getFile() );
        }
  -    }
  -*/
  -    /**
  -            add elements from the classpath <i>cp</i> to a Vector
  -            <i>jars</i> as file URLs (We use Vector for JDK 1.1 compat).
  -            <p>
  -            @param <b>cp</b> a String classpath of directory or jar file
  -                            elements separated by path.separator delimiters.
  -            @return a Vector of URLs.
  -    */
  -    public static Vector getJarsFromClassPath(String cp)
  -            throws IOException,MalformedURLException{
  -        Vector jars = new Vector();
  -        String sep = System.getProperty("path.separator");
  -        String token;
  -        StringTokenizer st;
  -        if(cp!=null){
  -            st = new StringTokenizer(cp,sep);
  -            while(st.hasMoreTokens()){
  -                File f = new File(st.nextToken());
  -                String path = f.getCanonicalPath();
  -                if(f.isDirectory()){
  -                        path += "/";
  -                }
  -                URL url = new URL("file","",path);
  -                if(!jars.contains(url)){
  -                        jars.addElement(url);
  -                }
  -            }
  -        }
  -        return jars;
  -    }
  -
  -    public String[] getJarFiles(String ld) {
  -     File dir = new File(ld);
  -        String[] names=null;
  -        if (dir.isDirectory()){
  -            names = dir.list( new FilenameFilter(){
  -            public boolean accept(File d, String name) {
  -                if (name.endsWith(".jar")){
  -                    return true;
  -                }
  -                return false;
  -            }
  -            });
  -        }
  -
  -     return names;
  -    }
  -
  -    Vector getClassPathV(String p0) throws Exception {
  -        Vector urlV=new Vector();
  -        try{
  -            String cpComp[]=getJarFiles(p0);
  -            if (cpComp != null){
  -                int jarCount=cpComp.length;
  -                for( int i=0; i< jarCount ; i++ ) {
  -                    urlV.addElement( getURL(  p0 , cpComp[i] ));
  -                }
  -            }
  -        }catch(Exception ex){
  -            ex.printStackTrace();
  -        }
  -        return urlV;
  -    }
  -
  -    private URL[] getURLs(Vector v){
  -        URL[] urls=new URL[ v.size() ];
  -        for( int i=0; i<v.size(); i++ ) {
  -            urls[i]=(URL)v.elementAt( i );
  -        }
  -        return urls;
       }
  +    
   
   }
   
  
  
  

Reply via email to