Produced by Araxis Merge on 9/25/2018 2:13:09 PM Central Daylight Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
# | Location | File | Last Modified |
---|---|---|---|
1 | build 3.zip\build 3\MHLTH_YS_137_Source\JavaScript\resources\javaJDF-1.8.0\src\com\sun\tools\sjavac\server | JavacServer.java | Mon Jan 22 14:47:06 2018 UTC |
2 | build 3.zip\build 3\MHLTH_YS_137_Source\JavaScript\resources\javaJDF-1.8.0\src\com\sun\tools\sjavac\server | JavacServer.java | Wed Sep 12 16:51:53 2018 UTC |
Description | Between Files 1 and 2 |
|
---|---|---|
Text Blocks | Lines | |
Unchanged | 2 | 1500 |
Changed | 1 | 2 |
Inserted | 0 | 0 |
Removed | 0 | 0 |
Whitespace | |
---|---|
Character case | Differences in character case are significant |
Line endings | Differences in line endings (CR and LF characters) are ignored |
CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
1 | /* | |
2 | * Copyrig ht (c) 201 1-2012, Or acle and/o r its affi liates. Al l rights r eserved. | |
3 | * DO NOT ALTER OR R EMOVE COPY RIGHT NOTI CES OR THI S FILE HEA DER. | |
4 | * | |
5 | * This co de is free software; you can r edistribut e it and/o r modify i t | |
6 | * under t he terms o f the GNU General Pu blic Licen se version 2 only, a s | |
7 | * publish ed by the Free Softw are Founda tion. Ora cle design ates this | |
8 | * particu lar file a s subject to the "Cl asspath" e xception a s provided | |
9 | * by Orac le in the LICENSE fi le that ac companied this code. | |
10 | * | |
11 | * This co de is dist ributed in the hope that it wi ll be usef ul, but WI THOUT | |
12 | * ANY WAR RANTY; wit hout even the implie d warranty of MERCHA NTABILITY or | |
13 | * FITNESS FOR A PAR TICULAR PU RPOSE. Se e the GNU General Pu blic Licen se | |
14 | * version 2 for mor e details (a copy is included in the LIC ENSE file that | |
15 | * accompa nied this code). | |
16 | * | |
17 | * You sho uld have r eceived a copy of th e GNU Gene ral Public License v ersion | |
18 | * 2 along with this work; if not, write to the Fr ee Softwar e Foundati on, | |
19 | * Inc., 5 1 Franklin St, Fifth Floor, Bo ston, MA 0 2110-1301 USA. | |
20 | * | |
21 | * Please contact Or acle, 500 Oracle Par kway, Redw ood Shores , CA 94065 USA | |
22 | * or visi t www.orac le.com if you need a dditional informatio n or have any | |
23 | * questio ns. | |
24 | */ | |
25 | package co m.sun.tool s.sjavac.s erver; | |
26 | ||
27 | import jav a.io.Buffe redReader; | |
28 | import jav a.io.File; | |
29 | import jav a.io.IOExc eption; | |
30 | import jav a.io.Input StreamRead er; | |
31 | import jav a.io.Print Writer; | |
32 | import jav a.io.FileN otFoundExc eption; | |
33 | import jav a.net.URI; | |
34 | import jav a.util.Has hSet; | |
35 | import jav a.util.Set ; | |
36 | import jav a.util.Has hMap; | |
37 | import jav a.util.Map ; | |
38 | ||
39 | import jav a.net.Inet Address; | |
40 | import jav a.net.Inet SocketAddr ess; | |
41 | import jav a.net.Serv erSocket; | |
42 | import jav a.net.Sock et; | |
43 | import jav a.net.Sock etAddress; | |
44 | import jav a.util.Arr ayList; | |
45 | import jav a.util.Ran dom; | |
46 | ||
47 | import com .sun.tools .sjavac.Ut il; | |
48 | import com .sun.tools .sjavac.Pr oblemExcep tion; | |
49 | import jav a.io.*; | |
50 | import jav a.util.*; | |
51 | ||
52 | /** | |
53 | * The Jav acServer c lass conta ins method s both to setup a se rver that responds t o requests and metho ds to conn ect to thi s server. | |
54 | * | |
55 | * <p><b>T his is NOT part of a ny support ed API. If you write code that depends o n this, yo u do so at your own risk. This code and its intern al interfa ces are | |
56 | * subject to change or deleti on without notice.</ b></p> | |
57 | */ | |
58 | public cla ss JavacSe rver { | |
59 | // Res ponding to this tcp/ ip port on localhost . | |
60 | ||
61 | privat e final Se rverSocket serverSoc ket; | |
62 | // The PW cookie sha red betwee n server a nd client through th e port fil e. | |
63 | privat e final lo ng myCooki e; | |
64 | // Whe n the serv er was sta rted. | |
65 | privat e long ser verStart; | |
66 | // Acc umulated b uild time for all re quests, no t counting idle time . | |
67 | privat e long tot alBuildTim e; | |
68 | // The javac ser ver specif ic log fil e. | |
69 | PrintW riter theL og; | |
70 | // The compiler pool that maintains the compil er threads . | |
71 | Compil erPool com pilerPool; | |
72 | // For the clien t, all por t files fe tched, one per start ed javac s erver. | |
73 | // Tho ugh usuall y only one javac ser ver is sta rted by a client. | |
74 | privat e static M ap<String, PortFile> allPortFi les; | |
75 | privat e static M ap<String, Long> max ServerMemo ry; | |
76 | final static int ERROR_FAT AL = -1; | |
77 | final static int ERROR_BUT _TRY_AGAIN = -4712; | |
78 | final static Str ing PROTOC OL_COOKIE_ VERSION = "----THE-C OOKIE-V2-- --"; | |
79 | final static Str ing PROTOC OL_CWD = " ----THE-CW D----"; | |
80 | final static Str ing PROTOC OL_ID = "- ---THE-ID- ---"; | |
81 | final static Str ing PROTOC OL_ARGS = "----THE-A RGS----"; | |
82 | final static Str ing PROTOC OL_SOURCES _TO_COMPIL E = "----T HE-SOURCES -TO-COMPIL E----"; | |
83 | final static Str ing PROTOC OL_VISIBLE _SOURCES = "----THE- VISIBLE-SO URCES----" ; | |
84 | final static Str ing PROTOC OL_END = " ----THE-EN D----"; | |
85 | final static Str ing PROTOC OL_STDOUT = "----THE -STDOUT--- -"; | |
86 | final static Str ing PROTOC OL_STDERR = "----THE -STDERR--- -"; | |
87 | final static Str ing PROTOC OL_PACKAGE _ARTIFACTS = "----TH E-PACKAGE_ ARTIFACTS- ---"; | |
88 | final static Str ing PROTOC OL_PACKAGE _DEPENDENC IES = "--- -THE-PACKA GE_DEPENDE NCIES----" ; | |
89 | final static Str ing PROTOC OL_PACKAGE _PUBLIC_AP IS = "---- THE-PACKAG E-PUBLIC-A PIS----"; | |
90 | final static Str ing PROTOC OL_SYSINFO = "----TH E-SYSINFO- ---"; | |
91 | final static Str ing PROTOC OL_RETURN_ CODE = "-- --THE-RETU RN-CODE--- -"; | |
92 | // Che ck if the portfile i s gone, ev ery 5 seco nds. | |
93 | static int CHECK _PORTFILE_ INTERVAL = 5; | |
94 | // Wai t 2 second s for resp onse, befo re giving up on java c server. | |
95 | static int CONNE CTION_TIME OUT = 2; | |
96 | static int WAIT_ BETWEEN_CO NNECT_ATTE MPTS = 1; | |
97 | static int MAX_N UM_CONNECT _ATTEMPTS = 3; | |
98 | ||
99 | /** | |
100 | * Acq uire the p ort file. Synchroniz ed since s everal thr eads insid e an smart javac wra pper clien t acquires the same port file at the sam e time. | |
101 | */ | |
102 | privat e static s ynchronize d PortFile getPortFi le(String filename) throws Fil eNotFoundE xception { | |
103 | if (allPortF iles == nu ll) { | |
104 | allPortF iles = new HashMap<S tring, Por tFile>(); | |
105 | } | |
106 | Po rtFile pf = allPortF iles.get(f ilename); | |
107 | if (pf == nu ll) { | |
108 | pf = new PortFile( filename); | |
109 | allPortF iles.put(f ilename, p f); | |
110 | } | |
111 | re turn pf; | |
112 | } | |
113 | ||
114 | /** | |
115 | * Get the cooki e used for this serv er. | |
116 | */ | |
117 | long g etCookie() { | |
118 | re turn myCoo kie; | |
119 | } | |
120 | ||
121 | /** | |
122 | * Get the port used for t his server . | |
123 | */ | |
124 | int ge tPort() { | |
125 | re turn serve rSocket.ge tLocalPort (); | |
126 | } | |
127 | ||
128 | /** | |
129 | * Sum up the to tal build time for t his javac server. | |
130 | */ | |
131 | public void addB uildTime(l ong inc) { | |
132 | to talBuildTi me += inc; | |
133 | } | |
134 | ||
135 | /** | |
136 | * Log this mess age. | |
137 | */ | |
138 | public void log( String msg ) { | |
139 | if (theLog ! = null) { | |
140 | theLog.p rintln(msg ); | |
141 | } else { | |
142 | System.e rr.println (msg); | |
143 | } | |
144 | } | |
145 | ||
146 | /** | |
147 | * Mak e sure the log is fl ushed. | |
148 | */ | |
149 | public void flus hLog() { | |
150 | if (theLog ! = null) { | |
151 | theLog.f lush(); | |
152 | } | |
153 | } | |
154 | ||
155 | /** | |
156 | * Sta rt a serve r using a settings s tring. Typ ically: "- -startserv er:portfil e=/tmp/mys erver,pool size=3" an d the stri ng "portfi le=/tmp/my server,poo lsize=3" | |
157 | * is sent as th e settings parameter . Returns 0 on succe ss, -1 on failure. | |
158 | */ | |
159 | public static in t startSer ver(String settings, PrintStre am err) { | |
160 | tr y { | |
161 | String p ortfile = Util.extra ctStringOp tion("port file", set tings); | |
162 | // The l og file co llects mor e javac se rver speci fic log in formation. | |
163 | String l ogfile = U til.extrac tStringOpt ion("logfi le", setti ngs); | |
164 | // The s tdouterr f ile collec ts all the System.ou t and Syst em.err wri tes to dis k. | |
165 | String s tdouterrfi le = Util. extractStr ingOption( "stdouterr file", set tings); | |
166 | // We co uld perhap s use Syst em.setOut and setErr here. | |
167 | // But f or the mom ent we rel y on the c lient to s pawn a she ll where s tdout | |
168 | // and s tderr are redirected already. | |
169 | // The p ool size i s a limit the number of concur rent compi ler thread s used. | |
170 | // The s erver migh t use less than thes e to avoid memory pr oblems. | |
171 | int pool size = Uti l.extractI ntOption(" poolsize", settings) ; | |
172 | if (pool size <= 0) { | |
173 | // I f not set, default t o the numb er of core s. | |
174 | pool size = Run time.getRu ntime().av ailablePro cessors(); | |
175 | } | |
176 | ||
177 | // How m any second s of inact ivity will the serve r accept b efore quit ting? | |
178 | int keep alive = Ut il.extract IntOption( "keepalive ", setting s); | |
179 | if (keep alive <= 0 ) { | |
180 | keep alive = 12 0; | |
181 | } | |
182 | // The p ort file i s locked a nd the ser ver port a nd cookie is written into it. | |
183 | PortFile portFile = getPortF ile(portfi le); | |
184 | JavacSer ver s; | |
185 | ||
186 | synchron ized (port File) { | |
187 | port File.lock( ); | |
188 | port File.getVa lues(); | |
189 | if ( portFile.c ontainsPor tInfo()) { | |
190 | err.printl n("Javac s erver not started be cause port file exist s!"); | |
191 | portFile.u nlock(); | |
192 | return -1; | |
193 | } | |
194 | s = new JavacS erver(pool size, logf ile); | |
195 | port File.setVa lues(s.get Port(), s. getCookie( )); | |
196 | port File.unloc k(); | |
197 | } | |
198 | ||
199 | // Run t he server. Will dele te the por t file whe n shutting down. | |
200 | // It wi ll shut do wn automat ically whe n no new r equests ha ve come in | |
201 | // durin g the last 125 secon ds. | |
202 | s.run(po rtFile, er r, keepali ve); | |
203 | // The r un loop fo r the serv er has exi ted. | |
204 | return 0 ; | |
205 | } catch (Exc eption e) { | |
206 | e.printS tackTrace( err); | |
207 | return - 1; | |
208 | } | |
209 | } | |
210 | ||
211 | /** | |
212 | * Dis patch a co mpilation request to a javac s erver. | |
213 | * | |
214 | * @pa ram args a re the com mand line args to ja vac and is allowed t o contain source fil es, @file and other command li ne options to javac. | |
215 | * | |
216 | * The generated classes, h files an d other ar tifacts fr om the jav ac invocat ion are st ored by th e javac se rver to di sk. | |
217 | * | |
218 | * @pa ram source s_to_compi le The sou rces to co mpile. | |
219 | * | |
220 | * @pa ram visibl eSources I f visible sources ha s a non ze ro size, t hen visibl e_sources are the on ly files i n the file system th at the jav ac server can see! | |
221 | * (So urces to c ompile are always vi sible.) Th e visible sources ar e those su pplied by the (filte red) -sour cepath | |
222 | * | |
223 | * @pa ram visibl eClasses I f visible classes fo r a specif ic root/ja r has a no n zero siz e, then vi sible_clas ses are th e only cla ss files t hat the ja vac server | |
224 | * can see, in t hat root/j ar. It map s from a c lasspath r oot or a j ar file to the set o f visible classes fo r that roo t/jar. | |
225 | * | |
226 | * The server re turn meta data about the build in the fo llowing pa rameters. | |
227 | * @pa ram packag e_artifact s, map fro m package name to se t of creat ed artifac ts for tha t package. | |
228 | * @pa ram packag e_dependen cies, map from packa ge name to set of pa ckages tha t it depen ds upon. | |
229 | * @pa ram packag e_pubapis, map from package na me to uniq ue string identifyin g its pub api. | |
230 | */ | |
231 | public static in t useServe r(String s ettings, S tring[] ar gs, | |
232 | Set<URI> sourcesTo Compile, | |
233 | Set<URI> visibleSo urces, | |
234 | Map<URI, Set<Strin g>> visibl eClasses, | |
235 | Map<Stri ng, Set<UR I>> packag eArtifacts , | |
236 | Map<Stri ng, Set<St ring>> pac kageDepend encies, | |
237 | Map<Stri ng, String > packageP ubapis, | |
238 | SysInfo sysinfo, | |
239 | PrintStr eam out, | |
240 | PrintStr eam err) { | |
241 | tr y { | |
242 | // The i d can perh aps be use d in the f uture by t he javac s erver to r euse the | |
243 | // JavaC ompiler in stance for several c ompiles us ing the sa me id. | |
244 | String i d = Util.e xtractStri ngOption(" id", setti ngs); | |
245 | String p ortfile = Util.extra ctStringOp tion("port file", set tings); | |
246 | String l ogfile = U til.extrac tStringOpt ion("logfi le", setti ngs); | |
247 | String s tdouterrfi le = Util. extractStr ingOption( "stdouterr file", set tings); | |
248 | String b ackground = Util.ext ractString Option("ba ckground", settings) ; | |
249 | if (back ground == null || !b ackground. equals("fa lse")) { | |
250 | back ground = " true"; | |
251 | } | |
252 | // The s javac opti on specifi es how the server pa rt of sjav ac is spaw ned. | |
253 | // If yo u have the experimen tal sjavac in your p ath, you a re done. I f not, you have | |
254 | // to po int to a c om.sun.too ls.sjavac. Main that supports - -startserv er | |
255 | // for e xample by setting: s javac=java %20-jar%20 ...javac.j ar%com.sun .tools.sja vac.Main | |
256 | String s javac = Ut il.extract StringOpti on("sjavac ", setting s); | |
257 | int pool size = Uti l.extractI ntOption(" poolsize", settings) ; | |
258 | int keep alive = Ut il.extract IntOption( "keepalive ", setting s); | |
259 | ||
260 | if (keep alive <= 0 ) { | |
261 | // D efault kee palive for server is 120 secon ds. | |
262 | // I .e. it wil l accept 1 20 seconds of inacti vity befor e quitting . | |
263 | keep alive = 12 0; | |
264 | } | |
265 | if (port file == nu ll) { | |
266 | err. println("N o portfile was speci fied!"); | |
267 | retu rn -1; | |
268 | } | |
269 | if (logf ile == nul l) { | |
270 | logf ile = port file + ".j avaclog"; | |
271 | } | |
272 | if (stdo uterrfile == null) { | |
273 | stdo uterrfile = portfile + ".stdou terr"; | |
274 | } | |
275 | // Defau lt to sjav ac and hop e it is in the path. | |
276 | if (sjav ac == null ) { | |
277 | sjav ac = "sjav ac"; | |
278 | } | |
279 | ||
280 | int atte mpts = 0; | |
281 | int rc = -1; | |
282 | do { | |
283 | Port File port_ file = get PortFile(p ortfile); | |
284 | sync hronized ( port_file) { | |
285 | port_file. lock(); | |
286 | port_file. getValues( ); | |
287 | port_file. unlock(); | |
288 | } | |
289 | if ( !port_file .containsP ortInfo()) { | |
290 | String cmd = fork(sj avac, port _file.getF ilename(), logfile, poolsize, keepalive, err, stdo uterrfile, backgroun d); | |
291 | ||
292 | if (backgr ound.equal s("true") && !port_f ile.waitFo rValidValu es()) { | |
293 | // Ouc h the serv er did not start! Le ts print i ts stdoute rrfile and the comma nd used. | |
294 | printF ailedAttem pt(cmd, st douterrfil e, err); | |
295 | // And give up. | |
296 | return -1; | |
297 | } | |
298 | } | |
299 | rc = connectAn dCompile(p ort_file, id, args, sourcesToC ompile, vi sibleSourc es, | |
300 | packag eArtifacts , packageD ependencie s, package Pubapis, s ysinfo, | |
301 | out, e rr); | |
302 | // T ry again u ntil we ma nage to co nnect. Any error aft er that | |
303 | // w ill cause the compil ation to f ail. | |
304 | if ( rc == ERRO R_BUT_TRY_ AGAIN) { | |
305 | // We coul d not conn ect to the server. T ry again. | |
306 | attempts++ ; | |
307 | try { | |
308 | Thread .sleep(WAI T_BETWEEN_ CONNECT_AT TEMPTS); | |
309 | } catch (I nterrupted Exception e) { | |
310 | } | |
311 | } | |
312 | } while (rc == ERR OR_BUT_TRY _AGAIN && attempts < MAX_NUM_C ONNECT_ATT EMPTS); | |
313 | return r c; | |
314 | } catch (Exc eption e) { | |
315 | e.printS tackTrace( err); | |
316 | return - 1; | |
317 | } | |
318 | } | |
319 | ||
320 | privat e static v oid printF ailedAttem pt(String cmd, Strin g f, Print Stream err ) { | |
321 | er r.println( "---- Fail ed to star t javac se rver with this comma nd -----") ; | |
322 | er r.println( cmd); | |
323 | tr y { | |
324 | Buffered Reader in = new Buff eredReader (new FileR eader(f)); | |
325 | err.prin tln("---- stdout/std err output from atte mpt to sta rt javac s erver ---- -"); | |
326 | for (;;) { | |
327 | Stri ng l = in. readLine() ; | |
328 | if ( l == null) { | |
329 | break; | |
330 | } | |
331 | err. println(l) ; | |
332 | } | |
333 | err.prin tln("----- ---------- ---------- ---------- ---------- ---------- ---------- -"); | |
334 | } catch (Exc eption e) { | |
335 | err.prin tln("The s tdout/stde rr output in file " + f + " do es not exi st and the server di d not star t."); | |
336 | } | |
337 | } | |
338 | ||
339 | /** | |
340 | * Spa wn the ser ver instan ce. | |
341 | */ | |
342 | ||
343 | privat e JavacSer ver(int po olSize, St ring logfi le) throws IOExcepti on { | |
344 | se rverStart = System.c urrentTime Millis(); | |
345 | // Create a server soc ket on a r andom port that is b ound to th e localhos t/127.0.0. 1 interfac e. | |
346 | // I.e only local proc esses can connect to this port . | |
347 | se rverSocket = new Ser verSocket( 0, 128, In etAddress. getByName( null)); | |
348 | co mpilerPool = new Com pilerPool( poolSize, this); | |
349 | Ra ndom rnd = new Rando m(); | |
350 | my Cookie = r nd.nextLon g(); | |
351 | th eLog = new PrintWrit er(logfile ); | |
352 | lo g("Javac s erver star ted. port= " + getPor t() + " da te=" + (ne w java.uti l.Date()) + " with p oolsize=" + poolSize ); | |
353 | fl ushLog(); | |
354 | } | |
355 | ||
356 | /** | |
357 | * For k a backgr ound proce ss. Return s the comm and line u sed that c an be prin ted if som ething fai led. | |
358 | */ | |
359 | privat e static S tring fork (String sj avac, Stri ng portfil e, String logfile, i nt poolsiz e, int kee palive, | |
360 | final Pr intStream err, Strin g stdouter rfile, Str ing backgr ound) | |
361 | throws I OException , ProblemE xception { | |
362 | if (stdouter rfile != n ull && std outerrfile .trim().eq uals("")) { | |
363 | stdouter rfile = nu ll; | |
364 | } | |
365 | fi nal String startserv er = "--st artserver: portfile=" + portfil e + ",logf ile=" + lo gfile + ", stdouterrf ile=" + st douterrfil e + ",pool size=" + p oolsize + ",keepaliv e="+ keepa live; | |
366 | ||
367 | if (backgrou nd.equals( "true")) { | |
368 | sjavac + = "%20" + startserve r; | |
369 | sjavac = sjavac.re placeAll(" %20", " ") ; | |
370 | sjavac = sjavac.re placeAll(" %2C", ",") ; | |
371 | // If th e java/sh/ cmd launch er fails t he failure will be c aptured by stdouterr because o f the redi rection he re. | |
372 | String[] cmd = {"/ bin/sh", " -c", sjava c + " >> " + stdoute rrfile + " 2>&1"}; | |
373 | if (!(ne w File("/b in/sh")).c anExecute( )) { | |
374 | Arra yList<Stri ng> wincmd = new Arr ayList<Str ing>(); | |
375 | winc md.add("cm d"); | |
376 | winc md.add("/c "); | |
377 | winc md.add("st art"); | |
378 | winc md.add("cm d"); | |
379 | winc md.add("/c "); | |
380 | winc md.add(sja vac + " >> " + stdou terrfile + " 2>&1"); | |
381 | cmd = wincmd.t oArray(new String[wi ncmd.size( )]); | |
382 | } | |
383 | Process pp = null; | |
384 | try { | |
385 | pp = Runtime.g etRuntime( ).exec(cmd ); | |
386 | } catch (Exception e) { | |
387 | e.pr intStackTr ace(err); | |
388 | e.pr intStackTr ace(new Pr intWriter( stdouterrf ile)); | |
389 | } | |
390 | StringBu ilder rs = new Strin gBuilder() ; | |
391 | for (Str ing s : cm d) { | |
392 | rs.a ppend(s + " "); | |
393 | } | |
394 | return r s.toString (); | |
395 | } | |
396 | ||
397 | // Do not sp awn a back ground ser ver, inste ad run it within the same JVM. | |
398 | Th read t = n ew Thread( ) { | |
399 | @Overrid e | |
400 | public v oid run() { | |
401 | try { | |
402 | JavacServe r.startSer ver(starts erver, err ); | |
403 | } ca tch (Throw able t) { | |
404 | t.printSta ckTrace(er r); | |
405 | } | |
406 | } | |
407 | }; | |
408 | t. start(); | |
409 | re turn ""; | |
410 | } | |
411 | ||
412 | /** | |
413 | * Exp ect this k ey on the next line read from the reader . | |
414 | */ | |
415 | privat e static b oolean exp ect(Buffer edReader i n, String key) throw s IOExcept ion { | |
416 | St ring s = i n.readLine (); | |
417 | if (s != nul l && s.equ als(key)) { | |
418 | return t rue; | |
419 | } | |
420 | re turn false ; | |
421 | } | |
422 | ||
423 | /** | |
424 | * Mak e a reques t to the s erver only to get th e maximum possible h eap size t o use for compilatio ns. | |
425 | * | |
426 | * @pa ram port_f ile The po rt file us ed to sync hronize cr eation of this serve r. | |
427 | * @pa ram id The identify of the com pilation. | |
428 | * @pa ram out St andard out informati on. | |
429 | * @pa ram err St andard err informati on. | |
430 | * @re turn The m aximum hea p size in bytes. | |
431 | */ | |
432 | public static Sy sInfo conn ectGetSysI nfo(String serverSet tings, Pri ntStream o ut, PrintS tream err) { | |
433 | Sy sInfo sysi nfo = new SysInfo(-1 , -1); | |
434 | St ring id = Util.extra ctStringOp tion("id", serverSet tings); | |
435 | St ring portf ile = Util .extractSt ringOption ("portfile ", serverS ettings); | |
436 | tr y { | |
437 | PortFile pf = getP ortFile(po rtfile); | |
438 | useServe r(serverSe ttings, ne w String[0 ], | |
439 | new HashSe t<URI>(), | |
440 | new HashSe t<URI>(), | |
441 | new HashMa p<URI, Set <String>>( ), | |
442 | new HashMa p<String, Set<URI>>( ), | |
443 | new HashMa p<String, Set<String >>(), | |
444 | new HashMa p<String, String>(), | |
445 | sysinfo, o ut, err); | |
446 | } catch (Exc eption e) { | |
447 | e.printS tackTrace( err); | |
448 | } | |
449 | re turn sysin fo; | |
450 | } | |
451 | ||
452 | /** | |
453 | * Con nect and c ompile usi ng the jav ac server settings a nd the arg s. When us ing more a dvanced fe atures, th e sources_ to_compile and visib le_sources are | |
454 | * sup plied to t he server and meta d ata is ret urned in p ackage_art ifacts, pa ckage_depe ndencies a nd package _pubapis. | |
455 | */ | |
456 | privat e static i nt connect AndCompile (PortFile portFile, String id, String[] args, | |
457 | Set<URI> sourcesTo Compile, | |
458 | Set<URI> visibleSo urces, | |
459 | Map<Stri ng, Set<UR I>> packag eArtifacts , | |
460 | Map<Stri ng, Set<St ring>> pac kageDepend encies, | |
461 | Map<Stri ng, String > packageP ublicApis, | |
462 | SysInfo sysinfo, | |
463 | PrintStr eam out, | |
464 | PrintStr eam err) { | |
465 | in t rc = -3; | |
466 | tr y { | |
467 | int port = portFil e.contains PortInfo() ? portFil e.getPort( ) : 0; | |
468 | if (port == 0) { | |
469 | retu rn ERROR_B UT_TRY_AGA IN; | |
470 | } | |
471 | long coo kie = port File.getCo okie(); | |
472 | ||
473 | // Acqui re the loc alhost/127 .0.0.1 add ress. | |
474 | InetAddr ess addr = InetAddre ss.getByNa me(null); | |
475 | SocketAd dress sock addr = new InetSocke tAddress(a ddr, port) ; | |
476 | Socket s ock = new Socket(); | |
477 | int time outMs = CO NNECTION_T IMEOUT * 1 000; | |
478 | try { | |
479 | sock .connect(s ockaddr, t imeoutMs); | |
480 | } catch (java.net. ConnectExc eption e) { | |
481 | err. println("C ould not c onnect to javac serv er found i n portfile : " + port File.getFi lename() + " " + e); | |
482 | retu rn ERROR_B UT_TRY_AGA IN; | |
483 | } | |
484 | if (!soc k.isConnec ted()) { | |
485 | err. println("C ould not c onnect to javac serv er found i n portfile : " + port File.getFi lename()); | |
486 | retu rn ERROR_B UT_TRY_AGA IN; | |
487 | } | |
488 | Buffered Reader in = new Buff eredReader (new Input StreamRead er(sock.ge tInputStre am())); | |
489 | PrintWri ter sockou t = new Pr intWriter( sock.getOu tputStream ()); | |
490 | ||
491 | sockout. println(PR OTOCOL_COO KIE_VERSIO N); | |
492 | sockout. println("" + cookie) ; | |
493 | sockout. println(PR OTOCOL_CWD ); | |
494 | sockout. println(Sy stem.getPr operty("us er.dir")); | |
495 | sockout. println(PR OTOCOL_ID) ; | |
496 | sockout. println(id ); | |
497 | sockout. println(PR OTOCOL_ARG S); | |
498 | for (Str ing s : ar gs) { | |
499 | Stri ngBuffer b uf = new S tringBuffe r(); | |
500 | Stri ng[] paths = s.split (File.path Separator) ; | |
501 | int c = 0; | |
502 | for (String pa th : paths ) { | |
503 | File f = n ew File(pa th); | |
504 | if (f.isFi le() || f. isDirector y()) { | |
505 | buf.ap pend(f.get AbsolutePa th()); | |
506 | c++; | |
507 | if (c < paths.le ngth) { | |
508 | bu f.append(F ile.pathSe parator); | |
509 | } | |
510 | } else { | |
511 | buf = new String Buffer(s); | |
512 | break; | |
513 | } | |
514 | } | |
515 | sock out.printl n(buf.toSt ring()); | |
516 | } | |
517 | sockout. println(PR OTOCOL_SOU RCES_TO_CO MPILE); | |
518 | for (URI uri : sou rcesToComp ile) { | |
519 | sock out.printl n(uri.toSt ring()); | |
520 | } | |
521 | sockout. println(PR OTOCOL_VIS IBLE_SOURC ES); | |
522 | for (URI uri : vis ibleSource s) { | |
523 | sock out.printl n(uri.toSt ring()); | |
524 | } | |
525 | sockout. println(PR OTOCOL_END ); | |
526 | sockout. flush(); | |
527 | ||
528 | StringBu ffer stdou t = new St ringBuffer (); | |
529 | StringBu ffer stder r = new St ringBuffer (); | |
530 | ||
531 | if (!exp ect(in, PR OTOCOL_STD OUT)) { | |
532 | retu rn ERROR_F ATAL; | |
533 | } | |
534 | // Load stdout | |
535 | for (;;) { | |
536 | Stri ng l = in. readLine() ; | |
537 | if ( l == null) { | |
538 | return ERR OR_FATAL; | |
539 | } | |
540 | if ( l.equals(P ROTOCOL_ST DERR)) { | |
541 | break; | |
542 | } | |
543 | stdo ut.append( l); | |
544 | stdo ut.append( '\n'); | |
545 | } | |
546 | // Load stderr | |
547 | for (;;) { | |
548 | Stri ng l = in. readLine() ; | |
549 | if ( l == null) { | |
550 | return ERR OR_FATAL; | |
551 | } | |
552 | if ( l.equals(P ROTOCOL_PA CKAGE_ARTI FACTS)) { | |
553 | break; | |
554 | } | |
555 | stde rr.append( l); | |
556 | stde rr.append( '\n'); | |
557 | } | |
558 | // Load the packag e artifact s | |
559 | Set<URI> lastUriSe t = null; | |
560 | for (;;) { | |
561 | Stri ng l = in. readLine() ; | |
562 | if ( l == null) { | |
563 | return ERR OR_FATAL; | |
564 | } | |
565 | if ( l.equals(P ROTOCOL_PA CKAGE_DEPE NDENCIES)) { | |
566 | break; | |
567 | } | |
568 | if ( l.length() > 1 && l. charAt(0) == '+') { | |
569 | String pkg = l.subst ring(1); | |
570 | lastUriSet = new Has hSet<URI>( ); | |
571 | packageArt ifacts.put (pkg, last UriSet); | |
572 | } el se if (l.l ength() > 1 && lastU riSet != n ull) { | |
573 | lastUriSet .add(new U RI(l.subst ring(1))); | |
574 | } | |
575 | } | |
576 | // Load package de pendencies | |
577 | Set<Stri ng> lastPa ckageSet = null; | |
578 | for (;;) { | |
579 | Stri ng l = in. readLine() ; | |
580 | if ( l == null) { | |
581 | return ERR OR_FATAL; | |
582 | } | |
583 | if ( l.equals(P ROTOCOL_PA CKAGE_PUBL IC_APIS)) { | |
584 | break; | |
585 | } | |
586 | if ( l.length() > 1 && l. charAt(0) == '+') { | |
587 | String pkg = l.subst ring(1); | |
588 | lastPackag eSet = new HashSet<S tring>(); | |
589 | packageDep endencies. put(pkg, l astPackage Set); | |
590 | } el se if (l.l ength() > 1 && lastP ackageSet != null) { | |
591 | lastPackag eSet.add(l .substring (1)); | |
592 | } | |
593 | } | |
594 | // Load package pu bapis | |
595 | Map<Stri ng, String Buffer> tm p = new Ha shMap<Stri ng, String Buffer>(); | |
596 | StringBu ffer lastP ublicApi = null; | |
597 | for (;;) { | |
598 | Stri ng l = in. readLine() ; | |
599 | if ( l == null) { | |
600 | return ERR OR_FATAL; | |
601 | } | |
602 | if ( l.equals(P ROTOCOL_SY SINFO)) { | |
603 | break; | |
604 | } | |
605 | if ( l.length() > 1 && l. charAt(0) == '+') { | |
606 | String pkg = l.subst ring(1); | |
607 | lastPublic Api = new StringBuff er(); | |
608 | tmp.put(pk g, lastPub licApi); | |
609 | } el se if (l.l ength() > 1 && lastP ublicApi ! = null) { | |
610 | lastPublic Api.append (l.substri ng(1)); | |
611 | lastPublic Api.append ("\n"); | |
612 | } | |
613 | } | |
614 | for (Str ing p : tm p.keySet() ) { | |
615 | asse rt (packag ePublicApi s.get(p) = = null); | |
616 | Stri ng api = t mp.get(p). toString() ; | |
617 | pack agePublicA pis.put(p, api); | |
618 | } | |
619 | // Now r eading the max memor y possible . | |
620 | for (;;) { | |
621 | Stri ng l = in. readLine() ; | |
622 | if ( l == null) { | |
623 | return ERR OR_FATAL; | |
624 | } | |
625 | if ( l.equals(P ROTOCOL_RE TURN_CODE) ) { | |
626 | break; | |
627 | } | |
628 | if ( l.startsWi th("num_co res=") && sysinfo != null) { | |
629 | sysinfo.nu mCores = I nteger.par seInt(l.su bstring(10 )); | |
630 | } | |
631 | if ( l.startsWi th("max_me mory=") && sysinfo ! = null) { | |
632 | sysinfo.ma xMemory = Long.parse Long(l.sub string(11) ); | |
633 | } | |
634 | } | |
635 | String l = in.read Line(); | |
636 | if (l == null) { | |
637 | err. println("N o return v alue from the server !"); | |
638 | retu rn ERROR_F ATAL; | |
639 | } | |
640 | rc = Int eger.parse Int(l); | |
641 | out.prin t(stdout); | |
642 | err.prin t(stderr); | |
643 | } catch (Exc eption e) { | |
644 | e.printS tackTrace( err); | |
645 | } | |
646 | re turn rc; | |
647 | } | |
648 | ||
649 | /** | |
650 | * Run the serve r thread u ntil it ex its. Eithe r because of inactiv ity or bec ause the p ort file h as been de leted by s omeone els e, or over taken by s ome other | |
651 | * jav ac server. | |
652 | */ | |
653 | privat e void run (PortFile portFile, PrintStrea m err, int keepalive ) { | |
654 | bo olean file Deleted = false; | |
655 | lo ng timeSin ceLastComp ile; | |
656 | tr y { | |
657 | // Every 5 second (check_por tfile_inte rval) we t est if the portfile has disapp eared => q uit | |
658 | // Or if the last request wa s finished more than 125 secon ds ago => quit | |
659 | // 125 = seconds_o f_inactivi ty_before_ shutdown+c heck_portf ile_interv al | |
660 | serverSo cket.setSo Timeout(CH ECK_PORTFI LE_INTERVA L*1000); | |
661 | for (;;) { | |
662 | try { | |
663 | Socket s = serverSoc ket.accept (); | |
664 | CompilerTh read ct = compilerPo ol.grabCom pilerThrea d(); | |
665 | ct.setSock et(s); | |
666 | compilerPo ol.execute (ct); | |
667 | flushLog() ; | |
668 | } ca tch (java. net.Socket TimeoutExc eption e) { | |
669 | if (compil erPool.num ActiveRequ ests() > 0 ) { | |
670 | // Nev er quit wh ile there are active requests! | |
671 | contin ue; | |
672 | } | |
673 | // If this is the ti meout afte r the port file | |
674 | // has bee n deleted by us. The n we truly stop. | |
675 | if (fileDe leted) { | |
676 | log("Q uitting be cause of " +(keepaliv e+CHECK_PO RTFILE_INT ERVAL)+" s econds of inactivity !"); | |
677 | break; | |
678 | } | |
679 | // Check i f the port file is st ill there. | |
680 | if (!portF ile.exists ()) { | |
681 | // Tim e to quit because th e portfile was delet ed by anot her | |
682 | // pro cess, prob ably by th e makefile that is d one buildi ng. | |
683 | log("Q uitting be cause port file was d eleted!"); | |
684 | flushL og(); | |
685 | break; | |
686 | } | |
687 | // Check i f portfile .stop is s till there . | |
688 | if (portFi le.markedF orStop()) { | |
689 | // Tim e to quit because an other proc ess touche d the file | |
690 | // ser ver.port.s top to sig nal that t he server should sto p. | |
691 | // Thi s is neces sary on so me operati ng systems that lock | |
692 | // the port file hard! | |
693 | log("Q uitting be cause a po rtfile.sto p file was found!"); | |
694 | portFi le.delete( ); | |
695 | flushL og(); | |
696 | break; | |
697 | } | |
698 | // Does th e portfile still poi nt to me? | |
699 | if (!portF ile.stillM yValues()) { | |
700 | // Tim e to quit because an other buil d has star ted. | |
701 | log("Q uitting be cause port file is no w owned by another j avac serve r!"); | |
702 | flushL og(); | |
703 | break; | |
704 | } | |
705 | ||
706 | // Check h ow long si nce the la st request finished. | |
707 | long diff = System.c urrentTime Millis() - compilerP ool.lastRe questFinis hed(); | |
708 | if (diff < keepalive * 1000) { | |
709 | // Do not quit i f we have waited les s than 120 seconds. | |
710 | contin ue; | |
711 | } | |
712 | // Ok, tim e to quit because of inactivit y. Perhaps the build | |
713 | // was kil led and th e portfile not clean ed up prop erly. | |
714 | portFile.d elete(); | |
715 | fileDelete d = true; | |
716 | log("" + k eepalive + " seconds of inacti vity quitt ing in " | |
717 | + CHEC K_PORTFILE _INTERVAL + " second s!"); | |
718 | flushLog() ; | |
719 | // Now we have a sec ond 5 seco nd grace | |
720 | // period where java c remote r equests | |
721 | // that ha ve loaded the data f rom the | |
722 | // recentl y deleted portfile c an connect | |
723 | // and com plete thei r requests . | |
724 | } | |
725 | } | |
726 | } catch (Exc eption e) { | |
727 | e.printS tackTrace( err); | |
728 | e.printS tackTrace( theLog); | |
729 | flushLog (); | |
730 | } finally { | |
731 | compiler Pool.shutd own(); | |
732 | } | |
733 | lo ng realTim e = System .currentTi meMillis() - serverS tart; | |
734 | lo g("Shuttin g down."); | |
735 | lo g("Total w all clock time " + r ealTime + "ms build time " + t otalBuildT ime + "ms" ); | |
736 | fl ushLog(); | |
737 | } | |
738 | ||
739 | public static vo id cleanup (String... args) { | |
740 | St ring setti ngs = Util .findServe rSettings( args); | |
741 | if (settings == null) return; | |
742 | St ring portf ile = Util .extractSt ringOption ("portfile ", setting s); | |
743 | St ring backg round = Ut il.extract StringOpti on("backgr ound", set tings); | |
744 | if (backgrou nd != null && backgr ound.equal s("false") ) { | |
745 | // If th e server r uns within this jvm, then dele te the por tfile, | |
746 | // since this jvm is about t o exit soo n. | |
747 | File f = new File( portfile); | |
748 | f.delete (); | |
749 | } | |
750 | } | |
751 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.