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