View Javadoc

1   package org.apache.maven.repository.legacy;
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.util.ArrayList;
25  import java.util.Collection;
26  import java.util.HashMap;
27  import java.util.LinkedHashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.maven.RepositoryUtils;
32  import org.apache.maven.artifact.Artifact;
33  import org.apache.maven.artifact.InvalidRepositoryException;
34  import org.apache.maven.artifact.factory.ArtifactFactory;
35  import org.apache.maven.artifact.metadata.ArtifactMetadata;
36  import org.apache.maven.artifact.repository.ArtifactRepository;
37  import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
38  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
39  import org.apache.maven.artifact.repository.Authentication;
40  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
41  import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
42  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
43  import org.apache.maven.artifact.resolver.ArtifactResolver;
44  import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
45  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
46  import org.apache.maven.artifact.versioning.VersionRange;
47  import org.apache.maven.model.Dependency;
48  import org.apache.maven.model.Exclusion;
49  import org.apache.maven.model.Plugin;
50  import org.apache.maven.model.Repository;
51  import org.apache.maven.model.RepositoryPolicy;
52  import org.apache.maven.repository.DelegatingLocalArtifactRepository;
53  import org.apache.maven.repository.LocalArtifactRepository;
54  import org.apache.maven.repository.ArtifactTransferListener;
55  import org.apache.maven.repository.MirrorSelector;
56  import org.apache.maven.repository.Proxy;
57  import org.apache.maven.repository.RepositorySystem;
58  import org.apache.maven.repository.ArtifactDoesNotExistException;
59  import org.apache.maven.repository.ArtifactTransferFailedException;
60  import org.apache.maven.settings.Mirror;
61  import org.apache.maven.settings.Server;
62  import org.apache.maven.settings.building.SettingsProblem;
63  import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
64  import org.apache.maven.settings.crypto.SettingsDecrypter;
65  import org.apache.maven.settings.crypto.SettingsDecryptionRequest;
66  import org.apache.maven.settings.crypto.SettingsDecryptionResult;
67  import org.codehaus.plexus.PlexusContainer;
68  import org.codehaus.plexus.component.annotations.Component;
69  import org.codehaus.plexus.component.annotations.Requirement;
70  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
71  import org.codehaus.plexus.logging.Logger;
72  import org.codehaus.plexus.util.StringUtils;
73  import org.sonatype.aether.RepositorySystemSession;
74  import org.sonatype.aether.repository.AuthenticationSelector;
75  import org.sonatype.aether.repository.ProxySelector;
76  import org.sonatype.aether.repository.RemoteRepository;
77  
78  /**
79   * @author Jason van Zyl
80   */
81  @Component( role = RepositorySystem.class, hint = "default" )
82  public class LegacyRepositorySystem
83      implements RepositorySystem
84  {
85  
86      @Requirement
87      private Logger logger;
88  
89      @Requirement
90      private ArtifactFactory artifactFactory;
91  
92      @Requirement
93      private ArtifactResolver artifactResolver;
94  
95      @Requirement
96      private ArtifactRepositoryFactory artifactRepositoryFactory;
97  
98      @Requirement( role = ArtifactRepositoryLayout.class )
99      private Map<String, ArtifactRepositoryLayout> layouts;
100 
101     @Requirement
102     private WagonManager wagonManager;
103 
104     @Requirement
105     private PlexusContainer plexus;
106 
107     @Requirement
108     private MirrorSelector mirrorSelector;
109 
110     @Requirement
111     private SettingsDecrypter settingsDecrypter;
112 
113     public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type )
114     {
115         return artifactFactory.createArtifact( groupId, artifactId, version, scope, type );
116     }
117 
118     public Artifact createArtifact( String groupId, String artifactId, String version, String packaging )
119     {
120         return artifactFactory.createBuildArtifact( groupId, artifactId, version, packaging );
121     }
122 
123     public Artifact createArtifactWithClassifier( String groupId, String artifactId, String version, String type,
124                                                   String classifier )
125     {
126         return artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
127     }
128 
129     public Artifact createProjectArtifact( String groupId, String artifactId, String metaVersionId )
130     {
131         return artifactFactory.createProjectArtifact( groupId, artifactId, metaVersionId );
132     }
133 
134     public Artifact createDependencyArtifact( Dependency d )
135     {
136         VersionRange versionRange;
137         try
138         {
139             versionRange = VersionRange.createFromVersionSpec( d.getVersion() );
140         }
141         catch ( InvalidVersionSpecificationException e )
142         {
143             return null;
144         }
145 
146         Artifact artifact =
147             artifactFactory.createDependencyArtifact( d.getGroupId(), d.getArtifactId(), versionRange, d.getType(),
148                                                       d.getClassifier(), d.getScope(), d.isOptional() );
149 
150         if ( Artifact.SCOPE_SYSTEM.equals( d.getScope() ) && d.getSystemPath() != null )
151         {
152             artifact.setFile( new File( d.getSystemPath() ) );
153         }
154 
155         if ( !d.getExclusions().isEmpty() )
156         {
157             List<String> exclusions = new ArrayList<String>();
158 
159             for ( Exclusion exclusion : d.getExclusions() )
160             {
161                 exclusions.add( exclusion.getGroupId() + ':' + exclusion.getArtifactId() );
162             }
163 
164             artifact.setDependencyFilter( new ExcludesArtifactFilter( exclusions ) );
165         }
166 
167         return artifact;
168     }
169 
170     public Artifact createExtensionArtifact( String groupId, String artifactId, String version )
171     {
172         VersionRange versionRange;
173         try
174         {
175             versionRange = VersionRange.createFromVersionSpec( version );
176         }
177         catch ( InvalidVersionSpecificationException e )
178         {
179             return null;
180         }
181 
182         return artifactFactory.createExtensionArtifact( groupId, artifactId, versionRange );
183     }
184 
185     public Artifact createParentArtifact( String groupId, String artifactId, String version )
186     {
187         return artifactFactory.createParentArtifact( groupId, artifactId, version );
188     }
189 
190     public Artifact createPluginArtifact( Plugin plugin )
191     {
192         VersionRange versionRange;
193         try
194         {
195             String version = plugin.getVersion();
196             if ( StringUtils.isEmpty( version ) )
197             {
198                 version = "RELEASE";
199             }
200             versionRange = VersionRange.createFromVersionSpec( version );
201         }
202         catch ( InvalidVersionSpecificationException e )
203         {
204             return null;
205         }
206 
207         return artifactFactory.createPluginArtifact( plugin.getGroupId(), plugin.getArtifactId(), versionRange );
208     }
209 
210     public ArtifactRepositoryPolicy buildArtifactRepositoryPolicy( RepositoryPolicy policy )
211     {
212         boolean enabled = true;
213 
214         String updatePolicy = null;
215 
216         String checksumPolicy = null;
217 
218         if ( policy != null )
219         {
220             enabled = policy.isEnabled();
221 
222             if ( policy.getUpdatePolicy() != null )
223             {
224                 updatePolicy = policy.getUpdatePolicy();
225             }
226             if ( policy.getChecksumPolicy() != null )
227             {
228                 checksumPolicy = policy.getChecksumPolicy();
229             }
230         }
231 
232         return new ArtifactRepositoryPolicy( enabled, updatePolicy, checksumPolicy );
233     }
234 
235     public ArtifactRepository createDefaultLocalRepository()
236         throws InvalidRepositoryException
237     {
238         return createLocalRepository( RepositorySystem.defaultUserLocalRepository );
239     }
240 
241     public ArtifactRepository createLocalRepository( File localRepository )
242         throws InvalidRepositoryException
243     {
244         return createRepository( "file://" + localRepository.toURI().getRawPath(),
245                                  RepositorySystem.DEFAULT_LOCAL_REPO_ID, true,
246                                  ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, true,
247                                  ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
248                                  ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE );
249     }
250 
251     public ArtifactRepository createDefaultRemoteRepository()
252         throws InvalidRepositoryException
253     {
254         return createRepository( RepositorySystem.DEFAULT_REMOTE_REPO_URL, RepositorySystem.DEFAULT_REMOTE_REPO_ID,
255                                  true, ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY, false,
256                                  ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
257                                  ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );
258     }
259 
260     public ArtifactRepository createLocalRepository( String url, String repositoryId )
261         throws IOException
262     {
263         return createRepository( canonicalFileUrl( url ), repositoryId, true,
264                                  ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, true,
265                                  ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
266                                  ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE );
267     }
268 
269     private String canonicalFileUrl( String url )
270         throws IOException
271     {
272         if ( !url.startsWith( "file:" ) )
273         {
274             url = "file://" + url;
275         }
276         else if ( url.startsWith( "file:" ) && !url.startsWith( "file://" ) )
277         {
278             url = "file://" + url.substring( "file:".length() );
279         }
280 
281         // So now we have an url of the form file://<path>
282 
283         // We want to eliminate any relative path nonsense and lock down the path so we
284         // need to fully resolve it before any sub-modules use the path. This can happen
285         // when you are using a custom settings.xml that contains a relative path entry
286         // for the local repository setting.
287 
288         File localRepository = new File( url.substring( "file://".length() ) );
289 
290         if ( !localRepository.isAbsolute() )
291         {
292             url = "file://" + localRepository.getCanonicalPath();
293         }
294 
295         return url;
296     }
297 
298     public ArtifactResolutionResult resolve( ArtifactResolutionRequest request )
299     {
300         /*
301          * Probably is not worth it, but here I make sure I restore request
302          * to its original state.
303          */
304         try
305         {
306             LocalArtifactRepository ideWorkspace =
307                 plexus.lookup( LocalArtifactRepository.class, LocalArtifactRepository.IDE_WORKSPACE );
308 
309             if ( request.getLocalRepository() instanceof DelegatingLocalArtifactRepository )
310             {
311                 DelegatingLocalArtifactRepository delegatingLocalRepository =
312                     (DelegatingLocalArtifactRepository) request.getLocalRepository();
313 
314                 LocalArtifactRepository orig = delegatingLocalRepository.getIdeWorspace();
315 
316                 delegatingLocalRepository.setIdeWorkspace( ideWorkspace );
317 
318                 try
319                 {
320                     return artifactResolver.resolve( request );
321                 }
322                 finally
323                 {
324                     delegatingLocalRepository.setIdeWorkspace( orig );
325                 }
326             }
327             else
328             {
329                 ArtifactRepository localRepository = request.getLocalRepository();
330                 DelegatingLocalArtifactRepository delegatingLocalRepository =
331                     new DelegatingLocalArtifactRepository( localRepository );
332                 delegatingLocalRepository.setIdeWorkspace( ideWorkspace );
333                 request.setLocalRepository( delegatingLocalRepository );
334                 try
335                 {
336                     return artifactResolver.resolve( request );
337                 }
338                 finally
339                 {
340                     request.setLocalRepository( localRepository );
341                 }
342             }
343         }
344         catch ( ComponentLookupException e )
345         {
346             // no ide workspace artifact resolution
347         }
348 
349         return artifactResolver.resolve( request );
350     }
351 
352     /*
353     public void addProxy( String protocol, String host, int port, String username, String password, String nonProxyHosts )
354     {
355         ProxyInfo proxyInfo = new ProxyInfo();
356         proxyInfo.setHost( host );
357         proxyInfo.setType( protocol );
358         proxyInfo.setPort( port );
359         proxyInfo.setNonProxyHosts( nonProxyHosts );
360         proxyInfo.setUserName( username );
361         proxyInfo.setPassword( password );
362 
363         proxies.put( protocol, proxyInfo );
364 
365         wagonManager.addProxy( protocol, host, port, username, password, nonProxyHosts );
366     }
367     */
368 
369     public List<ArtifactRepository> getEffectiveRepositories( List<ArtifactRepository> repositories )
370     {
371         if ( repositories == null )
372         {
373             return null;
374         }
375 
376         Map<String, List<ArtifactRepository>> reposByKey = new LinkedHashMap<String, List<ArtifactRepository>>();
377 
378         for ( ArtifactRepository repository : repositories )
379         {
380             String key = repository.getId();
381 
382             List<ArtifactRepository> aliasedRepos = reposByKey.get( key );
383 
384             if ( aliasedRepos == null )
385             {
386                 aliasedRepos = new ArrayList<ArtifactRepository>();
387                 reposByKey.put( key, aliasedRepos );
388             }
389 
390             aliasedRepos.add( repository );
391         }
392 
393         List<ArtifactRepository> effectiveRepositories = new ArrayList<ArtifactRepository>();
394 
395         for ( List<ArtifactRepository> aliasedRepos : reposByKey.values() )
396         {
397             List<ArtifactRepositoryPolicy> releasePolicies =
398                 new ArrayList<ArtifactRepositoryPolicy>( aliasedRepos.size() );
399 
400             for ( ArtifactRepository aliasedRepo : aliasedRepos )
401             {
402                 releasePolicies.add( aliasedRepo.getReleases() );
403             }
404 
405             ArtifactRepositoryPolicy releasePolicy = getEffectivePolicy( releasePolicies );
406 
407             List<ArtifactRepositoryPolicy> snapshotPolicies =
408                 new ArrayList<ArtifactRepositoryPolicy>( aliasedRepos.size() );
409 
410             for ( ArtifactRepository aliasedRepo : aliasedRepos )
411             {
412                 snapshotPolicies.add( aliasedRepo.getSnapshots() );
413             }
414 
415             ArtifactRepositoryPolicy snapshotPolicy = getEffectivePolicy( snapshotPolicies );
416 
417             ArtifactRepository aliasedRepo = aliasedRepos.get( 0 );
418 
419             ArtifactRepository effectiveRepository =
420                 createArtifactRepository( aliasedRepo.getId(), aliasedRepo.getUrl(), aliasedRepo.getLayout(),
421                                           snapshotPolicy, releasePolicy );
422 
423             effectiveRepository.setAuthentication( aliasedRepo.getAuthentication() );
424 
425             effectiveRepository.setProxy( aliasedRepo.getProxy() );
426 
427             effectiveRepositories.add( effectiveRepository );
428         }
429 
430         return effectiveRepositories;
431     }
432 
433     private ArtifactRepositoryPolicy getEffectivePolicy( Collection<ArtifactRepositoryPolicy> policies )
434     {
435         ArtifactRepositoryPolicy effectivePolicy = null;
436 
437         for ( ArtifactRepositoryPolicy policy : policies )
438         {
439             if ( effectivePolicy == null )
440             {
441                 effectivePolicy = new ArtifactRepositoryPolicy( policy );
442             }
443             else
444             {
445                 effectivePolicy.merge( policy );
446             }
447         }
448 
449         return effectivePolicy;
450     }
451 
452     public Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
453     {
454         return mirrorSelector.getMirror( repository, mirrors );
455     }
456 
457     public void injectMirror( List<ArtifactRepository> repositories, List<Mirror> mirrors )
458     {
459         if ( repositories != null && mirrors != null )
460         {
461             for ( ArtifactRepository repository : repositories )
462             {
463                 Mirror mirror = getMirror( repository, mirrors );
464 
465                 if ( mirror != null )
466                 {
467                     repository.setId( mirror.getId() );
468                     repository.setUrl( mirror.getUrl() );
469 
470                     if ( StringUtils.isNotEmpty( mirror.getLayout() ) )
471                     {
472                         repository.setLayout( getLayout( mirror.getLayout() ) );
473                     }
474                 }
475             }
476         }
477     }
478 
479     private Mirror getMirror( RepositorySystemSession session, ArtifactRepository repository )
480     {
481         if ( session != null )
482         {
483             org.sonatype.aether.repository.MirrorSelector selector = session.getMirrorSelector();
484             if ( selector != null )
485             {
486                 RemoteRepository repo = selector.getMirror( RepositoryUtils.toRepo( repository ) );
487                 if ( repo != null )
488                 {
489                     Mirror mirror = new Mirror();
490                     mirror.setId( repo.getId() );
491                     mirror.setUrl( repo.getUrl() );
492                     mirror.setLayout( repo.getContentType() );
493                     return mirror;
494                 }
495             }
496         }
497         return null;
498     }
499 
500     public void injectMirror( RepositorySystemSession session, List<ArtifactRepository> repositories )
501     {
502         if ( repositories != null && session != null )
503         {
504             for ( ArtifactRepository repository : repositories )
505             {
506                 Mirror mirror = getMirror( session, repository );
507 
508                 if ( mirror != null )
509                 {
510                     repository.setId( mirror.getId() );
511                     repository.setUrl( mirror.getUrl() );
512 
513                     if ( StringUtils.isNotEmpty( mirror.getLayout() ) )
514                     {
515                         repository.setLayout( getLayout( mirror.getLayout() ) );
516                     }
517                 }
518             }
519         }
520     }
521 
522     public void injectAuthentication( List<ArtifactRepository> repositories, List<Server> servers )
523     {
524         if ( repositories != null )
525         {
526             Map<String, Server> serversById = new HashMap<String, Server>();
527 
528             if ( servers != null )
529             {
530                 for ( Server server : servers )
531                 {
532                     if ( !serversById.containsKey( server.getId() ) )
533                     {
534                         serversById.put( server.getId(), server );
535                     }
536                 }
537             }
538 
539             for ( ArtifactRepository repository : repositories )
540             {
541                 Server server = serversById.get( repository.getId() );
542 
543                 if ( server != null )
544                 {
545                     SettingsDecryptionRequest request = new DefaultSettingsDecryptionRequest( server );
546                     SettingsDecryptionResult result = settingsDecrypter.decrypt( request );
547                     server = result.getServer();
548 
549                     if ( logger.isDebugEnabled() )
550                     {
551                         for ( SettingsProblem problem : result.getProblems() )
552                         {
553                             logger.debug( problem.getMessage(), problem.getException() );
554                         }
555                     }
556 
557                     Authentication authentication = new Authentication( server.getUsername(), server.getPassword() );
558                     authentication.setPrivateKey( server.getPrivateKey() );
559                     authentication.setPassphrase( server.getPassphrase() );
560 
561                     repository.setAuthentication( authentication );
562                 }
563                 else
564                 {
565                     repository.setAuthentication( null );
566                 }
567             }
568         }
569     }
570 
571     private Authentication getAuthentication( RepositorySystemSession session, ArtifactRepository repository )
572     {
573         if ( session != null )
574         {
575             AuthenticationSelector selector = session.getAuthenticationSelector();
576             if ( selector != null )
577             {
578                 org.sonatype.aether.repository.Authentication auth =
579                     selector.getAuthentication( RepositoryUtils.toRepo( repository ) );
580                 if ( auth != null )
581                 {
582                     Authentication result = new Authentication( auth.getUsername(), auth.getPassword() );
583                     result.setPrivateKey( auth.getPrivateKeyFile() );
584                     result.setPassphrase( auth.getPassphrase() );
585                     return result;
586                 }
587             }
588         }
589         return null;
590     }
591 
592     public void injectAuthentication( RepositorySystemSession session, List<ArtifactRepository> repositories )
593     {
594         if ( repositories != null && session != null )
595         {
596             for ( ArtifactRepository repository : repositories )
597             {
598                 repository.setAuthentication( getAuthentication( session, repository ) );
599             }
600         }
601     }
602 
603     private org.apache.maven.settings.Proxy getProxy( ArtifactRepository repository,
604                                                       List<org.apache.maven.settings.Proxy> proxies )
605     {
606         if ( proxies != null && repository.getProtocol() != null )
607         {
608             for ( org.apache.maven.settings.Proxy proxy : proxies )
609             {
610                 if ( proxy.isActive() && repository.getProtocol().equalsIgnoreCase( proxy.getProtocol() ) )
611                 {
612                     return proxy;
613                 }
614             }
615         }
616 
617         return null;
618     }
619 
620     public void injectProxy( List<ArtifactRepository> repositories, List<org.apache.maven.settings.Proxy> proxies )
621     {
622         if ( repositories != null )
623         {
624             for ( ArtifactRepository repository : repositories )
625             {
626                 org.apache.maven.settings.Proxy proxy = getProxy( repository, proxies );
627 
628                 if ( proxy != null )
629                 {
630                     SettingsDecryptionRequest request = new DefaultSettingsDecryptionRequest( proxy );
631                     SettingsDecryptionResult result = settingsDecrypter.decrypt( request );
632                     proxy = result.getProxy();
633 
634                     if ( logger.isDebugEnabled() )
635                     {
636                         for ( SettingsProblem problem : result.getProblems() )
637                         {
638                             logger.debug( problem.getMessage(), problem.getException() );
639                         }
640                     }
641 
642                     Proxy p = new Proxy();
643                     p.setHost( proxy.getHost() );
644                     p.setProtocol( proxy.getProtocol() );
645                     p.setPort( proxy.getPort() );
646                     p.setNonProxyHosts( proxy.getNonProxyHosts() );
647                     p.setUserName( proxy.getUsername() );
648                     p.setPassword( proxy.getPassword() );
649 
650                     repository.setProxy( p );
651                 }
652                 else
653                 {
654                     repository.setProxy( null );
655                 }
656             }
657         }
658     }
659 
660     private Proxy getProxy( RepositorySystemSession session, ArtifactRepository repository )
661     {
662         if ( session != null )
663         {
664             ProxySelector selector = session.getProxySelector();
665             if ( selector != null )
666             {
667                 org.sonatype.aether.repository.Proxy proxy = selector.getProxy( RepositoryUtils.toRepo( repository ) );
668                 if ( proxy != null )
669                 {
670                     Proxy p = new Proxy();
671                     p.setHost( proxy.getHost() );
672                     p.setProtocol( proxy.getType() );
673                     p.setPort( proxy.getPort() );
674                     if ( proxy.getAuthentication() != null )
675                     {
676                         p.setUserName( proxy.getAuthentication().getUsername() );
677                         p.setPassword( proxy.getAuthentication().getPassword() );
678                     }
679                     return p;
680                 }
681             }
682         }
683         return null;
684     }
685 
686     public void injectProxy( RepositorySystemSession session, List<ArtifactRepository> repositories )
687     {
688         if ( repositories != null && session != null )
689         {
690             for ( ArtifactRepository repository : repositories )
691             {
692                 repository.setProxy( getProxy( session, repository ) );
693             }
694         }
695     }
696 
697     public void retrieve( ArtifactRepository repository, File destination, String remotePath,
698                           ArtifactTransferListener transferListener )
699         throws ArtifactTransferFailedException, ArtifactDoesNotExistException
700     {
701         try
702         {
703             wagonManager.getRemoteFile( repository, destination, remotePath,
704                                         TransferListenerAdapter.newAdapter( transferListener ),
705                                         ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN, true );
706         }
707         catch ( org.apache.maven.wagon.TransferFailedException e )
708         {
709             throw new ArtifactTransferFailedException( getMessage( e, "Error transferring artifact." ), e );
710         }
711         catch ( org.apache.maven.wagon.ResourceDoesNotExistException e )
712         {
713             throw new ArtifactDoesNotExistException( getMessage( e, "Requested artifact does not exist." ), e );
714         }
715     }
716 
717     public void publish( ArtifactRepository repository, File source, String remotePath,
718                          ArtifactTransferListener transferListener )
719         throws ArtifactTransferFailedException
720     {
721         try
722         {
723             wagonManager.putRemoteFile( repository, source, remotePath,
724                                         TransferListenerAdapter.newAdapter( transferListener ) );
725         }
726         catch ( org.apache.maven.wagon.TransferFailedException e )
727         {
728             throw new ArtifactTransferFailedException( getMessage( e, "Error transferring artifact." ), e );
729         }
730     }
731 
732     //
733     // Artifact Repository Creation
734     //
735     public ArtifactRepository buildArtifactRepository( Repository repo )
736         throws InvalidRepositoryException
737     {
738         if ( repo != null )
739         {
740             String id = repo.getId();
741 
742             if ( StringUtils.isEmpty( id ) )
743             {
744                 throw new InvalidRepositoryException( "Repository identifier missing", "" );
745             }
746 
747             String url = repo.getUrl();
748 
749             if ( StringUtils.isEmpty( url ) )
750             {
751                 throw new InvalidRepositoryException( "URL missing for repository " + id, id );
752             }
753 
754             ArtifactRepositoryPolicy snapshots = buildArtifactRepositoryPolicy( repo.getSnapshots() );
755 
756             ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy( repo.getReleases() );
757 
758             return createArtifactRepository( id, url, getLayout( repo.getLayout() ), snapshots, releases );
759         }
760         else
761         {
762             return null;
763         }
764     }
765 
766     private ArtifactRepository createRepository( String url, String repositoryId, boolean releases,
767                                                  String releaseUpdates, boolean snapshots, String snapshotUpdates,
768                                                  String checksumPolicy )
769     {
770         ArtifactRepositoryPolicy snapshotsPolicy =
771             new ArtifactRepositoryPolicy( snapshots, snapshotUpdates, checksumPolicy );
772 
773         ArtifactRepositoryPolicy releasesPolicy =
774             new ArtifactRepositoryPolicy( releases, releaseUpdates, checksumPolicy );
775 
776         return createArtifactRepository( repositoryId, url, null, snapshotsPolicy, releasesPolicy );
777     }
778 
779     public ArtifactRepository createArtifactRepository( String repositoryId, String url,
780                                                         ArtifactRepositoryLayout repositoryLayout,
781                                                         ArtifactRepositoryPolicy snapshots,
782                                                         ArtifactRepositoryPolicy releases )
783     {
784         if ( repositoryLayout == null )
785         {
786             repositoryLayout = layouts.get( "default" );
787         }
788 
789         ArtifactRepository artifactRepository =
790             artifactRepositoryFactory.createArtifactRepository( repositoryId, url, repositoryLayout, snapshots,
791                                                                 releases );
792 
793         return artifactRepository;
794     }
795 
796     private static String getMessage( Throwable error, String def )
797     {
798         if ( error == null )
799         {
800             return def;
801         }
802         String msg = error.getMessage();
803         if ( StringUtils.isNotEmpty( msg ) )
804         {
805             return msg;
806         }
807         return getMessage( error.getCause(), def );
808     }
809 
810     private ArtifactRepositoryLayout getLayout( String id )
811     {
812         ArtifactRepositoryLayout layout = layouts.get( id );
813 
814         if ( layout == null )
815         {
816             layout = new UnknownRepositoryLayout( id, layouts.get( "default" ) );
817         }
818 
819         return layout;
820     }
821 
822     /**
823      * In the future, the legacy system might encounter repository types for which no layout components exists because
824      * the actual communication with the repository happens via a repository connector. As a minimum, the legacy system
825      * needs to retain the id of this layout so that the content type of the remote repository can still be accurately
826      * described.
827      */
828     static class UnknownRepositoryLayout
829         implements ArtifactRepositoryLayout
830     {
831 
832         private final String id;
833 
834         private final ArtifactRepositoryLayout fallback;
835 
836         public UnknownRepositoryLayout( String id, ArtifactRepositoryLayout fallback )
837         {
838             this.id = id;
839             this.fallback = fallback;
840         }
841 
842         public String getId()
843         {
844             return id;
845         }
846 
847         public String pathOf( Artifact artifact )
848         {
849             return fallback.pathOf( artifact );
850         }
851 
852         public String pathOfLocalRepositoryMetadata( ArtifactMetadata metadata, ArtifactRepository repository )
853         {
854             return fallback.pathOfLocalRepositoryMetadata( metadata, repository );
855         }
856 
857         public String pathOfRemoteRepositoryMetadata( ArtifactMetadata metadata )
858         {
859             return fallback.pathOfRemoteRepositoryMetadata( metadata );
860         }
861 
862     }
863 
864 }