1   package org.apache.maven.repository;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.net.MalformedURLException;
25  import java.net.URL;
26  import java.util.Collection;
27  
28  import org.codehaus.plexus.util.FileUtils;
29  import org.sonatype.aether.artifact.Artifact;
30  import org.sonatype.aether.repository.RemoteRepository;
31  import org.sonatype.aether.spi.connector.ArtifactDownload;
32  import org.sonatype.aether.spi.connector.ArtifactUpload;
33  import org.sonatype.aether.spi.connector.MetadataDownload;
34  import org.sonatype.aether.spi.connector.MetadataUpload;
35  import org.sonatype.aether.spi.connector.RepositoryConnector;
36  import org.sonatype.aether.transfer.ArtifactTransferException;
37  
38  /**
39   * @author Benjamin Bentmann
40   */
41  public class TestRepositoryConnector
42      implements RepositoryConnector
43  {
44  
45      private RemoteRepository repository;
46  
47      private File basedir;
48  
49      public TestRepositoryConnector( RemoteRepository repository )
50      {
51          this.repository = repository;
52          try
53          {
54              basedir = FileUtils.toFile( new URL( repository.getUrl() ) );
55          }
56          catch ( MalformedURLException e )
57          {
58              throw new IllegalStateException( e );
59          }
60      }
61  
62      public void close()
63      {
64      }
65  
66      public void get( Collection<? extends ArtifactDownload> artifactDownloads,
67                       Collection<? extends MetadataDownload> metadataDownloads )
68      {
69          if ( artifactDownloads != null )
70          {
71              for ( ArtifactDownload download : artifactDownloads )
72              {
73                  File remoteFile = new File( basedir, path( download.getArtifact() ) );
74                  try
75                  {
76                      FileUtils.copyFile( remoteFile, download.getFile() );
77                  }
78                  catch ( IOException e )
79                  {
80                      download.setException( new ArtifactTransferException( download.getArtifact(), repository, e ) );
81                  }
82              }
83          }
84      }
85  
86      private String path( Artifact artifact )
87      {
88          StringBuilder path = new StringBuilder( 128 );
89  
90          path.append( artifact.getGroupId().replace( '.', '/' ) ).append( '/' );
91  
92          path.append( artifact.getArtifactId() ).append( '/' );
93  
94          path.append( artifact.getBaseVersion() ).append( '/' );
95  
96          path.append( artifact.getArtifactId() ).append( '-' ).append( artifact.getVersion() );
97  
98          if ( artifact.getClassifier().length() > 0 )
99          {
100             path.append( '-' ).append( artifact.getClassifier() );
101         }
102 
103         path.append( '.' ).append( artifact.getExtension() );
104 
105         return path.toString();
106     }
107 
108     public void put( Collection<? extends ArtifactUpload> artifactUploads,
109                      Collection<? extends MetadataUpload> metadataUploads )
110     {
111         // TODO Auto-generated method stub
112 
113     }
114 
115 }