Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 04ffae3eafbdb2dda8aac14fa2da964f420e0f84 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*************************************************************************************
 * Copyright (c) 2011-2014 Red Hat, Inc. and others.
 * 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:
 *     Fred Bricon / JBoss by Red Hat - Initial implementation.
 ************************************************************************************/

package org.eclipse.m2e.profiles.core.internal.management;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osgi.util.NLS;

import org.codehaus.plexus.component.repository.exception.ComponentLookupException;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.Profile;
import org.apache.maven.model.Repository;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.SettingsUtils;

import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.IMaven;
import org.eclipse.m2e.core.internal.MavenPluginActivator;
import org.eclipse.m2e.core.internal.NoSuchComponentException;
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.project.ResolverConfiguration;
import org.eclipse.m2e.profiles.core.internal.IProfileManager;
import org.eclipse.m2e.profiles.core.internal.MavenProfilesCoreActivator;
import org.eclipse.m2e.profiles.core.internal.ProfileData;
import org.eclipse.m2e.profiles.core.internal.ProfileState;


/**
 * Maven Profile Manager
 * 
 * @author Fred Bricon
 * @since 1.5.0
 */
public class ProfileManager implements IProfileManager {

  public void updateActiveProfiles(final IMavenProjectFacade mavenProjectFacade, final List<String> profiles,
      final boolean isOffline, final boolean isForceUpdate, IProgressMonitor monitor) throws CoreException {
    if(mavenProjectFacade == null) {
      return;
    }
    final IProjectConfigurationManager configurationManager = MavenPlugin.getProjectConfigurationManager();

    IProject project = mavenProjectFacade.getProject();

    final ResolverConfiguration configuration = configurationManager.getResolverConfiguration(project);

    final String profilesAsString = getAsString(profiles);
    if(profilesAsString.equals(configuration.getSelectedProfiles())) {
      //Nothing changed
      return;
    }

    configuration.setSelectedProfiles(profilesAsString);
    boolean isSet = configurationManager.setResolverConfiguration(project, configuration);
    if(isSet) {
      MavenUpdateRequest request = new MavenUpdateRequest(project, isOffline, isForceUpdate);
      configurationManager.updateProjectConfiguration(request, monitor);
    }

  }

  private String getAsString(List<String> profiles) {
    StringBuilder sb = new StringBuilder();
    boolean addComma = false;
    if(profiles != null) {
      for(String p : profiles) {
        if(addComma) {
          sb.append(", "); //$NON-NLS-1$
        }
        sb.append(p);
        addComma = true;
      }
    }
    return sb.toString();
  }

  public Map<Profile, Boolean> getAvailableSettingsProfiles() throws CoreException {
    Map<Profile, Boolean> settingsProfiles = new LinkedHashMap<Profile, Boolean>();
    Settings settings = MavenPlugin.getMaven().getSettings();
    List<String> activeProfiles = settings.getActiveProfiles();

    for(org.apache.maven.settings.Profile sp : settings.getProfiles()) {
      Profile p = SettingsUtils.convertFromSettingsProfile(sp);
      boolean isAutomaticallyActivated = isActive(sp, activeProfiles);
      settingsProfiles.put(p, isAutomaticallyActivated);
    }
    return Collections.unmodifiableMap(settingsProfiles);
  }

  private boolean isActive(org.apache.maven.settings.Profile p, List<String> activeProfiles) {
    if(p.getActivation() != null && p.getActivation().isActiveByDefault()) {
      return true;
    }
    for(String activeProfile : activeProfiles) {
      if(activeProfile.equals(p.getId())) {
        return true;
      }
    }
    return false;
  }

  public List<ProfileData> getProfileDatas(IMavenProjectFacade facade, IProgressMonitor monitor) throws CoreException {
    if(facade == null) {
      return Collections.emptyList();
    }

    ResolverConfiguration resolverConfiguration = MavenPlugin.getProjectConfigurationManager()
        .getResolverConfiguration(facade.getProject());

    List<String> configuredProfiles = toList(resolverConfiguration.getSelectedProfiles());

    MavenProject mavenProject = facade.getMavenProject(monitor);

    List<Model> modelHierarchy = new ArrayList<>();

    getModelHierarchy(modelHierarchy, mavenProject.getModel(), monitor);

    List<Profile> availableProfiles = collectAvailableProfiles(modelHierarchy, monitor);

    final Map<Profile, Boolean> availableSettingsProfiles = getAvailableSettingsProfiles();

    availableProfiles.addAll(availableSettingsProfiles.keySet());

    List<ProfileData> statuses = new ArrayList<ProfileData>();

    Map<String, List<String>> allActiveProfiles = mavenProject.getInjectedProfileIds();

    for(Profile p : availableProfiles) {
      String pId = p.getId();
      ProfileData status = new ProfileData(pId);
      boolean isDisabled = configuredProfiles.contains("!" + pId);
      if(isActive(pId, allActiveProfiles)) {
        status.setActivationState(ProfileState.Active);
      } else if(isDisabled) {
        status.setActivationState(ProfileState.Disabled);
      }
      boolean isUserSelected = isDisabled || configuredProfiles.contains(pId);

      status.setUserSelected(isUserSelected);

      Boolean isAutoActiveSettingProfile = availableSettingsProfiles.get(p);
      boolean isAutoActive = (isAutoActiveSettingProfile != null && isAutoActiveSettingProfile)
          || (status.getActivationState().isActive() && !isUserSelected);
      status.setAutoActive(isAutoActive);
      status.setSource(findSource(p, modelHierarchy));
      statuses.add(status);
    }

    return Collections.unmodifiableList(statuses);
  }

  private boolean isActive(String profileId, Map<String, List<String>> profilesMap) {

    for(Map.Entry<String, List<String>> entry : profilesMap.entrySet()) {
      for(String pId : entry.getValue()) {
        if(pId.equals(profileId)) {
          return true;
        }
      }
    }

    return false;
  }

  private List<String> toList(String profilesAsText) {
    List<String> profiles;
    if(profilesAsText != null && profilesAsText.trim().length() > 0) {
      String[] profilesArray = profilesAsText.split("[,\\s\\|]");
      profiles = new ArrayList<String>(profilesArray.length);
      for(String profile : profilesArray) {
        profiles.add(profile);
      }
    } else {
      profiles = new ArrayList<String>(0);
    }
    return profiles;
  }

  private String findSource(Profile profile, List<Model> modelHierarchy) {
    if(profile != null) {
      if("settings.xml".equals(profile.getSource())) { //$NON-NLS-1$
        return profile.getSource();
      }
      for(Model m : modelHierarchy) {
        for(Profile p : m.getProfiles()) {
          if(p.equals(profile)) {
            return m.getArtifactId();
          }
        }
      }
    }
    return "undefined"; //$NON-NLS-1$
  }

  protected List<Profile> collectAvailableProfiles(List<Model> models, IProgressMonitor monitor) throws CoreException {
    List<Profile> profiles = new ArrayList<Profile>();
    for(Model m : models) {
      profiles.addAll(m.getProfiles());
    }
    return profiles;
  }

  protected List<Model> getModelHierarchy(List<Model> models, Model projectModel, IProgressMonitor monitor)
      throws CoreException {
    if(projectModel == null) {
      return null;
    }
    models.add(projectModel);
    Parent p = projectModel.getParent();
    if(p != null) {

      IMaven maven = MavenPlugin.getMaven();

      List<ArtifactRepository> repositories = new ArrayList<ArtifactRepository>();
      repositories.addAll(getProjectRepositories(projectModel));
      repositories.addAll(maven.getArtifactRepositories());

      Model parentModel = resolvePomModel(p.getGroupId(), p.getArtifactId(), p.getVersion(), repositories, monitor);
      if(parentModel != null) {
        getModelHierarchy(models, parentModel, monitor);
      }
    }
    return models;
  }

  private List<ArtifactRepository> getProjectRepositories(Model projectModel) {
    List<ArtifactRepository> repos = new ArrayList<ArtifactRepository>();
    List<Repository> modelRepos = projectModel.getRepositories();
    if(modelRepos != null && !modelRepos.isEmpty()) {
      RepositorySystem repositorySystem = getRepositorySystem();
      for(Repository modelRepo : modelRepos) {
        ArtifactRepository ar;
        try {
          ar = repositorySystem.buildArtifactRepository(modelRepo);
          if(ar != null) {
            repos.add(ar);
          }
        } catch(InvalidRepositoryException e) {
          MavenProfilesCoreActivator.log(e);
        }
      }
    }
    return repos;
  }

  private RepositorySystem getRepositorySystem() {
    try {
      //TODO find an alternative way to get the Maven RepositorySystem, or use Aether directly to resolve models??
      return MavenPluginActivator.getDefault().getPlexusContainer().lookup(RepositorySystem.class);
    } catch(ComponentLookupException e) {
      throw new NoSuchComponentException(e);
    }
  }

  private Model resolvePomModel(String groupId, String artifactId, String version,
      List<ArtifactRepository> repositories, IProgressMonitor monitor) throws CoreException {
    monitor.subTask(NLS.bind("Resolving {0}:{1}:{2}", new Object[] {groupId, artifactId, version}));

    IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getMavenProject(groupId, artifactId, version);
    IMaven maven = MavenPlugin.getMaven();

    if(facade != null) {
      return facade.getMavenProject(monitor).getModel();
    }

    Artifact artifact = maven.resolve(groupId, artifactId, version, "pom", null, repositories, monitor); //$NON-NLS-1$
    File file = artifact.getFile();
    if(file == null) {
      return null;
    }

    return maven.readModel(file);
  }
}

Back to the top