Java Code Examples for org.openide.filesystems.FileObject.refresh()

The following are Jave code examples for showing how to use refresh() of the org.openide.filesystems.FileObject class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: NbEditorUI.java   View Source Code Vote up 6 votes
@Override
public void run() {
    FileObject fo;
    do {
        synchronized (lock) {
            // let's be fair - get 1-st object
            Iterator<FileObject> iterator = objectsToRefresh.iterator();
            if (iterator.hasNext()) {
                fo = iterator.next();
                objectsToRefresh.remove(fo);
            } else {
                fo = null;
            }
        }
        if (fo != null) {
            fo.refresh();
        }
    } while (fo != null);
}
 
Example 2
Project: incubator-netbeans   File: HudsonRemoteFileSystem.java   View Source Code Vote up 6 votes
/**
 * For {@link HudsonInstanceImpl} to refresh after the workspace has been synchronized.
 */
@Override
public void refreshAll() {
    LOG.log(Level.FINE, "refreshing files in {0}", baseURL);
    synchronized (nonDirs) {
        nonDirs.clear();
        lastModified.clear();
        size.clear();
        headers.clear();
    }
    for (FileObject f : NbCollections.iterable(existingFileObjects(getRoot()))) {
        if (Thread.interrupted()) {
            return;
        }
        LOG.log(Level.FINER, "  refreshing {0}", f.getPath());
        f.refresh();
    }
}
 
Example 3
Project: incubator-netbeans   File: VCSFileProxyTest.java   View Source Code Vote up 6 votes
public void testCachedIsDirectoryChangedForFileFO() throws IOException {
    VCSFileProxy proxy = getFileProxy("something"+ System.currentTimeMillis());
    assertNotNull(proxy);
    assertFalse(proxy.isDirectory());
    assertTrue(proxy.isFile());
    
    // delete file ...
    File f = proxy.toFile();
    assertTrue(f.exists());
    assertTrue(f.isFile());
    FileObject fo = FileUtil.toFileObject(f);
    f.delete();
    assertFalse(f.exists());
    // ... and recreate as folder
    f.mkdirs();
    assertTrue(f.exists());
    assertTrue(f.isDirectory());
    fo.refresh();
    
    assertTrue(proxy.isDirectory());
    assertFalse(proxy.isFile());
}
 
Example 4
Project: incubator-netbeans   File: VCSFileProxyTest.java   View Source Code Vote up 6 votes
public void testCachedIsDirectoryChangedForFolderFO() throws IOException {
    VCSFileProxy proxy = getFolderProxy("something"+ System.currentTimeMillis());
    assertNotNull(proxy);
    assertTrue(proxy.isDirectory());
    assertFalse(proxy.isFile());
    
    // delete folder ...
    File f = proxy.toFile();
    assertTrue(f.exists());
    assertTrue(f.isDirectory());
    FileObject fo = FileUtil.toFileObject(f);
    f.delete();
    assertFalse(f.exists());
    // ... and recreate as file
    f.createNewFile();
    assertTrue(f.exists());
    assertTrue(f.isFile());
    fo.refresh();
    
    assertFalse(proxy.isDirectory());
    assertTrue(proxy.isFile());
}
 
Example 5
Project: incubator-netbeans   File: GeneratedFilesHelper.java   View Source Code Vote up 6 votes
/** Find (maybe cached) CRC for a file. Will open its own input stream. */
private static String getCrc32(FileObject fo) throws IOException {
    URL u = fo.toURL();
    fo.refresh();
    long footprint = fo.lastModified().getTime() ^ fo.getSize();
    String crc = findCachedCrc32(u, footprint);
    if (crc == null) {
        InputStream is = fo.getInputStream();
        try {
            crc = computeCrc32(new BufferedInputStream(is));
            cacheCrc32(crc, u, footprint);
        } finally {
            is.close();
        }
    }
    return crc;
}
 
Example 6
Project: incubator-netbeans   File: ResultsManager.java   View Source Code Vote up 6 votes
public boolean hasSnapshotsFor(Lookup.Provider project) {
    try {
        FileObject snapshotsFolder = ProfilerStorage.getProjectFolder(project, false);
        FileObject[] children;
        
        if (snapshotsFolder == null) {
            return false;
        }
        snapshotsFolder.refresh();
        children = snapshotsFolder.getChildren();
        for (FileObject child : children) {
            if (child.getExt().equalsIgnoreCase(SNAPSHOT_EXTENSION)) return true;
            if (checkHprofFile(FileUtil.toFile(child))) return true;
        }
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, Bundle.ResultsManager_ObtainSavedSnapshotsFailedMsg(e.getMessage()), e);            
    }
    return false;
}
 
Example 7
Project: incubator-netbeans   File: ChildrenSupportTest.java   View Source Code Vote up 6 votes
/**
 * Check that fix for bug 240156 {@link #testRefreshAfterCaseChange()}
 * doens't break anything.
 *
 * @throws IOException
 */
public void testChangeFileToDir() throws IOException {

    File dir = new File(getWorkDir(), "dir");
    dir.mkdir();
    FileObject dirFO = FileUtil.toFileObject(dir);

    File fileOrDir = new File(dir, "fileOrDir");
    fileOrDir.createNewFile();
    FileObject fileOrDirFO = FileUtil.toFileObject(fileOrDir);
    assertTrue(fileOrDirFO.isData());
    dirFO.refresh();

    fileOrDir.delete();
    dirFO.refresh();

    fileOrDir.mkdir();
    dirFO.refresh();

    fileOrDirFO = FileUtil.toFileObject(fileOrDir);
    assertTrue(fileOrDirFO.isFolder());
}
 
Example 8
Project: incubator-netbeans   File: GeneratedFilesHelper.java   View Source Code Vote up 5 votes
/** Find (maybe cached) CRC for a file, using a preexisting input stream (not closed by this method). */
private static String getCrc32(InputStream is, FileObject fo) throws IOException {
    URL u = fo.toURL();
    fo.refresh(); // in case was written on disk and we did not notice yet...
    long footprint = fo.lastModified().getTime() ^ fo.getSize();
    String crc = findCachedCrc32(u, footprint);
    if (crc == null) {
        crc = computeCrc32(is);
        cacheCrc32(crc, u, footprint);
    }
    return crc;
}
 
Example 9
Project: incubator-netbeans   File: ProvidedExtensionsTest.java   View Source Code Vote up 5 votes
public void testCreatedExternally() throws IOException {
    FileObject fo = FileUtil.toFileObject(getWorkDir());
    FileObject[] children = fo.getChildren(); // scan folder

    FileObject folder = fo.createFolder("folder");
    iListener.clear();
    assertEquals(0, iListener.implsCreatedExternallyCalls);
    File f = new File(FileUtil.toFile(fo), "file");
    f.createNewFile();
    assertEquals(0, iListener.implsCreatedExternallyCalls);
    fo.refresh();
    assertEquals(1, iListener.implsCreatedExternallyCalls);
}
 
Example 10
Project: incubator-netbeans   File: BaseFileObjectTestHid.java   View Source Code Vote up 5 votes
public void testFileTypeNotRemembered() throws Exception {
    String newFileName = "test";

    FileObject parent = root.getFileObject("testdir/mountdir10");
    assertNotNull(parent);
    assertTrue(parent.isFolder());
    parent.getChildren();

    // create a folder
    assertTrue(parent.createFolder(newFileName).isFolder());

    File parentFile = FileUtil.toFile(parent);
    assertNotNull(parentFile);
    assertTrue(parentFile.getAbsolutePath(),parentFile.exists());
    File newFile = new File(parentFile, newFileName);
    assertTrue(newFile.getAbsolutePath(), newFile.exists());

    // externally delete the folder
    assertTrue(newFile.getAbsolutePath(), newFile.delete());
    assertFalse(newFile.exists());

    // create a file with the same name as the deleted folder
    assertTrue(newFile.getAbsolutePath(), new File(parentFile, newFileName).createNewFile());
    assertTrue(newFile.exists());

    parent.refresh();

    FileObject fo = FileUtil.toFileObject(newFile);
    assertTrue(newFile.getAbsolutePath(), fo.isData());
}
 
Example 11
Project: incubator-netbeans   File: BaseFileObjectTestHid.java   View Source Code Vote up 4 votes
@RandomlyFails // NB-Core-Build #7927 (from FileBasedFileSystemWithUninitializedExtensionsTest): Didn't receive a FileEvent on the parent.
public void testEventsAfterCreatedFiles55550() throws Exception {
    FileObject parent = root.getFileObject("testdir/mountdir8");  
    assertNotNull(parent);
    assertTrue(parent.isFolder());
    parent.getChildren();
    
    File parentFile = FileUtil.toFile(parent);
    assertNotNull(parentFile);
    assertTrue(parentFile.getAbsolutePath(),parentFile.exists());
    File newFile = new File(parentFile, "sun-web.xml");
    assertFalse(newFile.getAbsolutePath(),newFile.exists());
                    
    class FCLImpl extends FileChangeAdapter {
        boolean created;
        @Override
        public void fileDataCreated(FileEvent e) {
            created = true;
            synchronized(BaseFileObjectTestHid.this) {
                BaseFileObjectTestHid.this.notifyAll();
            }
        }
    }        
    FCLImpl fl = new FCLImpl();        
    parent.addFileChangeListener(fl);
    
    assertTrue(newFile.getAbsolutePath(), newFile.createNewFile());
    assertTrue(newFile.exists());
    
    // !!!! This is the source of the problem !!!
    // ask for the new file
    // remove this line ans the test passes
    FileUtil.toFileObject(newFile);
    
    
    parent.refresh();
    synchronized(this) {
        wait(1000);
    }
    parent.removeFileChangeListener(fl);
    assertTrue("Didn't receive a FileEvent on the parent.", fl.created);
}
 
Example 12
Project: incubator-netbeans   File: InstallSupportImpl.java   View Source Code Vote up 4 votes
@Override
public void run() {
    for (int loop = 0; loop < 10; loop++) {
        // XXX: the modules list should be refresh automatically when config/Modules/ changes
        Map<File,Long> modifiedFiles = NbCollections.checkedMapByFilter(
            (Map)ev.getNewValue(), 
            File.class, Long.class, true
        );
        long now = System.currentTimeMillis();
        for (Map.Entry<File,Long> e : modifiedFiles.entrySet()) {
            touch(e.getKey(), Math.max(e.getValue(), now));
        }
        FileObject modulesRoot = FileUtil.getConfigFile(ModuleDeactivator.MODULES);
        if (modulesRoot != null) {
            /* XXX: uncomment when #205120 fixed.
            LOG.fine("Refreshing Modules directory"); // NOI18N
            modulesRoot.refresh();
            LOG.fine("Done refreshing Modules directory"); // NOI18N
             */
            LOG.fine("Refreshing whole MFS"); // NOI18N
            modulesRoot.refresh();
            try {
                FileUtil.getConfigRoot().getFileSystem().refresh(true);
            } catch (FileStateInvalidException ex) {
                Exceptions.printStackTrace(ex);
            }
            LOG.fine("Done refreshing MFS"); // NOI18N
        }
        boolean ok = true;
        for (File file : modifiedFiles.keySet()) {
            String rel = relativePath(file, new StringBuilder());
            if (rel == null) {
                continue;
            }
            FileObject fo = FileUtil.getConfigFile(rel);
            if (fo == null) {
                LOG.log(loop < 5 ? Level.FINE : Level.WARNING, "Cannot find " + rel);
                ok = false;
                continue;
            }
            LOG.fine("Refreshing " + fo);
            fo.refresh();
        }
        if (ok) {
            LOG.log(loop < 5 ? Level.FINE : Level.INFO, "All was OK on " + loop + " th iteration");
            break;
        }
    }
}
 
Example 13
Project: incubator-netbeans   File: ClassPath.java   View Source Code Vote up 4 votes
/**
 * Check whether a file is included in this entry.
 * @param file a file inside @{link #getRoot}
 * @return true if it is {@link FilteringPathResourceImplementation#includes included}
 * @throws IllegalArgumentException in case the argument is not beneath {@link #getRoot}, or {@link #getRoot} is null
 * @since org.netbeans.api.java/1 1.13
 */
public boolean includes(FileObject file) {
    if (!file.isValid()) {
        //Invalid FileObject is not included
        return false;
    }
    FileObject r = getRoot();
    if (r == null) {
        file.refresh();
        if (!file.isValid()) {
            return false;
        } else {
            throw new IllegalArgumentException("no root in " + url);
        }
    }
    String path = FileUtil.getRelativePath(r, file);
    if (path == null) {
        if (!file.isValid()) {
            //Already tested above, but re-test if still valid
            return false;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(file).append(" (valid: ").append(file.isValid()).append(") not in "). // NOI18N
           append(r).append(" (valid: ").append(r.isValid()).append(")"); // NOI18N
        if (file.getPath().startsWith(r.getPath())) {
            while (file.getPath().length() > r.getPath().length()) {
                file = file.getParent();
                sb.append("\nChildren of ").append(file).
                    append(" (valid: ").append(file.isValid()).append(")").
                    append(" are:\n  ").append(Arrays.toString(file.getChildren()));
            }
        } else {
            sb.append("\nRoot path is not prefix"); // NOI18N
        }
        throw new IllegalArgumentException(sb.toString());
    }
    if (file.isFolder()) {
        path += "/"; // NOI18N
    }
    return filter == null || filter.includes(url, path);
}
 
Example 14
Project: incubator-netbeans   File: ResultsManager.java   View Source Code Vote up 4 votes
public FileObject[] listSavedHeapdumps(Lookup.Provider project, File directory) {
    try {
        FileObject snapshotsFolder = null;
                
        if (project == null && directory != null) {
            snapshotsFolder = FileUtil.toFileObject(directory);
        } else {
            snapshotsFolder = ProfilerStorage.getProjectFolder(project, false);
        }

        if (snapshotsFolder == null) {
            return new FileObject[0];
        }

        snapshotsFolder.refresh();

        FileObject[] children = snapshotsFolder.getChildren();

        ArrayList /*<FileObject>*/ files = new ArrayList /*<FileObject>*/();

        for (int i = 0; i < children.length; i++) {
            FileObject child = children[i];
            if (checkHprofFile(FileUtil.toFile(children[i])))
                files.add(child);
        }

        Collections.sort(files,
                         new Comparator() {
                public int compare(Object o1, Object o2) {
                    FileObject f1 = (FileObject) o1;
                    FileObject f2 = (FileObject) o2;

                    return f1.getName().compareTo(f2.getName());
                }
            });

        return (FileObject[])files.toArray(new FileObject[0]);
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, Bundle.ResultsManager_ObtainSavedSnapshotsFailedMsg(e.getMessage()), e);

        return new FileObject[0];
    }
}
 
Example 15
Project: incubator-netbeans   File: FolderObjTest.java   View Source Code Vote up 4 votes
public void testRefreshDoesNotMultiplyFileObjects_89059 () throws Exception {
    FileObject fo = FileBasedFileSystem.getFileObject(testFile);
    fo.getChildren();
    FileSystem fs = fo.getFileSystem();
    FileChangeListener fcl = new FileChangeAdapter();
    OutputStream os = null;
    fs.addFileChangeListener(fcl);
    fo.addFileChangeListener(fcl);
    try {
        //no change
        int foInstancesInCache = Statistics.fileObjects();
        fs.refresh(false);
        assertTrue(foInstancesInCache >= Statistics.fileObjects());

        //internal change
        File ff = new File(testFile,"a/b/c/d/aa.txt");//NOI18N
        FileUtil.createData(ff);
        foInstancesInCache = Statistics.fileObjects();
        fs.refresh(false);
        assertTrue(foInstancesInCache >= Statistics.fileObjects());

        //external change
        FileObject ffObject = FileBasedFileSystem.getFileObject(ff);
        foInstancesInCache = Statistics.fileObjects();
        os = new java.io.FileOutputStream(ff);
        os.write("dsdopsdsd".getBytes());//NOI18N
        os.close();
        fs.refresh(false);
        assertTrue(foInstancesInCache >= Statistics.fileObjects());

        assertTrue(new File(testFile,"nfile").createNewFile());//NOI18N
        fs.refresh(false);
        fo.refresh(false);
        assertTrue(foInstancesInCache+1 >= Statistics.fileObjects());

        foInstancesInCache = Statistics.fileObjects();
        assertTrue(new File(testFile,"aa/bb/cc").mkdirs());//NOI18N
        fs.refresh(false);
        fo.refresh(false);
        assertTrue(foInstancesInCache+3 >= Statistics.fileObjects());
    } finally {
        if (os != null) {
            os.close();
        }
        fs.removeFileChangeListener(fcl);
        fo.removeFileChangeListener(fcl);
    }
}
 
Example 16
Project: incubator-netbeans   File: FileObjectCyclicSymlinksTest.java   View Source Code Vote up 3 votes
/**
 * Test that symbolic links are handled correctly, without infinite
 * recursion.
 *
 * Let's use the following data structure:
 * <pre>
 * f1
 *   f2
 *     f3
 *       f4
 *         f5 (symbolic link with target = f1)
 * </pre>
 *
 * @throws java.io.IOException
 */
public void test218795() throws IOException {

    File rootF = getWorkDir();
    createDirTreeWithChangingNames(rootF, 5);

    FileObject rootFO = FileUtil.toFileObject(rootF);
    assertNotNull(rootFO);
    assertEquals(rootF, FileUtil.toFile(rootFO));
    rootFO.refresh();
    assertEquals(4, countRecursiveChildren(rootFO));
}
 
Example 17
Project: incubator-netbeans   File: EmbeddingProviderImplTest.java   View Source Code Vote up 3 votes
protected void prepareTest(String code, int testIndex) throws Exception {
        File workDirWithIndexFile = testIndex != (-1) ? new File(getWorkDir(), Integer.toString(testIndex)) : getWorkDir();
        FileObject workDirWithIndex = FileUtil.toFileObject(workDirWithIndexFile);

        if (workDirWithIndex != null) {
            workDirWithIndex.delete();
        }

        workDirWithIndex = FileUtil.createFolder(workDirWithIndexFile);

        assertNotNull(workDirWithIndexFile);

        FileUtil.setMIMEType("hint", "text/x-javahints");

        FileObject sourceRoot = workDirWithIndex.createFolder("src");
//        FileObject buildRoot  = workDirWithIndex.createFolder("build");
//        FileObject cache = workDirWithIndex.createFolder("cache");

        FileObject data = FileUtil.createData(sourceRoot, "rule.hint");

        TestUtilities.copyStringToFile(data, code);

        data.refresh();

//        SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache);

        DataObject od = DataObject.find(data);
        EditorCookie ec = od.getLookup().lookup(EditorCookie.class);

        assertNotNull(ec);

        doc = ec.openDocument();

        doc.putProperty(Language.class, DeclarativeHintTokenId.language());
        doc.putProperty("mimeType", "text/x-javahints");
    }
 
Example 18
Project: incubator-netbeans   File: SourceUtilsTest.java   View Source Code Vote up 3 votes
private void prepareTest() throws Exception {
    File work = getWorkDir();
    FileObject workFO = FileUtil.toFileObject(work);
    
    assertNotNull(workFO);
    
    FileObject sourceRoot = workFO.getFileObject("src");
    if (sourceRoot == null) {
        sourceRoot = workFO.createFolder("src");
    }
    FileObject buildRoot  = workFO.createFolder("build");
    FileObject cache = workFO.createFolder("cache");
    FileObject packageRoot = sourceRoot.getFileObject("sourceutils");
    if (packageRoot == null) {
        packageRoot = sourceRoot.createFolder("sourceutils");
    }
    
    SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache);
    
    String capitalizedName = "T" + getName().substring(1);
    
    TestUtil.copyFiles(FileUtil.toFile(sourceRoot), "sourceutils/" + capitalizedName + ".java");
    
    packageRoot.refresh();
    
    FileObject testSource = packageRoot.getFileObject(capitalizedName + ".java");
    
    assertNotNull(testSource);

    SourceUtilsTestUtil.compileRecursively(sourceRoot);
    
    js = JavaSource.forFileObject(testSource);
    
    assertNotNull(js);
    
    info = SourceUtilsTestUtil.getCompilationInfo(js, JavaSource.Phase.RESOLVED);
    
    assertNotNull(info);
}
 
Example 19
Project: incubator-netbeans   File: BulkSearchTestPerformer.java   View Source Code Vote up 3 votes
private void prepareTest(String fileName, String code) throws Exception {
    clearWorkDir();

    FileUtil.refreshFor(File.listRoots());

    FileObject workFO = FileUtil.toFileObject(getWorkDir());

    assertNotNull(workFO);

    workFO.refresh();

    sourceRoot = workFO.createFolder("src");
    FileObject buildRoot  = workFO.createFolder("build");
    FileObject cache = workFO.createFolder("cache");

    FileObject data = FileUtil.createData(sourceRoot, fileName);
    File dataFile = FileUtil.toFile(data);

    assertNotNull(dataFile);

    TestUtilities.copyStringToFile(dataFile, code);

    SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache);

    DataObject od = DataObject.find(data);
    EditorCookie ec = od.getLookup().lookup(EditorCookie.class);

    assertNotNull(ec);

    doc = ec.openDocument();
    doc.putProperty(Language.class, JavaTokenId.language());

    JavaSource js = JavaSource.forFileObject(data);

    assertNotNull(js);

    info = SourceUtilsTestUtil.getCompilationInfo(js, Phase.RESOLVED);

    assertNotNull(info);
}
 
Example 20
Project: incubator-netbeans   File: TestFileUtils.java   View Source Code Vote up 2 votes
/**
 * Make sure the timestamp on a file changes.
 * @param f a file to touch (make newer)
 * @param ref if not null, make f newer than this file; else make f newer than it was before
 */
public static void touch(FileObject f, FileObject ref) throws IOException, InterruptedException {
    org.openide.util.test.TestFileUtils.touch(FileUtil.toFile(f), ref != null ? FileUtil.toFile(ref) : null);
    f.refresh();
}