Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 12c9f39400ce42054b854ff8c5f2070f8afc167d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.core.internal.index;

import java.util.ArrayList;
import java.util.Stack;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;

import org.eclipse.m2e.core.core.IMavenConstants;
import org.eclipse.m2e.core.internal.Messages;
import org.eclipse.m2e.core.jobs.IBackgroundProcessingQueue;

class IndexUpdaterJob extends Job implements IBackgroundProcessingQueue {

  public static class IndexUpdaterRule implements ISchedulingRule {

    public boolean contains(ISchedulingRule rule) {
      return rule == this;
    }

    public boolean isConflicting(ISchedulingRule rule) {
      return rule == this;
    }
    
  }  

  public interface IndexCommand {
    abstract void run(IProgressMonitor monitor) throws CoreException;
  }

  private final Stack<IndexUpdaterJob.IndexCommand> updateQueue = new Stack<IndexUpdaterJob.IndexCommand>();

  public IndexUpdaterJob(NexusIndexManager indexManager) {
    super(Messages.IndexUpdaterJob_title);
    setRule(new IndexUpdaterRule());
  }

  public void addCommand(IndexUpdaterJob.IndexCommand indexCommand) {
    updateQueue.add(indexCommand);
  }

  public IStatus run(IProgressMonitor monitor) {
    monitor.beginTask(getName(), IProgressMonitor.UNKNOWN);

    ArrayList<IStatus> problems = new ArrayList<IStatus>();

    while(!updateQueue.isEmpty()) {
      if (monitor.isCanceled()) {
        throw new OperationCanceledException();
      }

      IndexUpdaterJob.IndexCommand command = updateQueue.pop();
      try {
        command.run(monitor);
      } catch(CoreException ex) {
        problems.add(ex.getStatus());
      }
    }

    monitor.done();

    return problems.isEmpty()? Status.OK_STATUS: new MultiStatus(IMavenConstants.PLUGIN_ID, -1, problems.toArray(new IStatus[problems.size()]), null, null);
  }

  public boolean isEmpty() {
    return updateQueue.isEmpty();
  }
  
}

Back to the top