Search in sources :

Example 1 with VirtualFilePointerListener

use of com.intellij.openapi.vfs.pointers.VirtualFilePointerListener in project intellij-community by JetBrains.

the class VirtualFilePointerTest method testStressConcurrentAccess.

public void testStressConcurrentAccess() throws Throwable {
    final File tempDirectory = createTempDirectory();
    VirtualFilePointer fileToCreatePointer = createPointerByFile(tempDirectory, null);
    assertNotNull(fileToCreatePointer);
    VirtualFilePointerListener listener = new VirtualFilePointerListener() {
    };
    long start = System.currentTimeMillis();
    int i;
    for (i = 0; System.currentTimeMillis() < start + 15_000; i++) {
        Disposable disposable = Disposer.newDisposable();
        // supply listener to separate pointers under one root so that it will be removed on dispose
        VirtualFilePointerImpl bb = (VirtualFilePointerImpl) VirtualFilePointerManager.getInstance().create(fileToCreatePointer.getUrl() + "/bb", disposable, listener);
        if (i % 1000 == 0)
            LOG.info("i = " + i);
        int NThreads = Runtime.getRuntime().availableProcessors();
        CountDownLatch ready = new CountDownLatch(NThreads);
        Runnable read = () -> {
            try {
                ready.countDown();
                while (run) {
                    bb.myNode.myLastUpdated = -15;
                    bb.getUrl();
                }
            } catch (Throwable e) {
                exception = e;
            }
        };
        run = true;
        List<Thread> threads = IntStream.range(0, NThreads).mapToObj(n -> new Thread(read, "reader" + n)).collect(Collectors.toList());
        threads.forEach(Thread::start);
        ready.await();
        VirtualFilePointer bc = VirtualFilePointerManager.getInstance().create(fileToCreatePointer.getUrl() + "/b/c", disposable, listener);
        run = false;
        ConcurrencyUtil.joinAll(threads);
        if (exception != null)
            throw exception;
        Disposer.dispose(disposable);
    }
    System.out.println("final i = " + i);
}
Also used : Disposable(com.intellij.openapi.Disposable) IntStream(java.util.stream.IntStream) VirtualFilePointerManager(com.intellij.openapi.vfs.pointers.VirtualFilePointerManager) UIUtil(com.intellij.util.ui.UIUtil) ArrayUtil(com.intellij.util.ArrayUtil) com.intellij.testFramework(com.intellij.testFramework) JobLauncher(com.intellij.concurrency.JobLauncher) NonNls(org.jetbrains.annotations.NonNls) TimeoutException(java.util.concurrent.TimeoutException) Computable(com.intellij.openapi.util.Computable) ContainerUtil(com.intellij.util.containers.ContainerUtil) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) PathManagerEx(com.intellij.openapi.application.ex.PathManagerEx) Library(com.intellij.openapi.roots.libraries.Library) Job(com.intellij.concurrency.Job) OrderEntryUtil(com.intellij.openapi.roots.impl.OrderEntryUtil) Disposer(com.intellij.openapi.util.Disposer) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) VFileCreateEvent(com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent) VirtualFilePointerListener(com.intellij.openapi.vfs.pointers.VirtualFilePointerListener) FileUtil(com.intellij.openapi.util.io.FileUtil) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) VFileEvent(com.intellij.openapi.vfs.newvfs.events.VFileEvent) VirtualFilePointer(com.intellij.openapi.vfs.pointers.VirtualFilePointer) OrderRootType(com.intellij.openapi.roots.OrderRootType) LibraryUtil(com.intellij.openapi.roots.libraries.LibraryUtil) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Collection(java.util.Collection) ConcurrencyUtil(com.intellij.util.ConcurrencyUtil) IOException(java.io.IOException) MockVirtualFile(com.intellij.mock.MockVirtualFile) Collectors(java.util.stream.Collectors) Disposable(com.intellij.openapi.Disposable) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) TempFileSystem(com.intellij.openapi.vfs.ex.temp.TempFileSystem) Nullable(org.jetbrains.annotations.Nullable) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.openapi.vfs(com.intellij.openapi.vfs) NotNull(org.jetbrains.annotations.NotNull) CountDownLatch(java.util.concurrent.CountDownLatch) VirtualFilePointer(com.intellij.openapi.vfs.pointers.VirtualFilePointer) VirtualFilePointerListener(com.intellij.openapi.vfs.pointers.VirtualFilePointerListener) MockVirtualFile(com.intellij.mock.MockVirtualFile) File(java.io.File)

Example 2 with VirtualFilePointerListener

use of com.intellij.openapi.vfs.pointers.VirtualFilePointerListener in project intellij-community by JetBrains.

the class VirtualFilePointerTest method testJars2.

public void testJars2() throws Exception {
    final File tempDir = createTempDirectory();
    final File jarParent = new File(tempDir, "jarParent");
    assertTrue(jarParent.mkdir());
    final File jar = new File(jarParent, "x.jar");
    final File originalJar = new File(PathManagerEx.getTestDataPath() + "/psi/generics22/collect-2.2.jar");
    FileUtil.copy(originalJar, jar);
    // Make sure we receive events when jar changes
    LocalFileSystem.getInstance().refreshAndFindFileByIoFile(jar);
    final VirtualFilePointer[] pointersToWatch = new VirtualFilePointer[1];
    final VirtualFilePointerListener listener = new VirtualFilePointerListener() {

        @Override
        public void beforeValidityChanged(@NotNull VirtualFilePointer[] pointers) {
            verifyPointersInCorrectState(pointersToWatch);
        }

        @Override
        public void validityChanged(@NotNull VirtualFilePointer[] pointers) {
            verifyPointersInCorrectState(pointersToWatch);
        }
    };
    final String pathInJar = jar.getPath().replace(File.separatorChar, '/') + JarFileSystem.JAR_SEPARATOR;
    final String jarUrl = VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, pathInJar);
    final VirtualFilePointer jarPointer = myVirtualFilePointerManager.create(jarUrl, disposable, listener);
    pointersToWatch[0] = jarPointer;
    assertTrue(jarPointer.isValid());
    assertTrue(jar.delete());
    refreshVFS();
    verifyPointersInCorrectState(pointersToWatch);
    assertFalse(jarPointer.isValid());
    UIUtil.dispatchAllInvocationEvents();
    assertTrue(jarParent.exists());
    FileUtil.copy(originalJar, jar);
    assert jar.exists();
    refreshVFS();
    verifyPointersInCorrectState(pointersToWatch);
    assertTrue(jarPointer.isValid());
    UIUtil.dispatchAllInvocationEvents();
    assertTrue(jar.delete());
    refreshVFS();
    UIUtil.dispatchAllInvocationEvents();
    verifyPointersInCorrectState(pointersToWatch);
    assertFalse(jarPointer.isValid());
    UIUtil.dispatchAllInvocationEvents();
}
Also used : VirtualFilePointerListener(com.intellij.openapi.vfs.pointers.VirtualFilePointerListener) MockVirtualFile(com.intellij.mock.MockVirtualFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull) VirtualFilePointer(com.intellij.openapi.vfs.pointers.VirtualFilePointer)

Example 3 with VirtualFilePointerListener

use of com.intellij.openapi.vfs.pointers.VirtualFilePointerListener in project intellij-community by JetBrains.

the class VirtualFilePointerTest method testJars.

public void testJars() throws Exception {
    final File tempDir = createTempDirectory();
    VirtualFile vTemp = refreshAndFindFile(tempDir);
    assertTrue(vTemp.isValid());
    final File jarParent = new File(tempDir, "jarParent");
    assertTrue(jarParent.mkdir());
    final File jar = new File(jarParent, "x.jar");
    final File originalJar = new File(PathManagerEx.getTestDataPath() + "/psi/generics22/collect-2.2.jar");
    FileUtil.copy(originalJar, jar);
    // Make sure we receive events when jar changes
    LocalFileSystem.getInstance().refreshAndFindFileByIoFile(jar);
    final VirtualFilePointer[] pointersToWatch = new VirtualFilePointer[2];
    final VirtualFilePointerListener listener = new VirtualFilePointerListener() {

        @Override
        public void beforeValidityChanged(@NotNull VirtualFilePointer[] pointers) {
            verifyPointersInCorrectState(pointersToWatch);
        }

        @Override
        public void validityChanged(@NotNull VirtualFilePointer[] pointers) {
            verifyPointersInCorrectState(pointersToWatch);
        }
    };
    final VirtualFilePointer jarParentPointer = createPointerByFile(jarParent, listener);
    final String pathInJar = jar.getPath().replace(File.separatorChar, '/') + JarFileSystem.JAR_SEPARATOR;
    final String jarUrl = VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, pathInJar);
    final VirtualFilePointer jarPointer = myVirtualFilePointerManager.create(jarUrl, disposable, listener);
    pointersToWatch[0] = jarParentPointer;
    pointersToWatch[1] = jarPointer;
    assertTrue(jarParentPointer.isValid());
    assertTrue(jarPointer.isValid());
    assertTrue(jar.delete());
    assertTrue(jarParent.delete());
    refreshVFS();
    verifyPointersInCorrectState(pointersToWatch);
    assertFalse(jarParentPointer.isValid());
    assertFalse(jarPointer.isValid());
    UIUtil.dispatchAllInvocationEvents();
    assertEmpty(vTemp.getChildren());
    assertTrue(jarParent.mkdir());
    FileUtil.copy(originalJar, jar);
    assertTrue(jar.setLastModified(System.currentTimeMillis()));
    assertTrue(jar.exists());
    assertTrue(jarParent.exists());
    assertTrue(jarParent.getParentFile().exists());
    File child = assertOneElement(jarParent.listFiles());
    assertEquals(jar.getName(), child.getName());
    refreshVFS();
    verifyPointersInCorrectState(pointersToWatch);
    assertTrue(jarParentPointer.isValid());
    assertTrue(jarPointer.isValid());
    UIUtil.dispatchAllInvocationEvents();
    assertTrue(jar.delete());
    assertTrue(jarParent.delete());
    refreshVFS();
    UIUtil.dispatchAllInvocationEvents();
    verifyPointersInCorrectState(pointersToWatch);
    assertFalse(jarParentPointer.isValid());
    assertFalse(jarPointer.isValid());
    UIUtil.dispatchAllInvocationEvents();
}
Also used : MockVirtualFile(com.intellij.mock.MockVirtualFile) VirtualFilePointerListener(com.intellij.openapi.vfs.pointers.VirtualFilePointerListener) MockVirtualFile(com.intellij.mock.MockVirtualFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull) VirtualFilePointer(com.intellij.openapi.vfs.pointers.VirtualFilePointer)

Example 4 with VirtualFilePointerListener

use of com.intellij.openapi.vfs.pointers.VirtualFilePointerListener in project intellij-community by JetBrains.

the class VirtualFilePointerManagerImpl method before.

@Override
public void before(@NotNull final List<? extends VFileEvent> events) {
    List<FilePointerPartNode> toFireEvents = new ArrayList<>();
    List<FilePointerPartNode> toUpdateUrl = new ArrayList<>();
    VirtualFilePointer[] toFirePointers;
    synchronized (this) {
        incModificationCount();
        for (VFileEvent event : events) {
            if (event instanceof VFileDeleteEvent) {
                final VFileDeleteEvent deleteEvent = (VFileDeleteEvent) event;
                addPointersUnder(deleteEvent.getFile(), false, "", toFireEvents);
            } else if (event instanceof VFileCreateEvent) {
                final VFileCreateEvent createEvent = (VFileCreateEvent) event;
                addPointersUnder(createEvent.getParent(), true, createEvent.getChildName(), toFireEvents);
            } else if (event instanceof VFileCopyEvent) {
                final VFileCopyEvent copyEvent = (VFileCopyEvent) event;
                addPointersUnder(copyEvent.getNewParent(), true, copyEvent.getFile().getName(), toFireEvents);
            } else if (event instanceof VFileMoveEvent) {
                final VFileMoveEvent moveEvent = (VFileMoveEvent) event;
                VirtualFile eventFile = moveEvent.getFile();
                addPointersUnder(moveEvent.getNewParent(), true, eventFile.getName(), toFireEvents);
                List<FilePointerPartNode> nodes = new ArrayList<>();
                addPointersUnder(eventFile, false, "", nodes);
                // files deleted from eventFile and created in moveEvent.getNewParent()
                toFireEvents.addAll(nodes);
                for (FilePointerPartNode node : nodes) {
                    VirtualFilePointerImpl pointer = node.getAnyPointer();
                    VirtualFile file = pointer == null ? null : pointer.getFile();
                    if (file != null) {
                        toUpdateUrl.add(node);
                    }
                }
            } else if (event instanceof VFilePropertyChangeEvent) {
                final VFilePropertyChangeEvent change = (VFilePropertyChangeEvent) event;
                if (VirtualFile.PROP_NAME.equals(change.getPropertyName()) && !Comparing.equal(change.getOldValue(), change.getNewValue())) {
                    VirtualFile eventFile = change.getFile();
                    // e.g. for LightVirtualFiles
                    VirtualFile parent = eventFile.getParent();
                    addPointersUnder(parent, true, change.getNewValue().toString(), toFireEvents);
                    List<FilePointerPartNode> nodes = new ArrayList<>();
                    addPointersUnder(eventFile, false, "", nodes);
                    for (FilePointerPartNode node : nodes) {
                        VirtualFilePointerImpl pointer = node.getAnyPointer();
                        VirtualFile file = pointer == null ? null : pointer.getFile();
                        if (file != null) {
                            toUpdateUrl.add(node);
                        }
                    }
                }
            }
        }
        myEvents = new ArrayList<>();
        toFirePointers = toPointers(toFireEvents);
        for (final VirtualFilePointerListener listener : myPointers.keySet()) {
            if (listener == null)
                continue;
            List<VirtualFilePointer> filtered = ContainerUtil.filter(toFirePointers, pointer -> ((VirtualFilePointerImpl) pointer).getListener() == listener);
            if (!filtered.isEmpty()) {
                EventDescriptor event = new EventDescriptor(listener, filtered.toArray(new VirtualFilePointer[filtered.size()]));
                myEvents.add(event);
            }
        }
    }
    for (EventDescriptor descriptor : myEvents) {
        descriptor.fireBefore();
    }
    if (!toFireEvents.isEmpty()) {
        myBus.syncPublisher(VirtualFilePointerListener.TOPIC).beforeValidityChanged(toFirePointers);
    }
    myNodesToFire = toFireEvents;
    myNodesToUpdateUrl = toUpdateUrl;
    assertConsistency();
}
Also used : VirtualFilePointer(com.intellij.openapi.vfs.pointers.VirtualFilePointer) VirtualFilePointerListener(com.intellij.openapi.vfs.pointers.VirtualFilePointerListener) SmartList(com.intellij.util.SmartList)

Example 5 with VirtualFilePointerListener

use of com.intellij.openapi.vfs.pointers.VirtualFilePointerListener in project intellij-community by JetBrains.

the class MavenProjectsManagerWatcher method addFilePointer.

private void addFilePointer(File... settingsFiles) {
    Collection<String> pathsToWatch = new ArrayList<>(settingsFiles.length);
    for (File settingsFile : settingsFiles) {
        if (settingsFile == null)
            continue;
        File parentFile = settingsFile.getParentFile();
        if (parentFile != null) {
            String path = getNormalizedPath(parentFile);
            if (path != null) {
                pathsToWatch.add(path);
            }
        }
        String path = getNormalizedPath(settingsFile);
        if (path != null) {
            String url = VfsUtilCore.pathToUrl(path);
            mySettingsFilesPointers.add(VirtualFilePointerManager.getInstance().create(url, myChangedDocumentsQueue, new VirtualFilePointerListener() {
            }));
        }
    }
    myWatchedRoots.addAll(LocalFileSystem.getInstance().addRootsToWatch(pathsToWatch, false));
}
Also used : VirtualFilePointerListener(com.intellij.openapi.vfs.pointers.VirtualFilePointerListener) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File)

Aggregations

VirtualFilePointerListener (com.intellij.openapi.vfs.pointers.VirtualFilePointerListener)6 VirtualFilePointer (com.intellij.openapi.vfs.pointers.VirtualFilePointer)5 File (java.io.File)5 MockVirtualFile (com.intellij.mock.MockVirtualFile)4 NotNull (org.jetbrains.annotations.NotNull)3 Disposable (com.intellij.openapi.Disposable)2 Job (com.intellij.concurrency.Job)1 JobLauncher (com.intellij.concurrency.JobLauncher)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 PathManagerEx (com.intellij.openapi.application.ex.PathManagerEx)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)1 OrderRootType (com.intellij.openapi.roots.OrderRootType)1 ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)1 OrderEntryUtil (com.intellij.openapi.roots.impl.OrderEntryUtil)1 Library (com.intellij.openapi.roots.libraries.Library)1 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)1 LibraryUtil (com.intellij.openapi.roots.libraries.LibraryUtil)1 Computable (com.intellij.openapi.util.Computable)1 Disposer (com.intellij.openapi.util.Disposer)1