costin      00/12/28 15:19:57

  Modified:    .        build.xml
               src/share/org/apache/tomcat/context AutoSetup.java
               src/share/org/apache/tomcat/startup Main.java Tomcat.java
               src/share/org/apache/tomcat/task Expand.java
               src/share/org/apache/tomcat/util IntrospectionUtils.java
  Log:
  - fixed Main - now "java -jar tomcat.jar" should work ( as a replacement
  for the .sh files - on JDK1.1 we still need the scripts ).
  
  - moved generic introspection code to IntrospectionUtils
  
  - Expand.java is used in only one place and it's doing something trivial.
  It's also duplicating code from ant - there is no need to maintain a separate
  version.
  
  - StartTomcat is no longer needed
  
  Revision  Changes    Path
  1.99      +2 -0      jakarta-tomcat/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/build.xml,v
  retrieving revision 1.98
  retrieving revision 1.99
  diff -u -r1.98 -r1.99
  --- build.xml 2000/12/27 17:14:57     1.98
  +++ build.xml 2000/12/28 23:19:44     1.99
  @@ -116,11 +116,13 @@
         <!-- no dependencies -->
         <include name="org/apache/tomcat/startup/Main.java"/>    
         <include name="org/apache/tomcat/util/SimpleClassLoader.java"/>    
  +      <include name="org/apache/tomcat/util/IntrospectionUtils.java"/>    
       </javac>
       <jar jarfile="${tomcat.build}/lib/tomcat.jar" basedir="${tomcat.build}/classes" 
manifest="src/build/manifest"> 
         <include name="org/apache/tomcat/startup/Main.class"/> 
         <include name="org/apache/tomcat/startup/Main$*.class"/> 
         <include name="org/apache/tomcat/util/SimpleClassLoader**"/> 
  +      <include name="org/apache/tomcat/util/IntrospectionUtils**"/> 
       </jar>
     </target>
   
  
  
  
  1.21      +51 -16    
jakarta-tomcat/src/share/org/apache/tomcat/context/AutoSetup.java
  
  Index: AutoSetup.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/context/AutoSetup.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- AutoSetup.java    2000/12/26 23:01:07     1.20
  +++ AutoSetup.java    2000/12/28 23:19:47     1.21
  @@ -88,7 +88,6 @@
        *  virtual hosts too
        */
       public void engineStart(ContextManager cm) throws TomcatException {
  -     super.engineInit( cm );
        String home=cm.getHome();
        File webappD=new File(home + "/webapps");
        if (! webappD.exists() || ! webappD.isDirectory()) {
  @@ -96,17 +95,17 @@
            return ; // nothing to set up
        }
   
  -    Enumeration en=cm.getContexts();
  -    while (en.hasMoreElements()){
  -        Context ctx=(Context)en.nextElement();
  -        if( ctx.getHost()== null ) {
  -            // this is a context that goes into the default server
  -            // we care only about the root context for autosetup
  -            // until we define a pattern for automatic vhost setup.
  -            definedContexts.put( ctx.getPath(), ctx );
  -            if(debug>0) log("Register explicit context " + ctx.getPath());
  -        }
  -    }
  +     Enumeration en=cm.getContexts();
  +     while (en.hasMoreElements()){
  +         Context ctx=(Context)en.nextElement();
  +         if( ctx.getHost()== null ) {
  +             // this is a context that goes into the default server
  +             // we care only about the root context for autosetup
  +             // until we define a pattern for automatic vhost setup.
  +             definedContexts.put( ctx.getPath(), ctx );
  +             if(debug>0) log("Register explicit context " + ctx.getPath());
  +         }
  +     }
   
        String[] list = webappD.list();
        if( list.length==0 ) {
  @@ -122,11 +121,9 @@
                    // To update you need to "remove" the context first!!!
                    appDir.mkdirs();
                    // Expand war file
  -                 Expand expand=new Expand();
  -                 expand.setSrc( home + "/webapps/" + name );
  -                 expand.setDest( home + "/webapps/" + fname);
                    try {
  -                     expand.execute();
  +                     expand(home + "/webapps/" + name,
  +                            home + "/webapps/" + fname);
                    } catch( IOException ex) {
                        log("expanding webapp " + name, ex);
                        // do what ?
  @@ -173,6 +170,44 @@
               }
               }
        }
  +    }
  +
  +    private void expand( String src, String dest)
  +     throws IOException
  +    {
  +     File srcF=new File( source);
  +     File dir=new File( dest );
  +     
  +     ZipInputStream zis = new ZipInputStream(new FileInputStream(srcF));
  +     ZipEntry ze = null;
  +     
  +     while ((ze = zis.getNextEntry()) != null) {
  +         try {
  +             File f = new File(dir, ze.getName());
  +             // create intermediary directories - sometimes zip don't add them
  +             File dirF=new File(f.getParent());
  +             dirF.mkdirs();
  +             
  +             if (ze.isDirectory()) {
  +                 f.mkdirs(); 
  +             } else {
  +                 byte[] buffer = new byte[1024];
  +                 int length = 0;
  +                 FileOutputStream fos = new FileOutputStream(f);
  +                 
  +                 while ((length = zis.read(buffer)) >= 0) {
  +                     fos.write(buffer, 0, length);
  +                 }
  +                 
  +                 fos.close();
  +             }
  +         } catch( FileNotFoundException ex ) {
  +             //loghelper.log("FileNotFoundException: " +
  +             //   ze.getName(), Logger.ERROR );
  +             throw ex;
  +         }
  +     }
  +
       }
   
   }
  
  
  
  1.16      +22 -142   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.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- Main.java 2000/12/12 00:42:49     1.15
  +++ Main.java 2000/12/28 23:19:50     1.16
  @@ -67,37 +67,20 @@
   import java.net.*;
   
   import org.apache.tomcat.util.SimpleClassLoader;
  +import org.apache.tomcat.util.IntrospectionUtils;
   
  -// XXX there is a nice trick to "guess" TOMCAT_HOME from
  -// classpath - you open each component of cp and check if
  -// it contains this file. When you find it just take the
  -// path and use it.
  +// Depends:
  +// JDK1.1
  +// tomcat.util.IntrospectionUtils, SimpleClassLoader
   
  -// Since the .sh will need to include it in CP probably it
  -// already know where it is.
  -
  -
  -// <b> Thanks ANT </b>
  -
   /**
  - * Starter for Tomcat. This is the standalone started - the only component that is
  - * part of the system CLASSPATH. It will process command line options and
  - * load the right jars ( like JAXP and anything else required to start tomcat).
  - *
  - * This is a replacement for all the scripting we use to do in tomcat.sh and 
tomcat.bat.
  + * Starter for Tomcat.
    *
  - * This class have (only) the following dependencies( that need to be included in 
the
  - * same jar): 
  - *  - org.apache.tomcat.util.SimpleClassLoader - for JDK1.1
  + * This is a replacement/enhancement for the .sh and .bat files - you can
  + * use JDK1.2 "java -jar tomcat.jar", or ( for jdk 1.1 ) you just need to
  + * include a single jar file in the classpath.
    *
  - *
  - * <b>Starting tomcat</b>. 
  - * Add tcstarter.jar to CLASSPATH
  - * Launch org.apache.tomcat.startup.Tomcat with TOMCAT_HOME parameter
  - * pointing to the tomcat install directory.
  - * 
  - * 
  - * @author Costin
  + * @author Costin Manolache
    */
   public class Main {
       String installDir;
  @@ -109,15 +92,6 @@
       // null means user didn't set one
       String configFile;
       
  -    static boolean jdk12=false;
  -    static {
  -     try {
  -         Class.forName( "java.security.PrivilegedAction" );
  -         jdk12=true;
  -     } catch(Throwable ex ) {
  -     }
  -    }
  -
       public Main() {
       }
   
  @@ -137,39 +111,6 @@
   
       // -------------------- Guess tomcat.home --------------------
   
  -    public String guessTomcatHome() {
  -     // If -Dtomcat.home is used - Great
  -     String h=System.getProperty( "tomcat.home" );
  -     if( h!=null ) return h;
  -
  -     // Find the directory where tomcat.jar is located
  -     
  -     String cpath=System.getProperty( "java.class.path");
  -     String pathSep=System.getProperty( "path.separator");
  -     StringTokenizer st=new StringTokenizer( cpath, pathSep );
  -     while( st.hasMoreTokens() ) {
  -         String path=st.nextToken();
  -         //      log( "path " + path );
  -         if( path.endsWith( "tomcat.jar" ) ) {
  -             h=path.substring( 0, path.length() - "tomcat.jar".length() );
  -             //              log( "Path1 " + h );
  -             try {
  -                 File f=new File( h );
  -                 File f1=new File ( h, "..");
  -                 //    log( "Path2 " + f1 );
  -                 h = f1.getCanonicalPath();
  -                 //log( "Guessed " + h + " from " + path );
  -                 System.getProperties().put( "tomcat.home", h );
  -                 return h;
  -             } catch( Exception ex ) {
  -                 ex.printStackTrace();
  -             }
  -         }
  -     }
  -
  -     return null;
  -    }
  -    
       
       // -------------------- Utils --------------------
       
  @@ -203,34 +144,14 @@
       public String getLibDir() {
        if( libBase!=null ) return libBase;
   
  -     String pkg=guessTomcatHome();
  +     String pkg=IntrospectionUtils.guessHome("tomcat.home", "tomcat.jar");
  +     System.out.println("Guessed home=" + pkg);
        if( pkg!=null ) setLibDir( pkg + "/lib");
        else setLibDir("./lib");
        return libBase;
       }
   
  -    ClassLoader getURLClassLoader( URL urls[], ClassLoader parent )
  -     throws Exception
  -    {
  -     Class urlCL=Class.forName( "java.net.URLClassLoader");
  -     Class paramT[]=new Class[2];
  -     paramT[0]= urls.getClass();
  -     paramT[1]=ClassLoader.class;
  -     Method m=urlCL.getMethod( "newInstance", paramT);
  -
  -     ClassLoader cl=(ClassLoader)m.invoke( urlCL,
  -                                           new Object[] { urls, parent } );
  -     return cl;
  -    }
  -     
       
  -/*    String cpComp[]=new String[] { "../classes/", "jaxp.jar",
  -                                "parser.jar", "jasper.jar",
  -                                "webserver.jar",
  -                                   "tomcat_core.jar", "tomcat_util.jar",
  -                                   "tomcat_modules.jar", "tomcat_config.jar",
  -                                "facade.jar", "servlet.jar"};
  -*/
       void execute( String args[] ) throws Exception {
   
        try {
  @@ -272,67 +193,25 @@
            System.out.println("ParentL " + parentL );
   
            ClassLoader cl=null;
  -         if( jdk12 )
  -             cl= getURLClassLoader( urls, parentL );
  -         else
  +         cl= IntrospectionUtils.getURLClassLoader( urls, parentL );
  +         if( cl==null )
                cl=new SimpleClassLoader(urls, parentL);
   
  +         
  +         Class cls=cl.loadClass("org.apache.tomcat.startup.Tomcat");
  +         Object proxy=cls.newInstance();
            
  -         Object proxy=instantiate( cl, 
  -                                   "org.apache.tomcat.task.StartTomcat");
            processArgs( proxy, args );
  -         setAttribute( proxy, "parentClassLoader", parentL );
  +         IntrospectionUtils.setAttribute( proxy,
  +                                          "parentClassLoader", parentL );
            //      setAttribute( proxy, "serverClassPath", urls );
  -         execute(  proxy, "execute" );
  +         IntrospectionUtils.execute(  proxy, "execute" );
            return;
        } catch( Exception ex ) {
            ex.printStackTrace();
        }
       }
   
  -    /** Create an instance of the target task
  -     */
  -    Object instantiate( ClassLoader cl, String classN  ) throws Exception {
  -     Class sXmlC=cl.loadClass(classN );
  -     return sXmlC.newInstance();
  -    }
  -
  -    /** 
  -     Call void setAttribute( String ,Object )
  -    */
  -    void setAttribute( Object proxy, String n, Object v) throws Exception {
  -     Method executeM=null;
  -     Class c=proxy.getClass();
  -     Class params[]=new Class[2];
  -     params[0]= String.class;
  -     params[1]= Object.class;
  -     executeM=c.getMethod( "setAttribute", params );
  -     if( executeM == null ) {
  -         log("No setAttribute in " + proxy.getClass() );
  -         return;
  -     }
  -     log( "Setting " + n + "=" + v + "  in " + proxy);
  -     executeM.invoke(proxy, new Object[] { n, v });
  -     return; 
  -    }
  -
  -    /** Call execute() - any ant-like task should work
  -     */
  -    void execute( Object proxy, String method  ) throws Exception {
  -     Method executeM=null;
  -     Class c=proxy.getClass();
  -     Class params[]=new Class[0];
  -     //      params[0]=args.getClass();
  -     executeM=c.getMethod( method, params );
  -     if( executeM == null ) {
  -         log("No execute in " + proxy.getClass() );
  -         return;
  -     }
  -     log( "Calling proxy ");
  -     executeM.invoke(proxy, null );//new Object[] { args });
  -     return; 
  -    }
  -
       // -------------------- Command-line args processing --------------------
       /* Later
          static class Arg {
  @@ -360,7 +239,7 @@
   
            for( int j=0; j< args0.length ; j++ ) {
                if( args0[j].equalsIgnoreCase( arg )) {
  -                 setAttribute( proxy, args0[j], "true");
  +                 IntrospectionUtils.setAttribute( proxy, args0[j], "true");
                    break;
                }
            }
  @@ -368,7 +247,8 @@
                if( args1[j].equalsIgnoreCase( arg )) {
                    i++;
                    if( i < args.length )
  -                     setAttribute( proxy, args1[j], args[i]);
  +                     IntrospectionUtils.setAttribute( proxy,
  +                                                      args1[j], args[i]);
                    break;
                }
            }
  
  
  
  1.46      +8 -4      jakarta-tomcat/src/share/org/apache/tomcat/startup/Tomcat.java
  
  Index: Tomcat.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/startup/Tomcat.java,v
  retrieving revision 1.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- Tomcat.java       2000/12/28 21:08:06     1.45
  +++ Tomcat.java       2000/12/28 23:19:51     1.46
  @@ -34,7 +34,7 @@
       // relative to TOMCAT_HOME 
       static final String DEFAULT_CONFIG="conf/server.xml";
       
  -    Tomcat() {
  +    public Tomcat() {
        super("tc_log");
       }
   
  @@ -77,12 +77,16 @@
        
        tcat.start();
       }
  +
  +    public void setAction(String s ) {
  +     action=s;
  +    }
       
       public static void main(String args[] ) {
        try {
            Tomcat tomcat=new Tomcat();
  -         if( ! tcat.processArgs( args )) {
  -             action="help";
  +         if( ! tomcat.processArgs( args )) {
  +             tomcat.setAction("help");
            }
            tomcat.execute();
        } catch(Exception ex ) {
  @@ -117,7 +121,7 @@
            } else if (arg.equals("-stop")) {
                action="stop";
            } else if (arg.equals("-g") || arg.equals("-generateConfigs")) {
  -             doGenerate=true;
  +             // config generation is now a module. //doGenerate=true;
            } else if (arg.equals("-f") || arg.equals("-config")) {
                i++;
                if( i < args.length )
  
  
  
  1.4       +5 -3      jakarta-tomcat/src/share/org/apache/tomcat/task/Expand.java
  
  Index: Expand.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/task/Expand.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Expand.java       2000/09/29 07:01:45     1.3
  +++ Expand.java       2000/12/28 23:19:54     1.4
  @@ -56,7 +56,7 @@
   
   import java.io.*;
   import java.util.zip.*;
  -import org.apache.tomcat.util.log.*;
  +//import org.apache.tomcat.util.log.*;
   
   /**
    * Unzip a file. "Imported" from Ant, with small adaptations.
  @@ -67,7 +67,7 @@
       private String dest; // req
       private String source; // req
   
  -    Log loghelper = new Log("tc_log", this);    
  +    //    Log loghelper = new Log("tc_log", this);    
       
       /**
        * Do the work.
  @@ -103,7 +103,9 @@
                    fos.close();
                }
            } catch( FileNotFoundException ex ) {
  -             loghelper.log("FileNotFoundException: " +  ze.getName(), Logger.ERROR 
);
  +             //loghelper.log("FileNotFoundException: " +
  +             //   ze.getName(), Logger.ERROR );
  +             throw ex;
            }
        }
       }
  
  
  
  1.3       +100 -24   
jakarta-tomcat/src/share/org/apache/tomcat/util/IntrospectionUtils.java
  
  Index: IntrospectionUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/IntrospectionUtils.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- IntrospectionUtils.java   2000/11/06 15:12:32     1.2
  +++ IntrospectionUtils.java   2000/12/28 23:19:56     1.3
  @@ -60,40 +60,116 @@
   
   package org.apache.tomcat.util;
   import java.lang.reflect.*;
  +import java.net.*;
  +import java.io.*;
  +import java.util.*;
   
  +// Depends: JDK1.1
  +
   /**
    *  Utils for introspection and reflection
    */
   public final class IntrospectionUtils {
  +
  +    /** Call execute() - any ant-like task should work
  +     */
  +    public static void execute( Object proxy, String method  )
  +     throws Exception
  +    {
  +     Method executeM=null;
  +     Class c=proxy.getClass();
  +     Class params[]=new Class[0];
  +     //      params[0]=args.getClass();
  +     executeM=c.getMethod( method, params );
  +     if( executeM == null ) {
  +         throw new RuntimeException("No execute in " + proxy.getClass() );
  +     }
  +     executeM.invoke(proxy, null );//new Object[] { args });
  +    }
  +
  +    /** 
  +     *  Call void setAttribute( String ,Object )
  +     */
  +    public static void setAttribute( Object proxy, String n, Object v)
  +     throws Exception
  +    {
  +     Method executeM=null;
  +     Class c=proxy.getClass();
  +     Class params[]=new Class[2];
  +     params[0]= String.class;
  +     params[1]= Object.class;
  +     executeM=c.getMethod( "setAttribute", params );
  +     if( executeM == null ) {
  +         System.out.println("No setAttribute in " + proxy.getClass() );
  +         return;
  +     }
  +     if( false )
  +         System.out.println("Setting " + n + "=" + v + "  in " + proxy);
  +     executeM.invoke(proxy, new Object[] { n, v });
  +     return; 
  +    }
   
  -    /** Test if the interceptor implements a particular
  -     *  method
  +    /** Construct a URLClassLoader. Will compile and work in JDK1.1 too.
        */
  -    public static boolean hasHook( Object obj, String methodN ) {
  +    public static ClassLoader getURLClassLoader( URL urls[],
  +                                              ClassLoader parent )
  +    {
        try {
  -         Method myMethods[]=obj.getClass().getMethods();
  -         for( int i=0; i< myMethods.length; i++ ) {
  -             if( methodN.equals ( myMethods[i].getName() )) {
  -                 // check if it's overriden
  -                 Class declaring=myMethods[i].getDeclaringClass();
  -                 Class parentOfDeclaring=declaring.getSuperclass();
  -                 // this works only if the base class doesn't extend
  -                 // another class.
  -
  -                 // if the method is declared in a top level class
  -                 // like BaseInterceptor parent is Object, otherwise
  -                 // parent is BaseInterceptor or an intermediate class
  -                 if( ! "java.lang.Object".
  -                     equals(parentOfDeclaring.getName() )) {
  -                     return true;
  -                 }
  -             }
  -         }
  -     } catch ( Exception ex ) {
  +         Class urlCL=Class.forName( "java.net.URLClassLoader");
  +         Class paramT[]=new Class[2];
  +         paramT[0]= urls.getClass();
  +         paramT[1]=ClassLoader.class;
  +         Method m=urlCL.getMethod( "newInstance", paramT);
  +         
  +         ClassLoader cl=(ClassLoader)m.invoke( urlCL,
  +                                               new Object[] { urls,
  +                                                              parent } );
  +         return cl;
  +     } catch(ClassNotFoundException ex ) {
  +         // jdk1.1
  +         return null;
  +     } catch(Exception ex ) {
            ex.printStackTrace();
  +         return null;
        }
  -     return false;
       }
  +
  +
  +    /** Guess a product home by analyzing the class path.
  +     *  It works for product using the pattern: lib/executable.jar
  +     *  or if executable.jar is included in classpath by a shell
  +     *  script. ( java -jar also works )
  +     */
  +    public static String guessHome(String systemProperty, String jarName) {
  +     String h=null;
  +     
  +     if( systemProperty != null )
  +         h=System.getProperty( systemProperty );
  +     
  +     if( h!=null ) return h;
   
  -    
  +     // Find the directory where jarName.jar is located
  +     
  +     String cpath=System.getProperty( "java.class.path");
  +     String pathSep=System.getProperty( "path.separator");
  +     StringTokenizer st=new StringTokenizer( cpath, pathSep );
  +     while( st.hasMoreTokens() ) {
  +         String path=st.nextToken();
  +         //      log( "path " + path );
  +         if( path.endsWith( jarName ) ) {
  +             h=path.substring( 0, path.length() - jarName.length() );
  +             try {
  +                 File f=new File( h );
  +                 File f1=new File ( h, "..");
  +                 h = f1.getCanonicalPath();
  +                 if( systemProperty != null )
  +                     System.getProperties().put( systemProperty, h );
  +                 return h;
  +             } catch( Exception ex ) {
  +                 ex.printStackTrace();
  +             }
  +         }
  +     }
  +     return null;
  +    }
   }
  
  
  

Reply via email to