author | alundblad |
Fri, 04 Sep 2015 13:24:15 +0200 | |
changeset 32542 | f4e4f4c4f9f4 |
parent 32335 | 7df616378cf3 |
child 34752 | 9c262a013456 |
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 |
*/ |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
25 |
package com.sun.tools.sjavac.server; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
26 |
|
32542
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
27 |
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
|
28 |
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
|
29 |
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
|
30 |
|
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
31 |
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
|
32 |
import java.io.InputStreamReader; |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
33 |
import java.io.PrintWriter; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
34 |
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
|
35 |
import java.io.Writer; |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
36 |
import java.net.Socket; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
37 |
|
32542
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
38 |
import com.sun.tools.sjavac.AutoFlushWriter; |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
39 |
import com.sun.tools.sjavac.Log; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
40 |
|
32542
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
41 |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
42 |
/** |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
43 |
* 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
|
44 |
* - 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
|
45 |
* - Reads the appropriate arguments |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
46 |
* - 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
|
47 |
* - 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
|
48 |
* |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
49 |
* 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
|
50 |
* 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
|
51 |
* 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
|
52 |
* |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
53 |
* <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
|
54 |
* 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
|
55 |
* 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
|
56 |
* deletion without notice.</b> |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
57 |
*/ |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
58 |
public class RequestHandler implements Runnable { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
59 |
|
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
60 |
private final Socket socket; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
61 |
private final Sjavac sjavac; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
62 |
|
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
63 |
public RequestHandler(Socket socket, Sjavac sjavac) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
64 |
this.socket = socket; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
65 |
this.sjavac = sjavac; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
66 |
} |
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 |
@Override |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
69 |
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
|
70 |
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
|
71 |
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
|
72 |
|
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
73 |
// 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
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
args[i] = in.readLine(); |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
78 |
} |
32542
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
79 |
|
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
80 |
// Perform compilation |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
81 |
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
|
82 |
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
|
83 |
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
|
84 |
stdout.flush(); |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
85 |
stderr.flush(); |
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
86 |
|
f4e4f4c4f9f4
8129114: Sjavac should stream back compiler output to the client as soon as it becomes available
alundblad
parents:
32335
diff
changeset
|
87 |
// 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
|
88 |
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
|
89 |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
90 |
} catch (Exception ex) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
91 |
// 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
|
92 |
// 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
|
93 |
// compilation will fail. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
94 |
StringWriter sw = new StringWriter(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
95 |
ex.printStackTrace(new PrintWriter(sw)); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
96 |
Log.error(sw.toString()); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff
changeset
|
97 |
} |
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 |
} |