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                     authentication.setPrivateKey( server.getPrivateKey() );
554                     authentication.setPassphrase( server.getPassphrase() );
555 
556                     repository.setAuthentication( authentication );
557                 }
558                 else
559                 {
560                     repository.setAuthentication( null );
561                 }
562             }
563         }
564     }
565 
566     private Authentication getAuthentication( RepositorySystemSession session, ArtifactRepository repository )
567     {
568         if ( session != null )
569         {
570             AuthenticationSelector selector = session.getAuthenticationSelector();
571             if ( selector != null )
572             {
573                 org.sonatype.aether.repository.Authentication auth =
574                     selector.getAuthentication( RepositoryUtils.toRepo( repository ) );
575                 if ( auth != null )
576                 {
577                     Authentication result = new Authentication( auth.getUsername(), auth.getPassword() );
578                     result.setPrivateKey( auth.getPrivateKeyFile() );
579                     result.setPassphrase( auth.getPassphrase() );
580                     return result;
581                 }
582             }
583         }
584         return null;
585     }
586 
587     public void injectAuthentication( RepositorySystemSession session, List<ArtifactRepository> repositories )
588     {
589         if ( repositories != null && session != null )
590         {
591             for ( ArtifactRepository repository : repositories )
592             {
593                 repository.setAuthentication( getAuthentication( session, repository ) );
594             }
595         }
596     }
597 
598     private org.apache.maven.settings.Proxy getProxy( ArtifactRepository repository,
599                                                       List<org.apache.maven.settings.Proxy> proxies )
600     {
601         if ( proxies != null && repository.getProtocol() != null )
602         {
603             for ( org.apache.maven.settings.Proxy proxy : proxies )
604             {
605                 if ( proxy.isActive() && repository.getProtocol().equalsIgnoreCase( proxy.getProtocol() ) )
606                 {
607                     return proxy;
608                 }
609             }
610         }
611 
612         return null;
613     }
614 
615     public void injectProxy( List<ArtifactRepository> repositories, List<org.apache.maven.settings.Proxy> proxies )
616     {
617         if ( repositories != null )
618         {
619             for ( ArtifactRepository repository : repositories )
620             {
621                 org.apache.maven.settings.Proxy proxy = getProxy( repository, proxies );
622 
623                 if ( proxy != null )
624                 {
625                     SettingsDecryptionRequest request = new DefaultSettingsDecryptionRequest( proxy );
626                     SettingsDecryptionResult result = settingsDecrypter.decrypt( request );
627                     proxy = result.getProxy();
628 
629                     if ( logger.isDebugEnabled() )
630                     {
631                         for ( SettingsProblem problem : result.getProblems() )
632                         {
633                             logger.debug( problem.getMessage(), problem.getException() );
634                         }
635                     }
636 
637                     Proxy p = new Proxy();
638                     p.setHost( proxy.getHost() );
639                     p.setProtocol( proxy.getProtocol() );
640                     p.setPort( proxy.getPort() );
641                     p.setNonProxyHosts( proxy.getNonProxyHosts() );
642                     p.setUserName( proxy.getUsername() );
643                     p.setPassword( proxy.getPassword() );
644 
645                     repository.setProxy( p );
646                 }
647                 else
648                 {
649                     repository.setProxy( null );
650                 }
651             }
652         }
653     }
654 
655     private Proxy getProxy( RepositorySystemSession session, ArtifactRepository repository )
656     {
657         if ( session != null )
658         {
659             ProxySelector selector = session.getProxySelector();
660             if ( selector != null )
661             {
662                 org.sonatype.aether.repository.Proxy proxy = selector.getProxy( RepositoryUtils.toRepo( repository ) );
663                 if ( proxy != null )
664                 {
665                     Proxy p = new Proxy();
666                     p.setHost( proxy.getHost() );
667                     p.setProtocol( proxy.getType() );
668                     p.setPort( proxy.getPort() );
669                     if ( proxy.getAuthentication() != null )
670                     {
671                         p.setUserName( proxy.getAuthentication().getUsername() );
672                         p.setPassword( proxy.getAuthentication().getPassword() );
673                     }
674                     return p;
675                 }
676             }
677         }
678         return null;
679     }
680 
681     public void injectProxy( RepositorySystemSession session, List<ArtifactRepository> repositories )
682     {
683         if ( repositories != null && session != null )
684         {
685             for ( ArtifactRepository repository : repositories )
686             {
687                 repository.setProxy( getProxy( session, repository ) );
688             }
689         }
690     }
691 
692     public void retrieve( ArtifactRepository repository, File destination, String remotePath,
693                           ArtifactTransferListener transferListener )
694         throws ArtifactTransferFailedException, ArtifactDoesNotExistException
695     {
696         try
697         {
698             wagonManager.getRemoteFile( repository, destination, remotePath,
699                                         TransferListenerAdapter.newAdapter( transferListener ),
700                                         ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN, true );
701         }
702         catch ( org.apache.maven.wagon.TransferFailedException e )
703         {
704             throw new ArtifactTransferFailedException( getMessage( e, "Error transferring artifact." ), e );
705         }
706         catch ( org.apache.maven.wagon.ResourceDoesNotExistException e )
707         {
708             throw new ArtifactDoesNotExistException( getMessage( e, "Requested artifact does not exist." ), e );
709         }
710     }
711 
712     public void publish( ArtifactRepository repository, File source, String remotePath,
713                          ArtifactTransferListener transferListener )
714         throws ArtifactTransferFailedException
715     {
716         try
717         {
718             wagonManager.putRemoteFile( repository, source, remotePath,
719                                         TransferListenerAdapter.newAdapter( transferListener ) );
720         }
721         catch ( org.apache.maven.wagon.TransferFailedException e )
722         {
723             throw new ArtifactTransferFailedException( getMessage( e, "Error transferring artifact." ), e );
724         }
725     }
726 
727     //
728     // Artifact Repository Creation
729     //
730     public ArtifactRepository buildArtifactRepository( Repository repo )
731         throws InvalidRepositoryException
732     {
733         if ( repo != null )
734         {
735             String id = repo.getId();
736 
737             if ( StringUtils.isEmpty( id ) )
738             {
739                 throw new InvalidRepositoryException( "Repository identifier missing", "" );
740             }
741 
742             String url = repo.getUrl();
743 
744             if ( StringUtils.isEmpty( url ) )
745             {
746                 throw new InvalidRepositoryException( "URL missing for repository " + id, id );
747             }
748 
749             ArtifactRepositoryPolicy snapshots = buildArtifactRepositoryPolicy( repo.getSnapshots() );
750 
751             ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy( repo.getReleases() );
752 
753             return createArtifactRepository( id, url, getLayout( repo.getLayout() ), snapshots, releases );
754         }
755         else
756         {
757             return null;
758         }
759     }
760 
761     private ArtifactRepository createRepository( String url, String repositoryId, boolean releases,
762                                                  String releaseUpdates, boolean snapshots, String snapshotUpdates,
763                                                  String checksumPolicy )
764     {
765         ArtifactRepositoryPolicy snapshotsPolicy =
766             new ArtifactRepositoryPolicy( snapshots, snapshotUpdates, checksumPolicy );
767 
768         ArtifactRepositoryPolicy releasesPolicy =
769             new ArtifactRepositoryPolicy( releases, releaseUpdates, checksumPolicy );
770 
771         return createArtifactRepository( repositoryId, url, null, snapshotsPolicy, releasesPolicy );
772     }
773 
774     public ArtifactRepository createArtifactRepository( String repositoryId, String url,
775                                                         ArtifactRepositoryLayout repositoryLayout,
776                                                         ArtifactRepositoryPolicy snapshots,
777                                                         ArtifactRepositoryPolicy releases )
778     {
779         if ( repositoryLayout == null )
780         {
781             repositoryLayout = layouts.get( "default" );
782         }
783 
784         ArtifactRepository artifactRepository =
785             artifactRepositoryFactory.createArtifactRepository( repositoryId, url, repositoryLayout, snapshots,
786                                                                 releases );
787 
788         return artifactRepository;
789     }
790 
791     private static String getMessage( Throwable error, String def )
792     {
793         if ( error == null )
794         {
795             return def;
796         }
797         String msg = error.getMessage();
798         if ( StringUtils.isNotEmpty( msg ) )
799         {
800             return msg;
801         }
802         return getMessage( error.getCause(), def );
803     }
804 
805     private ArtifactRepositoryLayout getLayout( String id )
806     {
807         ArtifactRepositoryLayout layout = layouts.get( id );
808 
809         if ( layout == null )
810         {
811             layout = new UnknownRepositoryLayout( id, layouts.get( "default" ) );
812         }
813 
814         return layout;
815     }
816 
817     /**
818      * In the future, the legacy system might encounter repository types for which no layout components exists because
819      * the actual communication with the repository happens via a repository connector. As a minimum, the legacy system
820      * needs to retain the id of this layout so that the content type of the remote repository can still be accurately
821      * described.
822      */
823     static class UnknownRepositoryLayout
824         implements ArtifactRepositoryLayout
825     {
826 
827         private final String id;
828 
829         private final ArtifactRepositoryLayout fallback;
830 
831         public UnknownRepositoryLayout( String id, ArtifactRepositoryLayout fallback )
832         {
833             this.id = id;
834             this.fallback = fallback;
835         }
836 
837         public String getId()
838         {
839             return id;
840         }
841 
842         public String pathOf( Artifact artifact )
843         {
844             return fallback.pathOf( artifact );
845         }
846 
847         public String pathOfLocalRepositoryMetadata( ArtifactMetadata metadata, ArtifactRepository repository )
848         {
849             return fallback.pathOfLocalRepositoryMetadata( metadata, repository );
850         }
851 
852         public String pathOfRemoteRepositoryMetadata( ArtifactMetadata metadata )
853         {
854             return fallback.pathOfRemoteRepositoryMetadata( metadata );
855         }
856 
857     }
858 
859 }