Java Code Examples for org.openide.filesystems.FileUtil#addRecursiveListener()

The following examples show how to use org.openide.filesystems.FileUtil#addRecursiveListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WatcherDeadlockTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testDeadlockWhileRefesh() throws IOException {
    clearWorkDir();
    
    MockServices.setServices(Watcher.class, AnnotationProviderImpl.class);
    final File root = new File(getWorkDir(), "root");
    
    File f = new File(new File(new File(root, "x"), "y"), "z");
    f.mkdirs();
    final FileObject r = FileUtil.toFileObject(root);
    r.refresh(true);
    
    Set<FileObject> all = new HashSet<FileObject>();
    Enumeration<? extends FileObject> en = r.getChildren(true);
    while (en.hasMoreElements()) {
        FileObject fileObject = en.nextElement();
        all.add(fileObject);
    }
    assertEquals("Some files: " + all, 3, all.size());
    
    FileChangeListener l = new FileChangeAdapter();
    FileUtil.addRecursiveListener(l, root);
    
    FileChangeListener l2 = new FileChangeAdapter();
    FileUtil.addRecursiveListener(l2, root);
}
 
Example 2
Source File: FileUtilAddRecursiveListenerFilterTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testAddListenerGetsFiveCallbacks() throws IOException {
    class AtMostFive implements FileFilter {
        @Override
        public boolean accept(File pathname) {
            assertTrue("It is folder", pathname.isDirectory());
            int number = Integer.parseInt(pathname.getName());
            return number <= 5;
        }
        
    }
    
    FileUtil.addRecursiveListener(this, getWorkDir(), new AtMostFive(), null);

    File fifthChild = new File(new File(getWorkDir(), "5"), "new.5.txt");
    assertTrue(fifthChild.createNewFile());
    FileUtil.refreshFor(getWorkDir());
    assertEquals("One event delivered: " + events, 1, events.size());
    
    File seventhChild = new File(new File(getWorkDir(), "7"), "new.7.txt");
    assertTrue(seventhChild.createNewFile());
    FileUtil.refreshFor(getWorkDir());
    assertEquals("No other even delivered: " + events, 1, events.size());
}
 
Example 3
Source File: ChangeLiveSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public ChangeLiveSupport(V8Debugger dbg) {
    this.dbg = dbg;
    this.sourceChangeListener = new SourceChangeListener();
    this.sourceChangeRoots = dbg.getScriptsHandler().getLocalRoots();
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("new ChangeLiveSupport(), sourceChangeRoots = "+Arrays.toString(sourceChangeRoots));
    }
    if (sourceChangeRoots.length == 0) {
        FileUtil.addFileChangeListener(sourceChangeListener);
    } else {
        for (File root : sourceChangeRoots) {
            FileUtil.addRecursiveListener(sourceChangeListener, root);
        }
    }
    dbg.addListener(new V8Debugger.Listener() {
        @Override public void notifySuspended(boolean suspended) {}

        @Override public void notifyCurrentFrame(CallFrame cf) {}

        @Override public void notifyFinished() {
            destroy();
        }
    });
}
 
Example 4
Source File: PhpProject.java    From netbeans with Apache License 2.0 6 votes vote down vote up
void addSourceDirListener() {
    FileObject sourcesDirectory = getSourcesDirectory();
    if (sourcesDirectory == null) {
        return;
    }
    if (sourcesDirectory.equals(sourceDirectoryFileChangeListener.getSourceDir())) {
        // already listening to this source dir
        // this usually happens for new project - property change is fired _before_ project open
        return;
    }
    synchronized (sourceDirectoryFileChangeListener) {
        sourceDirectoryFileChangeListener.setSourceDir(sourcesDirectory);
        FileUtil.addRecursiveListener(sourceDirectoryFileChangeListener, FileUtil.toFile(sourcesDirectory), new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return isVisible(pathname);
            }
        }, null);
    }
}
 
Example 5
Source File: CopyResourcesOnSave.java    From netbeans with Apache License 2.0 5 votes vote down vote up
final void refresh() {
    synchronized (resourceUris) {
        List<Resource> resources = new ArrayList<Resource>();
        resources.addAll(nbproject.getMavenProject().getResources());
        resources.addAll(nbproject.getMavenProject().getTestResources());
        Set<File> old = new HashSet<File>(resourceUris);
        Set<File> added = new HashSet<File>();
        
            for (Resource res : resources) {
                String dir = res.getDirectory();
                if (dir == null) {
                    continue;
                }
                URI uri = FileUtilities.getDirURI(project.getProjectDirectory(), dir);
                File file = Utilities.toFile(uri);
                if (!old.contains(file) && !added.contains(file)) { // if a given file is there multiple times, we get assertion back from FileUtil. there can be only one listener+file tuple
                    FileUtil.addRecursiveListener(this, file);
                }
                added.add(file);
            }
        
        old.removeAll(added);
        for (File oldFile : old) {
            FileUtil.removeRecursiveListener(this, oldFile);
        }
        resourceUris.removeAll(old);
        resourceUris.addAll(added);
    }
}
 
Example 6
Source File: RecursiveListenerOnOffTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testRecursiveListenerIsOn() throws Exception {
    clearWorkDir();
    
    final File rootF = getWorkDir();
    final File dirF = new File(rootF, "dir");
    File fileF = new File(dirF, "file1");
    File subdirF = new File(dirF, "subdir");
    File subfileF = new File(subdirF, "subfile");
    File subsubdirF = new File(subdirF, "subsubdir");
    File subsubfileF = new File(subsubdirF, "subsubfile");
    subsubdirF.mkdirs();

    TestFileChangeListener fcl = new TestFileChangeListener();
    FileUtil.addRecursiveListener(fcl, dirF);
    
    FileObject fo = FileUtil.toFileObject(subsubdirF);
    assertNotNull("Found", fo);
    assertEquals("It is folder", FolderObj.class, fo.getClass());
    FolderObj obj = (FolderObj)fo;
    
    assertTrue("There is a listener around", obj.hasRecursiveListener());
    
    FileUtil.addRecursiveListener(fcl, subdirF);
    
    assertTrue("There is still a listener around", obj.hasRecursiveListener());
    
    FileUtil.removeRecursiveListener(fcl, dirF);

    assertTrue("Listener still remains around", obj.hasRecursiveListener());
    
    FileUtil.removeRecursiveListener(fcl, subdirF);
    assertFalse("No Listener anymore", obj.hasRecursiveListener());

    LOG.info("OK");
}
 
Example 7
Source File: FileUtilAddRecursiveListenerStopTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testAddListenerGetsFiveCallbacks() throws IOException {
    cnt = 10;
    FileUtil.addRecursiveListener(this, getWorkDir(), this);
    assertEquals("Counter decreased five times to five", 5, cnt);

    FileObject fourth = root.getFileObject("5");
    assertNotNull("Folder found", fourth);
    fourth.createData("Ahoj");
    assertEquals("One event delivered: " + events, 1, events.size());
}
 
Example 8
Source File: FsEventFromAtomicActionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testFiredFromManyAtomicActions() throws Exception {
    final File workDir = getWorkDir();
    final FileObject workDirFo = FileUtil.toFileObject(workDir);

    final MyAtomicAction myAtomicAction = new MyAtomicAction();
    MyFileChangeListener myChangeListener = new MyFileChangeListener(myAtomicAction);
    FileUtil.addRecursiveListener(myChangeListener, workDir);

    assertEquals("files before", 0, workDir.list().length);

    for (int i = 0; i < RUNS; ++i) {
        final int j = i;
        myAtomicAction.runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    FileUtil.createData(workDirFo, FILE_PREFIX + j);
                } catch (IOException ex) {
                    // checked later
                }
            }
        };
        FileUtil.runAtomicAction(myAtomicAction);
    }

    assertEquals("files after", RUNS, workDir.list().length);
    assertEquals(printEvents(myChangeListener.notFromAtomicAction), 0, myChangeListener.notFromAtomicAction.size());
    assertEquals("events", RUNS, myChangeListener.events.get());
}
 
Example 9
Source File: FsEventFromAtomicActionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testFiredFromOneAtomicAction() throws Exception {
    final File workDir = getWorkDir();
    final FileObject workDirFo = FileUtil.toFileObject(workDir);

    final AtomicAction myAtomicAction = new AtomicAction() {
        @Override
        public void run() throws IOException {
            try {
                for (int i = 0; i < RUNS; ++i) {
                    FileUtil.createData(workDirFo, FILE_PREFIX + i);
                }
            } catch (IOException ex) {
                // checked later
            }
        }
    };
    MyFileChangeListener myChangeListener = new MyFileChangeListener(myAtomicAction);
    FileUtil.addRecursiveListener(myChangeListener, workDir);

    assertEquals("files before", 0, workDir.list().length);

    FileUtil.runAtomicAction(myAtomicAction);

    assertEquals("files after", RUNS, workDir.list().length);
    assertEquals(printEvents(myChangeListener.notFromAtomicAction), 0, myChangeListener.notFromAtomicAction.size());
    assertEquals("events", RUNS, myChangeListener.events.get());
}
 
Example 10
Source File: RefreshSlowTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testByDefaultTheRefreshIgnoresRecListeners() throws IOException {
    Logger LOG = Logger.getLogger("test." + getName());
    if (!Watcher.isEnabled()) {
        LOG.warning("Have to skip the test, as native watching is disabled");
        LOG.log(Level.WARNING, "os.name: {0} os.version: {1} os.arch: {2}", new Object[] {
                System.getProperty("os.name"), 
                System.getProperty("os.version"), 
                System.getProperty("os.arch")
        });
        return;
    }
    
    File d = new File(new File(getWorkDir(), "dir"), "subdir");
    d.mkdirs();
    
    FileChangeAdapter ad = new FileChangeAdapter();
    
    FileUtil.addRecursiveListener(ad, getWorkDir());

    final FileObject fo = FileUtil.toFileObject(getWorkDir());
    Runnable r = (Runnable) fo.getFileSystem().getRoot().getAttribute("refreshSlow");
    final int cnt[] = { 0 };
    ActionEvent ae = new ActionEvent(this, 0, "") {
        @Override
        public void setSource(Object newSource) {
            assertTrue(newSource instanceof Object[]);
            Object[] arr = (Object[]) newSource;
            assertTrue("Three elements at least ", 3 <= arr.length);
            assertTrue("3rd is fileobject", arr[2] instanceof FileObject);
            FileObject checked = (FileObject) arr[2];
            assertFalse(checked + " shall not be a children of " + fo, FileUtil.isParentOf(fo, checked));
            super.setSource(newSource);
            cnt[0]++;
            fail("" + checked +"\n" + fo);
        }
    };
    
    r.equals(ae);
    r.run();
    
    assertEquals("No calls to refresh", 0, cnt[0]);
}
 
Example 11
Source File: RecursiveListenerOnOffTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testRecursiveListenerInsideRenamedFolder() throws Exception {

        final FileObject wd = FileUtil.toFileObject(getWorkDir());
        final FileObject prj = wd.createFolder("prj");          //NOI18N
        final FileObject src = prj.createFolder("src");         //NOI18N
        final FileObject pkg = src.createFolder("pkg");         //NOI18N
        final FileObject file = pkg.createData("Test","java");  //NOI18N

        class FL extends FileChangeAdapter {

            private final Semaphore sem = new Semaphore(0);
            private final Queue<File> waitFor = new ArrayDeque<File>();

            public synchronized void expect(final File... files) {
                waitFor.addAll(Arrays.asList(files));
            }

            public boolean await() throws InterruptedException {
                final int size;
                synchronized (this) {
                    size = waitFor.size();
                }
                return sem.tryAcquire(size, TIMEOUT, TimeUnit.SECONDS);
            }

            @Override
            public void fileChanged(FileEvent fe) {
                final File f = FileUtil.toFile(fe.getFile());
                final boolean remove;
                synchronized (this) {
                    remove = waitFor.remove(f);
                }
                if (remove) {
                    sem.release();
                }
            }

        }

        final FL fl = new FL();
        final File srcDir = FileUtil.toFile(src);
        FileUtil.addRecursiveListener(fl, srcDir);
        FileLock lck = prj.lock();
        try {
            prj.rename(lck, "prj2", null);      //NOI18N
        } finally {
            lck.releaseLock();
        }
        FileUtil.removeRecursiveListener(fl, srcDir);
        final File newSrcDir = FileUtil.toFile(src);
        FileUtil.addRecursiveListener(fl, newSrcDir);
        fl.expect(FileUtil.toFile(file));
        lck = file.lock();
        try {
            final OutputStream out = file.getOutputStream(lck);
            out.write(1);
            out.close();
        } finally {
            lck.releaseLock();
        }
        assertTrue(fl.await());
    }
 
Example 12
Source File: CoverageWatcher.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void start() {
    FileUtil.addRecursiveListener(fileChangeListener, coverageDir);
}