Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 69d18c4b10c089a760d648559af99d5a278b5c0e (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
/*******************************************************************************
 * Copyright (c) 2014 Takari, 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:
 *      Takari, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.core.internal.project;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

import org.eclipse.aether.RepositoryCache;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.RepositoryPolicy;

import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;

import org.apache.maven.project.MavenProject;

import org.eclipse.m2e.core.internal.embedder.MavenExecutionContext;


/**
 * @since 1.6
 */
abstract class ProjectCachePlunger<Key> {

  private final Logger log = LoggerFactory.getLogger(getClass());

  final Multimap<File, Key> projectKeys = HashMultimap.create();

  final Multimap<Key, File> keyProjects = HashMultimap.create();

  public void register(MavenProject project, Key cacheKey) {
    // project.file is null for parent pom.xml resolved from repositories
    File file = project.getFile();
    if(file != null) {
      projectKeys.put(file, cacheKey);
      keyProjects.put(cacheKey, file);
    }
  }

  public Set<File> removeProject(File pom, boolean forceDependencyUpdate) {
    RepositorySystemSession session = MavenExecutionContext.getThreadContext().getRepositorySession();
    RepositoryCache sessionCache = session.getCache();

    final Set<File> affectedProjects = new HashSet<>();

    for(Key cacheKey : projectKeys.removeAll(pom)) {
      keyProjects.remove(cacheKey, pom);
      if(forceDependencyUpdate && RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals(session.getUpdatePolicy())
          && sessionCache.get(session, cacheKey) == null) {
        sessionCache.put(session, cacheKey, Boolean.TRUE);
        for(File affectedPom : keyProjects.removeAll(cacheKey)) {
          affectedProjects.add(affectedPom);
          projectKeys.remove(affectedPom, cacheKey);
        }
      }
      if(!keyProjects.containsKey(cacheKey)) {
        flush(cacheKey);
        log.debug("Flushed cache entry for {}", cacheKey);
      }
    }

    return affectedProjects;
  }

  protected void disposeClassRealm(ClassRealm realm) {
    try {
      realm.getWorld().disposeRealm(realm.getId());
    } catch(NoSuchRealmException e) {
      // ignore
    }
  }

  protected abstract void flush(Key cacheKey);

  public void flush() {
    projectKeys.clear();
    keyProjects.clear();
  }
}

Back to the top