001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    
005    package graphlab.plugins.automaticupdator.net.interdirected.autoupdate;
006    
007    import java.io.BufferedReader;
008    import java.io.File;
009    import java.io.InputStreamReader;
010    
011    /**
012     * AntLauncher  This is basically a copy of org.apache.tools.ant.launch.Launcher.  I needed the
013     * run method to be public, and I needed the parameters to be assigned in the instantiation of the
014     * class instead.  This just seemed to make more sense to me than statically running the MAIN from the
015     * original Launcher, or executing it as an exec from a batch file.
016     *
017     * @author Michael Quattlebaum
018     * @see org.apache.tools.ant.launch.Launcher
019     */
020    public class AntLauncher {
021        private String[] args;
022        private File runDir = new File(".");
023        private String cp = "";
024        private String antloc = ".";
025    
026        /**
027         * AppendArgs  This method is used to add arguments to the arguments array
028         * used in ANT.  Any argument that could be passed on the command line of
029         * an ant launch can be added as an argument here.  If a value is to be passed
030         * with the argument, the user must pass the argument flag first
031         * (with the "-" character included).  For example, to add the -lib
032         * parameter, you must call this method twice, once with "-lib" and
033         * once with the actual value of "./lib".
034         *
035         * @param add_argument Argument to add to the arguments array.
036         */
037        public void AppendArgs(String add_argument) {
038            int argslen = (args == null) ? 0 : args.length;
039            String[] newArgs = new String[argslen + 1];
040            int i = 0;
041            for (; i < argslen; i++) {
042                newArgs[i] = args[i];
043            }
044            newArgs[i] = add_argument;
045            args = newArgs;
046        }
047    
048        /**
049         * AppendArgs  This is a shortcut to the AppendArgs(String) method in case
050         * you have a parameter/value pair that need to be added.  It just calls
051         * AppendArgs(String) twice.
052         *
053         * @param add_argument
054         * @param add_value
055         */
056        public void AppendArgs(String add_argument, String add_value) {
057            this.AppendArgs(add_argument);
058            this.AppendArgs(add_value);
059        }
060    
061        /**
062         * ResetArgs  This method resets the arguments array to a null value.  This should
063         * not be used unless you need to eliminate the default library location (-lib ./lib)
064         * from the parameters.
065         */
066        public void ResetArgs() {
067            args = null;
068        }
069    
070        /**
071         * SetRunDirectory is used to set the base run directory.  From the AutomatedUpdate, it
072         * should under normal conditions be set to the application directory.
073         *
074         * @param run_dir The directory that ANT should launch from.
075         */
076        public void SetRunDirectory(String run_dir) {
077            runDir = new File(run_dir);
078            if (!(runDir.exists() && runDir.isDirectory())) {
079                System.out.println("Run directory does not exist. Setting to current directory instead.");
080                runDir = new File(".");
081            }
082        }
083    
084        /**
085         * SetListenerClass sets the listener class file for the ANT build.  The listener class will
086         * then be used to set the output stream so that it is used by the status window.
087         *
088         * @param listener A String with the name of the listening class.
089         */
090        public void SetListenerClass(String listener) {
091            AppendArgs("-listener", listener);
092        }
093    
094        public void SetBuildFile(String buildfile) {
095            AppendArgs("-buildfile", buildfile);
096        }
097    
098        public void SetAntLocation(String antlocation) {
099            antloc = antlocation;
100            AppendArgs("-lib", antlocation + File.separator + "lib");
101        }
102    
103        public String GetAntLocation() {
104            return antloc;
105        }
106    
107        public void SetClassPath(String classpath) {
108            if (classpath.substring(0, 0) != "\"") classpath = "\"" + classpath;
109            if (classpath.substring(classpath.length() - 1) != "\"") classpath = classpath + "\"";
110            cp = classpath;
111        }
112    
113        /**
114         * run This method is used to create a new Java JVM to handle org.apache.tools.ant.launch.Launcher
115         * and capture standard out and redirect it to the command line.
116         */
117        public BufferedReader run() throws Exception {
118            String commandStr = "java";
119            Runtime rt = Runtime.getRuntime();
120            String[] javaargs = {
121                    "-classpath",
122                    GetAntLocation() + File.separator + "ant-launcher.jar",
123                    "-cp",
124                    cp,
125                    "org.apache.tools.ant.launch.Launcher"
126            };
127            for (int i = 0; i < javaargs.length; i++) {
128                commandStr = commandStr + " " + javaargs[i];
129            }
130            for (int i = 0; i < args.length; i++) {
131                commandStr = commandStr + " " + args[i];
132            }
133            Process process = rt.exec(commandStr);
134            InputStreamReader reader= new InputStreamReader(process.getInputStream());
135                    return new BufferedReader(reader);
136            }
137    }