1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.project.artifact;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.LinkedHashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Objects;
29 import java.util.Set;
30 import java.util.concurrent.ConcurrentHashMap;
31 import javax.inject.Named;
32 import javax.inject.Singleton;
33 import org.apache.maven.RepositoryUtils;
34 import org.apache.maven.artifact.Artifact;
35 import org.apache.maven.lifecycle.LifecycleExecutionException;
36 import org.apache.maven.project.MavenProject;
37 import org.eclipse.aether.RepositorySystemSession;
38 import org.eclipse.aether.repository.LocalRepository;
39 import org.eclipse.aether.repository.RemoteRepository;
40 import org.eclipse.aether.repository.WorkspaceRepository;
41
42
43
44
45
46
47 @Named
48 @Singleton
49 public class DefaultProjectArtifactsCache implements ProjectArtifactsCache {
50
51
52
53 protected static class CacheKey implements Key {
54
55 private final String groupId;
56
57 private final String artifactId;
58
59 private final String version;
60
61 private final Set<String> dependencyArtifacts;
62
63 private final WorkspaceRepository workspace;
64
65 private final LocalRepository localRepo;
66
67 private final List<RemoteRepository> repositories;
68
69 private final Set<String> collect;
70
71 private final Set<String> resolve;
72
73 private boolean aggregating;
74
75 private final int hashCode;
76
77 public CacheKey(
78 MavenProject project,
79 List<RemoteRepository> repositories,
80 Collection<String> scopesToCollect,
81 Collection<String> scopesToResolve,
82 boolean aggregating,
83 RepositorySystemSession session) {
84
85 groupId = project.getGroupId();
86 artifactId = project.getArtifactId();
87 version = project.getVersion();
88
89 Set<String> deps = new LinkedHashSet<>();
90 if (project.getDependencyArtifacts() != null) {
91 for (Artifact dep : project.getDependencyArtifacts()) {
92 deps.add(dep.toString());
93 }
94 }
95 dependencyArtifacts = Collections.unmodifiableSet(deps);
96
97 workspace = RepositoryUtils.getWorkspace(session);
98 this.localRepo = session.getLocalRepository();
99 this.repositories = new ArrayList<>(repositories.size());
100 for (RemoteRepository repository : repositories) {
101 if (repository.isRepositoryManager()) {
102 this.repositories.addAll(repository.getMirroredRepositories());
103 } else {
104 this.repositories.add(repository);
105 }
106 }
107 collect = scopesToCollect == null
108 ? Collections.emptySet()
109 : Collections.unmodifiableSet(new HashSet<>(scopesToCollect));
110 resolve = scopesToResolve == null
111 ? Collections.emptySet()
112 : Collections.unmodifiableSet(new HashSet<>(scopesToResolve));
113 this.aggregating = aggregating;
114
115 int hash = 17;
116 hash = hash * 31 + Objects.hashCode(groupId);
117 hash = hash * 31 + Objects.hashCode(artifactId);
118 hash = hash * 31 + Objects.hashCode(version);
119 hash = hash * 31 + Objects.hashCode(dependencyArtifacts);
120 hash = hash * 31 + Objects.hashCode(workspace);
121 hash = hash * 31 + Objects.hashCode(localRepo);
122 hash = hash * 31 + RepositoryUtils.repositoriesHashCode(repositories);
123 hash = hash * 31 + Objects.hashCode(collect);
124 hash = hash * 31 + Objects.hashCode(resolve);
125 hash = hash * 31 + Objects.hashCode(aggregating);
126 this.hashCode = hash;
127 }
128
129 @Override
130 public String toString() {
131 return groupId + ":" + artifactId + ":" + version;
132 }
133
134 @Override
135 public int hashCode() {
136 return hashCode;
137 }
138
139 @Override
140 public boolean equals(Object o) {
141 if (o == this) {
142 return true;
143 }
144
145 if (!(o instanceof CacheKey)) {
146 return false;
147 }
148
149 CacheKey that = (CacheKey) o;
150
151 return Objects.equals(groupId, that.groupId)
152 && Objects.equals(artifactId, that.artifactId)
153 && Objects.equals(version, that.version)
154 && Objects.equals(dependencyArtifacts, that.dependencyArtifacts)
155 && Objects.equals(workspace, that.workspace)
156 && Objects.equals(localRepo, that.localRepo)
157 && RepositoryUtils.repositoriesEquals(repositories, that.repositories)
158 && Objects.equals(collect, that.collect)
159 && Objects.equals(resolve, that.resolve)
160 && aggregating == that.aggregating;
161 }
162 }
163
164 protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<>();
165
166 @Override
167 public Key createKey(
168 MavenProject project,
169 Collection<String> scopesToCollect,
170 Collection<String> scopesToResolve,
171 boolean aggregating,
172 RepositorySystemSession session) {
173 return new CacheKey(
174 project,
175 project.getRemoteProjectRepositories(),
176 scopesToCollect,
177 scopesToResolve,
178 aggregating,
179 session);
180 }
181
182 @Override
183 public CacheRecord get(Key key) throws LifecycleExecutionException {
184 CacheRecord cacheRecord = cache.get(key);
185
186 if (cacheRecord != null && cacheRecord.getException() != null) {
187 throw cacheRecord.getException();
188 }
189
190 return cacheRecord;
191 }
192
193 @Override
194 public CacheRecord put(Key key, Set<Artifact> projectArtifacts) {
195 Objects.requireNonNull(projectArtifacts, "projectArtifacts cannot be null");
196
197 assertUniqueKey(key);
198
199 CacheRecord record = new CacheRecord(Collections.unmodifiableSet(new LinkedHashSet<>(projectArtifacts)));
200
201 cache.put(key, record);
202
203 return record;
204 }
205
206 protected void assertUniqueKey(Key key) {
207 if (cache.containsKey(key)) {
208 throw new IllegalStateException("Duplicate artifact resolution result for project " + key);
209 }
210 }
211
212 @Override
213 public CacheRecord put(Key key, LifecycleExecutionException exception) {
214 Objects.requireNonNull(exception, "exception cannot be null");
215
216 assertUniqueKey(key);
217
218 CacheRecord record = new CacheRecord(exception);
219
220 cache.put(key, record);
221
222 return record;
223 }
224
225 @Override
226 public void flush() {
227 cache.clear();
228 }
229
230 @Override
231 public void register(MavenProject project, Key cacheKey, CacheRecord record) {
232
233 }
234 }