1 |
/* |
/* |
2 |
Copyright (c) 2001 Free Software Foundation, Inc. |
Copyright (c) 2001, 2003 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
37 |
|
|
38 |
package gnu.java.rmi.rmic; |
package gnu.java.rmi.rmic; |
39 |
|
|
40 |
/** Subclass of Compiler that can be subclassed to invoke a process to |
import java.io.InputStream; |
41 |
* do its work. */ |
|
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 |
public abstract class CompilerProcess extends Compiler |
47 |
{ |
{ |
48 |
/** This is used to compute the command line for the process. */ |
/** This is used to compute the command line for the process. */ |
49 |
public abstract String[] computeArguments (String filename); |
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 |
|
*/ |
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 |
|
|
87 |
public void compile (String name) throws Exception |
public void compile (String name) throws Exception |
88 |
{ |
{ |
89 |
String[] args = computeArguments (name); |
String[] args = computeArguments (name); |
90 |
Process p = Runtime.getRuntime ().exec (args); |
Process p = Runtime.getRuntime ().exec (args); |
91 |
// FIXME: probably should collect compiler output here and then |
|
92 |
// put it into the exception message. |
/* 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; |
int result; |
106 |
while (true) |
while (true) |
107 |
{ |
{ |
117 |
if (result != 0) |
if (result != 0) |
118 |
{ |
{ |
119 |
// FIXME: wrong exception class. |
// FIXME: wrong exception class. |
120 |
throw new Exception ("compiler exited with status: " + result); |
throw new Exception ("compiler exited with status: " + result, |
121 |
|
new RMICException(stderr.toString())); |
122 |
} |
} |
123 |
} |
} |
124 |
} |
} |