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