Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ad9ac268a5d9ffe6967df834659b0a32c281b8e8 (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
/*******************************************************************************
 * Copyright (c) 2008 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.project.configurator;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osgi.util.NLS;

import org.apache.maven.plugin.MojoExecution;

import org.eclipse.m2e.core.internal.Messages;
import org.eclipse.m2e.core.internal.lifecycle.LifecycleMappingFactory;
import org.eclipse.m2e.core.internal.lifecycle.model.PluginExecutionAction;
import org.eclipse.m2e.core.internal.lifecycle.model.PluginExecutionMetadata;


/**
 * Abstract base class for customizable lifecycle mappings
 * 
 * @author igor
 */
public abstract class AbstractCustomizableLifecycleMapping extends AbstractLifecycleMapping {

  private Map<MojoExecutionKey, List<PluginExecutionMetadata>> effectiveMapping;

  private Map<String, AbstractProjectConfigurator> configurators;

  private List<MojoExecution> executionPlan;

  private List<MojoExecutionKey> notCoveredExecutions;

  @Override
  public void initializeMapping(List<MojoExecution> mojoExecutions,
      Map<MojoExecutionKey, List<PluginExecutionMetadata>> executionMapping) {

    this.effectiveMapping = executionMapping;

    this.executionPlan = mojoExecutions;

    // instantiate configurator
    this.configurators = new LinkedHashMap<String, AbstractProjectConfigurator>();
    for(List<PluginExecutionMetadata> executionMetadatas : effectiveMapping.values()) {
      for(PluginExecutionMetadata executionMetadata : executionMetadatas) {
        if(PluginExecutionAction.configurator == executionMetadata.getAction()) {
          String configuratorId = LifecycleMappingFactory.getProjectConfiguratorId(executionMetadata);
          if(!configurators.containsKey(configuratorId)) {
            try {
              configurators.put(configuratorId, LifecycleMappingFactory.createProjectConfigurator(executionMetadata));
            } catch(LifecycleMappingConfigurationException e) {
              addProblem(1, NLS.bind(Messages.ProjectConfiguratorNotAvailable, configuratorId));
            }
          }
        }
      }
    }

    // find and cache not-covered mojo execution keys
    notCoveredExecutions = new ArrayList<MojoExecutionKey>();
    all_mojo_executions: for(MojoExecution mojoExecution : executionPlan) {
      if(!isInterestingPhase(mojoExecution.getLifecyclePhase())) {
        continue;
      }
      MojoExecutionKey executionKey = new MojoExecutionKey(mojoExecution);
      List<PluginExecutionMetadata> executionMetadatas = effectiveMapping.get(executionKey);
      if(executionMetadatas != null) {
        for(PluginExecutionMetadata executionMetadata : executionMetadatas) {
          switch(executionMetadata.getAction()) {
            case ignore:
            case execute:
              continue all_mojo_executions;
            case configurator:
              if(configurators.containsKey(LifecycleMappingFactory.getProjectConfiguratorId(executionMetadata))) {
                continue all_mojo_executions;
              }
          }
        }
      }
      notCoveredExecutions.add(executionKey);
    }

  }

  public Map<MojoExecutionKey, List<AbstractBuildParticipant>> getBuildParticipants(IProgressMonitor monitor) {
    Map<MojoExecutionKey, List<AbstractBuildParticipant>> result = new LinkedHashMap<MojoExecutionKey, List<AbstractBuildParticipant>>();

    for(MojoExecution mojoExecution : executionPlan) {
      MojoExecutionKey executionKey = new MojoExecutionKey(mojoExecution);
      List<AbstractBuildParticipant> executionMappings = new ArrayList<AbstractBuildParticipant>();
      List<PluginExecutionMetadata> executionMetadatas = effectiveMapping.get(executionKey);
      if(executionMetadatas != null) {
        for(PluginExecutionMetadata executionMetadata : executionMetadatas) {
          switch(executionMetadata.getAction()) {
            case execute:
              executionMappings.add(LifecycleMappingFactory.createMojoExecutionBuildParicipant(mojoExecution,
                  executionMetadata));
              break;
            case configurator:
              AbstractProjectConfigurator configurator = configurators.get(LifecycleMappingFactory
                  .getProjectConfiguratorId(executionMetadata));
              AbstractBuildParticipant buildParticipant = configurator.getBuildParticipant(mojoExecution);
              if(buildParticipant != null) {
                executionMappings.add(buildParticipant);
              }
              break;
            case ignore:
              break;
          }
        }
      }

      result.put(executionKey, executionMappings);
    }

    return result;
  }

  public List<AbstractProjectConfigurator> getProjectConfigurators(IProgressMonitor monitor) {
    return new ArrayList<AbstractProjectConfigurator>(configurators.values());
  }

  public List<MojoExecutionKey> getNotCoveredMojoExecutions(IProgressMonitor monitor) {
    return notCoveredExecutions;
  }
}

Back to the top