author | chegar |
Mon, 18 Aug 2014 10:59:44 +0100 | |
changeset 26107 | a4a156a33c94 |
parent 26098 | langtools/src/share/classes/com/sun/tools/sjavac/client/SjavacClient.java@32588700060b |
child 26991 | 88d998b3bb4b |
permissions | -rw-r--r-- |
25605
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
1 |
/* |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
4 |
* |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
10 |
* |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
15 |
* accompanied this code). |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
16 |
* |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
20 |
* |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
23 |
* questions. |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
24 |
*/ |
c2d72342fe19
8050408: A few new Java src files for sjavac are missing copyright notices
jjg
parents:
25299
diff
changeset
|
25 |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
26 |
package com.sun.tools.sjavac.client; |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
27 |
|
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
28 |
import java.io.BufferedReader; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
29 |
import java.io.File; |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
30 |
import java.io.FileNotFoundException; |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
31 |
import java.io.FileReader; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
32 |
import java.io.IOException; |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
33 |
import java.io.ObjectInputStream; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
34 |
import java.io.ObjectOutputStream; |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
35 |
import java.io.PrintWriter; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
36 |
import java.io.StringWriter; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
37 |
import java.net.InetAddress; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
38 |
import java.net.InetSocketAddress; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
39 |
import java.net.Socket; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
40 |
import java.net.URI; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
41 |
import java.util.List; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
42 |
import java.util.Set; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
43 |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
44 |
import com.sun.tools.sjavac.Log; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
45 |
import com.sun.tools.sjavac.ProblemException; |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
46 |
import com.sun.tools.sjavac.Util; |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
47 |
import com.sun.tools.sjavac.server.CompilationResult; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
48 |
import com.sun.tools.sjavac.server.PortFile; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
49 |
import com.sun.tools.sjavac.server.Sjavac; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
50 |
import com.sun.tools.sjavac.server.SjavacServer; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
51 |
import com.sun.tools.sjavac.server.SysInfo; |
26088 | 52 |
import com.sun.tools.sjavac.options.Options; |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
53 |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
54 |
/** |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
55 |
* Sjavac implementation that delegates requests to a SjavacServer. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
56 |
* |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
57 |
* <p><b>This is NOT part of any supported API. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
58 |
* 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:
26088
diff
changeset
|
59 |
* 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:
26088
diff
changeset
|
60 |
* deletion without notice.</b> |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
61 |
*/ |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
62 |
public class SjavacClient implements Sjavac { |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
63 |
|
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
64 |
// The id can perhaps be used in the future by the javac server to reuse the |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
65 |
// JavaCompiler instance for several compiles using the same id. |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
66 |
private final String id; |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
67 |
private final String portfileName; |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
68 |
private final String logfile; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
69 |
private final String stdouterrfile; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
70 |
private final boolean background; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
71 |
|
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
72 |
// Default keepalive for server is 120 seconds. |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
73 |
// I.e. it will accept 120 seconds of inactivity before quitting. |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
74 |
private final int keepalive; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
75 |
private final int poolsize; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
76 |
|
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
77 |
// The sjavac option specifies how the server part of sjavac is spawned. |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
78 |
// If you have the experimental sjavac in your path, you are done. If not, you have |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
79 |
// to point to a com.sun.tools.sjavac.Main that supports --startserver |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
80 |
// for example by setting: sjavac=java%20-jar%20...javac.jar%com.sun.tools.sjavac.Main |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
81 |
private final String sjavacForkCmd; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
82 |
|
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
83 |
// Wait 2 seconds for response, before giving up on javac server. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
84 |
static int CONNECTION_TIMEOUT = 2000; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
85 |
static int MAX_CONNECT_ATTEMPTS = 3; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
86 |
static int WAIT_BETWEEN_CONNECT_ATTEMPTS = 2000; |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
87 |
|
26088 | 88 |
// Store the server conf settings here. |
89 |
private final String settings; |
|
90 |
||
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
91 |
public SjavacClient(Options options) { |
26088 | 92 |
String tmpServerConf = options.getServerConf(); |
93 |
String serverConf = (tmpServerConf!=null)? tmpServerConf : ""; |
|
94 |
String tmpId = Util.extractStringOption("id", serverConf); |
|
95 |
id = (tmpId!=null) ? tmpId : "id"+(((new java.util.Random()).nextLong())&Long.MAX_VALUE); |
|
96 |
String p = Util.extractStringOption("portfile", serverConf); |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
97 |
portfileName = (p!=null) ? p : options.getStateDir().toFile().getAbsolutePath()+File.separatorChar+"javac_server"; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
98 |
logfile = Util.extractStringOption("logfile", serverConf, portfileName + ".javaclog"); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
99 |
stdouterrfile = Util.extractStringOption("stdouterrfile", serverConf, portfileName + ".stdouterr"); |
26088 | 100 |
background = Util.extractBooleanOption("background", serverConf, true); |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
101 |
sjavacForkCmd = Util.extractStringOption("sjavac", serverConf, "sjavac"); |
26088 | 102 |
int poolsize = Util.extractIntOption("poolsize", serverConf); |
103 |
keepalive = Util.extractIntOption("keepalive", serverConf, 120); |
|
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
104 |
|
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
105 |
this.poolsize = poolsize > 0 ? poolsize : Runtime.getRuntime().availableProcessors(); |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
106 |
settings = (serverConf.equals("")) ? "id="+id+",portfile="+portfileName : serverConf; |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
107 |
} |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
108 |
|
26088 | 109 |
/** |
110 |
* Hand out the server settings. |
|
111 |
* @return The server settings, possibly a default value. |
|
112 |
*/ |
|
113 |
public String serverSettings() { |
|
114 |
return settings; |
|
115 |
} |
|
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
116 |
|
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
117 |
/** |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
118 |
* Make a request to the server only to get the maximum possible heap size to use for compilations. |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
119 |
* |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
120 |
* @param port_file The port file used to synchronize creation of this server. |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
121 |
* @param id The identify of the compilation. |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
122 |
* @param out Standard out information. |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
123 |
* @param err Standard err information. |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
124 |
* @return The maximum heap size in bytes. |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
125 |
*/ |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
126 |
@Override |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
127 |
public SysInfo getSysInfo() { |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
128 |
try (Socket socket = tryConnect()) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
129 |
// The ObjectInputStream constructor will block until the |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
130 |
// corresponding ObjectOutputStream has written and flushed the |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
131 |
// header, so it is important that the ObjectOutputStreams on server |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
132 |
// and client are opened before the ObjectInputStreams. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
133 |
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
134 |
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
135 |
oos.writeObject(id); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
136 |
oos.writeObject(SjavacServer.CMD_SYS_INFO); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
137 |
oos.flush(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
138 |
return (SysInfo) ois.readObject(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
139 |
} catch (IOException | ClassNotFoundException ex) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
140 |
Log.error("[CLIENT] Exception caught: " + ex); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
141 |
StringWriter sw = new StringWriter(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
142 |
ex.printStackTrace(new PrintWriter(sw)); |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
143 |
} |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
144 |
return null; |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
145 |
} |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
146 |
|
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
147 |
@Override |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
148 |
public CompilationResult compile(String protocolId, |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
149 |
String invocationId, |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
150 |
String[] args, |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
151 |
List<File> explicitSources, |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
152 |
Set<URI> sourcesToCompile, |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
153 |
Set<URI> visibleSources) { |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
154 |
CompilationResult result; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
155 |
try (Socket socket = tryConnect()) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
156 |
// The ObjectInputStream constructor will block until the |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
157 |
// corresponding ObjectOutputStream has written and flushed the |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
158 |
// header, so it is important that the ObjectOutputStreams on server |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
159 |
// and client are opened before the ObjectInputStreams. |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
160 |
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
161 |
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
162 |
oos.writeObject(id); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
163 |
oos.writeObject(SjavacServer.CMD_COMPILE); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
164 |
oos.writeObject(protocolId); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
165 |
oos.writeObject(invocationId); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
166 |
oos.writeObject(args); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
167 |
oos.writeObject(explicitSources); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
168 |
oos.writeObject(sourcesToCompile); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
169 |
oos.writeObject(visibleSources); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
170 |
oos.flush(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
171 |
result = (CompilationResult) ois.readObject(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
172 |
} catch (IOException | ClassNotFoundException ex) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
173 |
Log.error("Exception caught: " + ex); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
174 |
result = new CompilationResult(CompilationResult.ERROR_FATAL); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
175 |
result.stderr = ex.getMessage(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
176 |
} |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
177 |
return result; |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
178 |
} |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
179 |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
180 |
private Socket tryConnect() throws IOException { |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
181 |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
182 |
PortFile portFile; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
183 |
try { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
184 |
// This should be taken care of at a higher level (JDK-8048451) |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
185 |
portFile = SjavacServer.getPortFile(portfileName); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
186 |
} catch (FileNotFoundException e) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
187 |
// Reached for instance if directory of port file does not exist |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
188 |
Log.error("Port file inaccessable: " + e); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
189 |
throw new RuntimeException(e); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
190 |
} |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
191 |
for (int i = 0; i < MAX_CONNECT_ATTEMPTS; i++) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
192 |
Log.info(String.format("Trying to connect (attempt %d of %d)", |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
193 |
i+1, MAX_CONNECT_ATTEMPTS)); |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
194 |
try { |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
195 |
if (!makeSureServerIsRunning(portFile)) |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
196 |
continue; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
197 |
Socket socket = new Socket(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
198 |
InetAddress localhost = InetAddress.getByName(null); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
199 |
socket.connect(new InetSocketAddress(localhost, portFile.getPort()), |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
200 |
CONNECTION_TIMEOUT); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
201 |
return socket; |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
202 |
} catch (ProblemException | IOException ex) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
203 |
Log.error("Caught exception during tryConnect: " + ex); |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
204 |
} |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
205 |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
206 |
try { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
207 |
Thread.sleep(WAIT_BETWEEN_CONNECT_ATTEMPTS); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
208 |
} catch (InterruptedException e) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
209 |
Thread.currentThread().interrupt(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
210 |
} |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
211 |
} |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
212 |
throw new IOException("Could not connect to server"); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
213 |
} |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
214 |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
215 |
private boolean makeSureServerIsRunning(PortFile portFile) |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
216 |
throws IOException, ProblemException, FileNotFoundException { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
217 |
|
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
218 |
synchronized (portFile) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
219 |
portFile.lock(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
220 |
portFile.getValues(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
221 |
portFile.unlock(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
222 |
} |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
223 |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
224 |
if (!portFile.containsPortInfo()) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
225 |
String forkCmd = SjavacServer.fork(sjavacForkCmd, |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
226 |
portFile.getFilename(), |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
227 |
logfile, |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
228 |
poolsize, |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
229 |
keepalive, |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
230 |
System.err, |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
231 |
stdouterrfile, |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
232 |
background); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
233 |
if (!portFile.waitForValidValues()) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
234 |
// This can be simplified once JDK-8048457 has been addressed |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
235 |
// since we won't have an SjavacClient if background = false |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
236 |
if (background) { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
237 |
// There seems be some problem with spawning the external |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
238 |
// process (for instance no fork command provided and no |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
239 |
// sjavac on path) |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
240 |
StringWriter sw = new StringWriter(); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
241 |
SjavacClient.printFailedAttempt(forkCmd, |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
242 |
stdouterrfile, |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
243 |
new PrintWriter(sw)); |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
244 |
Log.error(sw.toString()); |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
245 |
} |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
246 |
} |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
247 |
} |
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
248 |
return portFile.containsPortInfo(); |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
249 |
} |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
250 |
|
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
251 |
|
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
252 |
public static void printFailedAttempt(String cmd, String f, PrintWriter err) { |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
253 |
err.println("---- Failed to start javac server with this command -----"); |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
254 |
err.println(cmd); |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
255 |
try { |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
256 |
BufferedReader in = new BufferedReader(new FileReader(f)); |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
257 |
err.println("---- stdout/stderr output from attempt to start javac server -----"); |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
258 |
for (;;) { |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
259 |
String l = in.readLine(); |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
260 |
if (l == null) { |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
261 |
break; |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
262 |
} |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
263 |
err.println(l); |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
264 |
} |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
265 |
err.println("------------------------------------------------------------------"); |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
266 |
} catch (Exception e) { |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
267 |
err.println("The stdout/stderr output in file " + f + " does not exist and the server did not start."); |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
268 |
} |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
269 |
} |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
270 |
|
26098
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
271 |
@Override |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
272 |
public void shutdown() { |
32588700060b
8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
26088
diff
changeset
|
273 |
// Nothing to clean up |
25299
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
274 |
} |
b4a7dcd657f5
8048594: The sjavac client/server protocol should be hidden behind an interface
alundblad
parents:
diff
changeset
|
275 |
} |