1 package org.apache.maven.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.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
46 implements RepositoryConnector
47 {
48
49 private RemoteRepository repository;
50
51 private File basedir;
52
53 public TestRepositoryConnector( RemoteRepository repository )
54 {
55 this.repository = repository;
56 try
57 {
58 basedir = FileUtils.toFile( new URL( repository.getUrl() ) );
59 }
60 catch ( MalformedURLException e )
61 {
62 throw new IllegalStateException( e );
63 }
64 }
65
66 public void close()
67 {
68 }
69
70 public void get( Collection<? extends ArtifactDownload> artifactDownloads,
71 Collection<? extends MetadataDownload> metadataDownloads )
72 {
73 if ( artifactDownloads != null )
74 {
75 for ( ArtifactDownload download : artifactDownloads )
76 {
77 File remoteFile = new File( basedir, path( download.getArtifact() ) );
78 try
79 {
80 FileUtils.copyFile( remoteFile, download.getFile() );
81 }
82 catch ( IOException e )
83 {
84 if ( !remoteFile.exists() )
85 {
86 download.setException( new ArtifactNotFoundException( download.getArtifact(), repository ) );
87 }
88 else
89 {
90 download.setException( new ArtifactTransferException( download.getArtifact(), repository, e ) );
91 }
92 }
93 }
94 }
95 if ( metadataDownloads != null )
96 {
97 for ( final MetadataDownload download : metadataDownloads )
98 {
99 File remoteFile = new File( basedir, path( download.getMetadata() ) );
100 try
101 {
102 FileUtils.copyFile( remoteFile, download.getFile() );
103 }
104 catch ( IOException e )
105 {
106 if ( !remoteFile.exists() )
107 {
108 download.setException( new MetadataNotFoundException( download.getMetadata(), repository ) );
109 }
110 else
111 {
112 download.setException( new MetadataTransferException( download.getMetadata(), repository, e ) );
113 }
114 }
115 }
116 }
117 }
118
119 private String path( Artifact artifact )
120 {
121 StringBuilder path = new StringBuilder( 128 );
122
123 path.append( artifact.getGroupId().replace( '.', '/' ) ).append( '/' );
124
125 path.append( artifact.getArtifactId() ).append( '/' );
126
127 path.append( artifact.getBaseVersion() ).append( '/' );
128
129 path.append( artifact.getArtifactId() ).append( '-' ).append( artifact.getVersion() );
130
131 if ( artifact.getClassifier().length() > 0 )
132 {
133 path.append( '-' ).append( artifact.getClassifier() );
134 }
135
136 path.append( '.' ).append( artifact.getExtension() );
137
138 return path.toString();
139 }
140
141 private String path( Metadata metadata )
142 {
143 StringBuilder path = new StringBuilder( 128 );
144
145 path.append( metadata.getGroupId().replace( '.', '/' ) ).append( '/' );
146
147 path.append( metadata.getArtifactId() ).append( '/' );
148
149 path.append( "maven-metadata.xml" );
150
151 return path.toString();
152 }
153
154 public void put( Collection<? extends ArtifactUpload> artifactUploads,
155 Collection<? extends MetadataUpload> metadataUploads )
156 {
157
158
159 }
160
161 }