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);
}
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();
}
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();
}
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();
}
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));
}
Aggregations