Search code examples
windowssharepointnetwork-programmingsolrpolling

Polling from a network directory


I have been working on the following project, some background:

I am an intern currently developing a new search system for my organization. The current setup is microsoft sharepoint 2013 in which the users upload files etc.. and on the other hand is the system I am developing which indexes all data being uploaded to apache SOLR.

I have been succesfull in mapping the sharepoint content repository to a network drive, and I can manually start my program to start indexing the conent of this network drive to SOLR using the Solrj api.

The problem I am facing however is that I am unable to poll events from this network drive. In my test build which ran local I used a watcher service to launch code (reindex documents, delete indexes) on file create, file modify and file delete.

This does not work unfortunantly with a url pointing to a network drive :(.

So the big question: Is there any API / library available for polling events from network drives?

Any help would be extemely appreciated !


Solution

  • So I fnally figured this one out, tried looking at .net's variant of the watcher service (system.io.filesystemwatcher) and i was having the same problem. I finally got it working by using java.io.FileAlterationMonitor / observer.

    Code:

    public class UNCWatcher {
    // A hardcoded path to a folder you are monitoring .
    public static final String FOLDER =
            "A:\\Department";
    
    public static void main(String[] args) throws Exception {
        // The monitor will perform polling on the folder every 5 seconds
        final long pollingInterval = 5 * 1000;
    
    
        File folder = new File(FOLDER);
    
        if (!folder.exists()) {
            // Test to see if monitored folder exists
            throw new RuntimeException("Directory not found: " + FOLDER);
        }
    
        FileAlterationObserver observer = new FileAlterationObserver(folder);
        FileAlterationMonitor monitor =
                new FileAlterationMonitor(pollingInterval);
        FileAlterationListener listener = new FileAlterationListenerAdaptor() {
            // Is triggered when a file is created in the monitored folder
            @Override
            public void onFileCreate(File file) {
                try {
                    // "file" is the reference to the newly created file
                    System.out.println("File created: "
                            + file.getCanonicalPath());
    
    
    
                    if(file.getName().endsWith(".docx")){
                        System.out.println("Uploaded resource is of type docx, preparing solr for indexing.");
                    }
    
    
                } catch (IOException e) {
                    e.printStackTrace(System.err);
                }
            }
    
            // Is triggered when a file is deleted from the monitored folder
            @Override
            public void onFileDelete(File file) {
                try {
                    // "file" is the reference to the removed file
                    System.out.println("File removed: "
                            + file.getCanonicalPath());
                    // "file" does not exists anymore in the location
                    System.out.println("File still exists in location: "
                            + file.exists());
                } catch (IOException e) {
                    e.printStackTrace(System.err);
                }
            }
        };
    
        observer.addListener(listener);
        monitor.addObserver(observer);
        System.out.println("Starting monitor service");
        monitor.start();
      }
    }