1 25 26 package org.objectweb.common; 27 28 import java.io.BufferedReader ; 29 import java.io.IOException ; 30 import java.io.InputStreamReader ; 31 import java.io.PrintStream ; 32 import java.util.Enumeration ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Vector ; 36 37 import org.objectweb.util.monolog.api.BasicLevel; 38 39 49 50 public class Cmd { 51 52 55 private static final String COMPILER_CLASS_NAME = "com.sun.tools.javac.Main"; 56 59 private static final String RMICOMPILER_CLASS_NAME = "sun.rmi.rmic.Main"; 60 61 64 private Vector mCmd = new Vector (); 65 66 69 private String [] mEnv = null; 70 71 74 private boolean tryToInvoke = false; 75 76 82 public Cmd(String cmd, boolean invoke) { 83 mCmd.addElement(cmd); 84 tryToInvoke = invoke; 85 } 86 87 91 public Cmd(String cmd) { 92 this(cmd, false); 93 } 94 95 101 public Cmd(String cmd, String [] env, boolean invoke) { 102 mCmd.addElement(cmd); 103 mEnv = env; 104 tryToInvoke = invoke; 105 } 106 107 111 public void addArgument(String arg) { 112 mCmd.addElement(arg); 113 } 114 115 119 public void addArguments(List args) { 120 for (Iterator it = args.iterator(); it.hasNext();) { 121 mCmd.addElement(it.next()); 122 } 123 } 124 125 131 public boolean run() { 132 133 if (tryToInvoke && (((String ) mCmd.get(0)).endsWith("javac"))) { 134 return compile(); 136 } 137 if (tryToInvoke && (((String ) mCmd.get(0)).endsWith("rmic"))) { 138 return rmicompile(); 139 } 140 141 RunnableStreamListener out, err; 142 Process proc = null; 143 boolean val = true; 144 try { 145 String [] cmd = new String [mCmd.size()]; 146 mCmd.copyInto(cmd); 147 proc = Runtime.getRuntime().exec(cmd, mEnv); 148 } catch (IOException e) { 149 TraceCore.logger.log(BasicLevel.ERROR, "exception", e); 150 return (false); 151 } 152 out = new RunnableStreamListener(new BufferedReader (new InputStreamReader (proc.getInputStream())), System.out); 153 new Thread (out, "stdout listener for " + this.toString()).start(); 154 err = new RunnableStreamListener(new BufferedReader (new InputStreamReader (proc.getErrorStream())), System.err); 155 new Thread (err, "stderr listener for " + this.toString()).start(); 156 try { 157 val = proc.waitFor() == 0; 158 } catch (InterruptedException e) { 159 TraceCore.logger.log(BasicLevel.ERROR, "exception", e); 160 val = false; 161 } finally { 162 if (proc != null) { 164 proc.destroy(); 165 } 166 } 167 168 return (val); 169 } 170 171 175 private boolean compile() { 176 String [] args = new String [mCmd.size() - 1]; 178 for (int i = 0; i < mCmd.size() - 1; i++) { 179 args[i] = (String ) mCmd.get(i + 1); 180 } 181 182 try { 183 Class c = Class.forName(COMPILER_CLASS_NAME); 185 Object compiler = c.newInstance(); 186 java.lang.reflect.Method compile = c.getMethod("compile", new Class [] {(new String [] {}).getClass()}); 187 int result = ((Integer ) compile.invoke(compiler, new Object [] {args})).intValue(); 188 return (result == 0); 189 } catch (Exception e) { 190 TraceCore.logger.log(BasicLevel.ERROR, "exception", e); 191 return false; 192 } 193 } 194 195 199 private boolean rmicompile() { 200 String [] args = new String [mCmd.size() - 1]; 202 for (int i = 0; i < mCmd.size() - 1; i++) { 203 args[i] = (String ) mCmd.get(i + 1); 204 } 205 206 try { 207 Class c = Class.forName(RMICOMPILER_CLASS_NAME); 209 Object compiler = c.newInstance(); 210 java.lang.reflect.Method compile = c.getMethod("compile", new Class [] {(new String [] {}).getClass()}); 211 int result = ((Integer ) compile.invoke(compiler, new Object [] {args})).intValue(); 212 return (result == 0); 213 } catch (Exception e) { 214 TraceCore.logger.log(BasicLevel.ERROR, "exception", e); 215 return false; 216 } 217 } 218 219 222 public Iterator getCommandLine() { 223 return mCmd.iterator(); 224 } 225 226 229 public String toString() { 230 StringBuffer buf = new StringBuffer (); 231 Enumeration e = mCmd.elements(); 232 while (e.hasMoreElements()) { 233 String arg = (String ) e.nextElement(); 234 if (arg == null) { 235 arg = "null"; 236 } 237 for (int i = 0; i < arg.length(); i++) { 238 if (Character.isWhitespace(arg.charAt(i))) { 239 arg = "\"" + arg + "\""; 240 break; 241 } 242 } 243 buf.append(arg); 244 buf.append(' '); 245 } 246 return buf.toString().trim(); 247 } 248 249 } 250 251 254 class RunnableStreamListener implements Runnable { 255 256 BufferedReader in_; 257 258 PrintStream stream_; 259 260 RunnableStreamListener(BufferedReader in, PrintStream stream) { 261 in_ = in; 262 stream_ = stream; 263 } 264 265 public void run() { 266 String line; 267 try { 268 while ((line = in_.readLine()) != null) { 269 stream_.println(line); 270 } 271 } catch (IOException e) { 272 stream_.println(e.toString()); 273 } 274 275 } 276 } 277 278 | Popular Tags |