1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
20
21 import java.io.File;
22 import java.nio.file.Path;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Objects;
29 import java.util.Optional;
30 import java.util.WeakHashMap;
31 import java.util.concurrent.CopyOnWriteArrayList;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
34
35 import org.apache.maven.api.Artifact;
36 import org.apache.maven.api.ArtifactCoordinate;
37 import org.apache.maven.api.Dependency;
38 import org.apache.maven.api.DependencyCoordinate;
39 import org.apache.maven.api.Listener;
40 import org.apache.maven.api.LocalRepository;
41 import org.apache.maven.api.Node;
42 import org.apache.maven.api.Project;
43 import org.apache.maven.api.RemoteRepository;
44 import org.apache.maven.api.Session;
45 import org.apache.maven.api.Version;
46 import org.apache.maven.api.VersionRange;
47 import org.apache.maven.api.annotations.Nonnull;
48 import org.apache.maven.api.model.Repository;
49 import org.apache.maven.api.services.ArtifactCoordinateFactory;
50 import org.apache.maven.api.services.ArtifactDeployer;
51 import org.apache.maven.api.services.ArtifactDeployerException;
52 import org.apache.maven.api.services.ArtifactFactory;
53 import org.apache.maven.api.services.ArtifactInstaller;
54 import org.apache.maven.api.services.ArtifactInstallerException;
55 import org.apache.maven.api.services.ArtifactManager;
56 import org.apache.maven.api.services.ArtifactResolver;
57 import org.apache.maven.api.services.ArtifactResolverException;
58 import org.apache.maven.api.services.DependencyCollector;
59 import org.apache.maven.api.services.DependencyCollectorException;
60 import org.apache.maven.api.services.DependencyCoordinateFactory;
61 import org.apache.maven.api.services.LocalRepositoryManager;
62 import org.apache.maven.api.services.RepositoryFactory;
63 import org.apache.maven.api.services.VersionParser;
64 import org.apache.maven.artifact.repository.ArtifactRepository;
65 import org.apache.maven.project.MavenProject;
66
67 import static org.apache.maven.internal.impl.Utils.nonNull;
68
69 public abstract class AbstractSession implements Session {
70
71 private final List<Listener> listeners = new CopyOnWriteArrayList<>();
72 private final Map<org.eclipse.aether.graph.DependencyNode, Node> allNodes =
73 Collections.synchronizedMap(new WeakHashMap<>());
74 private final Map<org.eclipse.aether.artifact.Artifact, Artifact> allArtifacts =
75 Collections.synchronizedMap(new WeakHashMap<>());
76 private final Map<org.eclipse.aether.repository.RemoteRepository, RemoteRepository> allRepositories =
77 Collections.synchronizedMap(new WeakHashMap<>());
78 private final Map<String, Project> allProjects = Collections.synchronizedMap(new WeakHashMap<>());
79 private final Map<org.eclipse.aether.graph.Dependency, Dependency> allDependencies =
80 Collections.synchronizedMap(new WeakHashMap<>());
81
82 public RemoteRepository getRemoteRepository(org.eclipse.aether.repository.RemoteRepository repository) {
83 return allRepositories.computeIfAbsent(repository, DefaultRemoteRepository::new);
84 }
85
86 public Node getNode(org.eclipse.aether.graph.DependencyNode node) {
87 return getNode(node, false);
88 }
89
90 public Node getNode(org.eclipse.aether.graph.DependencyNode node, boolean verbose) {
91 return allNodes.computeIfAbsent(node, n -> new DefaultNode(this, n, verbose));
92 }
93
94 @Nonnull
95 public Artifact getArtifact(@Nonnull org.eclipse.aether.artifact.Artifact artifact) {
96 return allArtifacts.computeIfAbsent(artifact, a -> new DefaultArtifact(this, a));
97 }
98
99 @Nonnull
100 public Dependency getDependency(@Nonnull org.eclipse.aether.graph.Dependency dependency) {
101 return allDependencies.computeIfAbsent(dependency, d -> new DefaultDependency(this, d));
102 }
103
104 public List<Project> getProjects(List<MavenProject> projects) {
105 return projects == null ? null : projects.stream().map(this::getProject).collect(Collectors.toList());
106 }
107
108 public Project getProject(MavenProject project) {
109 return allProjects.computeIfAbsent(project.getId(), id -> new DefaultProject(this, project));
110 }
111
112 public List<org.eclipse.aether.repository.RemoteRepository> toRepositories(List<RemoteRepository> repositories) {
113 return repositories == null
114 ? null
115 : repositories.stream().map(this::toRepository).collect(Collectors.toList());
116 }
117
118 public org.eclipse.aether.repository.RemoteRepository toRepository(RemoteRepository repository) {
119 if (repository instanceof DefaultRemoteRepository) {
120 return ((DefaultRemoteRepository) repository).getRepository();
121 } else {
122
123 throw new UnsupportedOperationException("Not implemented yet");
124 }
125 }
126
127 public org.eclipse.aether.repository.LocalRepository toRepository(LocalRepository repository) {
128 if (repository instanceof DefaultLocalRepository) {
129 return ((DefaultLocalRepository) repository).getRepository();
130 } else {
131
132 throw new UnsupportedOperationException("Not implemented yet");
133 }
134 }
135
136 public List<ArtifactRepository> toArtifactRepositories(List<RemoteRepository> repositories) {
137 return repositories == null
138 ? null
139 : repositories.stream().map(this::toArtifactRepository).collect(Collectors.toList());
140 }
141
142 public abstract ArtifactRepository toArtifactRepository(RemoteRepository repository);
143
144 public List<org.eclipse.aether.graph.Dependency> toDependencies(Collection<DependencyCoordinate> dependencies) {
145 return dependencies == null
146 ? null
147 : dependencies.stream().map(this::toDependency).collect(Collectors.toList());
148 }
149
150 public abstract org.eclipse.aether.graph.Dependency toDependency(DependencyCoordinate dependency);
151
152 public List<org.eclipse.aether.artifact.Artifact> toArtifacts(Collection<Artifact> artifacts) {
153 return artifacts == null
154 ? null
155 : artifacts.stream().map(this::toArtifact).collect(Collectors.toList());
156 }
157
158 public org.eclipse.aether.artifact.Artifact toArtifact(Artifact artifact) {
159 File file = getService(ArtifactManager.class)
160 .getPath(artifact)
161 .map(Path::toFile)
162 .orElse(null);
163 if (artifact instanceof DefaultArtifact) {
164 org.eclipse.aether.artifact.Artifact a = ((DefaultArtifact) artifact).getArtifact();
165 if (Objects.equals(file, a.getFile())) {
166 return a;
167 }
168 }
169 return new org.eclipse.aether.artifact.DefaultArtifact(
170 artifact.getGroupId(),
171 artifact.getArtifactId(),
172 artifact.getClassifier(),
173 artifact.getExtension(),
174 artifact.getVersion().toString(),
175 null,
176 file);
177 }
178
179 public org.eclipse.aether.artifact.Artifact toArtifact(ArtifactCoordinate coord) {
180 if (coord instanceof DefaultArtifactCoordinate) {
181 return ((DefaultArtifactCoordinate) coord).getCoordinate();
182 }
183 return new org.eclipse.aether.artifact.DefaultArtifact(
184 coord.getGroupId(),
185 coord.getArtifactId(),
186 coord.getClassifier(),
187 coord.getExtension(),
188 coord.getVersion().toString(),
189 null,
190 (File) null);
191 }
192
193 @Override
194 public void registerListener(@Nonnull Listener listener) {
195 listeners.add(nonNull(listener));
196 }
197
198 @Override
199 public void unregisterListener(@Nonnull Listener listener) {
200 listeners.remove(nonNull(listener));
201 }
202
203 @Nonnull
204 @Override
205 public Collection<Listener> getListeners() {
206 return Collections.unmodifiableCollection(listeners);
207 }
208
209
210
211
212
213
214
215
216
217
218 @Override
219 public LocalRepository createLocalRepository(Path path) {
220 return getService(RepositoryFactory.class).createLocal(path);
221 }
222
223
224
225
226
227
228 @Nonnull
229 @Override
230 public RemoteRepository createRemoteRepository(@Nonnull String id, @Nonnull String url) {
231 return getService(RepositoryFactory.class).createRemote(id, url);
232 }
233
234
235
236
237
238
239 @Nonnull
240 @Override
241 public RemoteRepository createRemoteRepository(@Nonnull Repository repository) {
242 return getService(RepositoryFactory.class).createRemote(repository);
243 }
244
245
246
247
248
249
250 @Override
251 public ArtifactCoordinate createArtifactCoordinate(
252 String groupId, String artifactId, String version, String extension) {
253 return getService(ArtifactCoordinateFactory.class).create(this, groupId, artifactId, version, extension);
254 }
255
256
257
258
259
260
261 @Override
262 public ArtifactCoordinate createArtifactCoordinate(
263 String groupId, String artifactId, String version, String classifier, String extension, String type) {
264 return getService(ArtifactCoordinateFactory.class)
265 .create(this, groupId, artifactId, version, classifier, extension, type);
266 }
267
268
269
270
271
272
273 @Override
274 public ArtifactCoordinate createArtifactCoordinate(Artifact artifact) {
275 return getService(ArtifactCoordinateFactory.class)
276 .create(
277 this,
278 artifact.getGroupId(),
279 artifact.getArtifactId(),
280 artifact.getVersion().asString(),
281 artifact.getClassifier(),
282 artifact.getExtension(),
283 null);
284 }
285
286
287
288
289
290
291 @Override
292 public Artifact createArtifact(String groupId, String artifactId, String version, String extension) {
293 return getService(ArtifactFactory.class).create(this, groupId, artifactId, version, extension);
294 }
295
296
297
298
299
300
301 @Override
302 public Artifact createArtifact(
303 String groupId, String artifactId, String version, String classifier, String extension, String type) {
304 return getService(ArtifactFactory.class)
305 .create(this, groupId, artifactId, version, classifier, extension, type);
306 }
307
308
309
310
311
312
313
314 @Override
315 public Artifact resolveArtifact(ArtifactCoordinate coordinate) {
316 return getService(ArtifactResolver.class)
317 .resolve(this, Collections.singletonList(coordinate))
318 .getArtifacts()
319 .keySet()
320 .iterator()
321 .next();
322 }
323
324
325
326
327
328
329
330 @Override
331 public Collection<Artifact> resolveArtifacts(ArtifactCoordinate... coordinates) {
332 return resolveArtifacts(Arrays.asList(coordinates));
333 }
334
335
336
337
338
339
340
341 @Override
342 public Collection<Artifact> resolveArtifacts(Collection<? extends ArtifactCoordinate> coordinates) {
343 return getService(ArtifactResolver.class)
344 .resolve(this, coordinates)
345 .getArtifacts()
346 .keySet();
347 }
348
349
350
351
352
353
354
355 @Override
356 public Artifact resolveArtifact(Artifact artifact) {
357 ArtifactCoordinate coordinate =
358 getService(ArtifactCoordinateFactory.class).create(this, artifact);
359 return resolveArtifact(coordinate);
360 }
361
362 @Override
363 public Collection<Artifact> resolveArtifacts(Artifact... artifacts) {
364 ArtifactCoordinateFactory acf = getService(ArtifactCoordinateFactory.class);
365 ArtifactCoordinate[] coords =
366 Stream.of(artifacts).map(a -> acf.create(this, a)).toArray(ArtifactCoordinate[]::new);
367 return resolveArtifacts(coords);
368 }
369
370
371
372
373
374
375
376 @Override
377 public void installArtifacts(Artifact... artifacts) {
378 installArtifacts(Arrays.asList(artifacts));
379 }
380
381
382
383
384
385
386
387 @Override
388 public void installArtifacts(Collection<Artifact> artifacts) {
389 getService(ArtifactInstaller.class).install(this, artifacts);
390 }
391
392
393
394
395
396
397
398 @Override
399 public void deployArtifact(RemoteRepository repository, Artifact... artifacts) {
400 getService(ArtifactDeployer.class).deploy(this, repository, Arrays.asList(artifacts));
401 }
402
403
404
405
406
407
408 @Override
409 public void setArtifactPath(@Nonnull Artifact artifact, @Nonnull Path path) {
410 getService(ArtifactManager.class).setPath(artifact, path);
411 }
412
413
414
415
416
417
418 @Nonnull
419 @Override
420 public Optional<Path> getArtifactPath(@Nonnull Artifact artifact) {
421 return getService(ArtifactManager.class).getPath(artifact);
422 }
423
424
425
426
427
428
429 @Override
430 public boolean isVersionSnapshot(@Nonnull String version) {
431 return getService(VersionParser.class).isSnapshot(version);
432 }
433
434
435
436
437
438
439 @Nonnull
440 @Override
441 public DependencyCoordinate createDependencyCoordinate(@Nonnull ArtifactCoordinate coordinate) {
442 return getService(DependencyCoordinateFactory.class).create(this, coordinate);
443 }
444
445
446
447
448
449
450 @Nonnull
451 public DependencyCoordinate createDependencyCoordinate(@Nonnull Dependency dependency) {
452 return getService(DependencyCoordinateFactory.class).create(this, dependency);
453 }
454
455
456
457
458
459
460
461 @Nonnull
462 @Override
463 public Node collectDependencies(@Nonnull Artifact artifact) {
464 return getService(DependencyCollector.class).collect(this, artifact).getRoot();
465 }
466
467
468
469
470
471
472
473 @Nonnull
474 @Override
475 public Node collectDependencies(@Nonnull Project project) {
476 return getService(DependencyCollector.class).collect(this, project).getRoot();
477 }
478
479
480
481
482
483
484
485 @Nonnull
486 @Override
487 public Node collectDependencies(@Nonnull DependencyCoordinate dependency) {
488 return getService(DependencyCollector.class).collect(this, dependency).getRoot();
489 }
490
491 @Override
492 public Path getPathForLocalArtifact(@Nonnull Artifact artifact) {
493 return getService(LocalRepositoryManager.class).getPathForLocalArtifact(this, getLocalRepository(), artifact);
494 }
495
496 @Override
497 public Path getPathForRemoteArtifact(RemoteRepository remote, Artifact artifact) {
498 return getService(LocalRepositoryManager.class)
499 .getPathForRemoteArtifact(this, getLocalRepository(), remote, artifact);
500 }
501
502 @Override
503 public Version parseVersion(String version) {
504 return getService(VersionParser.class).parseVersion(version);
505 }
506
507 @Override
508 public VersionRange parseVersionRange(String versionRange) {
509 return getService(VersionParser.class).parseVersionRange(versionRange);
510 }
511 }