33
33
import saker .build .thirdparty .saker .util .io .IOUtils ;
34
34
import saker .build .thirdparty .saker .util .io .ProcessUtils ;
35
35
import saker .build .thirdparty .saker .util .io .StreamUtils ;
36
- import saker .build .thirdparty .saker .util .io .UnsyncByteArrayOutputStream ;
37
36
import saker .java .compiler .impl .compile .handler .incremental .RemoteJavaCompilerCacheKey ;
38
37
import saker .java .compiler .impl .util .RMICompatUtil ;
39
38
@@ -54,31 +53,39 @@ public RemoteJavaRMIProcess(List<String> commands, String workingdirectory, Clas
54
53
}
55
54
proc = pb .start ();
56
55
57
- final int port ;
58
-
56
+ int port = 0 ;
59
57
try {
60
58
InputStream procin = proc .getInputStream ();
61
- try (UnsyncByteArrayOutputStream portnumbuf = new UnsyncByteArrayOutputStream ()) {
62
- while (true ) {
63
- int r = procin .read ();
64
- if (r < 0 || r == '\n' || r == '\r' ) {
65
- break ;
66
- }
67
- portnumbuf .write (r );
59
+ while (true ) {
60
+ int r = procin .read ();
61
+ if (r < 0 ) {
62
+ throw new IOException (
63
+ "Failed to read port number from remote Java compiler process, received EOF on standard output." );
64
+ }
65
+ if (r == '\n' || r == '\r' ) {
66
+ //reached end of port number value
67
+ break ;
68
+ }
69
+ if (r < '0' || r > '9' ) {
70
+ throw new IOException ("Invalid character read for port number from remote Java compiler process, port: "
71
+ + port + " char: 0x" + Integer .toHexString (r ));
68
72
}
69
- if ( portnumbuf . isEmpty ()) {
70
- //XXX reify exception?
71
- throw new IOException ("Failed to read port number." );
73
+ port = port * 10 + ( r - '0' );
74
+ if ( port > 0xFFFF ) {
75
+ throw new IOException ("Invalid port number read from remote Java compiler process: " + port );
72
76
}
73
- port = Integer .parseInt (portnumbuf .toString ());
77
+ }
78
+ if (port == 0 ) {
79
+ //XXX reify exception?
80
+ throw new IOException ("Failed to read port number from remote Java compiler process." );
74
81
}
75
82
address = new InetSocketAddress (InetAddress .getLoopbackAddress (), port );
76
83
RMIOptions options = new RMIOptions ().classLoader (cloader ).transferProperties (rmiproperties )
77
84
.workerThreadGroup (connectionThreadGroup );
78
85
if (RemoteJavaCompilerCacheKey .COLLECT_RMI_STATS ) {
79
86
options .collectStatistics (true );
80
87
}
81
- connection = options . connect ( address );
88
+ connection = RMICompatUtil . connectWithRMISocketConfiguration ( options , address );
82
89
if (RemoteJavaCompilerCacheKey .COLLECT_RMI_STATS ) {
83
90
RMICompatUtil .addDumpCloseListener (connection );
84
91
}
@@ -92,10 +99,21 @@ public void run() {
92
99
} catch (Exception e ) {
93
100
waitDestroyForcibly ();
94
101
printProcessStreams ();
95
- throw new IOException (
96
- "Failed to connect to remote Java process." + (address == null ? "" : " (" + address + ")" )
97
- + " (Exit code: " + ProcessUtils .getExitCodeIfExited (proc ) + ")" ,
98
- e );
102
+ StringBuilder sb = new StringBuilder ("Failed to connect to remote Java compiler process." );
103
+ if (address != null ) {
104
+ sb .append (" (" );
105
+ sb .append (address );
106
+ sb .append (")" );
107
+ }
108
+ sb .append (" (Exit code: " );
109
+ sb .append (ProcessUtils .getExitCodeIfExited (proc ));
110
+ sb .append (")" );
111
+ if (port != 0 ) {
112
+ sb .append (" (Port: " );
113
+ sb .append (port );
114
+ sb .append (")" );
115
+ }
116
+ throw new IOException (sb .toString (), e );
99
117
}
100
118
}
101
119
@@ -152,22 +170,33 @@ public boolean isValid() {
152
170
}
153
171
154
172
private void printProcessStreams () {
173
+ IOException e1 = null ;
174
+ IOException e2 = null ;
155
175
String cmd = StringUtils .toStringJoin ("\" " , "\" \" " , commands , "\" " );
156
- System .err .println (" ---- StdOut from command: " + cmd );
157
- try {
158
- StreamUtils .copyStream (proc .getInputStream (), System .err );
159
- } catch (IOException e ) {
160
- e .printStackTrace ();
161
- }
162
- System .err .println ();
163
- System .err .println (" ---- StdErr from command: " + cmd );
164
- try {
165
- StreamUtils .copyStream (proc .getErrorStream (), System .err );
166
- } catch (IOException e ) {
167
- e .printStackTrace ();
176
+ synchronized (System .err ) {
177
+ System .err .println (" ---- StdOut from command: " + cmd );
178
+ try {
179
+ StreamUtils .copyStream (proc .getInputStream (), System .err );
180
+ } catch (IOException e ) {
181
+ e1 = e ;
182
+ }
183
+ System .err .println ();
184
+ System .err .println (" ---- StdErr from command: " + cmd );
185
+ try {
186
+ StreamUtils .copyStream (proc .getErrorStream (), System .err );
187
+ } catch (IOException e ) {
188
+ e2 = e ;
189
+ }
190
+ System .err .println ();
191
+ System .err .println (" -------- " );
192
+ if (e1 != null ) {
193
+ e1 .printStackTrace (System .err );
194
+ }
195
+ if (e2 != null ) {
196
+ e2 .printStackTrace (System .err );
197
+ }
168
198
}
169
- System .err .println ();
170
- System .err .println (" -------- " );
199
+
171
200
}
172
201
173
202
public RMIConnection getConnection () {
0 commit comments