1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.repository;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.util.Collection;
28
29 import org.eclipse.aether.artifact.Artifact;
30 import org.eclipse.aether.metadata.Metadata;
31 import org.eclipse.aether.repository.RemoteRepository;
32 import org.eclipse.aether.spi.connector.ArtifactDownload;
33 import org.eclipse.aether.spi.connector.ArtifactUpload;
34 import org.eclipse.aether.spi.connector.MetadataDownload;
35 import org.eclipse.aether.spi.connector.MetadataUpload;
36 import org.eclipse.aether.spi.connector.RepositoryConnector;
37 import org.eclipse.aether.transfer.ArtifactNotFoundException;
38 import org.eclipse.aether.transfer.ArtifactTransferException;
39 import org.eclipse.aether.transfer.MetadataNotFoundException;
40 import org.eclipse.aether.transfer.MetadataTransferException;
41
42
43
44 public class TestRepositoryConnector implements RepositoryConnector {
45
46 private RemoteRepository repository;
47
48 private File basedir;
49
50 public TestRepositoryConnector(RemoteRepository repository) {
51 this.repository = repository;
52 String repositoryUrl = repository.getUrl();
53 if (repositoryUrl.contains("${")) {
54
55
56 this.basedir = null;
57 } else {
58 try {
59 URL url = new URL(repository.getUrl());
60 if ("file".equals(url.getProtocol())) {
61 basedir = new File(url.getPath());
62 }
63 } catch (MalformedURLException e) {
64 throw new IllegalStateException(e);
65 }
66 }
67 }
68
69 public void close() {}
70
71 public void get(
72 Collection<? extends ArtifactDownload> artifactDownloads,
73 Collection<? extends MetadataDownload> metadataDownloads) {
74 if (artifactDownloads != null) {
75 for (ArtifactDownload download : artifactDownloads) {
76 File remoteFile = new File(basedir, path(download.getArtifact()));
77 try {
78 Path dest = download.getFile().toPath();
79 Files.createDirectories(dest.getParent());
80 Files.copy(remoteFile.toPath(), dest);
81 } catch (IOException e) {
82 if (!remoteFile.exists()) {
83 download.setException(new ArtifactNotFoundException(download.getArtifact(), repository));
84 } else {
85 download.setException(new ArtifactTransferException(download.getArtifact(), repository, e));
86 }
87 }
88 }
89 }
90 if (metadataDownloads != null) {
91 for (final MetadataDownload download : metadataDownloads) {
92 File remoteFile = new File(basedir, path(download.getMetadata()));
93 try {
94 Path dest = download.getFile().toPath();
95 Files.createDirectories(dest.getParent());
96 Files.copy(remoteFile.toPath(), dest);
97 } catch (IOException e) {
98 if (!remoteFile.exists()) {
99 download.setException(new MetadataNotFoundException(download.getMetadata(), repository));
100 } else {
101 download.setException(new MetadataTransferException(download.getMetadata(), repository, e));
102 }
103 }
104 }
105 }
106 }
107
108 private String path(Artifact artifact) {
109 StringBuilder path = new StringBuilder(128);
110
111 path.append(artifact.getGroupId().replace('.', '/')).append('/');
112
113 path.append(artifact.getArtifactId()).append('/');
114
115 path.append(artifact.getBaseVersion()).append('/');
116
117 path.append(artifact.getArtifactId()).append('-').append(artifact.getVersion());
118
119 if (artifact.getClassifier().length() > 0) {
120 path.append('-').append(artifact.getClassifier());
121 }
122
123 path.append('.').append(artifact.getExtension());
124
125 return path.toString();
126 }
127
128 private String path(Metadata metadata) {
129 StringBuilder path = new StringBuilder(128);
130
131 path.append(metadata.getGroupId().replace('.', '/')).append('/');
132
133 path.append(metadata.getArtifactId()).append('/');
134
135 path.append("maven-metadata.xml");
136
137 return path.toString();
138 }
139
140 public void put(
141 Collection<? extends ArtifactUpload> artifactUploads,
142 Collection<? extends MetadataUpload> metadataUploads) {
143
144
145 }
146 }