Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f517f5b636ccfb4e3e5766355ae9f2c8c33ecd23 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*******************************************************************************
 * Copyright (c) 2011 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.internal.discovery.wizards;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.equinox.internal.p2.discovery.Catalog;
import org.eclipse.equinox.internal.p2.discovery.model.CatalogItem;
import org.eclipse.equinox.internal.p2.discovery.model.Tag;
import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogConfiguration;
import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogViewer;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.m2e.core.internal.lifecyclemapping.model.LifecycleMappingMetadata;
import org.eclipse.m2e.core.internal.lifecyclemapping.model.LifecycleMappingMetadataSource;
import org.eclipse.m2e.core.internal.lifecyclemapping.model.PluginExecutionMetadata;
import org.eclipse.m2e.core.project.configurator.MojoExecutionKey;
import org.eclipse.m2e.internal.discovery.DiscoveryActivator;
import org.eclipse.m2e.internal.discovery.MavenDiscovery;
import org.eclipse.m2e.internal.discovery.Messages;
import org.eclipse.ui.statushandlers.StatusManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@SuppressWarnings("restriction")
public class MavenCatalogViewer extends CatalogViewer {
  public static final Logger log = LoggerFactory.getLogger(MavenCatalogViewer.class);

  private static final String CONFIGURATOR_PREFIX = "configurator:"; //$NON-NLS-1$

  private static final String LIFECYCLE_PREFIX = "lifecycle:"; //$NON-NLS-1$

  private Set<String> installedFeatures;

  /*
   * Outside of tests the shellProvider should generally be a WizardPage which allows setting the header.
   */
  public MavenCatalogViewer(Catalog catalog, IShellProvider shellProvider, IRunnableContext context,
      CatalogConfiguration configuration) {
    super(catalog, shellProvider, context, configuration);
  }

  protected void postDiscovery(IProgressMonitor monitor) {
    final SubMonitor subMon = SubMonitor.convert(monitor, getCatalog().getItems().size() * 3);
    try {
      for(CatalogItem connector : getCatalog().getItems()) {
        connector.setInstalled(installedFeatures != null
            && installedFeatures.containsAll(connector.getInstallableUnits()));
        subMon.worked(1);
      }

      if(getCatalog().getItems().size() == installedFeatures.size()) {
        handleStatus(new Status(IStatus.ERROR, DiscoveryActivator.PLUGIN_ID, Messages.MavenCatalogViewer_allInstalled));
      } else {
        final MavenCatalogConfiguration config = (MavenCatalogConfiguration) getConfiguration();
        final Collection<String> selectedPackagingTypes = config.getSelectedPackagingTypes();
        final Collection<MojoExecutionKey> selectedMojos = config.getSelectedMojos();
        final Collection<String> selectedLifecycleIds = config.getSelectedLifecycleIds();
        final Collection<String> selectedConfiguratorIds = config.getSelectedConfiguratorIds();

        shellProvider.getShell().getDisplay().syncExec(new Runnable() {
          @SuppressWarnings("synthetic-access")
          public void run() {
            for(CatalogItem ci : getCatalog().getItems()) {
              boolean selected = false;
              subMon.worked(2);

              LifecycleMappingMetadataSource src = MavenDiscovery.getLifecycleMappingMetadataSource(ci);
              if(src != null) {
                for(String packagingType : selectedPackagingTypes) {
                  if(hasPackaging(src, packagingType)) {
                    selected = true;
                    select(ci);
                    break;
                  }
                }
                if(selected) {
                  continue;
                }
                for(MojoExecutionKey mojoExecution : selectedMojos) {
                  if(matchesFilter(src, mojoExecution)) {
                    selected = true;
                    select(ci);
                    break;
                  }
                }
                if(selected) {
                  continue;
                }
              }

              for(String configuratorId : selectedConfiguratorIds) {
                Tag configuratorIdTag = new Tag(CONFIGURATOR_PREFIX + configuratorId, CONFIGURATOR_PREFIX
                    + configuratorId);
                if(ci.hasTag(configuratorIdTag)) {
                  selected = true;
                  select(ci);
                  break;
                }
              }
              if(selected) {
                continue;
              }

              for(String lifecycleId : selectedLifecycleIds) {
                Tag lifecycleIdTag = new Tag(LIFECYCLE_PREFIX + lifecycleId, LIFECYCLE_PREFIX + lifecycleId);
                if(ci.hasTag(lifecycleIdTag)) {
                  select(ci);
                  break;
                }
              }
            }
          }
        });
      }
    } finally {
      subMon.done();
    }
  }

  @Override
  public void updateCatalog() {
    boolean wasCancelled = false;
    boolean wasError = false;
    final IStatus[] result = new IStatus[1];
    try {
      context.run(true, true, new IRunnableWithProgress() {
        @SuppressWarnings("synthetic-access")
        public void run(IProgressMonitor monitor) throws InterruptedException {
          SubMonitor submon = SubMonitor.convert(monitor, 100);
          try {
            if(installedFeatures == null) {
              installedFeatures = getInstalledFeatures(submon.newChild(10));
            }
            result[0] = getCatalog().performDiscovery(submon.newChild(80));
            if(monitor.isCanceled()) {
              throw new InterruptedException();
            }
            if(!getCatalog().getItems().isEmpty()) {
              postDiscovery(submon.newChild(10));
            }
          } finally {
            submon.done();
          }
        }
      });
    } catch(InvocationTargetException e) {
      result[0] = computeStatus(e, Messages.MavenCatalogViewer_unexpectedException);
    } catch(InterruptedException e) {
      // cancelled by user so nothing to do here.
      wasCancelled = true;
    }
    if(result[0] != null && !result[0].isOK()) {
      handleStatus(result[0]);
      wasError = true;
    }
    if(getCatalog() != null) {
      catalogUpdated(wasCancelled, wasError);
      verifyUpdateSiteAvailability();
    }
    // help UI tests
    viewer.setData("discoveryComplete", Boolean.TRUE); //$NON-NLS-1$
  }

  private void select(CatalogItem ci) {
    modifySelection(ci, true);
    ci.addTag(MavenDiscovery.APPLICABLE_TAG);
  }

  private void handleStatus(final IStatus status) {
    if(status.isOK()) {
      return;
    }

    if(shellProvider instanceof WizardPage) {
      shellProvider.getShell().getDisplay().asyncExec(new Runnable() {
        public void run() {
          // Display the error in the wizard header
          int messageType = IMessageProvider.INFORMATION;
          if(status.matches(IStatus.ERROR)) {
            messageType = IMessageProvider.ERROR;
          } else if(status.matches(IStatus.WARNING)) {
            messageType = IMessageProvider.WARNING;
          }
          ((WizardPage) shellProvider).setMessage(status.getMessage(), messageType);
          StatusManager.getManager().handle(status);
        }
      });
    } else {
      StatusManager.getManager().handle(status, StatusManager.SHOW | StatusManager.BLOCK | StatusManager.LOG);
    }
  }

  private static boolean matchesFilter(LifecycleMappingMetadataSource src, MojoExecutionKey mojoExecution) {
    for(PluginExecutionMetadata p : src.getPluginExecutions()) {
      if(p.getFilter().match(mojoExecution)) {
        return true;
      }
    }
    return false;
  }

  private static boolean hasPackaging(LifecycleMappingMetadataSource lifecycleMappingMetadataSource,
      String packagingType) {
    for(LifecycleMappingMetadata lifecycleMappingMetadata : lifecycleMappingMetadataSource.getLifecycleMappings()) {
      if(packagingType.equals(lifecycleMappingMetadata.getPackagingType())) {
        return true;
      }
    }
    return false;
  }
}

Back to the top