1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.project;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Objects;
28 import java.util.concurrent.ConcurrentHashMap;
29
30 import org.codehaus.plexus.classworlds.realm.ClassRealm;
31 import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
32 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
33 import org.eclipse.aether.graph.DependencyFilter;
34
35
36
37
38 @Named
39 @Singleton
40 public class DefaultProjectRealmCache implements ProjectRealmCache, Disposable {
41
42
43
44 protected static class CacheKey implements Key {
45
46 private final List<? extends ClassRealm> extensionRealms;
47
48 private final int hashCode;
49
50 public CacheKey(List<? extends ClassRealm> extensionRealms) {
51 this.extensionRealms =
52 (extensionRealms != null) ? Collections.unmodifiableList(extensionRealms) : Collections.emptyList();
53
54 this.hashCode = this.extensionRealms.hashCode();
55 }
56
57 @Override
58 public int hashCode() {
59 return hashCode;
60 }
61
62 @Override
63 public boolean equals(Object o) {
64 if (o == this) {
65 return true;
66 }
67
68 if (!(o instanceof CacheKey)) {
69 return false;
70 }
71
72 CacheKey other = (CacheKey) o;
73
74 return extensionRealms.equals(other.extensionRealms);
75 }
76
77 @Override
78 public String toString() {
79 return extensionRealms.toString();
80 }
81 }
82
83 protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<>();
84
85 @Override
86 public Key createKey(List<? extends ClassRealm> extensionRealms) {
87 return new CacheKey(extensionRealms);
88 }
89
90 public CacheRecord get(Key key) {
91 return cache.get(key);
92 }
93
94 public CacheRecord put(Key key, ClassRealm projectRealm, DependencyFilter extensionArtifactFilter) {
95 Objects.requireNonNull(projectRealm, "projectRealm cannot be null");
96
97 if (cache.containsKey(key)) {
98 throw new IllegalStateException("Duplicate project realm for extensions " + key);
99 }
100
101 CacheRecord record = new CacheRecord(projectRealm, extensionArtifactFilter);
102
103 cache.put(key, record);
104
105 return record;
106 }
107
108 public void flush() {
109 for (CacheRecord record : cache.values()) {
110 ClassRealm realm = record.getRealm();
111 try {
112 realm.getWorld().disposeRealm(realm.getId());
113 } catch (NoSuchRealmException e) {
114
115 }
116 }
117 cache.clear();
118 }
119
120 public void register(MavenProject project, Key key, CacheRecord record) {
121
122 }
123
124 @Override
125 public void dispose() {
126 flush();
127 }
128 }