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 java.util.ArrayList;
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.apache.maven.RepositoryUtils;
28 import org.apache.maven.artifact.ArtifactUtils;
29 import org.apache.maven.model.Plugin;
30 import org.apache.maven.plugin.descriptor.MojoDescriptor;
31 import org.apache.maven.plugin.descriptor.PluginDescriptor;
32 import org.codehaus.plexus.component.annotations.Component;
33 import org.codehaus.plexus.component.repository.ComponentDescriptor;
34 import org.eclipse.aether.RepositorySystemSession;
35 import org.eclipse.aether.repository.LocalRepository;
36 import org.eclipse.aether.repository.RemoteRepository;
37 import org.eclipse.aether.repository.WorkspaceRepository;
38
39
40
41
42
43
44
45
46
47
48
49 @Component(role = PluginDescriptorCache.class)
50 public class DefaultPluginDescriptorCache implements PluginDescriptorCache {
51
52 private Map<Key, PluginDescriptor> descriptors = new ConcurrentHashMap<>(128);
53 private Map<Key, Key> keys = new ConcurrentHashMap<>();
54
55 public void flush() {
56 descriptors.clear();
57 }
58
59 public Key createKey(Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session) {
60 return keys.computeIfAbsent(new CacheKey(plugin, repositories, session), k -> k);
61 }
62
63 public PluginDescriptor get(Key cacheKey) {
64 return clone(descriptors.get(cacheKey));
65 }
66
67 @Override
68 public PluginDescriptor get(Key key, PluginDescriptorSupplier supplier)
69 throws PluginDescriptorParsingException, PluginResolutionException, InvalidPluginDescriptorException {
70
71 try {
72 PluginDescriptor desc = descriptors.get(key);
73 if (desc == null) {
74 synchronized (key) {
75 desc = descriptors.get(key);
76 if (desc == null) {
77 desc = supplier.load();
78 descriptors.putIfAbsent(key, clone(desc));
79 }
80 }
81 }
82 return clone(desc);
83 } catch (PluginDescriptorParsingException | PluginResolutionException | InvalidPluginDescriptorException e) {
84 throw e;
85 }
86 }
87
88 public void put(Key cacheKey, PluginDescriptor pluginDescriptor) {
89 descriptors.put(cacheKey, clone(pluginDescriptor));
90 }
91
92 protected static PluginDescriptor clone(PluginDescriptor original) {
93 PluginDescriptor clone = null;
94
95 if (original != null) {
96 clone = new PluginDescriptor();
97
98 clone.setGroupId(original.getGroupId());
99 clone.setArtifactId(original.getArtifactId());
100 clone.setVersion(original.getVersion());
101 clone.setGoalPrefix(original.getGoalPrefix());
102 clone.setInheritedByDefault(original.isInheritedByDefault());
103
104 clone.setName(original.getName());
105 clone.setDescription(original.getDescription());
106 clone.setRequiredMavenVersion(original.getRequiredMavenVersion());
107 clone.setRequiredJavaVersion(original.getRequiredJavaVersion());
108
109 clone.setPluginArtifact(ArtifactUtils.copyArtifactSafe(original.getPluginArtifact()));
110
111 clone.setComponents(clone(original.getMojos(), clone));
112 clone.setId(original.getId());
113 clone.setIsolatedRealm(original.isIsolatedRealm());
114 clone.setSource(original.getSource());
115
116 clone.setDependencies(original.getDependencies());
117 }
118
119 return clone;
120 }
121
122 private static List<ComponentDescriptor<?>> clone(List<MojoDescriptor> mojos, PluginDescriptor pluginDescriptor) {
123 List<ComponentDescriptor<?>> clones = null;
124
125 if (mojos != null) {
126 clones = new ArrayList<>(mojos.size());
127
128 for (MojoDescriptor mojo : mojos) {
129 MojoDescriptor clone = mojo.clone();
130 clone.setPluginDescriptor(pluginDescriptor);
131 clones.add(clone);
132 }
133 }
134
135 return clones;
136 }
137
138 private static final class CacheKey implements Key {
139
140 private final String groupId;
141
142 private final String artifactId;
143
144 private final String version;
145
146 private final WorkspaceRepository workspace;
147
148 private final LocalRepository localRepo;
149
150 private final List<RemoteRepository> repositories;
151
152 private final int hashCode;
153
154 CacheKey(Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session) {
155 groupId = plugin.getGroupId();
156 artifactId = plugin.getArtifactId();
157 version = plugin.getVersion();
158
159 workspace = RepositoryUtils.getWorkspace(session);
160 localRepo = session.getLocalRepository();
161 this.repositories = new ArrayList<>(repositories.size());
162 for (RemoteRepository repository : repositories) {
163 if (repository.isRepositoryManager()) {
164 this.repositories.addAll(repository.getMirroredRepositories());
165 } else {
166 this.repositories.add(repository);
167 }
168 }
169
170 int hash = 17;
171 hash = hash * 31 + groupId.hashCode();
172 hash = hash * 31 + artifactId.hashCode();
173 hash = hash * 31 + version.hashCode();
174 hash = hash * 31 + hash(workspace);
175 hash = hash * 31 + localRepo.hashCode();
176 hash = hash * 31 + RepositoryUtils.repositoriesHashCode(repositories);
177 this.hashCode = hash;
178 }
179
180 @Override
181 public int hashCode() {
182 return hashCode;
183 }
184
185 @Override
186 public boolean equals(Object obj) {
187 if (this == obj) {
188 return true;
189 }
190
191 if (!(obj instanceof CacheKey)) {
192 return false;
193 }
194
195 CacheKey that = (CacheKey) obj;
196
197 return Objects.equals(this.artifactId, that.artifactId)
198 && Objects.equals(this.groupId, that.groupId)
199 && Objects.equals(this.version, that.version)
200 && Objects.equals(this.localRepo, that.localRepo)
201 && Objects.equals(this.workspace, that.workspace)
202 && RepositoryUtils.repositoriesEquals(this.repositories, that.repositories);
203 }
204
205 @Override
206 public String toString() {
207 return groupId + ':' + artifactId + ':' + version;
208 }
209
210 private static int hash(Object obj) {
211 return obj != null ? obj.hashCode() : 0;
212 }
213 }
214 }