com.intellij.openapi.application.AccessToken Java Examples

The following examples show how to use com.intellij.openapi.application.AccessToken. 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: IdeaLightweightExtension.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
private void initializeApplication(Application application) {
    DefaultPicoContainer pico = new DefaultPicoContainer();
    when(application.getPicoContainer()).thenReturn(pico);

    MessageBus bus = new SingleThreadedMessageBus(null);
    when(application.getMessageBus()).thenReturn(bus);

    // Service setup.  See ServiceManager
    pico.registerComponent(service(PasswordSafe.class, new MockPasswordSafe()));
    pico.registerComponent(service(VcsContextFactory.class, new MockVcsContextFactory()));

    VirtualFileManager vfm = mock(VirtualFileManager.class);
    when(application.getComponent(VirtualFileManager.class)).thenReturn(vfm);

    AccessToken readToken = mock(AccessToken.class);
    when(application.acquireReadActionLock()).thenReturn(readToken);

    ApplicationInfo appInfo = mock(ApplicationInfo.class);
    when(appInfo.getApiVersion()).thenReturn("IC-182.1.1");
    registerApplicationService(ApplicationInfo.class, appInfo);
}
 
Example #2
Source File: Unity3dProjectImportUtil.java    From consulo-unity3d with Apache License 2.0 6 votes vote down vote up
/**
 * this method will called from webservice thread
 */
private static void syncProjectStep2(@Nonnull final Project project,
									 @Nullable final Sdk sdk,
									 @Nullable UnityOpenFilePostHandlerRequest requestor,
									 final boolean runValidator,
									 UnitySetDefines unitySetDefines)
{
	Task.Backgroundable.queue(project, "Sync Project", indicator ->
	{
		AccessToken accessToken = HeavyProcessLatch.INSTANCE.processStarted("unity sync project");
		try
		{
			importAfterDefines(project, sdk, runValidator, indicator, requestor, unitySetDefines);
		}
		finally
		{
			accessToken.finish();
		}
	});
}
 
Example #3
Source File: VfsDirectoryBasedStorage.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static VirtualFile getFile(@Nonnull String fileName, @Nonnull VirtualFile parentVirtualFile, @Nonnull Object requestor) {
  VirtualFile file = parentVirtualFile.findChild(fileName);
  if (file != null) {
    return file;
  }

  AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(VfsDirectoryBasedStorage.class);
  try {
    return parentVirtualFile.createChildData(requestor, fileName);
  }
  catch (IOException e) {
    throw new StateStorageException(e);
  }
  finally {
    token.finish();
  }
}
 
Example #4
Source File: ConsoleHistoryController.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void saveHistory() {
  try {
    if (getModel().isEmpty()) return;
    if (myRootType.isHidden()) {
      saveHistoryOld();
      return;
    }
    AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(getClass());
    try {
      VirtualFile file = HistoryRootType.getInstance().findFile(null, getHistoryName(myRootType, myId), ScratchFileService.Option.create_if_missing);
      VfsUtil.saveText(file, StringUtil.join(getModel().getEntries(), myRootType.getEntrySeparator()));
    }
    finally {
      token.finish();
    }
  }
  catch (Exception ex) {
    LOG.error(ex);
  }
}
 
Example #5
Source File: FileEditorManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * @param splitters - taken getAllSplitters() value if parameter is null
 */
void runChange(@Nonnull FileEditorManagerChange change, @Nullable EditorsSplitters splitters) {
  Set<EditorsSplitters> target = new HashSet<>();
  if (splitters == null) {
    target.addAll(getAllSplitters());
  }
  else {
    target.add(splitters);
  }

  for (EditorsSplitters each : target) {
    AccessToken token = each.increaseChange();
    try {
      change.run(each);
    }
    finally {
      token.finish();
    }
  }
}
 
Example #6
Source File: ProhibitAWTEvents.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static AccessToken start(@Nonnull String activityName) {
  if(!Platform.current().isDesktop()) {
    return AccessToken.EMPTY_ACCESS_TOKEN;
  }

  if (!SwingUtilities.isEventDispatchThread()) {
    // some crazy highlighting queries getData outside EDT: https://youtrack.jetbrains.com/issue/IDEA-162970
    return AccessToken.EMPTY_ACCESS_TOKEN;
  }
  ProhibitAWTEvents dispatcher = new ProhibitAWTEvents(activityName);
  IdeEventQueue.getInstance().addPostprocessor(dispatcher, null);
  return new AccessToken() {
    @Override
    public void finish() {
      IdeEventQueue.getInstance().removePostprocessor(dispatcher);
    }
  };
}
 
Example #7
Source File: BaseApplication.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void load(@Nonnull String configPath, @Nonnull String optionsPath) throws IOException {
  IApplicationStore store = getStateStore();
  store.setOptionsPath(optionsPath);
  store.setConfigPath(configPath);

  fireBeforeApplicationLoaded();

  AccessToken token = HeavyProcessLatch.INSTANCE.processStarted("Loading application components");
  try {
    store.load();
  }
  catch (StateStorageException e) {
    throw new IOException(e.getMessage());
  }
  finally {
    token.finish();
  }
  myLoaded = true;

  createLocatorFile();
}
 
Example #8
Source File: PlatformComponentManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected <T> T runServiceInitialize(@Nonnull ServiceDescriptor descriptor, @Nonnull Supplier<T> runnable) {
  // prevent storages from flushing and blocking FS
  try (AccessToken ignored = HeavyProcessLatch.INSTANCE.processStarted("Creating component '" + descriptor.getImplementation() + "'")) {
    return super.runServiceInitialize(descriptor, runnable);
  }
}
 
Example #9
Source File: DesktopAsyncDataContext.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected Object calcData(@Nonnull Key dataId, Component focused) {
  try (AccessToken ignored = ProhibitAWTEvents.start("getData")) {
    for (WeakReference<Component> reference : myHierarchy) {
      Component component = SoftReference.dereference(reference);
      if (component == null) continue;
      DataProvider dataProvider = myProviders.get(component);
      if (dataProvider == null) continue;
      Object data = getDataManager().getDataFromProvider(dataProvider, dataId, null);
      if (data != null) return data;
    }
  }
  return null;
}
 
Example #10
Source File: ReadMostlyRWLock.java    From consulo with Apache License 2.0 5 votes vote down vote up
AccessToken writeSuspend() {
  boolean prev = writeSuspended;
  writeSuspended = true;
  writeUnlock();
  return new AccessToken() {
    @Override
    public void finish() {
      writeLock();
      writeSuspended = prev;
    }
  };
}
 
Example #11
Source File: SchemesManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void deleteFiles() {
  if (myFilesToDelete.isEmpty()) {
    return;
  }

  if (myProvider != null && myProvider.isEnabled()) {
    for (String nameWithoutExtension : myFilesToDelete) {
      deleteServerFile(nameWithoutExtension + mySchemeExtension);
      if (!DirectoryStorageData.DEFAULT_EXT.equals(mySchemeExtension)) {
        deleteServerFile(nameWithoutExtension + DirectoryStorageData.DEFAULT_EXT);
      }
    }
  }

  VirtualFile dir = getVirtualDir();
  if (dir != null) {
    AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(SchemesManagerImpl.class);
    try {
      for (VirtualFile file : dir.getChildren()) {
        if (myFilesToDelete.contains(file.getNameWithoutExtension())) {
          VfsDirectoryBasedStorage.deleteFile(file, this);
        }
      }
      myFilesToDelete.clear();
    }
    finally {
      token.finish();
    }
  }
}
 
Example #12
Source File: HeavyProcessLatch.java    From consulo with Apache License 2.0 5 votes vote down vote up
public AccessToken processStarted(@Nonnull String operationName, @Nonnull Type type) {
  myHeavyProcesses.put(operationName, type);
  myEventDispatcher.getMulticaster().processStarted();
  return new AccessToken() {
    @Override
    public void finish() {
      processFinished(operationName);
    }
  };
}
 
Example #13
Source File: DvcsUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static AccessToken workingTreeChangeStarted(@Nonnull Project project, @Nullable String activityName) {
  BackgroundTaskUtil.syncPublisher(BatchFileChangeListener.TOPIC).batchChangeStarted(project, activityName);
  return new AccessToken() {
    @Override
    public void finish() {
      BackgroundTaskUtil.syncPublisher(BatchFileChangeListener.TOPIC).batchChangeCompleted(project);
    }
  };
}
 
Example #14
Source File: ProjectStructureConfigurable.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void reset() {
  // need this to ensure VFS operations will not block because of storage flushing
  // and other maintenance IO tasks run in background
  AccessToken token = HeavyProcessLatch.INSTANCE.processStarted("Resetting Project Structure");

  try {
    myContext.reset();

    ShowSdksSettingsUtil settingsUtil = (ShowSdksSettingsUtil)ShowSettingsUtil.getInstance();

    settingsUtil.getSdksModel().reset();

    Configurable toSelect = null;
    for (Configurable each : myName2Config) {
      if (myUiState.lastEditedConfigurable != null && myUiState.lastEditedConfigurable.equals(each.getDisplayName())) {
        toSelect = each;
      }
      if (each instanceof MasterDetailsComponent) {
        ((MasterDetailsComponent)each).setHistory(myHistory);
      }
      each.reset();
    }

    myHistory.clear();

    if (toSelect == null && myName2Config.size() > 0) {
      toSelect = myName2Config.iterator().next();
    }

    removeSelected();

    navigateTo(toSelect != null ? createPlaceFor(toSelect) : null, false);

  }
  finally {
    token.finish();
  }
}
 
Example #15
Source File: ConsoleHistoryController.java    From consulo with Apache License 2.0 5 votes vote down vote up
public boolean loadHistory(String id, VirtualFile consoleFile) {
  try {
    VirtualFile file = myRootType.isHidden() ? null : HistoryRootType.getInstance().findFile(null, getHistoryName(myRootType, id), ScratchFileService.Option.existing_only);
    if (file == null) {
      if (loadHistoryOld(id)) {
        if (!myRootType.isHidden()) {
          // migrate content
          AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(getClass());
          try {
            VfsUtil.saveText(consoleFile, myContent);
          }
          finally {
            token.finish();
          }
        }
        return true;
      }
      return false;
    }
    String[] split = VfsUtilCore.loadText(file).split(myRootType.getEntrySeparator());
    getModel().resetEntries(Arrays.asList(split));
    return true;
  }
  catch (Exception ignored) {
    return false;
  }
}
 
Example #16
Source File: ScratchFileServiceImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public VirtualFile findFile(@Nonnull RootType rootType, @Nonnull String pathName, @Nonnull Option option) throws IOException {
  ApplicationManager.getApplication().assertReadAccessAllowed();

  String fullPath = getRootPath(rootType) + "/" + pathName;
  if (option != Option.create_new_always) {
    VirtualFile file = LocalFileSystem.getInstance().findFileByPath(fullPath);
    if (file != null && !file.isDirectory()) return file;
    if (option == Option.existing_only) return null;
  }
  String ext = PathUtil.getFileExtension(pathName);
  String fileNameExt = PathUtil.getFileName(pathName);
  String fileName = StringUtil.trimEnd(fileNameExt, ext == null ? "" : "." + ext);
  AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(getClass());
  try {
    VirtualFile dir = VfsUtil.createDirectories(PathUtil.getParentPath(fullPath));
    if (option == Option.create_new_always) {
      return VfsUtil.createChildSequent(LocalFileSystem.getInstance(), dir, fileName, StringUtil.notNullize(ext));
    }
    else {
      return dir.createChildData(LocalFileSystem.getInstance(), fileNameExt);
    }
  }
  finally {
    token.finish();
  }
}
 
Example #17
Source File: DesktopDataManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private <T> T getData(@Nonnull Key<T> dataId, final Component focusedComponent) {
  try (AccessToken ignored = ProhibitAWTEvents.start("getData")) {
    for (Component c = focusedComponent; c != null; c = c.getParent()) {
      final DataProvider dataProvider = getDataProviderEx(c);
      if (dataProvider == null) continue;
      T data = getDataFromProvider(dataProvider, dataId, null);
      if (data != null) return data;
    }
  }
  return null;
}
 
Example #18
Source File: BlazePyPositionConverter.java    From intellij with Apache License 2.0 5 votes vote down vote up
/** Convert from 1- to 0-indexed line numbering, and account for continuation lines */
private static int convertLocalLineToRemote(VirtualFile file, int line) {
  AccessToken lock = ApplicationManager.getApplication().acquireReadActionLock();
  try {
    final Document document = FileDocumentManager.getInstance().getDocument(file);
    if (document != null) {
      while (PyDebugSupportUtils.isContinuationLine(document, line)) {
        line++;
      }
    }
    return line + 1;
  } finally {
    lock.finish();
  }
}
 
Example #19
Source File: StorageUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void deleteFile(@Nonnull Object requestor, @Nonnull VirtualFile virtualFile) throws IOException {
  AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(StorageUtil.class);
  try {
    virtualFile.delete(requestor);
  }
  catch (FileNotFoundException e) {
    throw new ReadOnlyModificationException(VfsUtil.virtualToIoFile(virtualFile));
  }
  finally {
    token.finish();
  }
}
 
Example #20
Source File: Unity3dConsoleManager.java    From consulo-unity3d with Apache License 2.0 5 votes vote down vote up
@Nonnull
public AccessToken registerProcessor(@Nonnull Project project, @Nonnull Consumer<Collection<UnityLogPostHandlerRequest>> consumer)
{
	myMap.putValue(project, consumer);
	return new AccessToken()
	{
		@Override
		public void finish()
		{
			myMap.remove(project, consumer);
		}
	};
}
 
Example #21
Source File: ParameterPresentationBuilder.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
public AccessToken beginParameter(int index)
{
	int start = myBuilder.length();
	return new AccessToken()
	{
		@Override
		public void finish()
		{
			myRanges.put(index, new TextRange(start, myBuilder.length()));
		}
	};
}
 
Example #22
Source File: FileDocumentManagerImplTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void renameFile(VirtualFile file, String newName) throws IOException {
  AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(FileDocumentManagerImplTest.class);
  try {
    file.rename(null, newName);
  }
  finally {
    token.finish();
  }
}
 
Example #23
Source File: SymlinkHandlingTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testLinkDeleteIsSafe() throws Exception {
  File targetFile = createTestFile(myTempDir, "target");
  File linkFile = createSymLink(targetFile.getPath(), myTempDir + "/link");
  VirtualFile linkVFile = refreshAndFind(linkFile);
  assertTrue("link=" + linkFile + ", vLink=" + linkVFile, linkVFile != null && !linkVFile.isDirectory() && linkVFile.is(VFileProperty.SYMLINK));

  AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(getClass());
  try {
    linkVFile.delete(this);
  }
  finally {
    token.finish();
  }
  assertFalse(linkVFile.toString(), linkVFile.isValid());
  assertFalse(linkFile.exists());
  assertTrue(targetFile.exists());

  File targetDir = createTestDir(myTempDir, "targetDir");
  File childFile = new File(targetDir, "child.txt");
  assertTrue(childFile.getPath(), childFile.exists() || childFile.createNewFile());
  File linkDir = createSymLink(targetDir.getPath(), myTempDir + "/linkDir");
  VirtualFile linkVDir = refreshAndFind(linkDir);
  assertTrue("link=" + linkDir + ", vLink=" + linkVDir,
             linkVDir != null && linkVDir.isDirectory() && linkVDir.is(VFileProperty.SYMLINK) && linkVDir.getChildren().length == 1);

  token = ApplicationManager.getApplication().acquireWriteActionLock(getClass());
  try {
    linkVDir.delete(this);
  }
  finally {
    token.finish();
  }
  assertFalse(linkVDir.toString(), linkVDir.isValid());
  assertFalse(linkDir.exists());
  assertTrue(targetDir.exists());
  assertTrue(childFile.exists());
}
 
Example #24
Source File: FileWatcherTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void delete(File file) throws IOException {
  VirtualFile vFile = myFileSystem.findFileByIoFile(file);
  if (vFile != null) {
    AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(getClass());
    try {
      vFile.delete(this);
    }
    finally {
      token.finish();
    }
  }
  if (file.exists()) {
    FileUtil.delete(file);
  }
}
 
Example #25
Source File: VfsDirectoryBasedStorage.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void deleteFiles(@Nonnull VirtualFile dir) {
  AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(VfsDirectoryBasedStorage.class);
  try {
    for (VirtualFile file : dir.getChildren()) {
      if (removedFileNames.contains(file.getName())) {
        deleteFile(file, this);
      }
    }
  }
  finally {
    token.finish();
  }
}
 
Example #26
Source File: TransactionGuardEx.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public AccessToken startActivity(boolean userActivity) {
  return AccessToken.EMPTY_ACCESS_TOKEN;
}
 
Example #27
Source File: IdeKeyboardFocusManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean dispatchEvent(AWTEvent e) {
  try (AccessToken ignore = (EventQueue.isDispatchThread() ? IdeEventQueue.startActivity(e) : null)) {
    return super.dispatchEvent(e);
  }
}
 
Example #28
Source File: MockApplication.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public AccessToken acquireReadActionLock() {
  return AccessToken.EMPTY_ACCESS_TOKEN;
}
 
Example #29
Source File: MockApplication.java    From consulo with Apache License 2.0 4 votes vote down vote up
@RequiredUIAccess
@Nonnull
@Override
public AccessToken acquireWriteActionLock(@Nonnull Class marker) {
  return AccessToken.EMPTY_ACCESS_TOKEN;
}
 
Example #30
Source File: DvcsUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static AccessToken workingTreeChangeStarted(@Nonnull Project project) {
  return workingTreeChangeStarted(project, null);
}