View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.util.Collection;
26  
27  import org.codehaus.plexus.util.FileUtils;
28  import org.eclipse.aether.artifact.Artifact;
29  import org.eclipse.aether.metadata.Metadata;
30  import org.eclipse.aether.repository.RemoteRepository;
31  import org.eclipse.aether.spi.connector.ArtifactDownload;
32  import org.eclipse.aether.spi.connector.ArtifactUpload;
33  import org.eclipse.aether.spi.connector.MetadataDownload;
34  import org.eclipse.aether.spi.connector.MetadataUpload;
35  import org.eclipse.aether.spi.connector.RepositoryConnector;
36  import org.eclipse.aether.transfer.ArtifactNotFoundException;
37  import org.eclipse.aether.transfer.ArtifactTransferException;
38  import org.eclipse.aether.transfer.MetadataNotFoundException;
39  import org.eclipse.aether.transfer.MetadataTransferException;
40  
41  /**
42   * @author Benjamin Bentmann
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              // the repository url contains unresolved properties and getting the basedir is not possible
55              // in JDK 20+ 'new URL(string)' will fail if the string contains a curly brace
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                      FileUtils.copyFile(remoteFile, download.getFile());
79                  } catch (IOException e) {
80                      if (!remoteFile.exists()) {
81                          download.setException(new ArtifactNotFoundException(download.getArtifact(), repository));
82                      } else {
83                          download.setException(new ArtifactTransferException(download.getArtifact(), repository, e));
84                      }
85                  }
86              }
87          }
88          if (metadataDownloads != null) {
89              for (final MetadataDownload download : metadataDownloads) {
90                  File remoteFile = new File(basedir, path(download.getMetadata()));
91                  try {
92                      FileUtils.copyFile(remoteFile, download.getFile());
93                  } catch (IOException e) {
94                      if (!remoteFile.exists()) {
95                          download.setException(new MetadataNotFoundException(download.getMetadata(), repository));
96                      } else {
97                          download.setException(new MetadataTransferException(download.getMetadata(), repository, e));
98                      }
99                  }
100             }
101         }
102     }
103 
104     private String path(Artifact artifact) {
105         StringBuilder path = new StringBuilder(128);
106 
107         path.append(artifact.getGroupId().replace('.', '/')).append('/');
108 
109         path.append(artifact.getArtifactId()).append('/');
110 
111         path.append(artifact.getBaseVersion()).append('/');
112 
113         path.append(artifact.getArtifactId()).append('-').append(artifact.getVersion());
114 
115         if (artifact.getClassifier().length() > 0) {
116             path.append('-').append(artifact.getClassifier());
117         }
118 
119         path.append('.').append(artifact.getExtension());
120 
121         return path.toString();
122     }
123 
124     private String path(Metadata metadata) {
125         StringBuilder path = new StringBuilder(128);
126 
127         path.append(metadata.getGroupId().replace('.', '/')).append('/');
128 
129         path.append(metadata.getArtifactId()).append('/');
130 
131         path.append("maven-metadata.xml");
132 
133         return path.toString();
134     }
135 
136     public void put(
137             Collection<? extends ArtifactUpload> artifactUploads,
138             Collection<? extends MetadataUpload> metadataUploads) {
139         // TODO Auto-generated method stub
140 
141     }
142 }