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