24 */ |
24 */ |
25 |
25 |
26 package com.sun.tools.sjavac.client; |
26 package com.sun.tools.sjavac.client; |
27 |
27 |
28 import java.io.File; |
28 import java.io.File; |
29 import java.io.FileNotFoundException; |
|
30 import java.io.IOException; |
29 import java.io.IOException; |
31 import java.io.ObjectInputStream; |
30 import java.io.ObjectInputStream; |
32 import java.io.ObjectOutputStream; |
31 import java.io.ObjectOutputStream; |
33 import java.io.PrintStream; |
32 import java.io.PrintStream; |
34 import java.io.PrintWriter; |
|
35 import java.io.StringWriter; |
|
36 import java.net.InetAddress; |
33 import java.net.InetAddress; |
37 import java.net.InetSocketAddress; |
34 import java.net.InetSocketAddress; |
38 import java.net.Socket; |
35 import java.net.Socket; |
39 import java.net.URI; |
|
40 import java.util.ArrayList; |
36 import java.util.ArrayList; |
41 import java.util.Arrays; |
37 import java.util.Arrays; |
42 import java.util.List; |
38 import java.util.List; |
43 import java.util.Scanner; |
39 import java.util.Scanner; |
44 import java.util.Set; |
|
45 |
40 |
46 import com.sun.tools.sjavac.Log; |
41 import com.sun.tools.sjavac.Log; |
47 import com.sun.tools.sjavac.Util; |
42 import com.sun.tools.sjavac.Util; |
48 import com.sun.tools.sjavac.options.OptionHelper; |
43 import com.sun.tools.sjavac.options.OptionHelper; |
49 import com.sun.tools.sjavac.options.Options; |
44 import com.sun.tools.sjavac.options.Options; |
|
45 import com.sun.tools.sjavac.server.CompilationSubResult; |
50 import com.sun.tools.sjavac.server.CompilationResult; |
46 import com.sun.tools.sjavac.server.CompilationResult; |
51 import com.sun.tools.sjavac.server.PortFile; |
47 import com.sun.tools.sjavac.server.PortFile; |
52 import com.sun.tools.sjavac.server.Sjavac; |
48 import com.sun.tools.sjavac.server.Sjavac; |
53 import com.sun.tools.sjavac.server.SjavacServer; |
49 import com.sun.tools.sjavac.server.SjavacServer; |
54 import com.sun.tools.sjavac.server.SysInfo; |
|
55 |
50 |
56 /** |
51 /** |
57 * Sjavac implementation that delegates requests to a SjavacServer. |
52 * Sjavac implementation that delegates requests to a SjavacServer. |
58 * |
53 * |
59 * <p><b>This is NOT part of any supported API. |
54 * <p><b>This is NOT part of any supported API. |
87 static int WAIT_BETWEEN_CONNECT_ATTEMPTS = 2000; |
82 static int WAIT_BETWEEN_CONNECT_ATTEMPTS = 2000; |
88 |
83 |
89 // Store the server conf settings here. |
84 // Store the server conf settings here. |
90 private final String settings; |
85 private final String settings; |
91 |
86 |
92 // This constructor should not throw FileNotFoundException (to be resolved |
87 public SjavacClient(Options options) throws PortFileInaccessibleException { |
93 // in JDK-8060030) |
|
94 public SjavacClient(Options options) throws FileNotFoundException { |
|
95 String tmpServerConf = options.getServerConf(); |
88 String tmpServerConf = options.getServerConf(); |
96 String serverConf = (tmpServerConf!=null)? tmpServerConf : ""; |
89 String serverConf = (tmpServerConf!=null)? tmpServerConf : ""; |
97 String tmpId = Util.extractStringOption("id", serverConf); |
90 String tmpId = Util.extractStringOption("id", serverConf); |
98 id = (tmpId!=null) ? tmpId : "id"+(((new java.util.Random()).nextLong())&Long.MAX_VALUE); |
91 id = (tmpId!=null) ? tmpId : "id"+(((new java.util.Random()).nextLong())&Long.MAX_VALUE); |
99 String defaultPortfile = options.getStateDir() |
92 String defaultPortfile = options.getStateDir() |
101 .toAbsolutePath() |
94 .toAbsolutePath() |
102 .toString(); |
95 .toString(); |
103 String portfileName = Util.extractStringOption("portfile", serverConf, defaultPortfile); |
96 String portfileName = Util.extractStringOption("portfile", serverConf, defaultPortfile); |
104 try { |
97 try { |
105 portFile = SjavacServer.getPortFile(portfileName); |
98 portFile = SjavacServer.getPortFile(portfileName); |
106 } catch (FileNotFoundException e) { |
99 } catch (PortFileInaccessibleException e) { |
107 // Reached for instance if directory of port file does not exist |
|
108 Log.error("Port file inaccessable: " + e); |
100 Log.error("Port file inaccessable: " + e); |
109 throw e; |
101 throw e; |
110 } |
102 } |
111 logfile = Util.extractStringOption("logfile", serverConf, portfileName + ".javaclog"); |
103 logfile = Util.extractStringOption("logfile", serverConf, portfileName + ".javaclog"); |
112 stdouterrfile = Util.extractStringOption("stdouterrfile", serverConf, portfileName + ".stdouterr"); |
104 stdouterrfile = Util.extractStringOption("stdouterrfile", serverConf, portfileName + ".stdouterr"); |
124 */ |
116 */ |
125 public String serverSettings() { |
117 public String serverSettings() { |
126 return settings; |
118 return settings; |
127 } |
119 } |
128 |
120 |
129 /** |
|
130 * Make a request to the server only to get the maximum possible heap size to use for compilations. |
|
131 */ |
|
132 @Override |
121 @Override |
133 public SysInfo getSysInfo() { |
122 public CompilationResult compile(String[] args) { |
134 try (Socket socket = tryConnect()) { |
|
135 // The ObjectInputStream constructor will block until the |
|
136 // corresponding ObjectOutputStream has written and flushed the |
|
137 // header, so it is important that the ObjectOutputStreams on server |
|
138 // and client are opened before the ObjectInputStreams. |
|
139 ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); |
|
140 ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); |
|
141 oos.writeObject(id); |
|
142 oos.writeObject(SjavacServer.CMD_SYS_INFO); |
|
143 oos.flush(); |
|
144 return (SysInfo) ois.readObject(); |
|
145 } catch (IOException | ClassNotFoundException ex) { |
|
146 Log.error("[CLIENT] Exception caught: " + ex); |
|
147 Log.debug(Util.getStackTrace(ex)); |
|
148 } catch (InterruptedException ie) { |
|
149 Thread.currentThread().interrupt(); // Restore interrupt |
|
150 Log.error("[CLIENT] getSysInfo interrupted."); |
|
151 Log.debug(Util.getStackTrace(ie)); |
|
152 } |
|
153 return null; |
|
154 } |
|
155 |
|
156 @Override |
|
157 public CompilationResult compile(String protocolId, |
|
158 String invocationId, |
|
159 String[] args, |
|
160 List<File> explicitSources, |
|
161 Set<URI> sourcesToCompile, |
|
162 Set<URI> visibleSources) { |
|
163 CompilationResult result; |
123 CompilationResult result; |
164 try (Socket socket = tryConnect()) { |
124 try (Socket socket = tryConnect()) { |
165 // The ObjectInputStream constructor will block until the |
125 // The ObjectInputStream constructor will block until the |
166 // corresponding ObjectOutputStream has written and flushed the |
126 // corresponding ObjectOutputStream has written and flushed the |
167 // header, so it is important that the ObjectOutputStreams on server |
127 // header, so it is important that the ObjectOutputStreams on server |
168 // and client are opened before the ObjectInputStreams. |
128 // and client are opened before the ObjectInputStreams. |
169 ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); |
129 ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); |
170 ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); |
130 ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); |
171 oos.writeObject(id); |
131 oos.writeObject(id); |
172 oos.writeObject(SjavacServer.CMD_COMPILE); |
132 oos.writeObject(SjavacServer.CMD_COMPILE); |
173 oos.writeObject(protocolId); |
|
174 oos.writeObject(invocationId); |
|
175 oos.writeObject(args); |
133 oos.writeObject(args); |
176 oos.writeObject(explicitSources); |
|
177 oos.writeObject(sourcesToCompile); |
|
178 oos.writeObject(visibleSources); |
|
179 oos.flush(); |
134 oos.flush(); |
180 result = (CompilationResult) ois.readObject(); |
135 result = (CompilationResult) ois.readObject(); |
181 } catch (IOException | ClassNotFoundException ex) { |
136 } catch (IOException | ClassNotFoundException ex) { |
182 Log.error("[CLIENT] Exception caught: " + ex); |
137 Log.error("[CLIENT] Exception caught: " + ex); |
183 result = new CompilationResult(CompilationResult.ERROR_FATAL); |
138 result = new CompilationResult(CompilationSubResult.ERROR_FATAL); |
184 result.stderr = Util.getStackTrace(ex); |
139 result.stderr = Util.getStackTrace(ex); |
185 } catch (InterruptedException ie) { |
140 } catch (InterruptedException ie) { |
186 Thread.currentThread().interrupt(); // Restore interrupt |
141 Thread.currentThread().interrupt(); // Restore interrupt |
187 Log.error("[CLIENT] compile interrupted."); |
142 Log.error("[CLIENT] compile interrupted."); |
188 result = new CompilationResult(CompilationResult.ERROR_FATAL); |
143 result = new CompilationResult(CompilationSubResult.ERROR_FATAL); |
189 result.stderr = Util.getStackTrace(ie); |
144 result.stderr = Util.getStackTrace(ie); |
190 } |
145 } |
191 return result; |
146 return result; |
192 } |
147 } |
193 |
148 |