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          try {
53              basedir = FileUtils.toFile(new URL(repository.getUrl()));
54          } catch (MalformedURLException e) {
55              throw new IllegalStateException(e);
56          }
57      }
58  
59      public void close() {}
60  
61      public void get(
62              Collection<? extends ArtifactDownload> artifactDownloads,
63              Collection<? extends MetadataDownload> metadataDownloads) {
64          if (artifactDownloads != null) {
65              for (ArtifactDownload download : artifactDownloads) {
66                  File remoteFile = new File(basedir, path(download.getArtifact()));
67                  try {
68                      FileUtils.copyFile(remoteFile, download.getFile());
69                  } catch (IOException e) {
70                      if (!remoteFile.exists()) {
71                          download.setException(new ArtifactNotFoundException(download.getArtifact(), repository));
72                      } else {
73                          download.setException(new ArtifactTransferException(download.getArtifact(), repository, e));
74                      }
75                  }
76              }
77          }
78          if (metadataDownloads != null) {
79              for (final MetadataDownload download : metadataDownloads) {
80                  File remoteFile = new File(basedir, path(download.getMetadata()));
81                  try {
82                      FileUtils.copyFile(remoteFile, download.getFile());
83                  } catch (IOException e) {
84                      if (!remoteFile.exists()) {
85                          download.setException(new MetadataNotFoundException(download.getMetadata(), repository));
86                      } else {
87                          download.setException(new MetadataTransferException(download.getMetadata(), repository, e));
88                      }
89                  }
90              }
91          }
92      }
93  
94      private String path(Artifact artifact) {
95          StringBuilder path = new StringBuilder(128);
96  
97          path.append(artifact.getGroupId().replace('.', '/')).append('/');
98  
99          path.append(artifact.getArtifactId()).append('/');
100 
101         path.append(artifact.getBaseVersion()).append('/');
102 
103         path.append(artifact.getArtifactId()).append('-').append(artifact.getVersion());
104 
105         if (artifact.getClassifier().length() > 0) {
106             path.append('-').append(artifact.getClassifier());
107         }
108 
109         path.append('.').append(artifact.getExtension());
110 
111         return path.toString();
112     }
113 
114     private String path(Metadata metadata) {
115         StringBuilder path = new StringBuilder(128);
116 
117         path.append(metadata.getGroupId().replace('.', '/')).append('/');
118 
119         path.append(metadata.getArtifactId()).append('/');
120 
121         path.append("maven-metadata.xml");
122 
123         return path.toString();
124     }
125 
126     public void put(
127             Collection<? extends ArtifactUpload> artifactUploads,
128             Collection<? extends MetadataUpload> metadataUploads) {
129         // TODO Auto-generated method stub
130 
131     }
132 }