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

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IFile;

import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.ArtifactKey;
import org.eclipse.m2e.core.internal.MavenPluginActivator;


/**
 * Registry of all workspace projects and their dependencies.
 * 
 * @author igor
 */
abstract class BasicProjectRegistry implements Serializable {

  private static final long serialVersionUID = 5542512601401896748L;

  private final String m2e_version = MavenPluginActivator.getQualifiedVersion();

  /**
   * Map<ArtifactKey, IPath> Maps ArtifactKey to full workspace IPath of the POM file that defines this artifact.
   */
  protected final Map<ArtifactKey, IFile> workspaceArtifacts = new HashMap<ArtifactKey, IFile>();

  /**
   * Maps full pom IPath to MavenProjectFacade
   */
  protected final Map<IFile, MavenProjectFacade> workspacePoms = new HashMap<IFile, MavenProjectFacade>();

  /**
   * Maps required capabilities to projects that require them.
   */
  protected final Map<VersionlessKey, Map<RequiredCapability, Set<IFile>>> requiredCapabilities = new HashMap<VersionlessKey, Map<RequiredCapability, Set<IFile>>>();

  /**
   * Maps project pom.xml file to the capabilities provided by the project
   */
  protected final Map<IFile, Set<Capability>> projectCapabilities = new HashMap<IFile, Set<Capability>>();

  /**
   * Maps project pom.xml file to the capabilities required by the project
   */
  protected final Map<IFile, Set<RequiredCapability>> projectRequirements = new HashMap<IFile, Set<RequiredCapability>>();

  protected BasicProjectRegistry() {
  }

  protected BasicProjectRegistry(BasicProjectRegistry other) {
    replaceWith(other);
  }

  protected final void replaceWith(BasicProjectRegistry other) {
    clear();

    copy(other.workspaceArtifacts, workspaceArtifacts);
    copy(other.workspacePoms, workspacePoms);
    copy(other.projectCapabilities, projectCapabilities);
    copy(other.projectRequirements, projectRequirements);
    copy(other.requiredCapabilities, requiredCapabilities);
  }

  /**
   * THIS IS NOT A GENERIC DEEP COPY IMPLEMENTATION!
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  private static void copy(Map from, Map to) {
    for(Map.Entry entry : (Set<Map.Entry>) from.entrySet()) {
      Object value = entry.getValue();
      if(value instanceof Map) {
        Map map = new HashMap();
        copy((Map) value, map);
        value = map;
      } else if(value instanceof Set) {
        Set set = new HashSet((Set) value);
        value = set;
      }
      to.put(entry.getKey(), value);
    }
  }

  public MavenProjectFacade getProjectFacade(IFile pom) {
    return workspacePoms.get(pom);
  }

  public MavenProjectFacade getProjectFacade(String groupId, String artifactId, String version) {
    IFile path = workspaceArtifacts.get(new ArtifactKey(groupId, artifactId, version, null));
    if(path == null) {
      return null;
    }
    return workspacePoms.get(path);
  }

  /**
   * @TODO return a List
   */
  public MavenProjectFacade[] getProjects() {
    return workspacePoms.values().toArray(new MavenProjectFacade[workspacePoms.size()]);
  }

  public IFile getWorkspaceArtifact(ArtifactKey key) {
    return workspaceArtifacts.get(key);
  }

  protected void clear() {
    workspaceArtifacts.clear();
    workspacePoms.clear();
    requiredCapabilities.clear();
    projectCapabilities.clear();
    projectRequirements.clear();
  }

  public boolean isValid() {
    return MavenPluginActivator.getQualifiedVersion().equals(m2e_version) //
        && workspaceArtifacts != null //
        && workspacePoms != null //
        && requiredCapabilities != null //
        && projectCapabilities != null //
        && projectRequirements != null //
        && areFacadesValid();
  }

  private boolean areFacadesValid() {
    for(MavenProjectFacade facade : workspacePoms.values()) {
      if(facade == null || facade.getPom() == null || facade.getPom().getLocation() == null) {
        return false;
      }
    }
    return true;
  }

  protected Set<RequiredCapability> getProjectRequirements(IFile pom) {
    return projectRequirements.get(pom);
  }
}

Back to the top