1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import java.io.File;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Objects;
29 import java.util.concurrent.ConcurrentHashMap;
30
31 import org.apache.maven.artifact.Artifact;
32 import org.apache.maven.project.ExtensionDescriptor;
33 import org.apache.maven.project.MavenProject;
34 import org.codehaus.plexus.classworlds.realm.ClassRealm;
35 import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
36 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
37
38
39
40
41 @Named
42 @Singleton
43 public class DefaultExtensionRealmCache implements ExtensionRealmCache, Disposable {
44
45
46
47 protected static class CacheKey implements Key {
48
49 private final List<File> files;
50
51 private final List<Long> timestamps;
52
53 private final List<Long> sizes;
54
55 private final List<String> ids;
56
57 private final int hashCode;
58
59 public CacheKey(List<Artifact> extensionArtifacts) {
60 this.files = new ArrayList<>(extensionArtifacts.size());
61 this.timestamps = new ArrayList<>(extensionArtifacts.size());
62 this.sizes = new ArrayList<>(extensionArtifacts.size());
63 this.ids = new ArrayList<>(extensionArtifacts.size());
64
65 for (Artifact artifact : extensionArtifacts) {
66 File file = artifact.getFile();
67 files.add(file);
68 timestamps.add((file != null) ? Long.valueOf(file.lastModified()) : Long.valueOf(0));
69 sizes.add((file != null) ? Long.valueOf(file.length()) : Long.valueOf(0));
70 ids.add(artifact.getVersion());
71 }
72
73 this.hashCode =
74 31 * files.hashCode() + 31 * ids.hashCode() + 31 * timestamps.hashCode() + 31 * sizes.hashCode();
75 }
76
77 @Override
78 public int hashCode() {
79 return hashCode;
80 }
81
82 @Override
83 public boolean equals(Object o) {
84 if (o == this) {
85 return true;
86 }
87
88 if (!(o instanceof CacheKey)) {
89 return false;
90 }
91
92 CacheKey other = (CacheKey) o;
93
94 return ids.equals(other.ids)
95 && files.equals(other.files)
96 && timestamps.equals(other.timestamps)
97 && sizes.equals(other.sizes);
98 }
99
100 @Override
101 public String toString() {
102 return files.toString();
103 }
104 }
105
106 protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<>();
107
108 @Override
109 public Key createKey(List<Artifact> extensionArtifacts) {
110 return new CacheKey(extensionArtifacts);
111 }
112
113 public CacheRecord get(Key key) {
114 return cache.get(key);
115 }
116
117 public CacheRecord put(
118 Key key, ClassRealm extensionRealm, ExtensionDescriptor extensionDescriptor, List<Artifact> artifacts) {
119 Objects.requireNonNull(extensionRealm, "extensionRealm cannot be null");
120
121 if (cache.containsKey(key)) {
122 throw new IllegalStateException("Duplicate extension realm for extension " + key);
123 }
124
125 CacheRecord record = new CacheRecord(extensionRealm, extensionDescriptor, artifacts);
126
127 cache.put(key, record);
128
129 return record;
130 }
131
132 public void flush() {
133 for (CacheRecord record : cache.values()) {
134 ClassRealm realm = record.getRealm();
135 try {
136 realm.getWorld().disposeRealm(realm.getId());
137 } catch (NoSuchRealmException e) {
138
139 }
140 }
141 cache.clear();
142 }
143
144 public void register(MavenProject project, Key key, CacheRecord record) {
145
146 }
147
148 public void dispose() {
149 flush();
150 }
151 }