View Javadoc
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.eclipse.aether.artifact.Artifact;
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  
39  /**
40   * @author Benjamin Bentmann
41   */
42  public class TestRepositoryConnector
43      implements RepositoryConnector
44  {
45  
46      private RemoteRepository repository;
47  
48      private File basedir;
49  
50      public TestRepositoryConnector( RemoteRepository repository )
51      {
52          this.repository = repository;
53          try
54          {
55              basedir = FileUtils.toFile( new URL( repository.getUrl() ) );
56          }
57          catch ( MalformedURLException e )
58          {
59              throw new IllegalStateException( e );
60          }
61      }
62  
63      public void close()
64      {
65      }
66  
67      public void get( Collection<? extends ArtifactDownload> artifactDownloads,
68                       Collection<? extends MetadataDownload> metadataDownloads )
69      {
70          if ( artifactDownloads != null )
71          {
72              for ( ArtifactDownload download : artifactDownloads )
73              {
74                  File remoteFile = new File( basedir, path( download.getArtifact() ) );
75                  try
76                  {
77                      FileUtils.copyFile( remoteFile, download.getFile() );
78                  }
79                  catch ( IOException e )
80                  {
81                      if ( !remoteFile.exists() )
82                      {
83                          download.setException( new ArtifactNotFoundException( download.getArtifact(), repository ) );
84                      }
85                      else
86                      {
87                          download.setException( new ArtifactTransferException( download.getArtifact(), repository, e ) );
88                      }
89                  }
90              }
91          }
92      }
93  
94      private String path( Artifact artifact )
95      {
96          StringBuilder path = new StringBuilder( 128 );
97  
98          path.append( artifact.getGroupId().replace( '.', '/' ) ).append( '/' );
99  
100         path.append( artifact.getArtifactId() ).append( '/' );
101 
102         path.append( artifact.getBaseVersion() ).append( '/' );
103 
104         path.append( artifact.getArtifactId() ).append( '-' ).append( artifact.getVersion() );
105 
106         if ( artifact.getClassifier().length() > 0 )
107         {
108             path.append( '-' ).append( artifact.getClassifier() );
109         }
110 
111         path.append( '.' ).append( artifact.getExtension() );
112 
113         return path.toString();
114     }
115 
116     public void put( Collection<? extends ArtifactUpload> artifactUploads,
117                      Collection<? extends MetadataUpload> metadataUploads )
118     {
119         // TODO Auto-generated method stub
120 
121     }
122 
123 }