381 for(;;) {
382 final int l = in.read(buf);
383 if(l == -1) {
384 return Base64.getUrlEncoder().withoutPadding().encodeToString(digest.digest());
385 }
386 digest.update(buf, 0, l);
387 }
388 }
389 } else if(protocol.equals("file")) {
390 // Development
391 final String fileStr = url.getFile();
392 final String className = OptimisticTypesPersistence.class.getName();
393 final int packageNameLen = className.lastIndexOf('.');
394 final String dirStr = fileStr.substring(0, fileStr.length() - packageNameLen - 1);
395 final File dir = new File(dirStr);
396 return "dev-" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date(getLastModifiedClassFile(
397 dir, 0L)));
398 } else if(protocol.equals("jrt")) {
399 return getJrtVersionDirName();
400 } else {
401 throw new AssertionError();
402 }
403 }
404
405 private static long getLastModifiedClassFile(final File dir, final long max) {
406 long currentMax = max;
407 for(final File f: dir.listFiles()) {
408 if(f.getName().endsWith(".class")) {
409 final long lastModified = f.lastModified();
410 if (lastModified > currentMax) {
411 currentMax = lastModified;
412 }
413 } else if (f.isDirectory()) {
414 final long lastModified = getLastModifiedClassFile(f, currentMax);
415 if (lastModified > currentMax) {
416 currentMax = lastModified;
417 }
418 }
419 }
420 return currentMax;
421 }
539 private static long getTime(final Path path) {
540 try {
541 return Files.getLastModifiedTime(path).toMillis();
542 } catch (final IOException e) {
543 // All files for which we can't retrieve the last modified date will be considered oldest.
544 return -1L;
545 }
546 }
547 }
548
549 private static int getMaxFiles() {
550 final String str = Options.getStringProperty("nashorn.typeInfo.maxFiles", null);
551 if (str == null) {
552 return DEFAULT_MAX_FILES;
553 } else if ("unlimited".equals(str)) {
554 return UNLIMITED_FILES;
555 }
556 return Math.max(0, Integer.parseInt(str));
557 }
558
559 // version directory name if nashorn is loaded from jrt:/ URL
560 private static String getJrtVersionDirName() throws Exception {
561 final FileSystem fs = getJrtFileSystem();
562 // consider all .class resources under nashorn module to compute checksum
563 final Path nashorn = fs.getPath("/jdk.scripting.nashorn");
564 if (! Files.isDirectory(nashorn)) {
565 throw new FileNotFoundException("missing /jdk.scripting.nashorn dir in jrt fs");
566 }
567 final MessageDigest digest = MessageDigest.getInstance("SHA-1");
568 Files.walk(nashorn).forEach(new Consumer<Path>() {
569 @Override
570 public void accept(final Path p) {
571 // take only the .class resources.
572 if (Files.isRegularFile(p) && p.toString().endsWith(".class")) {
573 try {
574 digest.update(Files.readAllBytes(p));
575 } catch (final IOException ioe) {
576 throw new UncheckedIOException(ioe);
577 }
578 }
579 }
580 });
581 return Base64.getUrlEncoder().withoutPadding().encodeToString(digest.digest());
582 }
583
584 // get the default jrt FileSystem instance
585 private static FileSystem getJrtFileSystem() {
|
381 for(;;) {
382 final int l = in.read(buf);
383 if(l == -1) {
384 return Base64.getUrlEncoder().withoutPadding().encodeToString(digest.digest());
385 }
386 digest.update(buf, 0, l);
387 }
388 }
389 } else if(protocol.equals("file")) {
390 // Development
391 final String fileStr = url.getFile();
392 final String className = OptimisticTypesPersistence.class.getName();
393 final int packageNameLen = className.lastIndexOf('.');
394 final String dirStr = fileStr.substring(0, fileStr.length() - packageNameLen - 1);
395 final File dir = new File(dirStr);
396 return "dev-" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date(getLastModifiedClassFile(
397 dir, 0L)));
398 } else if(protocol.equals("jrt")) {
399 return getJrtVersionDirName();
400 } else {
401 throw new AssertionError("unknown protocol");
402 }
403 }
404
405 private static long getLastModifiedClassFile(final File dir, final long max) {
406 long currentMax = max;
407 for(final File f: dir.listFiles()) {
408 if(f.getName().endsWith(".class")) {
409 final long lastModified = f.lastModified();
410 if (lastModified > currentMax) {
411 currentMax = lastModified;
412 }
413 } else if (f.isDirectory()) {
414 final long lastModified = getLastModifiedClassFile(f, currentMax);
415 if (lastModified > currentMax) {
416 currentMax = lastModified;
417 }
418 }
419 }
420 return currentMax;
421 }
539 private static long getTime(final Path path) {
540 try {
541 return Files.getLastModifiedTime(path).toMillis();
542 } catch (final IOException e) {
543 // All files for which we can't retrieve the last modified date will be considered oldest.
544 return -1L;
545 }
546 }
547 }
548
549 private static int getMaxFiles() {
550 final String str = Options.getStringProperty("nashorn.typeInfo.maxFiles", null);
551 if (str == null) {
552 return DEFAULT_MAX_FILES;
553 } else if ("unlimited".equals(str)) {
554 return UNLIMITED_FILES;
555 }
556 return Math.max(0, Integer.parseInt(str));
557 }
558
559 private static final String JRT_NASHORN_DIR = "/modules/jdk.scripting.nashorn";
560
561 // version directory name if nashorn is loaded from jrt:/ URL
562 private static String getJrtVersionDirName() throws Exception {
563 final FileSystem fs = getJrtFileSystem();
564 // consider all .class resources under nashorn module to compute checksum
565 final Path nashorn = fs.getPath(JRT_NASHORN_DIR);
566 if (! Files.isDirectory(nashorn)) {
567 throw new FileNotFoundException("missing " + JRT_NASHORN_DIR + " dir in jrt fs");
568 }
569 final MessageDigest digest = MessageDigest.getInstance("SHA-1");
570 Files.walk(nashorn).forEach(new Consumer<Path>() {
571 @Override
572 public void accept(final Path p) {
573 // take only the .class resources.
574 if (Files.isRegularFile(p) && p.toString().endsWith(".class")) {
575 try {
576 digest.update(Files.readAllBytes(p));
577 } catch (final IOException ioe) {
578 throw new UncheckedIOException(ioe);
579 }
580 }
581 }
582 });
583 return Base64.getUrlEncoder().withoutPadding().encodeToString(digest.digest());
584 }
585
586 // get the default jrt FileSystem instance
587 private static FileSystem getJrtFileSystem() {
|