package org.jetbrains.plugins.groovy.springloaded;

import com.intellij.debugger.PositionManager;
import com.intellij.debugger.PositionManagerFactory;
import com.intellij.debugger.engine.DebugProcess;

/**
 * Factory for position manager to debug classes reloaded by com.springsource.springloaded
 * @author Sergey Evdokimov
 */
public class SpringLoadedPositionManagerFactory extends PositionManagerFactory {

  @Override
  public PositionManager createPositionManager(final DebugProcess process) {
    try {
      if (process.getVirtualMachineProxy().classesByName("com.springsource.loaded.agent.SpringLoadedAgent").size() > 0
          || process.getVirtualMachineProxy().classesByName("org.springsource.loaded.agent.SpringLoadedAgent").size() > 0) {
        return new SpringLoadedPositionManager(process);
      }
    }
    catch (Exception ignored) {
      // Some problem with virtual machine.
    }

    return null;
  }
}

@@ -193,16 +193,168 @@
 Process;
+%0Aimport com.intellij.openapi.application.AccessToken;%0Aimport com.intellij.openapi.application.ApplicationManager;%0Aimport com.intellij.psi.JavaPsiFacade;
 %0A%0A/**%0A *
@@ -653,13 +653,484 @@
     
-try %7B
+AccessToken accessToken = ApplicationManager.getApplication().acquireReadActionLock();%0A    try %7B%0A      JavaPsiFacade facade = JavaPsiFacade.getInstance(process.getProject());%0A      if (facade.findPackage(%22com.springsource.loaded%22) != null %7C%7C facade.findPackage(%22org.springsource.loaded%22) != null) %7B%0A        return new SpringLoadedPositionManager(process);%0A      %7D%0A    %7D%0A    finally %7B%0A      accessToken.finish();%0A    %7D%0A%0A    try %7B%0A      // Check spring loaded for remote process
 %0A   

Support spring loaded for remote process.