Cmd.java

00001 
00026 package org.objectweb.common;
00027 
00028 import java.io.BufferedReader;
00029 import java.io.IOException;
00030 import java.io.InputStreamReader;
00031 import java.io.PrintStream;
00032 import java.util.Enumeration;
00033 import java.util.Iterator;
00034 import java.util.List;
00035 import java.util.Vector;
00036 
00037 import org.objectweb.util.monolog.api.BasicLevel;
00038 
00050 public class Cmd {
00051 
00055     private static final String COMPILER_CLASS_NAME = "com.sun.tools.javac.Main";
00059     private static final String RMICOMPILER_CLASS_NAME = "sun.rmi.rmic.Main";
00060 
00064     private Vector mCmd = new Vector();
00065 
00069     private String[] mEnv = null;
00070 
00074     private boolean tryToInvoke = false;
00075 
00082     public Cmd(String cmd, boolean invoke) {
00083         mCmd.addElement(cmd);
00084         tryToInvoke = invoke;
00085     }
00086 
00091     public Cmd(String cmd) {
00092         this(cmd, false);
00093     }
00094 
00101     public Cmd(String cmd, String[] env, boolean invoke) {
00102         mCmd.addElement(cmd);
00103         mEnv = env;
00104         tryToInvoke = invoke;
00105     }
00106 
00111     public void addArgument(String arg) {
00112         mCmd.addElement(arg);
00113     }
00114 
00119     public void addArguments(List args) {
00120         for (Iterator it = args.iterator(); it.hasNext();) {
00121             mCmd.addElement(it.next());
00122         }
00123     }
00124 
00131     public boolean run() {
00132 
00133         if (tryToInvoke && (((String) mCmd.get(0)).endsWith("javac"))) {
00134             // Bug #100587 fixed to work around a limitation on Windows
00135             return compile();
00136         }
00137         if (tryToInvoke && (((String) mCmd.get(0)).endsWith("rmic"))) {
00138             return rmicompile();
00139         }
00140 
00141         RunnableStreamListener out, err;
00142         Process proc = null;
00143         boolean val = true;
00144         try {
00145             String[] cmd = new String[mCmd.size()];
00146             mCmd.copyInto(cmd);
00147             proc = Runtime.getRuntime().exec(cmd, mEnv);
00148         } catch (IOException e) {
00149             TraceCore.logger.log(BasicLevel.ERROR, "exception", e);
00150             return (false);
00151         }
00152         out = new RunnableStreamListener(new BufferedReader(new InputStreamReader(proc.getInputStream())), System.out);
00153         new Thread(out, "stdout listener for " + this.toString()).start();
00154         err = new RunnableStreamListener(new BufferedReader(new InputStreamReader(proc.getErrorStream())), System.err);
00155         new Thread(err, "stderr listener for " + this.toString()).start();
00156         try {
00157             val = proc.waitFor() == 0;
00158         } catch (InterruptedException e) {
00159             TraceCore.logger.log(BasicLevel.ERROR, "exception", e);
00160             val = false;
00161         }
00162 
00163         return (val);
00164     }
00165 
00170     private boolean compile() {
00171         // first arg should not be added because it's the name of the command
00172         String[] args = new String[mCmd.size() - 1];
00173         for (int i = 0; i < mCmd.size() - 1; i++) {
00174             args[i] = (String) mCmd.get(i + 1);
00175         }
00176 
00177         try {
00178             // Use reflection
00179             Class c = Class.forName(COMPILER_CLASS_NAME);
00180             Object compiler = c.newInstance();
00181             java.lang.reflect.Method compile = c.getMethod("compile", new Class[] {(new String[] {}).getClass()});
00182             int result = ((Integer) compile.invoke(compiler, new Object[] {args})).intValue();
00183             return (result == 0);
00184         } catch (Exception e) {
00185             TraceCore.logger.log(BasicLevel.ERROR, "exception", e);
00186             return false;
00187         }
00188     }
00189 
00194     private boolean rmicompile() {
00195         // first arg should not be added because it's the name of the command
00196         String[] args = new String[mCmd.size() - 1];
00197         for (int i = 0; i < mCmd.size() - 1; i++) {
00198             args[i] = (String) mCmd.get(i + 1);
00199         }
00200 
00201         try {
00202             // Use reflection
00203             Class c = Class.forName(RMICOMPILER_CLASS_NAME);
00204             Object compiler = c.newInstance();
00205             java.lang.reflect.Method compile = c.getMethod("compile", new Class[] {(new String[] {}).getClass()});
00206             int result = ((Integer) compile.invoke(compiler, new Object[] {args})).intValue();
00207             return (result == 0);
00208         } catch (Exception e) {
00209             TraceCore.logger.log(BasicLevel.ERROR, "exception", e);
00210             return false;
00211         }
00212     }
00213 
00214 
00218     public String toString() {
00219         StringBuffer buf = new StringBuffer();
00220         Enumeration e = mCmd.elements();
00221         while (e.hasMoreElements()) {
00222             String arg = (String) e.nextElement();
00223             if (arg == null) {
00224                 arg = "null";
00225             }
00226             for (int i = 0; i < arg.length(); i++) {
00227                 if (Character.isWhitespace(arg.charAt(i))) {
00228                     arg = "\"" + arg + "\"";
00229                     break;
00230                 }
00231             }
00232             buf.append(arg);
00233             buf.append(' ');
00234         }
00235         return buf.toString().trim();
00236     }
00237 
00238 }
00239 
00243 class RunnableStreamListener implements Runnable {
00244 
00245     BufferedReader in_;
00246 
00247     PrintStream stream_;
00248 
00249     RunnableStreamListener(BufferedReader in, PrintStream stream) {
00250         in_ = in;
00251         stream_ = stream;
00252     }
00253 
00254     public void run() {
00255         String line;
00256         try {
00257             while ((line = in_.readLine()) != null) {
00258                 stream_.println(line);
00259             }
00260         } catch (IOException e) {
00261             stream_.println(e.toString());
00262         }
00263     }
00264 }
00265 

Generated on Tue Feb 15 15:05:12 2005 for JOnAS by  doxygen 1.3.9.1