Maven: handling unknown exception during project reading (IDEA-51988)
[fedora-idea.git] / plugins / maven / src / main / java / org / jetbrains / idea / maven / project / MavenProjectsProcessor.java
blobe61aae9358db8a8facbaae8a2ee38de6bafeb1b9
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.jetbrains.idea.maven.project;
18 import com.intellij.openapi.project.Project;
19 import com.intellij.openapi.util.Condition;
20 import com.intellij.util.concurrency.Semaphore;
21 import org.jetbrains.idea.maven.embedder.MavenConsole;
22 import org.jetbrains.idea.maven.execution.SoutMavenConsole;
23 import org.jetbrains.idea.maven.utils.*;
25 import java.util.LinkedList;
26 import java.util.Queue;
28 public class MavenProjectsProcessor {
29 private final Project myProject;
30 private final String myTitle;
31 private final boolean myCancellable;
32 private final MavenEmbeddersManager myEmbeddersManager;
34 private final Queue<MavenProjectsProcessorTask> myQueue = new LinkedList<MavenProjectsProcessorTask>();
35 private boolean isProcessing;
37 private volatile boolean isStopped;
39 public MavenProjectsProcessor(Project project, String title, boolean cancellable, MavenEmbeddersManager embeddersManager) {
40 myProject = project;
41 myTitle = title;
42 myCancellable = cancellable;
43 myEmbeddersManager = embeddersManager;
46 public void scheduleTask(MavenProjectsProcessorTask task) {
47 synchronized (myQueue) {
48 if (!isProcessing && !MavenUtil.isNoBackgroundMode()) {
49 isProcessing = true;
50 startProcessing(task);
51 return;
53 if (myQueue.contains(task)) return;
54 myQueue.add(task);
58 public void removeTask(MavenProjectsProcessorTask task) {
59 synchronized (myQueue) {
60 myQueue.remove(task);
64 public void waitForCompletion() {
65 if (isStopped) return;
67 if (MavenUtil.isNoBackgroundMode()) {
68 synchronized (myQueue) {
69 while (!myQueue.isEmpty()) {
70 startProcessing(myQueue.poll());
73 return;
76 final Semaphore semaphore = new Semaphore();
77 semaphore.down();
78 scheduleTask(new MavenProjectsProcessorTask() {
79 public void perform(Project project, MavenEmbeddersManager embeddersManager, MavenConsole console, MavenProgressIndicator indicator)
80 throws MavenProcessCanceledException {
81 semaphore.up();
83 });
85 while (true) {
86 if (isStopped || semaphore.waitFor(1000)) return;
90 public void stop() {
91 isStopped = true;
92 synchronized (myQueue) {
93 myQueue.clear();
97 private void startProcessing(final MavenProjectsProcessorTask task) {
98 MavenUtil.runInBackground(myProject, myTitle, myCancellable, new MavenTask() {
99 public void run(MavenProgressIndicator indicator) throws MavenProcessCanceledException {
100 Condition<MavenProgressIndicator> condition = new Condition<MavenProgressIndicator>() {
101 public boolean value(MavenProgressIndicator mavenProgressIndicator) {
102 return isStopped;
105 indicator.addCancelCondition(condition);
106 try {
107 doProcessPendingTasks(indicator, task);
109 finally {
110 indicator.removeCancelCondition(condition);
116 private void doProcessPendingTasks(MavenProgressIndicator indicator, MavenProjectsProcessorTask task)
117 throws MavenProcessCanceledException {
118 int counter = 0;
119 try {
120 while (true) {
121 indicator.checkCanceled();
122 counter++;
124 int remained;
125 synchronized (myQueue) {
126 remained = myQueue.size();
128 indicator.setFraction(counter / (double)(counter + remained));
130 try {
131 task.perform(myProject, myEmbeddersManager, new SoutMavenConsole(), indicator);
133 catch (MavenProcessCanceledException e) {
134 throw e;
136 catch (Throwable e) {
137 MavenLog.LOG.error(e);
140 synchronized (myQueue) {
141 task = myQueue.poll();
142 if (task == null) {
143 isProcessing = false;
144 return;
149 catch (MavenProcessCanceledException e) {
150 synchronized (myQueue) {
151 myQueue.clear();
152 isProcessing = false;
154 throw e;