1 package org.apache.maven.artifact.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.util.Collections;
24 import java.util.List;
25
26 import org.apache.maven.RepositoryUtils;
27 import org.apache.maven.artifact.metadata.ArtifactMetadata;
28 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
29 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
30 import org.apache.maven.artifact.repository.metadata.RepositoryMetadataStoreException;
31 import org.apache.maven.repository.Proxy;
32 import org.sonatype.aether.RepositorySystem;
33 import org.sonatype.aether.RepositorySystemSession;
34 import org.sonatype.aether.artifact.Artifact;
35 import org.sonatype.aether.metadata.Metadata;
36 import org.sonatype.aether.repository.LocalArtifactRegistration;
37 import org.sonatype.aether.repository.LocalArtifactRequest;
38 import org.sonatype.aether.repository.LocalArtifactResult;
39 import org.sonatype.aether.repository.LocalMetadataRegistration;
40 import org.sonatype.aether.repository.LocalMetadataRequest;
41 import org.sonatype.aether.repository.LocalMetadataResult;
42 import org.sonatype.aether.repository.LocalRepository;
43 import org.sonatype.aether.repository.LocalRepositoryManager;
44 import org.sonatype.aether.repository.RemoteRepository;
45 import org.sonatype.aether.util.DefaultRepositorySystemSession;
46 import org.sonatype.aether.util.FilterRepositorySystemSession;
47
48
49
50
51
52
53
54 public class LegacyLocalRepositoryManager
55 implements LocalRepositoryManager
56 {
57
58 private final ArtifactRepository delegate;
59
60 private final LocalRepository repo;
61
62 private final boolean realLocalRepo;
63
64 public static RepositorySystemSession overlay( ArtifactRepository repository, RepositorySystemSession session,
65 RepositorySystem system )
66 {
67 if ( repository == null || repository.getBasedir() == null )
68 {
69 return session;
70 }
71
72 if ( session != null )
73 {
74 LocalRepositoryManager lrm = session.getLocalRepositoryManager();
75 if ( lrm != null && lrm.getRepository().getBasedir().equals( new File( repository.getBasedir() ) ) )
76 {
77 return session;
78 }
79 }
80 else
81 {
82 session = new DefaultRepositorySystemSession();
83 }
84
85 final LocalRepositoryManager llrm = new LegacyLocalRepositoryManager( repository );
86
87 return new FilterRepositorySystemSession( session )
88 {
89 @Override
90 public LocalRepositoryManager getLocalRepositoryManager()
91 {
92 return llrm;
93 }
94 };
95 }
96
97 private LegacyLocalRepositoryManager( ArtifactRepository delegate )
98 {
99 if ( delegate == null )
100 {
101 throw new IllegalArgumentException( "local repository delegate missing" );
102 }
103 this.delegate = delegate;
104
105 ArtifactRepositoryLayout layout = delegate.getLayout();
106 repo =
107 new LocalRepository( new File( delegate.getBasedir() ),
108 ( layout != null ) ? layout.getClass().getSimpleName() : "legacy" );
109
110
111
112
113
114
115
116
117
118
119
120
121
122 realLocalRepo = ( layout instanceof DefaultRepositoryLayout ) && "local".equals( delegate.getId() );
123 }
124
125 public LocalRepository getRepository()
126 {
127 return repo;
128 }
129
130 public String getPathForLocalArtifact( Artifact artifact )
131 {
132 if ( realLocalRepo )
133 {
134 return delegate.pathOf( RepositoryUtils.toArtifact( artifact.setVersion( artifact.getBaseVersion() ) ) );
135 }
136 return delegate.pathOf( RepositoryUtils.toArtifact( artifact ) );
137 }
138
139 public String getPathForRemoteArtifact( Artifact artifact, RemoteRepository repository, String context )
140 {
141 return delegate.pathOf( RepositoryUtils.toArtifact( artifact ) );
142 }
143
144 public String getPathForLocalMetadata( Metadata metadata )
145 {
146 return delegate.pathOfLocalRepositoryMetadata( new ArtifactMetadataAdapter( metadata ), delegate );
147 }
148
149 public String getPathForRemoteMetadata( Metadata metadata, RemoteRepository repository, String context )
150 {
151 return delegate.pathOfLocalRepositoryMetadata( new ArtifactMetadataAdapter( metadata ),
152 new ArtifactRepositoryAdapter( repository ) );
153 }
154
155 public LocalArtifactResult find( RepositorySystemSession session, LocalArtifactRequest request )
156 {
157 String path = getPathForLocalArtifact( request.getArtifact() );
158 File file = new File( getRepository().getBasedir(), path );
159
160 LocalArtifactResult result = new LocalArtifactResult( request );
161 if ( file.isFile() )
162 {
163 result.setFile( file );
164 result.setAvailable( true );
165 }
166
167 return result;
168 }
169
170 public LocalMetadataResult find( RepositorySystemSession session, LocalMetadataRequest request )
171 {
172 Metadata metadata = request.getMetadata();
173
174 String path;
175 if ( request.getRepository() == null )
176 {
177 path = getPathForLocalMetadata( metadata );
178 }
179 else
180 {
181 path = getPathForRemoteMetadata( metadata, request.getRepository(), request.getContext() );
182 }
183
184 File file = new File( getRepository().getBasedir(), path );
185
186 LocalMetadataResult result = new LocalMetadataResult( request );
187 if ( file.isFile() )
188 {
189 result.setFile( file );
190 }
191
192 return result;
193 }
194
195 public void add( RepositorySystemSession session, LocalArtifactRegistration request )
196 {
197
198 }
199
200 public void add( RepositorySystemSession session, LocalMetadataRegistration request )
201 {
202
203 }
204
205 static class ArtifactMetadataAdapter
206 implements ArtifactMetadata
207 {
208
209 private final Metadata metadata;
210
211 public ArtifactMetadataAdapter( Metadata metadata )
212 {
213 this.metadata = metadata;
214 }
215
216 public boolean storedInArtifactVersionDirectory()
217 {
218 return metadata.getVersion().length() > 0;
219 }
220
221 public boolean storedInGroupDirectory()
222 {
223 return metadata.getArtifactId().length() <= 0;
224 }
225
226 public String getGroupId()
227 {
228 return nullify( metadata.getGroupId() );
229 }
230
231 public String getArtifactId()
232 {
233 return nullify( metadata.getArtifactId() );
234 }
235
236 public String getBaseVersion()
237 {
238 return nullify( metadata.getVersion() );
239 }
240
241 private String nullify( String str )
242 {
243 return ( str == null || str.length() <= 0 ) ? null : str;
244 }
245
246 public Object getKey()
247 {
248 return metadata.toString();
249 }
250
251 public String getRemoteFilename()
252 {
253 return metadata.getType();
254 }
255
256 public String getLocalFilename( ArtifactRepository repository )
257 {
258 return insertRepositoryKey( getRemoteFilename(), repository.getKey() );
259 }
260
261 private String insertRepositoryKey( String filename, String repositoryKey )
262 {
263 String result;
264 int idx = filename.indexOf( '.' );
265 if ( idx < 0 )
266 {
267 result = filename + '-' + repositoryKey;
268 }
269 else
270 {
271 result = filename.substring( 0, idx ) + '-' + repositoryKey + filename.substring( idx );
272 }
273 return result;
274 }
275
276 public void merge( org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata )
277 {
278
279 }
280
281 public void merge( ArtifactMetadata metadata )
282 {
283
284 }
285
286 public void storeInLocalRepository( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
287 throws RepositoryMetadataStoreException
288 {
289
290 }
291
292 public String extendedToString()
293 {
294 return metadata.toString();
295 }
296
297 }
298
299 static class ArtifactRepositoryAdapter
300 implements ArtifactRepository
301 {
302
303 private final RemoteRepository repository;
304
305 public ArtifactRepositoryAdapter( RemoteRepository repository )
306 {
307 this.repository = repository;
308 }
309
310 public String pathOf( org.apache.maven.artifact.Artifact artifact )
311 {
312 return null;
313 }
314
315 public String pathOfRemoteRepositoryMetadata( ArtifactMetadata artifactMetadata )
316 {
317 return null;
318 }
319
320 public String pathOfLocalRepositoryMetadata( ArtifactMetadata metadata, ArtifactRepository repository )
321 {
322 return null;
323 }
324
325 public String getUrl()
326 {
327 return repository.getUrl();
328 }
329
330 public void setUrl( String url )
331 {
332 }
333
334 public String getBasedir()
335 {
336 return null;
337 }
338
339 public String getProtocol()
340 {
341 return repository.getProtocol();
342 }
343
344 public String getId()
345 {
346 return repository.getId();
347 }
348
349 public void setId( String id )
350 {
351 }
352
353 public ArtifactRepositoryPolicy getSnapshots()
354 {
355 return null;
356 }
357
358 public void setSnapshotUpdatePolicy( ArtifactRepositoryPolicy policy )
359 {
360 }
361
362 public ArtifactRepositoryPolicy getReleases()
363 {
364 return null;
365 }
366
367 public void setReleaseUpdatePolicy( ArtifactRepositoryPolicy policy )
368 {
369 }
370
371 public ArtifactRepositoryLayout getLayout()
372 {
373 return null;
374 }
375
376 public void setLayout( ArtifactRepositoryLayout layout )
377 {
378 }
379
380 public String getKey()
381 {
382 return getId();
383 }
384
385 public boolean isUniqueVersion()
386 {
387 return true;
388 }
389
390 public boolean isBlacklisted()
391 {
392 return false;
393 }
394
395 public void setBlacklisted( boolean blackListed )
396 {
397 }
398
399 public org.apache.maven.artifact.Artifact find( org.apache.maven.artifact.Artifact artifact )
400 {
401 return null;
402 }
403
404 public List<String> findVersions( org.apache.maven.artifact.Artifact artifact )
405 {
406 return Collections.emptyList();
407 }
408
409 public boolean isProjectAware()
410 {
411 return false;
412 }
413
414 public void setAuthentication( Authentication authentication )
415 {
416 }
417
418 public Authentication getAuthentication()
419 {
420 return null;
421 }
422
423 public void setProxy( Proxy proxy )
424 {
425 }
426
427 public Proxy getProxy()
428 {
429 return null;
430 }
431
432 public List<ArtifactRepository> getMirroredRepositories()
433 {
434 return Collections.emptyList();
435 }
436
437 public void setMirroredRepositories( List<ArtifactRepository> mirroredRepositories )
438 {
439 }
440
441 }
442
443 }