author | vasya |
Mon, 14 Dec 2015 20:18:19 +0100 | |
changeset 34752 | 9c262a013456 |
parent 32542 | f4e4f4c4f9f4 |
child 36161 | a025c0619f25 |
permissions | -rw-r--r-- |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
1 |
/* |
32335 | 2 |
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
4 |
* |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
10 |
* |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
15 |
* accompanied this code). |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
16 |
* |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
20 |
* |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
23 |
* questions. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
24 |
*/ |
34752
9c262a013456
8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents:
32542
diff
changeset
|
25 |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
26 |
package com.sun.tools.sjavac.server; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
27 |
|
32542
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
28 |
import static com.sun.tools.sjavac.server.SjavacServer.LINE_TYPE_RC; |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
29 |
import static com.sun.tools.sjavac.server.SjavacServer.LINE_TYPE_STDERR; |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
30 |
import static com.sun.tools.sjavac.server.SjavacServer.LINE_TYPE_STDOUT; |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
31 |
|
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
32 |
import java.io.BufferedReader; |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
33 |
import java.io.InputStreamReader; |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
34 |
import java.io.PrintWriter; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
35 |
import java.io.StringWriter; |
32542
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
36 |
import java.io.Writer; |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
37 |
import java.net.Socket; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
38 |
|
32542
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
39 |
import com.sun.tools.sjavac.AutoFlushWriter; |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
40 |
import com.sun.tools.sjavac.Log; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
41 |
|
32542
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
42 |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
43 |
/** |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
44 |
* A RequestHandler handles requests performed over a socket. Specifically it |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
45 |
* - Reads the command string specifying which method is to be invoked |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
46 |
* - Reads the appropriate arguments |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
47 |
* - Delegates the actual invocation to the given sjavac implementation |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
48 |
* - Writes the result back to the socket output stream |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
49 |
* |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
50 |
* None of the work performed by this class is really bound by the CPU. It |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
51 |
* should be completely fine to have a large number of RequestHandlers active. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
52 |
* To limit the number of concurrent compilations, use PooledSjavac. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
53 |
* |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
54 |
* <p><b>This is NOT part of any supported API. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
55 |
* If you write code that depends on this, you do so at your own risk. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
56 |
* This code and its internal interfaces are subject to change or |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
57 |
* deletion without notice.</b> |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
58 |
*/ |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
59 |
public class RequestHandler implements Runnable { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
60 |
|
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
61 |
private final Socket socket; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
62 |
private final Sjavac sjavac; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
63 |
|
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
64 |
public RequestHandler(Socket socket, Sjavac sjavac) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
65 |
this.socket = socket; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
66 |
this.sjavac = sjavac; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
67 |
} |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
68 |
|
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
69 |
@Override |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
70 |
public void run() { |
32542
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
71 |
try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
72 |
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) { |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
73 |
|
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
74 |
// Read argument array |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
75 |
int n = Integer.parseInt(in.readLine()); |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
76 |
String[] args = new String[n]; |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
77 |
for (int i = 0; i < n; i++) { |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
78 |
args[i] = in.readLine(); |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
79 |
} |
32542
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
80 |
|
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
81 |
// Perform compilation |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
82 |
Writer stdout = new LinePrefixFilterWriter(new AutoFlushWriter(out), LINE_TYPE_STDOUT + ":"); |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
83 |
Writer stderr = new LinePrefixFilterWriter(new AutoFlushWriter(out), LINE_TYPE_STDERR + ":"); |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
84 |
int rc = sjavac.compile(args, stdout, stderr); |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
85 |
stdout.flush(); |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
86 |
stderr.flush(); |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
87 |
|
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
88 |
// Send return code back to client |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
89 |
out.println(LINE_TYPE_RC + ":" + rc); |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
90 |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
91 |
} catch (Exception ex) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
92 |
// Not much to be done at this point. The client side request |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
93 |
// code will most likely throw an IOException and the |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
94 |
// compilation will fail. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
95 |
StringWriter sw = new StringWriter(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
96 |
ex.printStackTrace(new PrintWriter(sw)); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
97 |
Log.error(sw.toString()); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
98 |
} |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
99 |
} |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
100 |
} |