Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 796c1ab9906c696917a3be538c96a1a298fdbfbe (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
/*******************************************************************************
 * 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;

import java.io.File;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import javax.inject.Singleton;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ResolutionGroup;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.project.artifact.DefaultMavenMetadataCache;
import org.apache.maven.project.artifact.MavenMetadataCache;

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


/**
 * EclipseMavenMetadataCache
 * 
 * @author igor
 */
@Singleton
public class EclipseMavenMetadataCache extends DefaultMavenMetadataCache implements MavenMetadataCache, IManagedCache {

  @Override
  public void put(Artifact artifact, boolean resolveManagedVersions, ArtifactRepository localRepository,
      List<ArtifactRepository> remoteRepositories, ResolutionGroup result) {

    ArtifactKey gav = new ArtifactKey(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), null);

    if("pom".equals(artifact.getType())) { //$NON-NLS-1$
      // new project pom, remove any existing project entries
      removeProject(gav);
    }

    super.put(artifact, resolveManagedVersions, localRepository, remoteRepositories, result);
  }

  @Override
  public Set<File> removeProject(File pom, ArtifactKey key, boolean force) {
    removeProject(key);
    return Collections.emptySet();
  }

  private void removeProject(ArtifactKey key) {
    if(key == null) {
      return;
    }

    Iterator<Entry<CacheKey, CacheRecord>> iter = cache.entrySet().iterator();

    while(iter.hasNext()) {
      Entry<CacheKey, CacheRecord> entry = iter.next();
      CacheRecord record = entry.getValue();

      if(equals(record.getArtifact(), key) || contains(record.getArtifacts(), key)) {
        iter.remove();
      }
    }
  }

  private boolean contains(List<Artifact> artifacts, ArtifactKey key) {
    for(Artifact artifact : artifacts) {
      if(equals(artifact, key)) {
        return true;
      }
    }
    return false;
  }

  private boolean equals(Artifact artifact, ArtifactKey key) {
    /*
     * maybe too conservative, but purge anything that matches GAbV (bV==baseVersion)
     */
    return eq(key.getGroupId(), artifact.getGroupId()) //
        && eq(key.getArtifactId(), artifact.getArtifactId()) //
        && eq(key.getVersion(), artifact.getBaseVersion());
  }

  private static <T> boolean eq(T a, T b) {
    return a != null ? a.equals(b) : b == null;
  }
}

Back to the top