ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From co...@apache.org
Subject cvs commit: jakarta-ant/proposal/mutant/src/java/start/org/apache/ant/start Main.java
Date Wed, 03 Apr 2002 15:49:06 GMT
conor       02/04/03 07:49:06

  Modified:    proposal/mutant build.xml
               proposal/mutant/src/java/start/org/apache/ant/start
                        Main.java
  Log:
  Extract front end class info from Jar
  
  Revision  Changes    Path
  1.18      +6 -2      jakarta-ant/proposal/mutant/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/mutant/build.xml,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -w -u -r1.17 -r1.18
  --- build.xml	3 Apr 2002 15:02:47 -0000	1.17
  +++ build.xml	3 Apr 2002 15:49:06 -0000	1.18
  @@ -84,7 +84,11 @@
       <javac destdir="${bin.dir}/cli" srcdir="${java.dir}/cli" debug="${debug}">
         <classpath refid="classpath.cli"/>
       </javac>
  -    <jar basedir="${bin.dir}/cli" jarfile="${distlib.dir}/frontend/cli.jar"/>
  +    <jar basedir="${bin.dir}/cli" jarfile="${distlib.dir}/frontend/cli.jar">
  +      <manifest>
  +        <attribute name="Main-Class" value="org.apache.ant.cli.Commandline"/>
  +      </manifest>
  +    </jar>
     </target>
   
     <target name="start" depends="init">
  @@ -199,7 +203,7 @@
       <mkdir dir="${javadocs.dir}"/>
       <javadoc packagenames="org.apache.*"
                useexternalfile="yes"
  -             sourcepath="${java.dir}/antcore:${java.dir}/init:${java.dir}/common:${java.dir}/frontend:${java.dir}/start"
  +             sourcepath="${java.dir}/antcore:${java.dir}/init:${java.dir}/common:${java.dir}/cli:${java.dir}/start"
                destdir="${javadocs.dir}"
                author="true" 
                private ="true"
  
  
  
  1.9       +41 -3     jakarta-ant/proposal/mutant/src/java/start/org/apache/ant/start/Main.java
  
  Index: Main.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/mutant/src/java/start/org/apache/ant/start/Main.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -w -u -r1.8 -r1.9
  --- Main.java	3 Apr 2002 15:02:48 -0000	1.8
  +++ Main.java	3 Apr 2002 15:49:06 -0000	1.9
  @@ -53,9 +53,13 @@
    */
   package org.apache.ant.start;
   
  +import java.io.IOException;
   import java.lang.reflect.Method;
   import java.net.URL;
   import java.net.URLClassLoader;
  +import java.util.jar.Manifest;
  +import java.util.jar.Attributes;
  +import java.util.jar.JarInputStream;
   import org.apache.ant.init.InitConfig;
   
   /**
  @@ -67,10 +71,9 @@
    */
   public class Main {
       /** The actual class that implements the command line front end. */
  -    public static final String COMMANDLINE_CLASS
  +    public static final String DEFAULT_COMMANDLINE_CLASS
            = "org.apache.ant.cli.Commandline";
   
  -         
       /** The default front end name */
       public static final String DEFAULT_FRONTEND = "cli";
            
  @@ -103,8 +106,14 @@
               //System.out.println("Front End Loader config");
               //LoaderUtils.dumpLoader(System.out, frontEndLoader);
   
  +            String mainClass = getMainClass(frontendJar);
  +            System.out.println("Front end main class = " + mainClass);
  +            if (mainClass == null) {
  +                mainClass = DEFAULT_COMMANDLINE_CLASS;
  +            }
  +            
               // Now start the front end by reflection.
  -            Class commandLineClass = Class.forName(COMMANDLINE_CLASS, true,
  +            Class commandLineClass = Class.forName(mainClass, true,
                   frontEndLoader);
   
               final Class[] param = {Class.forName("[Ljava.lang.String;"),
  @@ -115,6 +124,35 @@
               startMethod.invoke(null, argument);
           } catch (Exception e) {
               e.printStackTrace();
  +        }
  +    }
  +    
  +    /**
  +     * Pick up the main class from a jar's manifest
  +     *
  +     * @param jarURL the URL to the jar
  +     * @return the jar's main-class or null if it is not specified or cannot be 
  +     *         determined.
  +     */
  +    private String getMainClass(URL jarURL) {
  +        try {
  +            JarInputStream stream = null;
  +            try {
  +                stream = new JarInputStream(jarURL.openStream());
  +                Manifest manifest = stream.getManifest();
  +                if (manifest == null) {
  +                    return null;
  +                }
  +                Attributes mainAttributes = manifest.getMainAttributes();
  +                return mainAttributes.getValue("Main-Class");
  +            } finally {
  +                if (stream != null) {
  +                    stream.close();
  +                }
  +            }
  +        } catch (IOException e) {
  +                // ignore
  +            return null;
           }
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message