maven: correctly cancelling progress in the project wizard
authorAnton Makeev <Anton.Makeev@jetbrains.com>
Thu, 16 Sep 2010 13:50:53 +0000 (17:50 +0400)
committerAnton Makeev <Anton.Makeev@jetbrains.com>
Fri, 17 Sep 2010 07:02:35 +0000 (11:02 +0400)
plugins/maven/src/main/java/org/jetbrains/idea/maven/facade/MavenFacadeManager.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/FileFinder.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/wizards/MavenProjectBuilder.java

index 0f1715bfab68b241b4f3bf57755cc933763ccb37..8fcac72af9b71ac9b9a2ad0905df95420169c01f 100644 (file)
@@ -74,7 +74,9 @@ public class MavenFacadeManager {
   private MavenFacade myFacade;
 
   private RemoteMavenFacadeLogger myLogger = new RemoteMavenFacadeLogger();
+  private boolean myLoggerExported;
   private RemoteMavenFacadeDownloadListener myDownloadListener = new RemoteMavenFacadeDownloadListener();
+  private boolean myDownloadListenerExported;
 
   public static MavenFacadeManager getInstance() {
     return ServiceManager.getService(MavenFacadeManager.class);
@@ -100,36 +102,57 @@ public class MavenFacadeManager {
     ShutDownTracker.getInstance().registerShutdownTask(new Runnable() {
       public void run() {
         mySupport.stopAll();
-        if (myFacade != null) {
-          try {
-            UnicastRemoteObject.unexportObject(myLogger, true);
-            UnicastRemoteObject.unexportObject(myDownloadListener, true);
-          }
-          catch (RemoteException e) {
-            MavenLog.LOG.error(e);
-          }
-        }
+        disposeFacade();
       }
     });
   }
 
+  private void disposeFacade() {
+    if (myFacade != null) {
+      mySupport.release(myFacade, "");
+      myFacade = null;
+    }
+
+    if (myLoggerExported) {
+      try {
+        UnicastRemoteObject.unexportObject(myLogger, true);
+      }
+      catch (RemoteException e) {
+        MavenLog.LOG.error(e);
+      }
+      myLoggerExported = false;
+    }
+    if (myDownloadListenerExported) {
+      try {
+        UnicastRemoteObject.unexportObject(myDownloadListener, true);
+      }
+      catch (RemoteException e) {
+        MavenLog.LOG.error(e);
+      }
+      myDownloadListenerExported = false;
+    }
+  }
+
   private synchronized MavenFacade getFacade() {
-    try {
-      myFacade.ping();
+    if (myFacade != null) {
+      try {
+        myFacade.ping();
+      }
+      catch (Exception ex) {
+        disposeFacade();
+      }
     }
-    catch (Exception ex) {
+    if (myFacade == null) {
       try {
-        if (myFacade == null) {
-          UnicastRemoteObject.exportObject(myLogger, 0);
-          UnicastRemoteObject.exportObject(myDownloadListener, 0);
-        }
-        else {
-          // todo [anton] if myFacade != null reinit
-        }
+        myLoggerExported = UnicastRemoteObject.exportObject(myLogger, 0) != null;
+        myDownloadListenerExported = UnicastRemoteObject.exportObject(myDownloadListener, 0) != null;
+
         myFacade = mySupport.acquire(this, "");
         myFacade.set(myLogger, myDownloadListener);
       }
       catch (Exception e) {
+        disposeFacade();
+
         if (e instanceof RuntimeException) throw (RuntimeException)e;
         throw new RuntimeException(e);
       }
index da81dba739fecb2daa2279dcddd8fd065a17cfa7..423c55221c5ac3d3b3989391c78b0b0828279dee 100644 (file)
  */
 package org.jetbrains.idea.maven.utils;
 
-import com.intellij.openapi.application.ApplicationManager;
-import com.intellij.openapi.progress.ProgressIndicator;
-import com.intellij.openapi.util.Computable;
-import com.intellij.openapi.vfs.LocalFileSystem;
 import com.intellij.openapi.vfs.VirtualFile;
 import org.jetbrains.idea.maven.model.MavenConstants;
 
 import java.util.List;
 
-/**
- * @author Vladislav.Kaznacheev
- */
 public class FileFinder {
   public static List<VirtualFile> findPomFiles(VirtualFile[] roots,
                                                boolean lookForNested,
-                                               ProgressIndicator indicator,
-                                               List<VirtualFile> result) {
+                                               MavenProgressIndicator indicator,
+                                               List<VirtualFile> result) throws MavenProcessCanceledException {
     for (VirtualFile f : roots) {
-      if (indicator.isCanceled()) break;
+      indicator.checkCanceled();
       indicator.setText2(f.getPath());
 
       if (f.isDirectory()) {
         if (lookForNested) {
+          f.refresh(false, false);
           findPomFiles(f.getChildren(), lookForNested, indicator, result);
         }
       }
@@ -50,17 +44,4 @@ public class FileFinder {
 
     return result;
   }
-
-  public static VirtualFile refreshRecursively(final String path) {
-    return ApplicationManager.getApplication().runWriteAction(new Computable<VirtualFile>() {
-      @SuppressWarnings({"ConstantConditions"})
-      public VirtualFile compute() {
-        final VirtualFile dir = LocalFileSystem.getInstance().refreshAndFindFileByPath(path);
-        if (dir != null) {
-          dir.refresh(false, true);
-        }
-        return dir;
-      }
-    });
-  }
 }
index eb3e42320b22c9f5ffeecccabf812aa4fd4c1709..9e7a9eac121329620dab28f0b2a5014213eae8bc 100644 (file)
@@ -26,6 +26,7 @@ import com.intellij.openapi.projectRoots.Sdk;
 import com.intellij.openapi.roots.ui.configuration.ModulesConfigurator;
 import com.intellij.openapi.roots.ui.configuration.ModulesProvider;
 import com.intellij.openapi.util.Computable;
+import com.intellij.openapi.vfs.LocalFileSystem;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.packaging.artifacts.ModifiableArtifactModel;
 import com.intellij.projectImport.ProjectImportBuilder;
@@ -115,19 +116,19 @@ public class MavenProjectBuilder extends ProjectImportBuilder<MavenProject> {
     getParameters().myProfiles.clear();
     getParameters().myMavenProjectTree = null;
 
-    getParameters().myImportRoot = FileFinder.refreshRecursively(root);
-    if (getParameters().myImportRoot == null) return false;
-
-    final VirtualFile file = getRootDirectory();
-    if (file == null) return false;
-
     return runConfigurationProcess(ProjectBundle.message("maven.scanning.projects"), new MavenTask() {
       public void run(MavenProgressIndicator indicator) throws MavenProcessCanceledException {
         indicator.setText(ProjectBundle.message("maven.locating.files"));
 
+        getParameters().myImportRoot = LocalFileSystem.getInstance().refreshAndFindFileByPath(root);
+        if (getParameters().myImportRoot == null) throw new MavenProcessCanceledException();
+
+        final VirtualFile file = getRootDirectory();
+        if (file == null) throw new MavenProcessCanceledException();
+
         getParameters().myFiles = FileFinder.findPomFiles(file.getChildren(),
                                                           getImportingSettings().isLookForNested(),
-                                                          indicator.getIndicator(),
+                                                          indicator,
                                                           new ArrayList<VirtualFile>());
 
         collectProfiles(indicator);