1 | /** |
2 | JavaX runner version 17 |
3 | |
4 | Changes to v16: |
5 | -removed compile dependency on Eclipse package |
6 | -cleans cache on startup (all files older than 1 hour - might want to change in the future for long-running programs) |
7 | -better version string, -version and -sysprop options |
8 | -uses server-transpiled translators for quicker compilation |
9 | |
10 | */ |
11 | |
12 | class _x17 { |
13 | static final String version = "JavaX 17"; |
14 | |
15 | static boolean verbose = false, translate = false, list = false, virtualizeTranslators = true; |
16 | static String translateTo = null; |
17 | static boolean preferCached = false, safeOnly = false, noID = false, noPrefetch = false; |
18 | static List<String[]> mainTranslators = new ArrayList<String[]>(); |
19 | private static Map<Long, String> memSnippetCache = new HashMap<Long, String>(); |
20 | private static int processesStarted, compilations; |
21 | |
22 | // snippet ID -> md5 |
23 | private static HashMap<Long, String> prefetched = new HashMap<Long, String>(); |
24 | private static File virtCache; |
25 | |
26 | // doesn't work yet |
27 | private static Map<String, Class<?>> programCache = new HashMap<String, Class<?>>(); |
28 | static boolean cacheTranslators = false; |
29 | |
30 | // this should work (caches transpiled translators) |
31 | private static HashMap<Long, Object[]> translationCache = new HashMap<Long, Object[]>(); |
32 | static boolean cacheTranspiledTranslators = true; |
33 | |
34 | // which snippets are available pre-transpiled server-side? |
35 | private static Set<Long> hasTranspiledSet = new HashSet<Long>(); |
36 | static boolean useServerTranspiled = true; |
37 | |
38 | public static void main(String[] args) throws Exception { |
39 | File ioBaseDir = new File("."), inputDir = null, outputDir = null; |
40 | String src = null; |
41 | List<String> programArgs = new ArrayList<String>(); |
42 | |
43 | for (int i = 0; i < args.length; i++) { |
44 | String arg = args[i]; |
45 | |
46 | if (arg.equals("-version")) { |
47 | showVersion(); |
48 | return; |
49 | } |
50 | |
51 | if (arg.equals("-sysprop")) { |
52 | showSystemProperties(); |
53 | return; |
54 | } |
55 | |
56 | if (arg.equals("-v") || arg.equals("-verbose")) |
57 | verbose = true; |
58 | else if (arg.equals("-finderror")) |
59 | verbose = true; |
60 | else if (arg.equals("-offline") || arg.equalsIgnoreCase("-prefercached")) |
61 | preferCached = true; |
62 | else if (arg.equals("-novirt")) |
63 | virtualizeTranslators = false; |
64 | else if (arg.equals("-safeonly")) |
65 | safeOnly = true; |
66 | else if (arg.equals("-noid")) |
67 | noID = true; |
68 | else if (arg.equals("-nocachetranspiled")) |
69 | cacheTranspiledTranslators = false; |
70 | else if (arg.equals("-localtranspile")) |
71 | useServerTranspiled = false; |
72 | else if (arg.equals("translate")) |
73 | translate = true; |
74 | else if (arg.equals("list")) { |
75 | list = true; |
76 | virtualizeTranslators = false; // so they are silenced |
77 | } else if (arg.equals("run")) { |
78 | // it's the default command anyway |
79 | } else if (arg.startsWith("input=")) |
80 | inputDir = new File(arg.substring(6)); |
81 | else if (arg.startsWith("output=")) |
82 | outputDir = new File(arg.substring(7)); |
83 | else if (arg.equals("with")) |
84 | mainTranslators.add(new String[] {args[++i], null}); |
85 | else if (translate && arg.equals("to")) |
86 | translateTo = args[++i]; |
87 | else if (src == null) { |
88 | //System.out.println("src=" + arg); |
89 | src = arg; |
90 | } else |
91 | programArgs.add(arg); |
92 | } |
93 | |
94 | cleanCache(); |
95 | |
96 | if (useServerTranspiled) |
97 | noPrefetch = true; |
98 | |
99 | if (src == null) src = "."; |
100 | |
101 | // Might actually want to write to 2 disk caches (global/per program). |
102 | if (virtualizeTranslators && !preferCached) |
103 | virtCache = TempDirMaker_make(); |
104 | |
105 | if (inputDir != null) { |
106 | ioBaseDir = TempDirMaker_make(); |
107 | System.out.println("Taking input from: " + inputDir.getAbsolutePath()); |
108 | System.out.println("Output is in: " + new File(ioBaseDir, "output").getAbsolutePath()); |
109 | copyInput(inputDir, new File(ioBaseDir, "input")); |
110 | } |
111 | |
112 | javaxmain(src, ioBaseDir, translate, list, programArgs.toArray(new String[programArgs.size()])); |
113 | |
114 | if (outputDir != null) { |
115 | copyInput(new File(ioBaseDir, "output"), outputDir); |
116 | System.out.println("Output copied to: " + outputDir.getAbsolutePath()); |
117 | } |
118 | |
119 | if (verbose) { |
120 | // print stats |
121 | System.out.println("Processes started: " + processesStarted + ", compilations: " + compilations); |
122 | } |
123 | } |
124 | |
125 | public static void javaxmain(String src, File ioDir, boolean translate, boolean list, |
126 | String[] args) throws Exception { |
127 | File srcDir; |
128 | if (isSnippetID(src)) { |
129 | prefetch(src); |
130 | srcDir = loadSnippetAsMainJava(src); |
131 | } else { |
132 | srcDir = new File(src); |
133 | |
134 | // if the argument is a file, it is assumed to be main.java |
135 | if (srcDir.isFile()) { |
136 | srcDir = TempDirMaker_make(); |
137 | copy(new File(src), new File(srcDir, "main.java")); |
138 | } |
139 | |
140 | if (!new File(srcDir, "main.java").exists()) { |
141 | showVersion(); |
142 | System.out.println("No main.java found, exiting"); |
143 | return; |
144 | } |
145 | } |
146 | |
147 | // translate |
148 | |
149 | List<File> libraries = new ArrayList<File>(); |
150 | File X = topLevelTranslate(srcDir, libraries); |
151 | System.err.println("Translated " + src); |
152 | |
153 | // save prefetch data |
154 | if (isSnippetID(src)) |
155 | savePrefetchData(src); |
156 | |
157 | // list or run |
158 | |
159 | if (translate) { |
160 | File to = X; |
161 | if (translateTo != null) |
162 | if (new File(translateTo).isDirectory()) |
163 | to = new File(translateTo, "main.java"); |
164 | else |
165 | to = new File(translateTo); |
166 | if (to != X) |
167 | copy(new File(X, "main.java"), to); |
168 | System.out.println("Program translated to: " + to.getAbsolutePath()); |
169 | } else if (list) |
170 | System.out.println(loadTextFile(new File(X, "main.java").getPath(), null)); |
171 | else |
172 | javax2(X, ioDir, false, false, libraries, args, null); |
173 | } |
174 | |
175 | private static void prefetch(String mainSnippetID) throws IOException { |
176 | if (noPrefetch) return; |
177 | |
178 | long mainID = parseSnippetID(mainSnippetID); |
179 | String s = mainID + " " + loadTextFile(new File(System.getProperty("user.home"), ".tinybrain/prefetch/" + mainID + ".txt").getPath(), ""); |
180 | String[] ids = s.trim().split(" "); |
181 | if (ids.length > 1) { |
182 | String url = "http://tinybrain.de:8080/tb-int/prefetch.php?ids=" + URLEncoder.encode(s, "UTF-8"); |
183 | String data = loadPage(new URL(url)); |
184 | String[] split = data.split(" "); |
185 | if (split.length == ids.length) |
186 | for (int i = 0; i < ids.length; i++) |
187 | prefetched.put(parseSnippetID(ids[i]), split[i]); |
188 | } |
189 | } |
190 | |
191 | private static void savePrefetchData(String mainSnippetID) throws IOException { |
192 | List<String> ids = new ArrayList<String>(); |
193 | long mainID = parseSnippetID(mainSnippetID); |
194 | |
195 | for (long id : memSnippetCache.keySet()) |
196 | if (id != mainID) |
197 | ids.add(String.valueOf(id)); |
198 | |
199 | saveTextFile(new File(System.getProperty("user.home"),".tinybrain/prefetch/" + mainID + ".txt").getPath(), join(" ", ids)); |
200 | } |
201 | |
202 | static File topLevelTranslate(File srcDir, List<File> libraries_out) throws Exception { |
203 | File X = srcDir; |
204 | X = applyTranslators(X, mainTranslators, libraries_out); // translators supplied on command line (unusual) |
205 | |
206 | // actual inner translation of the JavaX source |
207 | X = defaultTranslate(X, libraries_out); |
208 | return X; |
209 | } |
210 | |
211 | private static File defaultTranslate(File x, List<File> libraries_out) throws Exception { |
212 | x = luaPrintToJavaPrint(x); |
213 | x = repeatAutoTranslate(x, libraries_out); |
214 | return x; |
215 | } |
216 | |
217 | private static File repeatAutoTranslate(File x, List<File> libraries_out) throws Exception { |
218 | while (true) { |
219 | File y = autoTranslate(x, libraries_out); |
220 | if (y == x) |
221 | return x; |
222 | x = y; |
223 | } |
224 | } |
225 | |
226 | private static File autoTranslate(File x, List<File> libraries_out) throws Exception { |
227 | String main = loadTextFile(new File(x, "main.java").getPath(), null); |
228 | List<String> lines = toLines(main); |
229 | List<String[]> translators = findTranslators(lines); |
230 | if (translators.isEmpty()) |
231 | return x; |
232 | |
233 | main = fromLines(lines); |
234 | File newDir = TempDirMaker_make(); |
235 | saveTextFile(new File(newDir, "main.java").getPath(), main); |
236 | return applyTranslators(newDir, translators, libraries_out); |
237 | } |
238 | |
239 | private static List<String[]> findTranslators(List<String> lines) { |
240 | List<String[]> translators = new ArrayList<String[]>(); |
241 | Pattern pattern = Pattern.compile("^!([0-9# \t]+)"); |
242 | Pattern pArgs = Pattern.compile("^\\s*\\((.*)\\)"); |
243 | for (ListIterator<String> iterator = lines.listIterator(); iterator.hasNext(); ) { |
244 | String line = iterator.next(); |
245 | line = line.trim(); |
246 | Matcher matcher = pattern.matcher(line); |
247 | if (matcher.find()) { |
248 | String[] t = matcher.group(1).split("[ \t]+"); |
249 | String rest = line.substring(matcher.end()); |
250 | String arg = null; |
251 | if (t.length == 1) { |
252 | Matcher mArgs = pArgs.matcher(rest); |
253 | if (mArgs.find()) |
254 | arg = mArgs.group(1); |
255 | } |
256 | for (String transi : t) |
257 | translators.add(new String[]{transi, arg}); |
258 | iterator.remove(); |
259 | } |
260 | } |
261 | return translators; |
262 | } |
263 | |
264 | public static List<String> toLines(String s) { |
265 | List<String> lines = new ArrayList<String>(); |
266 | int start = 0; |
267 | while (true) { |
268 | int i = toLines_nextLineBreak(s, start); |
269 | if (i < 0) { |
270 | if (s.length() > start) lines.add(s.substring(start)); |
271 | break; |
272 | } |
273 | |
274 | lines.add(s.substring(start, i)); |
275 | if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n') |
276 | i += 2; |
277 | else |
278 | ++i; |
279 | |
280 | start = i; |
281 | } |
282 | return lines; |
283 | } |
284 | |
285 | private static int toLines_nextLineBreak(String s, int start) { |
286 | for (int i = start; i < s.length(); i++) { |
287 | char c = s.charAt(i); |
288 | if (c == '\r' || c == '\n') |
289 | return i; |
290 | } |
291 | return -1; |
292 | } |
293 | |
294 | public static String fromLines(List<String> lines) { |
295 | StringBuilder buf = new StringBuilder(); |
296 | for (String line : lines) { |
297 | buf.append(line).append('\n'); |
298 | } |
299 | return buf.toString(); |
300 | } |
301 | |
302 | private static File applyTranslators(File x, List<String[]> translators, List<File> libraries_out) throws Exception { |
303 | for (String[] translator : translators) |
304 | x = applyTranslator(x, translator[0], translator[1], libraries_out); |
305 | return x; |
306 | } |
307 | |
308 | // also takes a library |
309 | private static File applyTranslator(File x, String translator, String arg, List<File> libraries_out) throws Exception { |
310 | if (verbose) |
311 | System.out.println("Using translator " + translator + " on sources in " + x.getPath()); |
312 | |
313 | File newDir = runTranslatorOnInput(translator, null, arg, x, !verbose, libraries_out); |
314 | |
315 | if (!new File(newDir, "main.java").exists()) { |
316 | throw new Exception("Translator " + translator + " did not generate main.java"); |
317 | // TODO: show translator output |
318 | } |
319 | if (verbose) |
320 | System.out.println("Translated with " + translator + " from " + x.getPath() + " to " + newDir.getPath()); |
321 | x = newDir; |
322 | return x; |
323 | } |
324 | |
325 | private static File luaPrintToJavaPrint(File x) throws IOException { |
326 | File newDir = TempDirMaker_make(); |
327 | String code = loadTextFile(new File(x, "main.java").getPath(), null); |
328 | code = luaPrintToJavaPrint(code); |
329 | if (verbose) |
330 | System.out.println(code); |
331 | saveTextFile(new File(newDir, "main.java").getPath(), code); |
332 | return newDir; |
333 | } |
334 | |
335 | public static String luaPrintToJavaPrint(String code) { |
336 | return ("\n" + code).replaceAll( |
337 | "(\n\\s*)print (\".*\")", |
338 | "$1System.out.println($2);").substring(1); |
339 | } |
340 | |
341 | public static File loadSnippetAsMainJava(String snippetID) throws IOException { |
342 | checkProgramSafety(snippetID); |
343 | File srcDir = TempDirMaker_make(); |
344 | saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippet(snippetID)); |
345 | return srcDir; |
346 | } |
347 | |
348 | public static File loadSnippetAsMainJavaVerified(String snippetID, String hash) throws IOException { |
349 | checkProgramSafety(snippetID); |
350 | File srcDir = TempDirMaker_make(); |
351 | saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippetVerified(snippetID, hash)); |
352 | return srcDir; |
353 | } |
354 | |
355 | /** returns output dir */ |
356 | private static File runTranslatorOnInput(String snippetID, String hash, String arg, File input, |
357 | boolean silent, |
358 | List<File> libraries_out) throws Exception { |
359 | long id = parseSnippetID(snippetID); |
360 | File libraryFile = DiskSnippetCache_getLibrary(id); |
361 | if (libraryFile != null) { |
362 | loadLibrary(snippetID, libraries_out, libraryFile); |
363 | return input; |
364 | } |
365 | |
366 | String[] args = arg != null ? new String[]{arg} : new String[0]; |
367 | |
368 | File srcDir = hash == null ? loadSnippetAsMainJava(snippetID) |
369 | : loadSnippetAsMainJavaVerified(snippetID, hash); |
370 | long mainJavaSize = new File(srcDir, "main.java").length(); |
371 | |
372 | if (mainJavaSize == 0) { // no text in snippet? assume it's a library |
373 | loadLibrary(snippetID, libraries_out, libraryFile); |
374 | return input; |
375 | } |
376 | |
377 | List<File> libraries = new ArrayList<File>(); |
378 | Object[] cached = translationCache.get(id); |
379 | if (cached != null) { |
380 | //System.err.println("Taking translator " + snippetID + " from cache!"); |
381 | srcDir = (File) cached[0]; |
382 | libraries = (List<File>) cached[1]; |
383 | } else if (hasTranspiledSet.contains(id)) { |
384 | System.err.println("Trying pretranspiled translator: #" + snippetID); |
385 | String transpiledSrc = getServerTranspiled(snippetID); |
386 | if (!transpiledSrc.isEmpty()) { |
387 | srcDir = TempDirMaker_make(); |
388 | saveTextFile(new File(srcDir, "main.java").getPath(), transpiledSrc); |
389 | translationCache.put(id, cached = new Object[] {srcDir, libraries}); |
390 | } |
391 | } |
392 | |
393 | File ioBaseDir = TempDirMaker_make(); |
394 | |
395 | /*Class<?> mainClass = programCache.get("" + parseSnippetID(snippetID)); |
396 | if (mainClass != null) |
397 | return runCached(ioBaseDir, input, args);*/ |
398 | // Doesn't work yet because virtualized directories are hardcoded in translator... |
399 | |
400 | if (cached == null) { |
401 | System.err.println("Translating translator #" + id); |
402 | srcDir = defaultTranslate(srcDir, libraries); |
403 | System.err.println("Translated translator #" + id); |
404 | if (cacheTranspiledTranslators) |
405 | translationCache.put(id, new Object[]{srcDir, libraries}); |
406 | } |
407 | |
408 | boolean runInProcess = false; |
409 | |
410 | if (virtualizeTranslators) { |
411 | if (verbose) System.out.println("Virtualizing translator"); |
412 | |
413 | //srcDir = applyTranslator(srcDir, "#2000351"); // I/O-virtualize the translator |
414 | // that doesn't work because it recurses infinitely... |
415 | |
416 | // So we do it right here: |
417 | String s = loadTextFile(new File(srcDir, "main.java").getPath(), null); |
418 | s = s.replaceAll("new\\s+File\\(", "virtual.newFile("); |
419 | s = s.replaceAll("new\\s+FileInputStream\\(", "virtual.newFileInputStream("); |
420 | s = s.replaceAll("new\\s+FileOutputStream\\(", "virtual.newFileOutputStream("); |
421 | s += "\n\n" + loadSnippet("#2000355"); // load class virtual |
422 | |
423 | // change baseDir |
424 | s = s.replace("virtual_baseDir = \"\";", |
425 | "virtual_baseDir = " + javaQuote(ioBaseDir.getAbsolutePath()) + ";"); |
426 | |
427 | // forward snippet cache (virtualized one) |
428 | File dir = virtCache != null ? virtCache : DiskSnippetCache_dir; |
429 | s = s.replace("static File DiskSnippetCache_dir;", |
430 | "static File DiskSnippetCache_dir = new File(" + javaQuote(dir.getAbsolutePath()) + ");"); |
431 | s = s.replace("static boolean preferCached = false;", "static boolean preferCached = true;"); |
432 | |
433 | if (verbose) { |
434 | System.out.println("==BEGIN VIRTUALIZED TRANSLATOR=="); |
435 | System.out.println(s); |
436 | System.out.println("==END VIRTUALIZED TRANSLATOR=="); |
437 | } |
438 | srcDir = TempDirMaker_make(); |
439 | saveTextFile(new File(srcDir, "main.java").getPath(), s); |
440 | |
441 | // TODO: silence translator also |
442 | runInProcess = true; |
443 | } |
444 | |
445 | return runJavaX(ioBaseDir, srcDir, input, silent, runInProcess, libraries, |
446 | args, cacheTranslators ? "" + id : null); |
447 | } |
448 | |
449 | private static String getServerTranspiled(String snippetID) throws IOException { |
450 | long id = parseSnippetID(snippetID); |
451 | URL url = new URL("http://tinybrain.de:8080/tb-int/get-transpiled.php?raw=1&id=" + id); |
452 | return loadPage(url); |
453 | } |
454 | |
455 | static void checkProgramSafety(String snippetID) throws IOException { |
456 | if (!safeOnly) return; |
457 | URL url = new URL("http://tinybrain.de:8080/tb-int/is-javax-safe.php?id=" + parseSnippetID(snippetID)); |
458 | String text = loadPage(url); |
459 | if (!text.startsWith("{\"safe\":\"1\"}")) |
460 | throw new RuntimeException("Translator not safe: #" + parseSnippetID(snippetID)); |
461 | } |
462 | |
463 | private static void loadLibrary(String snippetID, List<File> libraries_out, File libraryFile) throws IOException { |
464 | if (verbose) |
465 | System.out.println("Assuming " + snippetID + " is a library."); |
466 | |
467 | if (libraryFile == null) { |
468 | byte[] data = loadDataSnippetImpl(snippetID); |
469 | DiskSnippetCache_putLibrary(parseSnippetID(snippetID), data); |
470 | libraryFile = DiskSnippetCache_getLibrary(parseSnippetID(snippetID)); |
471 | } |
472 | |
473 | if (!libraries_out.contains(libraryFile)) |
474 | libraries_out.add(libraryFile); |
475 | } |
476 | |
477 | private static byte[] loadDataSnippetImpl(String snippetID) throws IOException { |
478 | byte[] data; |
479 | try { |
480 | URL url = new URL("http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" |
481 | + parseSnippetID(snippetID) + "&contentType=application/binary"); |
482 | System.err.println("Loading library: " + url); |
483 | data = loadBinaryPage(url.openConnection()); |
484 | if (verbose) |
485 | System.err.println("Bytes loaded: " + data.length); |
486 | } catch (FileNotFoundException e) { |
487 | throw new IOException("Binary snippet #" + snippetID + " not found or not public"); |
488 | } |
489 | return data; |
490 | } |
491 | |
492 | /** returns output dir */ |
493 | private static File runJavaX(File ioBaseDir, File originalSrcDir, File originalInput, |
494 | boolean silent, boolean runInProcess, |
495 | List<File> libraries, String[] args, String cacheAs) throws Exception { |
496 | File srcDir = new File(ioBaseDir, "src"); |
497 | File inputDir = new File(ioBaseDir, "input"); |
498 | File outputDir = new File(ioBaseDir, "output"); |
499 | copyInput(originalSrcDir, srcDir); |
500 | copyInput(originalInput, inputDir); |
501 | javax2(srcDir, ioBaseDir, silent, runInProcess, libraries, args, cacheAs); |
502 | return outputDir; |
503 | } |
504 | |
505 | private static void copyInput(File src, File dst) throws IOException { |
506 | copyDirectory(src, dst); |
507 | } |
508 | |
509 | public static boolean hasFile(File inputDir, String name) { |
510 | return new File(inputDir, name).exists(); |
511 | } |
512 | |
513 | public static void copyDirectory(File src, File dst) throws IOException { |
514 | if (verbose) System.out.println("Copying " + src.getAbsolutePath() + " to " + dst.getAbsolutePath()); |
515 | dst.mkdirs(); |
516 | File[] files = src.listFiles(); |
517 | if (files == null) return; |
518 | for (File file : files) { |
519 | File dst1 = new File(dst, file.getName()); |
520 | if (file.isDirectory()) |
521 | copyDirectory(file, dst1); |
522 | else { |
523 | if (verbose) System.out.println("Copying " + file.getAbsolutePath() + " to " + dst1.getAbsolutePath()); |
524 | copy(file, dst1); |
525 | } |
526 | } |
527 | } |
528 | |
529 | /** Quickly copy a file without a progress bar or any other fancy GUI... :) */ |
530 | public static void copy(File src, File dest) throws IOException { |
531 | FileInputStream inputStream = new FileInputStream(src); |
532 | FileOutputStream outputStream = new FileOutputStream(dest); |
533 | try { |
534 | copy(inputStream, outputStream); |
535 | inputStream.close(); |
536 | } finally { |
537 | outputStream.close(); |
538 | } |
539 | } |
540 | |
541 | public static void copy(InputStream in, OutputStream out) throws IOException { |
542 | byte[] buf = new byte[65536]; |
543 | while (true) { |
544 | int n = in.read(buf); |
545 | if (n <= 0) return; |
546 | out.write(buf, 0, n); |
547 | } |
548 | } |
549 | |
550 | /** writes safely (to temp file, then rename) */ |
551 | public static void saveTextFile(String fileName, String contents) throws IOException { |
552 | File file = new File(fileName); |
553 | File parentFile = file.getParentFile(); |
554 | if (parentFile != null) |
555 | parentFile.mkdirs(); |
556 | String tempFileName = fileName + "_temp"; |
557 | FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); |
558 | OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles); |
559 | PrintWriter printWriter = new PrintWriter(outputStreamWriter); |
560 | printWriter.print(contents); |
561 | printWriter.close(); |
562 | if (file.exists() && !file.delete()) |
563 | throw new IOException("Can't delete " + fileName); |
564 | |
565 | if (!new File(tempFileName).renameTo(file)) |
566 | throw new IOException("Can't rename " + tempFileName + " to " + fileName); |
567 | } |
568 | |
569 | /** writes safely (to temp file, then rename) */ |
570 | public static void saveBinaryFile(String fileName, byte[] contents) throws IOException { |
571 | File file = new File(fileName); |
572 | File parentFile = file.getParentFile(); |
573 | if (parentFile != null) |
574 | parentFile.mkdirs(); |
575 | String tempFileName = fileName + "_temp"; |
576 | FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); |
577 | fileOutputStream.write(contents); |
578 | fileOutputStream.close(); |
579 | if (file.exists() && !file.delete()) |
580 | throw new IOException("Can't delete " + fileName); |
581 | |
582 | if (!new File(tempFileName).renameTo(file)) |
583 | throw new IOException("Can't rename " + tempFileName + " to " + fileName); |
584 | } |
585 | |
586 | public static String loadTextFile(String fileName, String defaultContents) throws IOException { |
587 | if (!new File(fileName).exists()) |
588 | return defaultContents; |
589 | |
590 | FileInputStream fileInputStream = new FileInputStream(fileName); |
591 | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles); |
592 | return loadTextFile(inputStreamReader, (int) new File(fileName).length()); |
593 | } |
594 | |
595 | public static String loadTextFile(Reader reader, int length) throws IOException { |
596 | try { |
597 | char[] chars = new char[length]; |
598 | int n = reader.read(chars); |
599 | return new String(chars, 0, n); |
600 | } finally { |
601 | reader.close(); |
602 | } |
603 | } |
604 | |
605 | static File DiskSnippetCache_dir; |
606 | |
607 | public static void initDiskSnippetCache(File dir) { |
608 | DiskSnippetCache_dir = dir; |
609 | dir.mkdirs(); |
610 | } |
611 | |
612 | // Data files are immutable, use centralized cache |
613 | public static synchronized File DiskSnippetCache_getLibrary(long snippetID) throws IOException { |
614 | File file = new File(getGlobalCache(), "data_" + snippetID + ".jar"); |
615 | if (verbose) |
616 | System.out.println("Checking data cache: " + file.getPath()); |
617 | return file.exists() ? file : null; |
618 | } |
619 | |
620 | public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException { |
621 | return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null); |
622 | } |
623 | |
624 | private static File DiskSnippetCache_getFile(long snippetID) { |
625 | return new File(DiskSnippetCache_dir, "" + snippetID); |
626 | } |
627 | |
628 | public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException { |
629 | saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet); |
630 | } |
631 | |
632 | public static synchronized void DiskSnippetCache_putLibrary(long snippetID, byte[] data) throws IOException { |
633 | saveBinaryFile(new File(getGlobalCache(), "data_" + snippetID).getPath() + ".jar", data); |
634 | } |
635 | |
636 | public static File DiskSnippetCache_getDir() { |
637 | return DiskSnippetCache_dir; |
638 | } |
639 | |
640 | public static void initSnippetCache() { |
641 | if (DiskSnippetCache_dir == null) |
642 | initDiskSnippetCache(getGlobalCache()); |
643 | } |
644 | |
645 | private static File getGlobalCache() { |
646 | File file = new File(System.getProperty("user.home"), ".tinybrain/snippet-cache"); |
647 | file.mkdirs(); |
648 | return file; |
649 | } |
650 | |
651 | public static String loadSnippetVerified(String snippetID, String hash) throws IOException { |
652 | String text = loadSnippet(snippetID); |
653 | String realHash = getHash(text.getBytes("UTF-8")); |
654 | if (!realHash.equals(hash)) { |
655 | String msg; |
656 | if (hash.isEmpty()) |
657 | msg = "Here's your hash for " + snippetID + ", please put in your program: " + realHash; |
658 | else |
659 | msg = "Hash mismatch for " + snippetID + ": " + realHash + " (new) vs " + hash + " - has tinybrain.de been hacked??"; |
660 | throw new RuntimeException(msg); |
661 | } |
662 | return text; |
663 | } |
664 | |
665 | public static String getHash(byte[] data) { |
666 | return bytesToHex(getFullFingerprint(data)); |
667 | } |
668 | |
669 | public static byte[] getFullFingerprint(byte[] data) { |
670 | try { |
671 | return MessageDigest.getInstance("MD5").digest(data); |
672 | } catch (NoSuchAlgorithmException e) { |
673 | throw new RuntimeException(e); |
674 | } |
675 | } |
676 | |
677 | public static String bytesToHex(byte[] bytes) { |
678 | return bytesToHex(bytes, 0, bytes.length); |
679 | } |
680 | |
681 | public static String bytesToHex(byte[] bytes, int ofs, int len) { |
682 | StringBuilder stringBuilder = new StringBuilder(len*2); |
683 | for (int i = 0; i < len; i++) { |
684 | String s = "0" + Integer.toHexString(bytes[ofs+i]); |
685 | stringBuilder.append(s.substring(s.length()-2, s.length())); |
686 | } |
687 | return stringBuilder.toString(); |
688 | } |
689 | |
690 | public static String loadSnippet(String snippetID) throws IOException { |
691 | return loadSnippet(parseSnippetID(snippetID)); |
692 | } |
693 | |
694 | public static long parseSnippetID(String snippetID) { |
695 | return Long.parseLong(shortenSnippetID(snippetID)); |
696 | } |
697 | |
698 | private static String shortenSnippetID(String snippetID) { |
699 | if (snippetID.startsWith("#")) |
700 | snippetID = snippetID.substring(1); |
701 | String httpBlaBla = "http://tinybrain.de/"; |
702 | if (snippetID.startsWith(httpBlaBla)) |
703 | snippetID = snippetID.substring(httpBlaBla.length()); |
704 | return snippetID; |
705 | } |
706 | |
707 | public static boolean isSnippetID(String snippetID) { |
708 | snippetID = shortenSnippetID(snippetID); |
709 | return isInteger(snippetID) && Long.parseLong(snippetID) != 0; |
710 | } |
711 | |
712 | public static boolean isInteger(String s) { |
713 | return Pattern.matches("\\-?\\d+", s); |
714 | } |
715 | |
716 | public static String loadSnippet(long snippetID) throws IOException { |
717 | String text = memSnippetCache.get(snippetID); |
718 | if (text != null) |
719 | return text; |
720 | |
721 | initSnippetCache(); |
722 | text = DiskSnippetCache_get(snippetID); |
723 | if (preferCached && text != null) |
724 | return text; |
725 | |
726 | String md5 = text != null ? md5(text) : "-"; |
727 | if (text != null) { |
728 | String hash = prefetched.get(snippetID); |
729 | if (hash != null) { |
730 | if (md5.equals(hash)) { |
731 | memSnippetCache.put(snippetID, text); |
732 | return text; |
733 | } else |
734 | prefetched.remove(snippetID); // (maybe this is not necessary) |
735 | } |
736 | } |
737 | |
738 | try { |
739 | /*URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID); |
740 | text = loadPage(url);*/ |
741 | String theURL = "http://tinybrain.de:8080/getraw.php?id=" + snippetID + "&getmd5=1&utf8=1&usetranspiled=1"; |
742 | if (text != null) { |
743 | //System.err.println("MD5: " + md5); |
744 | theURL += "&md5=" + md5; |
745 | } |
746 | URL url = new URL(theURL); |
747 | String page = loadPage(url); |
748 | |
749 | // parse & drop transpilation flag available line |
750 | int i = page.indexOf('\n'); |
751 | boolean hasTranspiled = page.substring(0, i).trim().equals("1"); |
752 | if (hasTranspiled) |
753 | hasTranspiledSet.add(snippetID); |
754 | else |
755 | hasTranspiledSet.remove(snippetID); |
756 | page = page.substring(i+1); |
757 | |
758 | if (page.startsWith("==*#*==")) { |
759 | // same, keep text |
760 | //System.err.println("Snippet unchanged, keeping."); |
761 | } else { |
762 | // drop md5 line |
763 | i = page.indexOf('\n'); |
764 | String hash = page.substring(0, i).trim(); |
765 | text = page.substring(i+1); |
766 | |
767 | String myHash = md5(text); |
768 | if (myHash.equals(hash)) { |
769 | //System.err.println("Hash match: " + hash); |
770 | } else |
771 | System.err.println("Hash mismatch"); |
772 | } |
773 | } catch (FileNotFoundException e) { |
774 | e.printStackTrace(); |
775 | throw new IOException("Snippet #" + snippetID + " not found or not public"); |
776 | } |
777 | |
778 | memSnippetCache.put(snippetID, text); |
779 | |
780 | try { |
781 | initSnippetCache(); |
782 | DiskSnippetCache_put(snippetID, text); |
783 | } catch (IOException e) { |
784 | System.err.println("Minor warning: Couldn't save snippet to cache (" + DiskSnippetCache_getDir() + ")"); |
785 | } |
786 | |
787 | return text; |
788 | } |
789 | |
790 | private static String md5(String text) { |
791 | try { |
792 | return bytesToHex(md5impl(text.getBytes("UTF-8"))); // maybe different than the way PHP does it... |
793 | } catch (UnsupportedEncodingException e) { |
794 | throw new RuntimeException(e); |
795 | } |
796 | } |
797 | |
798 | public static byte[] md5impl(byte[] data) { |
799 | try { |
800 | return MessageDigest.getInstance("MD5").digest(data); |
801 | } catch (NoSuchAlgorithmException e) { |
802 | throw new RuntimeException(e); |
803 | } |
804 | } |
805 | |
806 | private static String loadPage(URL url) throws IOException { |
807 | System.err.println("Loading: " + url.toExternalForm()); |
808 | URLConnection con = url.openConnection(); |
809 | return loadPage(con, url); |
810 | } |
811 | |
812 | public static String loadPage(URLConnection con, URL url) throws IOException { |
813 | setHeaders(con); |
814 | String contentType = con.getContentType(); |
815 | if (contentType == null) |
816 | throw new IOException("Page could not be read: " + url); |
817 | //Log.info("Content-Type: " + contentType); |
818 | String charset = guessCharset(contentType); |
819 | //System.err.println("Charset: " + charset); |
820 | Reader r = new InputStreamReader(con.getInputStream(), charset); |
821 | StringBuilder buf = new StringBuilder(); |
822 | while (true) { |
823 | int ch = r.read(); |
824 | if (ch < 0) |
825 | break; |
826 | //Log.info("Chars read: " + buf.length()); |
827 | buf.append((char) ch); |
828 | } |
829 | return buf.toString(); |
830 | } |
831 | |
832 | public static byte[] loadBinaryPage(URLConnection con) throws IOException { |
833 | setHeaders(con); |
834 | ByteArrayOutputStream buf = new ByteArrayOutputStream(); |
835 | InputStream inputStream = con.getInputStream(); |
836 | while (true) { |
837 | int ch = inputStream.read(); |
838 | if (ch < 0) |
839 | break; |
840 | buf.write(ch); |
841 | } |
842 | inputStream.close(); |
843 | return buf.toByteArray(); |
844 | } |
845 | |
846 | private static void setHeaders(URLConnection con) throws IOException { |
847 | String computerID = getComputerID(); |
848 | if (computerID != null) |
849 | con.setRequestProperty("X-ComputerID", computerID); |
850 | } |
851 | |
852 | public static String guessCharset(String contentType) { |
853 | Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*"); |
854 | Matcher m = p.matcher(contentType); |
855 | /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */ |
856 | return m.matches() ? m.group(1) : "ISO-8859-1"; |
857 | } |
858 | |
859 | /** runs a transpiled set of sources */ |
860 | public static void javax2(File srcDir, File ioBaseDir, boolean silent, boolean runInProcess, |
861 | List<File> libraries, String[] args, String cacheAs) throws Exception { |
862 | File classesDir = TempDirMaker_make(); |
863 | String javacOutput = compileJava(srcDir, libraries, classesDir); |
864 | |
865 | // run |
866 | |
867 | if (verbose) System.out.println("Running program (" + srcDir.getAbsolutePath() |
868 | + ") on io dir " + ioBaseDir.getAbsolutePath() + (runInProcess ? "[in-process]" : "") + "\n"); |
869 | runProgram(javacOutput, classesDir, ioBaseDir, silent, runInProcess, libraries, args, cacheAs); |
870 | } |
871 | |
872 | static String compileJava(File srcDir, List<File> libraries, File classesDir) throws IOException { |
873 | ++compilations; |
874 | |
875 | // collect sources |
876 | |
877 | List<File> sources = new ArrayList<File>(); |
878 | if (verbose) System.out.println("Scanning for sources in " + srcDir.getPath()); |
879 | scanForSources(srcDir, sources, true); |
880 | if (sources.isEmpty()) |
881 | throw new IOException("No sources found"); |
882 | |
883 | // compile |
884 | |
885 | File optionsFile = File.createTempFile("javax", ""); |
886 | if (verbose) System.out.println("Compiling " + sources.size() + " source(s) to " + classesDir.getPath()); |
887 | String options = "-d " + bashQuote(classesDir.getPath()); |
888 | writeOptions(sources, libraries, optionsFile, options); |
889 | classesDir.mkdirs(); |
890 | return invokeJavac(optionsFile); |
891 | } |
892 | |
893 | private static void runProgram(String javacOutput, File classesDir, File ioBaseDir, |
894 | boolean silent, boolean runInProcess, |
895 | List<File> libraries, String[] args, String cacheAs) throws Exception { |
896 | // print javac output if compile failed and it hasn't been printed yet |
897 | boolean didNotCompile = !didCompile(classesDir); |
898 | if (verbose || didNotCompile) |
899 | System.out.println(javacOutput); |
900 | if (didNotCompile) |
901 | return; |
902 | |
903 | if (runInProcess |
904 | || (ioBaseDir.getAbsolutePath().equals(new File(".").getAbsolutePath()) && !silent)) { |
905 | runProgramQuick(classesDir, libraries, args, cacheAs); |
906 | return; |
907 | } |
908 | |
909 | boolean echoOK = false; |
910 | // TODO: add libraries to class path |
911 | String bashCmd = "(cd " + bashQuote(ioBaseDir.getAbsolutePath()) + " && (java -cp " |
912 | + bashQuote(classesDir.getAbsolutePath()) + " main" + (echoOK ? "; echo ok" : "") + "))"; |
913 | if (verbose) System.out.println(bashCmd); |
914 | String output = backtick(bashCmd); |
915 | if (verbose || !silent) |
916 | System.out.println(output); |
917 | } |
918 | |
919 | static boolean didCompile(File classesDir) { |
920 | return hasFile(classesDir, "main.class"); |
921 | } |
922 | |
923 | private static void runProgramQuick(File classesDir, List<File> libraries, |
924 | String[] args, String cacheAs) throws Exception { |
925 | // collect urls |
926 | URL[] urls = new URL[libraries.size()+1]; |
927 | urls[0] = classesDir.toURI().toURL(); |
928 | for (int i = 0; i < libraries.size(); i++) |
929 | urls[i+1] = libraries.get(i).toURI().toURL(); |
930 | |
931 | // make class loader |
932 | URLClassLoader classLoader = new URLClassLoader(urls); |
933 | |
934 | // load JavaX main class |
935 | Class<?> mainClass = classLoader.loadClass("main"); |
936 | |
937 | if (cacheAs != null) |
938 | programCache.put(cacheAs, mainClass); |
939 | |
940 | // run main method |
941 | Method main = mainClass.getMethod("main", String[].class); |
942 | main.invoke(null, (Object) args); |
943 | } |
944 | |
945 | private static String invokeJavac(File optionsFile) throws IOException { |
946 | String output; |
947 | try { |
948 | output = invokeEcj(optionsFile); |
949 | } catch (Exception e) { |
950 | if (verbose) { |
951 | System.err.println("ecj not found or misconfigured - using javac"); |
952 | e.printStackTrace(); |
953 | } |
954 | output = backtick("javac " + bashQuote("@" + optionsFile.getPath())); |
955 | } |
956 | if (verbose) System.out.println(output); |
957 | return output; |
958 | } |
959 | |
960 | // throws ClassNotFoundException if ecj is not in classpath |
961 | static String invokeEcj(File optionsFile) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
962 | Class batchCompiler = getEclipseCompiler(); |
963 | |
964 | StringWriter writer = new StringWriter(); |
965 | PrintWriter printWriter = new PrintWriter(writer); |
966 | |
967 | // add more eclipse options in the line below |
968 | |
969 | String[] args = { "@" + optionsFile.getPath(), |
970 | "-source", "1.7", |
971 | "-nowarn" |
972 | }; |
973 | Method compile = batchCompiler.getDeclaredMethod("compile", args.getClass(), PrintWriter.class, PrintWriter.class, |
974 | Class.forName("org.eclipse.jdt.core.compiler.CompilationProgress")); |
975 | compile.invoke(null, args, printWriter, printWriter, null); |
976 | return writer.toString(); |
977 | } |
978 | |
979 | private static Class<?> getEclipseCompiler() throws ClassNotFoundException { |
980 | return Class.forName("org.eclipse.jdt.core.compiler.batch.BatchCompiler"); |
981 | } |
982 | |
983 | private static void writeOptions(List<File> sources, List<File> libraries, |
984 | File optionsFile, String moreOptions) throws IOException { |
985 | FileWriter writer = new FileWriter(optionsFile); |
986 | for (File source : sources) |
987 | writer.write(bashQuote(source.getPath()) + " "); |
988 | if (!libraries.isEmpty()) { |
989 | List<String> cp = new ArrayList<String>(); |
990 | for (File lib : libraries) |
991 | cp.add(lib.getAbsolutePath()); |
992 | writer.write("-cp " + bashQuote(join(File.pathSeparator, cp)) + " "); |
993 | } |
994 | writer.write(moreOptions); |
995 | writer.close(); |
996 | } |
997 | |
998 | static void scanForSources(File source, List<File> sources, boolean topLevel) { |
999 | if (source.isFile() && source.getName().endsWith(".java")) |
1000 | sources.add(source); |
1001 | else if (source.isDirectory() && !isSkippedDirectoryName(source.getName(), topLevel)) { |
1002 | File[] files = source.listFiles(); |
1003 | for (File file : files) |
1004 | scanForSources(file, sources, false); |
1005 | } |
1006 | } |
1007 | |
1008 | private static boolean isSkippedDirectoryName(String name, boolean topLevel) { |
1009 | if (topLevel) return false; // input or output ok as highest directory (intentionally specified by user, not just found by a directory scan in which case we probably don't want it. it's more like heuristics actually.) |
1010 | return name.equalsIgnoreCase("input") || name.equalsIgnoreCase("output"); |
1011 | } |
1012 | |
1013 | public static String backtick(String cmd) throws IOException { |
1014 | ++processesStarted; |
1015 | File outFile = File.createTempFile("_backtick", ""); |
1016 | File scriptFile = File.createTempFile("_backtick", isWindows() ? ".bat" : ""); |
1017 | |
1018 | String command = cmd + ">" + bashQuote(outFile.getPath()) + " 2>&1"; |
1019 | //Log.info("[Backtick] " + command); |
1020 | try { |
1021 | saveTextFile(scriptFile.getPath(), command); |
1022 | String[] command2; |
1023 | if (isWindows()) |
1024 | command2 = new String[] { scriptFile.getPath() }; |
1025 | else |
1026 | command2 = new String[] { "/bin/bash", scriptFile.getPath() }; |
1027 | Process process = Runtime.getRuntime().exec(command2); |
1028 | try { |
1029 | process.waitFor(); |
1030 | } catch (InterruptedException e) { |
1031 | throw new RuntimeException(e); |
1032 | } |
1033 | process.exitValue(); |
1034 | return loadTextFile(outFile.getPath(), ""); |
1035 | } finally { |
1036 | scriptFile.delete(); |
1037 | } |
1038 | } |
1039 | |
1040 | /** possibly improvable */ |
1041 | public static String javaQuote(String text) { |
1042 | return bashQuote(text); |
1043 | } |
1044 | |
1045 | /** possibly improvable */ |
1046 | public static String bashQuote(String text) { |
1047 | if (text == null) return null; |
1048 | return "\"" + text |
1049 | .replace("\\", "\\\\") |
1050 | .replace("\"", "\\\"") |
1051 | .replace("\n", "\\n") |
1052 | .replace("\r", "\\r") + "\""; |
1053 | } |
1054 | |
1055 | public final static String charsetForTextFiles = "UTF8"; |
1056 | |
1057 | static long TempDirMaker_lastValue; |
1058 | |
1059 | public static File TempDirMaker_make() { |
1060 | File dir = new File(System.getProperty("user.home"), ".javax/" + TempDirMaker_newValue()); |
1061 | dir.mkdirs(); |
1062 | return dir; |
1063 | } |
1064 | |
1065 | private static long TempDirMaker_newValue() { |
1066 | long value; |
1067 | do |
1068 | value = System.currentTimeMillis(); |
1069 | while (value == TempDirMaker_lastValue); |
1070 | TempDirMaker_lastValue = value; |
1071 | return value; |
1072 | } |
1073 | |
1074 | public static String join(String glue, Iterable<String> strings) { |
1075 | StringBuilder buf = new StringBuilder(); |
1076 | Iterator<String> i = strings.iterator(); |
1077 | if (i.hasNext()) { |
1078 | buf.append(i.next()); |
1079 | while (i.hasNext()) |
1080 | buf.append(glue).append(i.next()); |
1081 | } |
1082 | return buf.toString(); |
1083 | } |
1084 | |
1085 | public static boolean isWindows() { |
1086 | return System.getProperty("os.name").contains("Windows"); |
1087 | } |
1088 | |
1089 | public static String makeRandomID(int length) { |
1090 | Random random = new Random(); |
1091 | char[] id = new char[length]; |
1092 | for (int i = 0; i< id.length; i++) |
1093 | id[i] = (char) ((int) 'a' + random.nextInt(26)); |
1094 | return new String(id); |
1095 | } |
1096 | |
1097 | static String computerID; |
1098 | public static String getComputerID() throws IOException { |
1099 | if (noID) return null; |
1100 | if (computerID == null) { |
1101 | File file = new File(System.getProperty("user.home"), ".tinybrain/computer-id"); |
1102 | computerID = loadTextFile(file.getPath(), null); |
1103 | if (computerID == null) { |
1104 | computerID = makeRandomID(12); |
1105 | saveTextFile(file.getPath(), computerID); |
1106 | } |
1107 | if (verbose) |
1108 | System.out.println("Local computer ID: " + computerID); |
1109 | } |
1110 | return computerID; |
1111 | } |
1112 | |
1113 | static int fileDeletions; |
1114 | |
1115 | static void cleanCache() { |
1116 | if (verbose) |
1117 | System.out.println("Cleaning cache"); |
1118 | fileDeletions = 0; |
1119 | File javax = new File(System.getProperty("user.home"), ".javax"); |
1120 | long now = System.currentTimeMillis(); |
1121 | File[] files = javax.listFiles(); |
1122 | if (files != null) for (File dir : files) { |
1123 | if (dir.isDirectory() && Pattern.compile("\\d+").matcher(dir.getName()).matches()) { |
1124 | long time = Long.parseLong(dir.getName()); |
1125 | long seconds = (now - time) / 1000; |
1126 | long minutes = seconds / 60; |
1127 | long hours = minutes / 60; |
1128 | if (hours >= 1) { |
1129 | //System.out.println("Can delete " + dir.getAbsolutePath() + ", age: " + hours + " h"); |
1130 | removeDir(dir); |
1131 | } |
1132 | } |
1133 | } |
1134 | if (verbose && fileDeletions != 0) |
1135 | System.out.println("Cleaned cache. File deletions: " + fileDeletions); |
1136 | } |
1137 | |
1138 | static void removeDir(File dir) { |
1139 | if (dir.getAbsolutePath().indexOf(".javax") < 0) // security check! |
1140 | return; |
1141 | for (File f : dir.listFiles()) { |
1142 | if (f.isDirectory()) |
1143 | removeDir(f); |
1144 | else { |
1145 | if (verbose) |
1146 | System.out.println("Deleting " + f.getAbsolutePath()); |
1147 | f.delete(); |
1148 | ++fileDeletions; |
1149 | } |
1150 | } |
1151 | dir.delete(); |
1152 | } |
1153 | |
1154 | static void showSystemProperties() { |
1155 | System.out.println("System properties:\n"); |
1156 | for (Map.Entry<Object, Object> entry : System.getProperties().entrySet()) { |
1157 | System.out.println(" " + entry.getKey() + " = " + entry.getValue()); |
1158 | } |
1159 | System.out.println(); |
1160 | } |
1161 | |
1162 | static void showVersion() { |
1163 | //showSystemProperties(); |
1164 | boolean eclipseFound = hasEclipseCompiler(); |
1165 | //String platform = System.getProperty("java.vendor") + " " + System.getProperty("java.runtime.name") + " " + System.getProperty("java.version"); |
1166 | String platform = System.getProperty("java.vm.name") + " " + System.getProperty("java.version"); |
1167 | String os = System.getProperty("os.name"), arch = System.getProperty("os.arch"); |
1168 | System.out.println("This is " + version + "."); |
1169 | System.out.println("[Details: " + |
1170 | (eclipseFound ? "Eclipse compiler (good)" : "javac (not so good)") |
1171 | + ", " + platform + ", " + arch + ", " + os + "]"); |
1172 | } |
1173 | |
1174 | private static boolean hasEclipseCompiler() { |
1175 | boolean compilerFound = false; |
1176 | try { getEclipseCompiler(); compilerFound = true; } catch (ClassNotFoundException e) {} |
1177 | return compilerFound; |
1178 | } |
1179 | } |
Began life as a copy of #1000397
Snippet is not live.
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #2000485 |
Snippet name: | _x17.java (JavaX 17, embeddable) |
Eternal ID of this version: | #2000485/1 |
Text MD5: | 3427e2f2d77e48993573243b2b971a30 |
Author: | stefan |
Category: | |
Type: | New Tinybrain snippet |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-08-02 18:35:41 |
Source code size: | 43190 bytes / 1179 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 599 / 188 |
Referenced in: | [show references] |