Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d7666cc93ed9a4c87519768eb1b887191668e5b4 (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
/*******************************************************************************
 * 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.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;

import org.eclipse.m2e.core.embedder.ArtifactKey;


/**
 * WorkspaceStateDelta
 * 
 * @author igor
 */
public class MutableProjectRegistry extends BasicProjectRegistry implements IProjectRegistry {

  private static final long serialVersionUID = -4106047696261024741L;

  private final ProjectRegistry parent;

  private final int parentVersion;

  private boolean closed;

  public MutableProjectRegistry(ProjectRegistry state) {
    super(state);
    this.parent = state;
    this.parentVersion = state.getVersion();
  }

  private void assertNotClosed() {
    if(closed) {
      throw new IllegalStateException("Can't modify closed MutableProjectRegistry"); //$NON-NLS-1$
    }
  }

  public void setProject(IFile pom, MavenProjectFacade facade) {
    assertNotClosed();

    // remove
    MavenProjectFacade oldFacade = workspacePoms.remove(pom);
    if(oldFacade != null) {
      workspaceArtifacts.remove(oldFacade.getArtifactKey());
    }
    if(facade != null) {
      // Add the project to workspaceProjects map
      workspacePoms.put(pom, facade);

      // Add the project to workspaceArtifacts map
      workspaceArtifacts.put(facade.getArtifactKey(), pom);
    }
  }

  public void removeProject(IFile pom, ArtifactKey mavenProject) {
    assertNotClosed();

    // remove project from requiredCapabilities map
    removeRequiredCapabilities(pom);

    // Remove the project from workspaceProjects, projectRequirements and projectCapabilities maps
    workspacePoms.remove(pom);
    projectRequirements.remove(pom);
    projectCapabilities.remove(pom);

    // Remove the project from workspaceArtifacts map
    if(mavenProject != null) {
      workspaceArtifacts.remove(mavenProject);
    }
  }

  static boolean isSameProject(IResource r1, IResource r2) {
    if(r1 == null || r2 == null) {
      return false;
    }
    return r1.getProject().equals(r2.getProject());
  }

  public Set<IFile> removeWorkspaceModules(IFile pom, ArtifactKey mavenProject) {
    assertNotClosed();

    return getDependents(MavenCapability.createMavenParent(mavenProject), true);
  }

  public boolean isStale() {
    return parentVersion != parent.getVersion();
  }

  public void close() {
    this.closed = true;

    clear();
  }

  private boolean isClosed() {
    return closed;
  }

  // IProjectRegistry

  public MavenProjectFacade getProjectFacade(IFile pom) {
    if(isClosed()) {
      return parent.getProjectFacade(pom);
    }
    return super.getProjectFacade(pom);
  }

  public MavenProjectFacade getProjectFacade(String groupId, String artifactId, String version) {
    if(isClosed()) {
      return parent.getProjectFacade(groupId, artifactId, version);
    }
    return super.getProjectFacade(groupId, artifactId, version);
  }

  public MavenProjectFacade[] getProjects() {
    if(isClosed()) {
      return parent.getProjects();
    }
    return super.getProjects();
  }

  public IFile getWorkspaceArtifact(ArtifactKey key) {
    if(isClosed()) {
      return parent.getWorkspaceArtifact(key);
    }
    return super.getWorkspaceArtifact(key);
  }

  // low level access and manipulation

  /**
   * Returns all workspace projects that require given Capability.
   */
  public Set<IFile> getDependents(Capability capability, boolean remove) {
    return getDependents(capability, false, remove);
  }

  /**
   * Returns all workspace projects that require given Capability of a certain version, if available
   */
  public Set<IFile> getVersionedDependents(Capability capability, boolean remove) {
    return getDependents(capability, true, remove);
  }

  private Set<IFile> getDependents(Capability capability, boolean versionMatch, boolean remove) {
    Map<RequiredCapability, Set<IFile>> rs = requiredCapabilities.get(capability.getVersionlessKey());
    if(rs == null) {
      return Collections.emptySet();
    }
    Set<IFile> result = new LinkedHashSet<IFile>();
    Iterator<Entry<RequiredCapability, Set<IFile>>> iter = rs.entrySet().iterator();
    while(iter.hasNext()) {
      Entry<RequiredCapability, Set<IFile>> entry = iter.next();
      if(entry.getKey().isPotentialMatch(capability, versionMatch)) {
        result.addAll(entry.getValue());
        if(remove) {
          iter.remove();
        }
      }
    }
    if(remove && rs.isEmpty()) {
      requiredCapabilities.remove(capability.getVersionlessKey());
    }
    return result;
  }

  /**
   * Returns all workspace projects that require given versionless Capability.
   */
  public Set<IFile> getDependents(VersionlessKey capability, boolean remove) {
    Map<RequiredCapability, Set<IFile>> rs;
    if(remove) {
      rs = requiredCapabilities.remove(capability);
    } else {
      rs = requiredCapabilities.get(capability);
    }
    if(rs == null) {
      return Collections.emptySet();
    }
    Set<IFile> result = new LinkedHashSet<IFile>();
    for(Set<IFile> dependents : rs.values()) {
      result.addAll(dependents);
    }
    return result;
  }

  private void addRequiredCapability(IFile pom, RequiredCapability req) {
    Map<RequiredCapability, Set<IFile>> keyEntry = requiredCapabilities.get(req.getVersionlessKey());
    if(keyEntry == null) {
      keyEntry = new HashMap<RequiredCapability, Set<IFile>>();
      requiredCapabilities.put(req.getVersionlessKey(), keyEntry);
    }
    Set<IFile> poms = keyEntry.get(req);
    if(poms == null) {
      poms = new HashSet<IFile>();
      keyEntry.put(req, poms);
    }
    poms.add(pom);
  }

  public Set<Capability> setCapabilities(IFile pom, Set<Capability> capabilities) {
    return capabilities != null ? projectCapabilities.put(pom, capabilities) : projectCapabilities.remove(pom);
  }

  public Set<RequiredCapability> setRequirements(IFile pom, Set<RequiredCapability> requirements) {
    removeRequiredCapabilities(pom);
    if(requirements != null) {
      for(RequiredCapability requirement : requirements) {
        addRequiredCapability(pom, requirement);
      }
      return projectRequirements.put(pom, requirements);
    }
    return projectRequirements.remove(pom);
  }

  private void removeRequiredCapabilities(IFile pom) {
    // TODO likely too slow
    Iterator<Entry<VersionlessKey, Map<RequiredCapability, Set<IFile>>>> keysIter = requiredCapabilities.entrySet()
        .iterator();
    while(keysIter.hasNext()) {
      Entry<VersionlessKey, Map<RequiredCapability, Set<IFile>>> keysEntry = keysIter.next();
      Iterator<Entry<RequiredCapability, Set<IFile>>> requirementsIter = keysEntry.getValue().entrySet().iterator();
      while(requirementsIter.hasNext()) {
        Entry<RequiredCapability, Set<IFile>> requirementsEntry = requirementsIter.next();
        requirementsEntry.getValue().remove(pom);
        if(requirementsEntry.getValue().isEmpty()) {
          // was last project that required this capability
          requirementsIter.remove();
        }
      }
      if(keysEntry.getValue().isEmpty()) {
        // was last project that required this capability versionless key
        keysIter.remove();
      }
    }
  }

}

Back to the top