Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2929989c01a8ae3939e67a448bcd30ef08a675b3 (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
/*******************************************************************************
 * 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.net.URL;
import java.util.Collection;

import org.apache.maven.plugin.MojoExecution;
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.operation.IRunnableContext;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.m2e.core.internal.lifecycle.LifecycleMappingFactory;
import org.eclipse.m2e.core.internal.lifecycle.model.LifecycleMappingMetadata;
import org.eclipse.m2e.core.internal.lifecycle.model.LifecycleMappingMetadataSource;
import org.eclipse.m2e.core.internal.lifecycle.model.PluginExecutionMetadata;
import org.eclipse.m2e.internal.discovery.MavenDiscovery;
import org.eclipse.m2e.internal.discovery.Messages;
import org.eclipse.osgi.util.NLS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@SuppressWarnings("restriction")
public class MavenCatalogViewer extends CatalogViewer {
  private 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 static final String PATH = "lifecycle/"; //$NON-NLS-1$

  private static final String EXT = ".xml"; //$NON-NLS-1$

  public MavenCatalogViewer(Catalog catalog, IShellProvider shellProvider, IRunnableContext context,
      CatalogConfiguration configuration) {
    super(catalog, shellProvider, context, configuration);
  }

  @Override
  protected void postDiscovery() {
    super.postDiscovery();

    final MavenCatalogConfiguration config = (MavenCatalogConfiguration) getConfiguration();
    final Collection<String> selectedPackagingTypes = config.getSelectedPackagingTypes();
    final Collection<MojoExecution> 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;

          LifecycleMappingMetadataSource src = getLifecycleMappingMetadataSource(ci);
          if(src != null) {
            for(String packagingType : selectedPackagingTypes) {
              if(hasPackaging(src, packagingType)) {
                selected = true;
                select(ci);
                break;
              }
            }
            if(selected) {
              continue;
            }
            for(MojoExecution 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;
            }
          }
        }
      }
    });
  }

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

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

  private LifecycleMappingMetadataSource getLifecycleMappingMetadataSource(CatalogItem ci) {
    try {
      return LifecycleMappingFactory.createLifecycleMappingMetadataSource(getLifecycleMappingMetadataSourceURL(ci));
    } catch(Exception e) {
      log.warn(NLS.bind(Messages.MavenCatalogViewer_Error_loading_lifecycle, ci.getId()), e);
      return null;
    }
  }

  private static URL getLifecycleMappingMetadataSourceURL(CatalogItem ci) {
    return ci.getSource().getResource(PATH + ci.getId() + EXT);
  }

  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