public FileSystemResource(String path) { this.path = path; this.file = Paths.toFile(baseDirectory, path); }
@Override public File getFile(String path) { return Paths.toFile(baseDirectory, path); } });
@Override public boolean move(String path, String target) { path = Paths.valid(path); target = Paths.valid(target); File file = Paths.toFile(baseDirectory, path); File dest = Paths.toFile(baseDirectory, target); if (!file.exists() && !dest.exists()) { return true; // moving an undefined resource } try { dest.getParentFile().mkdirs(); // Make sure there's somewhere to move to. java.nio.file.Files.move( java.nio.file.Paths.get(file.getAbsolutePath()), java.nio.file.Paths.get(dest.getAbsolutePath()), StandardCopyOption.ATOMIC_MOVE); return true; } catch (IOException e) { throw new IllegalStateException("Unable to move " + path + " to " + target, e); } }
@Override public boolean remove(String path) { path = Paths.valid(path); File file = Paths.toFile(baseDirectory, path); return Files.delete(file); }
} else { file = Paths.toFile(root(), resource.path());
@Override protected Resource getResource(String path) throws Exception { File file = Paths.toFile(null, path); if (!file.isAbsolute()) { // in linux, an absolute path might appear relative if the root slash has been removed. // This can also occur with the root path if java.io.tmpdir is relative. String rootPath = folder.getRoot().getPath(); String rootPathWithoutSlash = rootPath.startsWith("/") ? rootPath.substring(1) : rootPath; if (path.contains(rootPathWithoutSlash)) { file = Paths.toFile(new File("/"), path); } else { file = Paths.toFile(folder.getRoot(), path); } } return Files.asResource(file); }
@Test public void directoryEvents() throws Exception { File fileA = Paths.toFile(store.baseDirectory, "FileA"); File fileB = Paths.toFile(store.baseDirectory, "FileB"); File dirC = Paths.toFile(store.baseDirectory, "DirC"); File fileD = Paths.toFile(store.baseDirectory, "DirC/FileD"); File dirE = Paths.toFile(store.baseDirectory, "DirE");
@Test public void fileEvents() throws Exception { File fileD = Paths.toFile(store.baseDirectory, "DirC/FileD"); AwaitResourceListener listener = new AwaitResourceListener(); store.get("DirC/FileD").addListener(listener); store.watcher.schedule(30, TimeUnit.MILLISECONDS); long before = fileD.lastModified(); long after = touch(fileD); assertTrue("touched", after > before); ResourceNotification n = listener.await(5, TimeUnit.SECONDS); assertNotNull("detected event", n); assertEquals("file modified", Kind.ENTRY_MODIFY, n.getKind()); assertTrue("Resource only", n.events().isEmpty()); listener.reset(); fileD.delete(); n = listener.await(5, TimeUnit.SECONDS); assertEquals("file removed", Kind.ENTRY_DELETE, n.getKind()); listener.reset(); fileD.createNewFile(); n = listener.await(5, TimeUnit.SECONDS); assertEquals("file created", Kind.ENTRY_CREATE, n.getKind()); store.get("DirC/FileD").removeListener(listener); } /**
@Override protected Resource getResource(String path) throws Exception { File file = Paths.toFile(null, path); if (!file.isAbsolute()) { // in linux, an absolute path might appear relative if the root slash has been removed. // This can also occur with the root path if java.io.tmpdir is relative. String rootPath = folder.getRoot().getPath(); String rootPathWithoutSlash = rootPath.startsWith("/") ? rootPath.substring(1) : rootPath; if (path.contains(rootPathWithoutSlash)) { file = Paths.toFile(new File("/"), path); } else { file = Paths.toFile(folder.getRoot(), path); } } return Files.asResource(file); }
@Test public void directoryEvents() throws Exception { File fileA = Paths.toFile(store.baseDirectory, "FileA"); File fileB = Paths.toFile(store.baseDirectory, "FileB"); File dirC = Paths.toFile(store.baseDirectory, "DirC"); File fileD = Paths.toFile(store.baseDirectory, "DirC/FileD"); File dirE = Paths.toFile(store.baseDirectory, "DirE");
@Test public void fileEvents() throws Exception { File fileD = Paths.toFile(store.baseDirectory, "DirC/FileD"); AwaitResourceListener listener = new AwaitResourceListener(); store.get("DirC/FileD").addListener(listener); store.watcher.schedule(30, TimeUnit.MILLISECONDS); long before = fileD.lastModified(); long after = touch(fileD); assertTrue("touched", after > before); ResourceNotification n = listener.await(5, TimeUnit.SECONDS); assertNotNull("detected event", n); assertEquals("file modified", Kind.ENTRY_MODIFY, n.getKind()); assertTrue("Resource only", n.events().isEmpty()); listener.reset(); fileD.delete(); n = listener.await(5, TimeUnit.SECONDS); assertEquals("file removed", Kind.ENTRY_DELETE, n.getKind()); listener.reset(); fileD.createNewFile(); n = listener.await(5, TimeUnit.SECONDS); assertEquals("file created", Kind.ENTRY_CREATE, n.getKind()); store.get("DirC/FileD").removeListener(listener); } /**
Files.asResource( new File( Paths.toFile( dd.get(Paths.BASE).dir(), Paths.path("workspaces", "gs", "foo", "t1")), Files.asResource( new File( Paths.toFile( dd.get(Paths.BASE).dir(), Paths.path("workspaces", "gs", "foo", "t1")), Files.asResource( new File( Paths.toFile( td.get(Paths.BASE).dir(), Paths.path("workspaces", "gs", "foo", "t1")), Files.asResource( new File( Paths.toFile( td.get(Paths.BASE).dir(), Paths.path("workspaces", "gs", "foo", "t1")),