1 /* 2 Copyright (c) 2001, 2003 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 6 GNU Classpath is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 GNU Classpath is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GNU Classpath; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 19 02111-1307 USA. 20 21 Linking this library statically or dynamically with other modules is 22 making a combined work based on this library. Thus, the terms and 23 conditions of the GNU General Public License cover the whole 24 combination. 25 26 As a special exception, the copyright holders of this library give you 27 permission to link this library with independent modules to produce an 28 executable, regardless of the license terms of these independent 29 modules, and to copy and distribute the resulting executable under 30 terms of your choice, provided that you also meet, for each linked 31 independent module, the terms and conditions of the license of that 32 module. An independent module is a module which is not derived from 33 or based on this library. If you modify this library, you may extend 34 this exception to your version of the library, but you are not 35 obligated to do so. If you do not wish to do so, delete this 36 exception statement from your version. */ 37 38 package gnu.java.rmi.rmic; 39 40 import java.io.InputStream; 41 42 /** 43 * Subclass of Compiler that can be subclassed to invoke a process to 44 * do its work. 45 */ 46 public abstract class CompilerProcess extends Compiler 47 { 48 /** This is used to compute the command line for the process. */ computeArguments(String filename)49 public abstract String[] computeArguments (String filename); 50 51 /** 52 * This is used to compute the command line for the process. 53 * Most compilers typically arrange their arguments as in 54 * <compiler name and arguments> <optional destination> <filename>. 55 * This method builds an argument array out that. It should be used 56 * to define computeArguments for those compilers that follow the 57 * argument convention described above. 58 */ computeTypicalArguments(String[] compilerArgs, String destination, String filename)59 public static String[] computeTypicalArguments(String[] compilerArgs, 60 String destination, String filename) 61 { 62 /* length of compiler specific arguments */ 63 final int len = compilerArgs.length; 64 65 /* length of returned array of arguments */ 66 final int arglen = len + (destination == null ? 0 : 2) + 1; 67 68 /* Allocate String array for computed arguments. */ 69 String [] args = new String[arglen]; 70 71 /* Fill in compiler arguments. */ 72 System.arraycopy(compilerArgs, 0, args, 0, len); 73 74 /* Fill in destination argument if necessary. */ 75 if (destination != null) 76 { 77 args[len] = "-d"; 78 args[len + 1] = destination; 79 } 80 81 /* Fill in filename */ 82 args[arglen - 1] = filename; 83 84 return args; 85 } 86 compile(String name)87 public void compile (String name) throws Exception 88 { 89 String[] args = computeArguments (name); 90 Process p = Runtime.getRuntime ().exec (args); 91 92 /* Print compiler output to System.out. */ 93 InputStream procin = p.getInputStream(); 94 for (int ch = procin.read(); ch != -1; ch = procin.read()) 95 System.out.print((char) ch); 96 97 /* Collect compiler error output in a buffer. 98 * If compilation fails, it will be used for an error message. 99 */ 100 StringBuffer stderr = new StringBuffer(); 101 InputStream procerr = p.getErrorStream(); 102 for (int ch = procerr.read(); ch != -1; ch = procerr.read()) 103 stderr.append((char) ch); 104 105 int result; 106 while (true) 107 { 108 try 109 { 110 result = p.waitFor (); 111 break; 112 } 113 catch (InterruptedException _) 114 { 115 } 116 } 117 if (result != 0) 118 { 119 // FIXME: wrong exception class. 120 throw new Exception ("compiler exited with status: " + result, 121 new RMICException(stderr.toString())); 122 } 123 } 124 } 125