@Test public void verify_bulk_transfer_half_cached() { int fileCount = 20; List<String> paths = new ArrayList<String>(); for(int i = 0; i != fileCount; ++i) { if (i % 2 == 0) { ByteBlob blob = new ByteBlob("test-blob-" + i, generateData(i)); console.cacheFile(blob); } } for(int i = 0; i != fileCount; ++i) { ByteBlob blob = new ByteBlob("test-blob-" + i, generateData(i)); paths.add(console.cacheFile(blob)); } for(int i = 0; i != fileCount; ++i) { String path = paths.get(i); assertTrue(new File(path).isFile()); assertEquals("File size ", (30 << 10) + i, new File(path).length()); } }
@Override public String cacheFile(FileBlob blob) { ensureRunning(); try { File file = blob.getLocalFile(); if (file != null && file.isFile()) { return file.getCanonicalPath(); } else { synchronized(hashCache) { String hashKey = blob.getContentHash() + "___" + blob.getFileName(); if (hashCache.containsKey(hashKey)) { return hashCache.get(hashKey); } else { File f = new File(new File(cacheDir, blob.getContentHash()), blob.getFileName()); f.getParentFile().mkdirs(); File tmp = File.createTempFile(f.getName(), "", f.getParentFile()); FileOutputStream fos = new FileOutputStream(tmp); StreamHelper.copy(blob.getContent(), fos); fos.close(); tmp.renameTo(f); f.deleteOnExit(); String path = f.getCanonicalPath(); hashCache.put(hashKey, path); return path; } } } } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }
LocalControlConsole implements HostControlConsole { @Override public String cacheFile(FileBlob blob) { ensureRunning(); try { File file = blob.getLocalFile(); if (file != null && file.isFile()) { return file.getCanonicalPath(); } else { synchronized(hashCache) { String hashKey = blob.getContentHash() + "___" + blob.getFileName(); if (hashCache.containsKey(hashKey)) { return hashCache.get(hashKey); } else { File f = new File(new File(cacheDir, blob.getContentHash()), blob.getFileName()); f.getParentFile().mkdirs(); File tmp = File.createTempFile(f.getName(), "", f.getParentFile()); FileOutputStream fos = new FileOutputStream(tmp); StreamHelper.copy(blob.getContent(), fos); fos.close(); tmp.renameTo(f); f.deleteOnExit(); String path = f.getCanonicalPath(); hashCache.put(hashKey, path); return path; } } } } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } }
LocalControlConsole implements HostControlConsole { @Override public String cacheFile(FileBlob blob) { ensureRunning(); try { File file = blob.getLocalFile(); if (file != null && file.isFile()) { return file.getCanonicalPath(); } else { synchronized(hashCache) { String hashKey = blob.getContentHash() + "___" + blob.getFileName(); if (hashCache.containsKey(hashKey)) { return hashCache.get(hashKey); } else { File f = new File(new File(cacheDir, blob.getContentHash()), blob.getFileName()); f.getParentFile().mkdirs(); File tmp = File.createTempFile(f.getName(), "", f.getParentFile()); FileOutputStream fos = new FileOutputStream(tmp); StreamHelper.copy(blob.getContent(), fos); fos.close(); tmp.renameTo(f); f.deleteOnExit(); String path = f.getCanonicalPath(); hashCache.put(hashKey, path); return path; } } } } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } LocalControlConsole();  LocalControlConsole(String tmpDir); }
LocalControlConsole implements HostControlConsole { @Override public String cacheFile(FileBlob blob) { ensureRunning(); try { File file = blob.getLocalFile(); if (file != null && file.isFile()) { return file.getCanonicalPath(); } else { synchronized(hashCache) { String hashKey = blob.getContentHash() + "___" + blob.getFileName(); if (hashCache.containsKey(hashKey)) { return hashCache.get(hashKey); } else { File f = new File(new File(cacheDir, blob.getContentHash()), blob.getFileName()); f.getParentFile().mkdirs(); File tmp = File.createTempFile(f.getName(), "", f.getParentFile()); FileOutputStream fos = new FileOutputStream(tmp); StreamHelper.copy(blob.getContent(), fos); fos.close(); tmp.renameTo(f); f.deleteOnExit(); String path = f.getCanonicalPath(); hashCache.put(hashKey, path); return path; } } } } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } LocalControlConsole();  LocalControlConsole(String tmpDir); @Override boolean isLocalFileSystem(); @Override String cacheFile(FileBlob blob); @Override List<String> cacheFiles(List<? extends FileBlob> blobs); @Override Destroyable openSocket(SocketHandler handler); @Override Destroyable startProcess(String workingDir, String[] command, Map<String, String> env, ProcessHandler handler); @Override void terminate(); File getCacheDir(); }
LocalControlConsole implements HostControlConsole { @Override public String cacheFile(FileBlob blob) { ensureRunning(); try { File file = blob.getLocalFile(); if (file != null && file.isFile()) { return file.getCanonicalPath(); } else { synchronized(hashCache) { String hashKey = blob.getContentHash() + "___" + blob.getFileName(); if (hashCache.containsKey(hashKey)) { return hashCache.get(hashKey); } else { File f = new File(new File(cacheDir, blob.getContentHash()), blob.getFileName()); f.getParentFile().mkdirs(); File tmp = File.createTempFile(f.getName(), "", f.getParentFile()); FileOutputStream fos = new FileOutputStream(tmp); StreamHelper.copy(blob.getContent(), fos); fos.close(); tmp.renameTo(f); f.deleteOnExit(); String path = f.getCanonicalPath(); hashCache.put(hashKey, path); return path; } } } } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } LocalControlConsole();  LocalControlConsole(String tmpDir); @Override boolean isLocalFileSystem(); @Override String cacheFile(FileBlob blob); @Override List<String> cacheFiles(List<? extends FileBlob> blobs); @Override Destroyable openSocket(SocketHandler handler); @Override Destroyable startProcess(String workingDir, String[] command, Map<String, String> env, ProcessHandler handler); @Override void terminate(); File getCacheDir();  }