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
108 clone.setPluginArtifact(ArtifactUtils.copyArtifactSafe(original.getPluginArtifact()));
109
110 clone.setComponents(clone(original.getMojos(), clone));
111 clone.setId(original.getId());
112 clone.setIsolatedRealm(original.isIsolatedRealm());
113 clone.setSource(original.getSource());
114
115 clone.setDependencies(original.getDependencies());
116 }
117
118 return clone;
119 }
120
121 private static List<ComponentDescriptor<?>> clone(List<MojoDescriptor> mojos, PluginDescriptor pluginDescriptor) {
122 List<ComponentDescriptor<?>> clones = null;
123
124 if (mojos != null) {
125 clones = new ArrayList<>(mojos.size());
126
127 for (MojoDescriptor mojo : mojos) {
128 MojoDescriptor clone = mojo.clone();
129 clone.setPluginDescriptor(pluginDescriptor);
130 clones.add(clone);
131 }
132 }
133
134 return clones;
135 }
136
137 private static final class CacheKey implements Key {
138
139 private final String groupId;
140
141 private final String artifactId;
142
143 private final String version;
144
145 private final WorkspaceRepository workspace;
146
147 private final LocalRepository localRepo;
148
149 private final List<RemoteRepository> repositories;
150
151 private final int hashCode;
152
153 CacheKey(Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session) {
154 groupId = plugin.getGroupId();
155 artifactId = plugin.getArtifactId();
156 version = plugin.getVersion();
157
158 workspace = RepositoryUtils.getWorkspace(session);
159 localRepo = session.getLocalRepository();
160 this.repositories = new ArrayList<>(repositories.size());
161 for (RemoteRepository repository : repositories) {
162 if (repository.isRepositoryManager()) {
163 this.repositories.addAll(repository.getMirroredRepositories());
164 } else {
165 this.repositories.add(repository);
166 }
167 }
168
169 int hash = 17;
170 hash = hash * 31 + groupId.hashCode();
171 hash = hash * 31 + artifactId.hashCode();
172 hash = hash * 31 + version.hashCode();
173 hash = hash * 31 + hash(workspace);
174 hash = hash * 31 + localRepo.hashCode();
175 hash = hash * 31 + RepositoryUtils.repositoriesHashCode(repositories);
176 this.hashCode = hash;
177 }
178
179 @Override
180 public int hashCode() {
181 return hashCode;
182 }
183
184 @Override
185 public boolean equals(Object obj) {
186 if (this == obj) {
187 return true;
188 }
189
190 if (!(obj instanceof CacheKey)) {
191 return false;
192 }
193
194 CacheKey that = (CacheKey) obj;
195
196 return Objects.equals(this.artifactId, that.artifactId)
197 && Objects.equals(this.groupId, that.groupId)
198 && Objects.equals(this.version, that.version)
199 && Objects.equals(this.localRepo, that.localRepo)
200 && Objects.equals(this.workspace, that.workspace)
201 && RepositoryUtils.repositoriesEquals(this.repositories, that.repositories);
202 }
203
204 @Override
205 public String toString() {
206 return groupId + ':' + artifactId + ':' + version;
207 }
208
209 private static int hash(Object obj) {
210 return obj != null ? obj.hashCode() : 0;
211 }
212 }
213 }