e(fx)clipse 1.2.0 – New service to observe a filesystem path


With 1.2.0 the e(fx)clipse core runtime bundle comes with a service who is able to track filesystem modifications. Since Java7 there’s an API to observe filesystem paths and so has the Eclipse Core Resources System which is used inside your Eclipse IDE.

The service we created looks like this:

public interface FilesystemService {
  public enum Kind {
    CREATE,
    MODIFY,
    DELETE
  }
  public Subscription observePath(URI path, BiConsumer<Kind, URI> consumer);
  public Subscription observePath(Path path, BiConsumer<Kind, Path> consumer);
  public boolean applies(URI uri);
}

and you use it like this:

import org.eclipse.fx.core.Util;
import org.eclipse.fx.core.FilesystemService;
import org.eclipse.fx.core.URI;

public class Sample {
   private static Optional<Subscription> void observe(String pathUri, Consumer<Kind,URI> c) {
    URI uri = URI.createURI(pathUri);
    return Util.lookupService(FilesystemService.class)
       .stream()
       .filter(fs -> fs.applies(uri))
       .findFirst()
       .map(fs -> fs.observe(uri,c));
   }

   public static void main(String[] args) {
     observe("file:/User/tomschindl", 
       (kind,uri) -> System.out.println( kind + " => " + uri));
   }
}

Things to notice:

  • There can be multiple Filesystem Services depending on the URI you want to observe
  • This whole stuff works in plain Java as well as it does in an OSGi-Environment
  • The system is extensible so you could plug-in your own filesystem service for you special filesystem and register it using the Java built-in service loader and the OSGi-Service registry
This entry was posted in e(fx)clipse and tagged . Bookmark the permalink.

1 Response to e(fx)clipse 1.2.0 – New service to observe a filesystem path

  1. Pingback: JavaFX links of the week, January 26 // JavaFX News, Demos and Insight // FX Experience

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.