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