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
45 public class TestRepositoryConnector implements RepositoryConnector {
46
47 private RemoteRepository repository;
48
49 private File basedir;
50
51 public TestRepositoryConnector(RemoteRepository repository) {
52 this.repository = repository;
53 String repositoryUrl = repository.getUrl();
54 if (repositoryUrl.contains("${")) {
55
56
57 this.basedir = null;
58 } else {
59 try {
60 URL url = new URL(repository.getUrl());
61 if ("file".equals(url.getProtocol())) {
62 basedir = new File(url.getPath());
63 }
64 } catch (MalformedURLException e) {
65 throw new IllegalStateException(e);
66 }
67 }
68 }
69
70 public void close() {}
71
72 public void get(
73 Collection<? extends ArtifactDownload> artifactDownloads,
74 Collection<? extends MetadataDownload> metadataDownloads) {
75 if (artifactDownloads != null) {
76 for (ArtifactDownload download : artifactDownloads) {
77 File remoteFile = new File(basedir, path(download.getArtifact()));
78 try {
79 Path dest = download.getFile().toPath();
80 Files.createDirectories(dest.getParent());
81 Files.copy(remoteFile.toPath(), dest);
82 } catch (IOException e) {
83 if (!remoteFile.exists()) {
84 download.setException(new ArtifactNotFoundException(download.getArtifact(), repository));
85 } else {
86 download.setException(new ArtifactTransferException(download.getArtifact(), repository, e));
87 }
88 }
89 }
90 }
91 if (metadataDownloads != null) {
92 for (final MetadataDownload download : metadataDownloads) {
93 File remoteFile = new File(basedir, path(download.getMetadata()));
94 try {
95 Path dest = download.getFile().toPath();
96 Files.createDirectories(dest.getParent());
97 Files.copy(remoteFile.toPath(), dest);
98 } catch (IOException e) {
99 if (!remoteFile.exists()) {
100 download.setException(new MetadataNotFoundException(download.getMetadata(), repository));
101 } else {
102 download.setException(new MetadataTransferException(download.getMetadata(), repository, e));
103 }
104 }
105 }
106 }
107 }
108
109 private String path(Artifact artifact) {
110 StringBuilder path = new StringBuilder(128);
111
112 path.append(artifact.getGroupId().replace('.', '/')).append('/');
113
114 path.append(artifact.getArtifactId()).append('/');
115
116 path.append(artifact.getBaseVersion()).append('/');
117
118 path.append(artifact.getArtifactId()).append('-').append(artifact.getVersion());
119
120 if (artifact.getClassifier().length() > 0) {
121 path.append('-').append(artifact.getClassifier());
122 }
123
124 path.append('.').append(artifact.getExtension());
125
126 return path.toString();
127 }
128
129 private String path(Metadata metadata) {
130 StringBuilder path = new StringBuilder(128);
131
132 path.append(metadata.getGroupId().replace('.', '/')).append('/');
133
134 path.append(metadata.getArtifactId()).append('/');
135
136 path.append("maven-metadata.xml");
137
138 return path.toString();
139 }
140
141 public void put(
142 Collection<? extends ArtifactUpload> artifactUploads,
143 Collection<? extends MetadataUpload> metadataUploads) {
144
145
146 }
147 }