Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a517aa21ca1f0027be8ac2d22d2227492c120740 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*******************************************************************************
 * Copyright (c) 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.ui.internal;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.WorkspaceJob;
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.SubProgressMonitor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.progress.IProgressConstants;

import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.IProjectConfigurationManager;
import org.eclipse.m2e.core.project.MavenUpdateRequest;
import org.eclipse.m2e.core.ui.internal.actions.OpenMavenConsoleAction;
import org.eclipse.m2e.core.ui.internal.util.M2EUIUtils;


public class UpdateConfigurationJob extends WorkspaceJob {
  private static final Logger log = LoggerFactory.getLogger(UpdateConfigurationJob.class);

  private final IProject[] projects;

  private final boolean offline;

  private final boolean forceUpdate;

  public UpdateConfigurationJob(IProject[] projects) {
    this(projects, MavenPlugin.getMavenConfiguration().isOffline(), false /*forceUpdate*/);
  }

  public UpdateConfigurationJob(IProject[] projects, boolean offline, boolean forceUpdate) {
    super(Messages.UpdateSourcesAction_job_update_conf);
    this.projects = projects;
    this.offline = offline;
    this.forceUpdate = forceUpdate;

    setRule(MavenPlugin.getProjectConfigurationManager().getRule());
  }

  public IStatus runInWorkspace(IProgressMonitor monitor) {
    IProjectConfigurationManager configurationManager = MavenPlugin.getProjectConfigurationManager();

    setProperty(IProgressConstants.ACTION_PROPERTY, new OpenMavenConsoleAction());
    monitor.beginTask(getName(), projects.length);

    long l1 = System.currentTimeMillis();
    log.info("Update started"); //$NON-NLS-1$

    MultiStatus status = null;
    //project names to the errors encountered when updating them
    Map<String, Throwable> updateErrors = new HashMap<String, Throwable>();

    for(IProject project : projects) {
      if(monitor.isCanceled()) {
        throw new OperationCanceledException();
      }

      monitor.subTask(project.getName());
      IMavenProjectFacade projectFacade = MavenPlugin.getMavenProjectRegistry().create(project, monitor);
      if(projectFacade != null) {
        try {
          MavenUpdateRequest request = new MavenUpdateRequest(project, offline, forceUpdate);
          configurationManager.updateProjectConfiguration(request, new SubProgressMonitor(monitor, 1));
        } catch(CoreException ex) {
          if(status == null) {
            status = new MultiStatus(IMavenConstants.PLUGIN_ID, IStatus.ERROR, //
                Messages.UpdateSourcesAction_error_cannot_update, null);
          }
          status.add(ex.getStatus());
          updateErrors.put(project.getName(), ex);
        } catch(IllegalArgumentException e) {
          status = new MultiStatus(IMavenConstants.PLUGIN_ID, IStatus.ERROR, //
              Messages.UpdateSourcesAction_error_cannot_update, null);
          updateErrors.put(project.getName(), e);
        }
      }
    }
    if(updateErrors.size() > 0) {
      handleErrors(updateErrors);
    }
    long l2 = System.currentTimeMillis();
    log.info(NLS.bind("Update completed: {0} sec", ((l2 - l1) / 1000))); //$NON-NLS-1$

    return status != null ? status : Status.OK_STATUS;
  }

  private void handleErrors(final Map<String, Throwable> updateErrors) {
    final Display display = Display.getDefault();
    if(display != null) {
      display.asyncExec(new Runnable() {
        public void run() {
          M2EUIUtils.showErrorsForProjectsDialog(display.getActiveShell(),
              Messages.UpdateSourcesAction_error_title, Messages.UpdateSourcesAction_error_message, updateErrors);
        }
      });
    }
  }
}

Back to the top