371 try (final InputStream in = file.openStream()) { 372 final byte[] buf = new byte[128*1024]; 373 final MessageDigest digest = MessageDigest.getInstance("SHA-1"); 374 for(;;) { 375 final int l = in.read(buf); 376 if(l == -1) { 377 return Base64.getUrlEncoder().withoutPadding().encodeToString(digest.digest()); 378 } 379 digest.update(buf, 0, l); 380 } 381 } 382 } else if(protocol.equals("file")) { 383 // Development 384 final String fileStr = url.getFile(); 385 final String className = OptimisticTypesPersistence.class.getName(); 386 final int packageNameLen = className.lastIndexOf('.'); 387 final String dirStr = fileStr.substring(0, fileStr.length() - packageNameLen - 1); 388 final File dir = new File(dirStr); 389 return "dev-" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date(getLastModifiedClassFile( 390 dir, 0L))); 391 } else { 392 throw new AssertionError(); 393 } 394 } 395 396 private static long getLastModifiedClassFile(final File dir, final long max) { 397 long currentMax = max; 398 for(final File f: dir.listFiles()) { 399 if(f.getName().endsWith(".class")) { 400 final long lastModified = f.lastModified(); 401 if (lastModified > currentMax) { 402 currentMax = lastModified; 403 } 404 } else if (f.isDirectory()) { 405 final long lastModified = getLastModifiedClassFile(f, currentMax); 406 if (lastModified > currentMax) { 407 currentMax = lastModified; 408 } 409 } 410 } | 371 try (final InputStream in = file.openStream()) { 372 final byte[] buf = new byte[128*1024]; 373 final MessageDigest digest = MessageDigest.getInstance("SHA-1"); 374 for(;;) { 375 final int l = in.read(buf); 376 if(l == -1) { 377 return Base64.getUrlEncoder().withoutPadding().encodeToString(digest.digest()); 378 } 379 digest.update(buf, 0, l); 380 } 381 } 382 } else if(protocol.equals("file")) { 383 // Development 384 final String fileStr = url.getFile(); 385 final String className = OptimisticTypesPersistence.class.getName(); 386 final int packageNameLen = className.lastIndexOf('.'); 387 final String dirStr = fileStr.substring(0, fileStr.length() - packageNameLen - 1); 388 final File dir = new File(dirStr); 389 return "dev-" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date(getLastModifiedClassFile( 390 dir, 0L))); 391 } else if(protocol.equals("jrt")) { 392 // FIXME: revisit this for a better option with jrt 393 return "jrt"; 394 } else { 395 throw new AssertionError(); 396 } 397 } 398 399 private static long getLastModifiedClassFile(final File dir, final long max) { 400 long currentMax = max; 401 for(final File f: dir.listFiles()) { 402 if(f.getName().endsWith(".class")) { 403 final long lastModified = f.lastModified(); 404 if (lastModified > currentMax) { 405 currentMax = lastModified; 406 } 407 } else if (f.isDirectory()) { 408 final long lastModified = getLastModifiedClassFile(f, currentMax); 409 if (lastModified > currentMax) { 410 currentMax = lastModified; 411 } 412 } 413 } |