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.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Objects;
28 import java.util.concurrent.ConcurrentHashMap;
29
30 import org.apache.maven.RepositoryUtils;
31 import org.apache.maven.model.Plugin;
32 import org.apache.maven.plugin.descriptor.PluginDescriptor;
33 import org.eclipse.aether.RepositorySystemSession;
34 import org.eclipse.aether.repository.LocalRepository;
35 import org.eclipse.aether.repository.RemoteRepository;
36 import org.eclipse.aether.repository.WorkspaceRepository;
37
38
39
40
41
42
43
44
45
46
47 @Named
48 @Singleton
49 public class DefaultPluginDescriptorCache implements PluginDescriptorCache {
50
51 private Map<Key, PluginDescriptor> descriptors = new ConcurrentHashMap<>(128);
52 private Map<Key, Key> keys = new ConcurrentHashMap<>();
53
54 public void flush() {
55 descriptors.clear();
56 }
57
58 public Key createKey(Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session) {
59 return keys.computeIfAbsent(new CacheKey(plugin, repositories, session), k -> k);
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
70 try {
71 PluginDescriptor desc = descriptors.get(key);
72 if (desc == null) {
73 synchronized (key) {
74 desc = descriptors.get(key);
75 if (desc == null) {
76 desc = supplier.load();
77 descriptors.putIfAbsent(key, clone(desc));
78 }
79 }
80 }
81 return clone(desc);
82 } catch (PluginDescriptorParsingException | PluginResolutionException | InvalidPluginDescriptorException e) {
83 throw e;
84 }
85 }
86
87 public void put(Key cacheKey, PluginDescriptor pluginDescriptor) {
88 descriptors.put(cacheKey, clone(pluginDescriptor));
89 }
90
91 protected static PluginDescriptor clone(PluginDescriptor original) {
92 return new PluginDescriptor(original);
93 }
94
95 private static final class CacheKey implements Key {
96
97 private final String groupId;
98
99 private final String artifactId;
100
101 private final String version;
102
103 private final WorkspaceRepository workspace;
104
105 private final LocalRepository localRepo;
106
107 private final List<RemoteRepository> repositories;
108
109 private final int hashCode;
110
111 CacheKey(Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session) {
112 groupId = plugin.getGroupId();
113 artifactId = plugin.getArtifactId();
114 version = plugin.getVersion();
115
116 workspace = RepositoryUtils.getWorkspace(session);
117 localRepo = session.getLocalRepository();
118 this.repositories = new ArrayList<>(repositories.size());
119 for (RemoteRepository repository : repositories) {
120 if (repository.isRepositoryManager()) {
121 this.repositories.addAll(repository.getMirroredRepositories());
122 } else {
123 this.repositories.add(repository);
124 }
125 }
126
127 int hash = 17;
128 hash = hash * 31 + groupId.hashCode();
129 hash = hash * 31 + artifactId.hashCode();
130 hash = hash * 31 + version.hashCode();
131 hash = hash * 31 + hash(workspace);
132 hash = hash * 31 + localRepo.hashCode();
133 hash = hash * 31 + RepositoryUtils.repositoriesHashCode(repositories);
134 this.hashCode = hash;
135 }
136
137 @Override
138 public int hashCode() {
139 return hashCode;
140 }
141
142 @Override
143 public boolean equals(Object obj) {
144 if (this == obj) {
145 return true;
146 }
147
148 if (!(obj instanceof CacheKey)) {
149 return false;
150 }
151
152 CacheKey that = (CacheKey) obj;
153
154 return Objects.equals(this.artifactId, that.artifactId)
155 && Objects.equals(this.groupId, that.groupId)
156 && Objects.equals(this.version, that.version)
157 && Objects.equals(this.localRepo, that.localRepo)
158 && Objects.equals(this.workspace, that.workspace)
159 && RepositoryUtils.repositoriesEquals(this.repositories, that.repositories);
160 }
161
162 @Override
163 public String toString() {
164 return groupId + ':' + artifactId + ':' + version;
165 }
166
167 private static int hash(Object obj) {
168 return obj != null ? obj.hashCode() : 0;
169 }
170 }
171 }