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
) {
42 myCancellable
= cancellable
;
43 myEmbeddersManager
= embeddersManager
;
46 public void scheduleTask(MavenProjectsProcessorTask task
) {
47 synchronized (myQueue
) {
48 if (!isProcessing
&& !MavenUtil
.isNoBackgroundMode()) {
50 startProcessing(task
);
53 if (myQueue
.contains(task
)) return;
58 public void removeTask(MavenProjectsProcessorTask task
) {
59 synchronized (myQueue
) {
64 public void waitForCompletion() {
65 if (isStopped
) return;
67 if (MavenUtil
.isNoBackgroundMode()) {
68 synchronized (myQueue
) {
69 while (!myQueue
.isEmpty()) {
70 startProcessing(myQueue
.poll());
76 final Semaphore semaphore
= new Semaphore();
78 scheduleTask(new MavenProjectsProcessorTask() {
79 public void perform(Project project
, MavenEmbeddersManager embeddersManager
, MavenConsole console
, MavenProgressIndicator indicator
)
80 throws MavenProcessCanceledException
{
86 if (isStopped
|| semaphore
.waitFor(1000)) return;
92 synchronized (myQueue
) {
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
) {
105 indicator
.addCancelCondition(condition
);
107 doProcessPendingTasks(indicator
, task
);
110 indicator
.removeCancelCondition(condition
);
116 private void doProcessPendingTasks(MavenProgressIndicator indicator
, MavenProjectsProcessorTask task
)
117 throws MavenProcessCanceledException
{
121 indicator
.checkCanceled();
125 synchronized (myQueue
) {
126 remained
= myQueue
.size();
128 indicator
.setFraction(counter
/ (double)(counter
+ remained
));
131 task
.perform(myProject
, myEmbeddersManager
, new SoutMavenConsole(), indicator
);
133 catch (MavenProcessCanceledException e
) {
136 catch (Throwable e
) {
137 MavenLog
.LOG
.error(e
);
140 synchronized (myQueue
) {
141 task
= myQueue
.poll();
143 isProcessing
= false;
149 catch (MavenProcessCanceledException e
) {
150 synchronized (myQueue
) {
152 isProcessing
= false;