View Javadoc
1   package org.apache.maven.plugins.site.deploy;
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 org.apache.maven.artifact.manager.WagonManager;
23  import org.apache.maven.doxia.site.decoration.inheritance.URIPathDescriptor;
24  import org.apache.maven.execution.MavenExecutionRequest;
25  import org.apache.maven.execution.MavenSession;
26  import org.apache.maven.model.DistributionManagement;
27  import org.apache.maven.model.Site;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.logging.Log;
30  import org.apache.maven.plugins.annotations.Component;
31  import org.apache.maven.plugins.annotations.Parameter;
32  import org.apache.maven.plugins.site.AbstractSiteMojo;
33  import org.apache.maven.plugins.site.deploy.wagon.BugFixedRepository;
34  import org.apache.maven.project.MavenProject;
35  import org.apache.maven.settings.Proxy;
36  import org.apache.maven.settings.Server;
37  import org.apache.maven.settings.Settings;
38  import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
39  import org.apache.maven.settings.crypto.SettingsDecrypter;
40  import org.apache.maven.settings.crypto.SettingsDecryptionResult;
41  import org.apache.maven.wagon.CommandExecutionException;
42  import org.apache.maven.wagon.CommandExecutor;
43  import org.apache.maven.wagon.ConnectionException;
44  import org.apache.maven.wagon.ResourceDoesNotExistException;
45  import org.apache.maven.wagon.TransferFailedException;
46  import org.apache.maven.wagon.UnsupportedProtocolException;
47  import org.apache.maven.wagon.Wagon;
48  import org.apache.maven.wagon.authentication.AuthenticationException;
49  import org.apache.maven.wagon.authentication.AuthenticationInfo;
50  import org.apache.maven.wagon.authorization.AuthorizationException;
51  import org.apache.maven.wagon.observers.Debug;
52  import org.apache.maven.wagon.proxy.ProxyInfo;
53  import org.apache.maven.wagon.repository.Repository;
54  import org.codehaus.classworlds.ClassRealm;
55  import org.codehaus.plexus.PlexusConstants;
56  import org.codehaus.plexus.PlexusContainer;
57  import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
58  import org.codehaus.plexus.component.configurator.ComponentConfigurator;
59  import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
60  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
61  import org.codehaus.plexus.configuration.PlexusConfiguration;
62  import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
63  import org.codehaus.plexus.context.Context;
64  import org.codehaus.plexus.context.ContextException;
65  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
66  import org.codehaus.plexus.util.StringUtils;
67  import org.codehaus.plexus.util.xml.Xpp3Dom;
68  
69  import java.io.File;
70  import java.lang.reflect.Method;
71  import java.net.MalformedURLException;
72  import java.net.URL;
73  import java.util.List;
74  import java.util.Locale;
75  import java.util.Set;
76  
77  /**
78   * Abstract base class for deploy mojos.
79   * Since 2.3 this includes {@link SiteStageMojo} and {@link SiteStageDeployMojo}.
80   *
81   * @author ltheussl
82   * @since 2.3
83   */
84  public abstract class AbstractDeployMojo
85      extends AbstractSiteMojo
86      implements Contextualizable
87  {
88      /**
89       * Directory containing the generated project sites and report distributions.
90       *
91       * @since 2.3
92       */
93      @Parameter( alias = "outputDirectory", defaultValue = "${project.reporting.outputDirectory}", required = true )
94      private File inputDirectory;
95  
96      /**
97       * Whether to run the "chmod" command on the remote site after the deploy.
98       * Defaults to "true".
99       *
100      * @since 2.1
101      */
102     @Parameter( property = "maven.site.chmod", defaultValue = "true" )
103     private boolean chmod;
104 
105     /**
106      * The mode used by the "chmod" command. Only used if chmod = true.
107      * Defaults to "g+w,a+rX".
108      *
109      * @since 2.1
110      */
111     @Parameter( property = "maven.site.chmod.mode", defaultValue = "g+w,a+rX" )
112     private String chmodMode;
113 
114     /**
115      * The options used by the "chmod" command. Only used if chmod = true.
116      * Defaults to "-Rf".
117      *
118      * @since 2.1
119      */
120     @Parameter( property = "maven.site.chmod.options", defaultValue = "-Rf" )
121     private String chmodOptions;
122 
123     /**
124      * Set this to 'true' to skip site deployment.
125      *
126      * @since 3.0
127      */
128     @Parameter( property = "maven.site.deploy.skip", defaultValue = "false" )
129     private boolean skipDeploy;
130 
131     /**
132      */
133     @Component
134     private WagonManager wagonManager;
135 
136     /**
137      * The current user system settings for use in Maven.
138      */
139     @Parameter( defaultValue = "${settings}", readonly = true )
140     private Settings settings;
141 
142     /**
143      * @since 3.0-beta-2
144      */
145     @Parameter( defaultValue = "${session}", readonly = true )
146     protected MavenSession mavenSession;
147 
148     private String topDistributionManagementSiteUrl;
149 
150     private Site deploySite;
151 
152     private PlexusContainer container;
153 
154     /**
155      * {@inheritDoc}
156      */
157     public void execute()
158         throws MojoExecutionException
159     {
160         if ( skip && isDeploy() )
161         {
162             getLog().info( "maven.site.skip = true: Skipping site deployment" );
163             return;
164         }
165 
166         if ( skipDeploy && isDeploy() )
167         {
168             getLog().info( "maven.site.deploy.skip = true: Skipping site deployment" );
169             return;
170         }
171 
172         deployTo( new BugFixedRepository( getDeploySite().getId(), appendSlash( getDeploySite().getUrl() ) ) );
173     }
174 
175     /**
176      * Make sure the given url ends with a slash.
177      *
178      * @param url a String.
179      * @return if url already ends with '/' it is returned unchanged,
180      *         otherwise a '/' character is appended.
181      */
182     protected static String appendSlash( final String url )
183     {
184         if ( url.endsWith( "/" ) )
185         {
186             return url;
187         }
188         else
189         {
190             return url + "/";
191         }
192     }
193 
194 
195     /**
196      * Detect if the mojo is staging or deploying.
197      *
198      * @return <code>true</code> if the mojo is for deploy and not staging (local or deploy)
199      */
200     protected abstract boolean isDeploy();
201 
202     /**
203      * Get the top distribution management site url, used for module relative path calculations.
204      * This should be a top-level URL, ie above modules and locale sub-directories. Each deploy mojo
205      * can tweak algorithm to determine this top site by implementing determineTopDistributionManagementSiteUrl().
206      *
207      * @return the site for deployment
208      * @throws MojoExecutionException
209      * @see #determineTopDistributionManagementSiteUrl()
210      */
211     protected String getTopDistributionManagementSiteUrl()
212         throws MojoExecutionException
213     {
214         if ( topDistributionManagementSiteUrl == null )
215         {
216             topDistributionManagementSiteUrl = determineTopDistributionManagementSiteUrl();
217 
218             if ( !isDeploy() )
219             {
220                 getLog().debug( "distributionManagement.site.url relative path: " + getDeployModuleDirectory() );
221             }
222         }
223         return topDistributionManagementSiteUrl;
224     }
225 
226     protected abstract String determineTopDistributionManagementSiteUrl()
227         throws MojoExecutionException;
228 
229     /**
230      * Get the site used for deployment, with its id to look up credential settings and the target URL for the deploy.
231      * This should be a top-level URL, ie above modules and locale sub-directories. Each deploy mojo
232      * can tweak algorithm to determine this deploy site by implementing determineDeploySite().
233      *
234      * @return the site for deployment
235      * @throws MojoExecutionException
236      * @see #determineDeploySite()
237      */
238     protected Site getDeploySite()
239         throws MojoExecutionException
240     {
241         if ( deploySite == null )
242         {
243             deploySite = determineDeploySite();
244         }
245         return deploySite;
246     }
247 
248     protected abstract Site determineDeploySite()
249         throws MojoExecutionException;
250 
251     /**
252      * Find the relative path between the distribution URLs of the top site and the current project.
253      *
254      * @return the relative path or "./" if the two URLs are the same.
255      * @throws MojoExecutionException
256      */
257     protected String getDeployModuleDirectory()
258         throws MojoExecutionException
259     {
260         String relative = siteTool.getRelativePath( getSite( project ).getUrl(),
261                                                     getTopDistributionManagementSiteUrl() );
262 
263         // SiteTool.getRelativePath() uses File.separatorChar,
264         // so we need to convert '\' to '/' in order for the URL to be valid for Windows users
265         relative = relative.replace( '\\', '/' );
266 
267         return ( "".equals( relative ) ) ? "./" : relative;
268     }
269 
270     /**
271      * Use wagon to deploy the generated site to a given repository.
272      *
273      * @param repository the repository to deploy to.
274      *                   This needs to contain a valid, non-null {@link Repository#getId() id}
275      *                   to look up credentials for the deploy, and a valid, non-null
276      *                   {@link Repository#getUrl() scm url} to deploy to.
277      * @throws MojoExecutionException if the deploy fails.
278      */
279     private void deployTo( final Repository repository )
280         throws MojoExecutionException
281     {
282         if ( !inputDirectory.exists() )
283         {
284             throw new MojoExecutionException( "The site does not exist, please run site:site first" );
285         }
286 
287         if ( getLog().isDebugEnabled() )
288         {
289             getLog().debug( "Deploying to '" + repository.getUrl() + "',\n    Using credentials from server id '"
290                                 + repository.getId() + "'" );
291         }
292 
293         deploy( inputDirectory, repository );
294     }
295 
296     private void deploy( final File directory, final Repository repository )
297         throws MojoExecutionException
298     {
299         // TODO: work on moving this into the deployer like the other deploy methods
300         final Wagon wagon = getWagon( repository, wagonManager );
301 
302         try
303         {
304             configureWagon( wagon, repository.getId(), settings, container, getLog() );
305         }
306         catch ( TransferFailedException e )
307         {
308             throw new MojoExecutionException( "Unable to configure Wagon: '" + repository.getProtocol() + "'", e );
309         }
310 
311         try
312         {
313             final ProxyInfo proxyInfo;
314             if ( !isMaven3OrMore() )
315             {
316                 proxyInfo = getProxyInfo( repository, wagonManager );
317             }
318             else
319             {
320                 try
321                 {
322                     SettingsDecrypter settingsDecrypter = container.lookup( SettingsDecrypter.class );
323 
324                     proxyInfo = getProxy( repository, settingsDecrypter );
325                 }
326                 catch ( ComponentLookupException cle )
327                 {
328                     throw new MojoExecutionException( "Unable to lookup SettingsDecrypter: " + cle.getMessage(), cle );
329                 }
330             }
331 
332             push( directory, repository, wagon, proxyInfo, getLocales(), getDeployModuleDirectory() );
333 
334             if ( chmod )
335             {
336                 chmod( wagon, repository, chmodOptions, chmodMode );
337             }
338         }
339         finally
340         {
341             try
342             {
343                 wagon.disconnect();
344             }
345             catch ( ConnectionException e )
346             {
347                 getLog().error( "Error disconnecting wagon - ignored", e );
348             }
349         }
350     }
351 
352     private Wagon getWagon( final Repository repository, final WagonManager manager )
353         throws MojoExecutionException
354     {
355         final Wagon wagon;
356 
357         try
358         {
359             wagon = manager.getWagon( repository );
360         }
361         catch ( UnsupportedProtocolException e )
362         {
363             String shortMessage = "Unsupported protocol: '" + repository.getProtocol() + "' for site deployment to "
364                 + "distributionManagement.site.url=" + repository.getUrl() + ".";
365             String longMessage =
366                 "\n" + shortMessage + "\n" + "Currently supported protocols are: " + getSupportedProtocols() + ".\n"
367                     + "    Protocols may be added through wagon providers.\n" + "    For more information, see "
368                     + "http://maven.apache.org/plugins/maven-site-plugin/examples/adding-deploy-protocol.html";
369 
370             getLog().error( longMessage );
371 
372             throw new MojoExecutionException( shortMessage );
373         }
374         catch ( TransferFailedException e )
375         {
376             throw new MojoExecutionException( "Unable to configure Wagon: '" + repository.getProtocol() + "'", e );
377         }
378 
379         if ( !wagon.supportsDirectoryCopy() )
380         {
381             throw new MojoExecutionException(
382                 "Wagon protocol '" + repository.getProtocol() + "' doesn't support directory copying" );
383         }
384 
385         return wagon;
386     }
387 
388     private String getSupportedProtocols()
389     {
390         try
391         {
392             Set<String> protocols = container.lookupMap( Wagon.class ).keySet();
393 
394             return StringUtils.join( protocols.iterator(), ", " );
395         }
396         catch ( ComponentLookupException e )
397         {
398             // in the unexpected case there is a problem when instantiating a wagon provider
399             getLog().error( e );
400         }
401         return "";
402     }
403 
404     private void push( final File inputDirectory, final Repository repository, final Wagon wagon,
405                        final ProxyInfo proxyInfo, final List<Locale> localesList, final String relativeDir )
406         throws MojoExecutionException
407     {
408         AuthenticationInfo authenticationInfo = wagonManager.getAuthenticationInfo( repository.getId() );
409         getLog().debug( "authenticationInfo with id '" + repository.getId() + "': "
410                             + ( ( authenticationInfo == null ) ? "-" : authenticationInfo.getUserName() ) );
411 
412         try
413         {
414             Debug debug = new Debug();
415 
416             wagon.addSessionListener( debug );
417 
418             wagon.addTransferListener( debug );
419 
420             if ( proxyInfo != null )
421             {
422                 getLog().debug( "connect with proxyInfo" );
423                 wagon.connect( repository, authenticationInfo, proxyInfo );
424             }
425             else if ( proxyInfo == null && authenticationInfo != null )
426             {
427                 getLog().debug( "connect with authenticationInfo and without proxyInfo" );
428                 wagon.connect( repository, authenticationInfo );
429             }
430             else
431             {
432                 getLog().debug( "connect without authenticationInfo and without proxyInfo" );
433                 wagon.connect( repository );
434             }
435 
436             getLog().info( "Pushing " + inputDirectory );
437 
438             // Default is first in the list
439             final String defaultLocale = localesList.get( 0 ).getLanguage();
440 
441             for ( Locale locale : localesList )
442             {
443                 if ( locale.getLanguage().equals( defaultLocale ) )
444                 {
445                     // TODO: this also uploads the non-default locales,
446                     // is there a way to exclude directories in wagon?
447                     getLog().info( "   >>> to " + repository.getUrl() + relativeDir );
448 
449                     wagon.putDirectory( inputDirectory, relativeDir );
450                 }
451                 else
452                 {
453                     getLog().info( "   >>> to " + repository.getUrl() + locale.getLanguage() + "/" + relativeDir );
454 
455                     wagon.putDirectory( new File( inputDirectory, locale.getLanguage() ),
456                                         locale.getLanguage() + "/" + relativeDir );
457                 }
458             }
459         }
460         catch ( ResourceDoesNotExistException e )
461         {
462             throw new MojoExecutionException( "Error uploading site", e );
463         }
464         catch ( TransferFailedException e )
465         {
466             throw new MojoExecutionException( "Error uploading site", e );
467         }
468         catch ( AuthorizationException e )
469         {
470             throw new MojoExecutionException( "Error uploading site", e );
471         }
472         catch ( ConnectionException e )
473         {
474             throw new MojoExecutionException( "Error uploading site", e );
475         }
476         catch ( AuthenticationException e )
477         {
478             throw new MojoExecutionException( "Error uploading site", e );
479         }
480     }
481 
482     private static void chmod( final Wagon wagon, final Repository repository, final String chmodOptions,
483                                final String chmodMode )
484         throws MojoExecutionException
485     {
486         try
487         {
488             if ( wagon instanceof CommandExecutor )
489             {
490                 CommandExecutor exec = (CommandExecutor) wagon;
491                 exec.executeCommand( "chmod " + chmodOptions + " " + chmodMode + " " + repository.getBasedir() );
492             }
493             // else ? silently ignore, FileWagon is not a CommandExecutor!
494         }
495         catch ( CommandExecutionException e )
496         {
497             throw new MojoExecutionException( "Error uploading site", e );
498         }
499     }
500 
501     /**
502      * <p>
503      * Get the <code>ProxyInfo</code> of the proxy associated with the <code>host</code>
504      * and the <code>protocol</code> of the given <code>repository</code>.
505      * </p>
506      * <p>
507      * Extract from <a href="http://java.sun.com/j2se/1.5.0/docs/guide/net/properties.html">
508      * J2SE Doc : Networking Properties - nonProxyHosts</a> : "The value can be a list of hosts,
509      * each separated by a |, and in addition a wildcard character (*) can be used for matching"
510      * </p>
511      * <p>
512      * Defensively support for comma (",") and semi colon (";") in addition to pipe ("|") as separator.
513      * </p>
514      *
515      * @param repository   the Repository to extract the ProxyInfo from.
516      * @param wagonManager the WagonManager used to connect to the Repository.
517      * @return a ProxyInfo object instantiated or <code>null</code> if no matching proxy is found
518      */
519     public static ProxyInfo getProxyInfo( Repository repository, WagonManager wagonManager )
520     {
521         ProxyInfo proxyInfo = wagonManager.getProxy( repository.getProtocol() );
522 
523         if ( proxyInfo == null )
524         {
525             return null;
526         }
527 
528         String host = repository.getHost();
529         String nonProxyHostsAsString = proxyInfo.getNonProxyHosts();
530         for ( String nonProxyHost : StringUtils.split( nonProxyHostsAsString, ",;|" ) )
531         {
532             if ( StringUtils.contains( nonProxyHost, "*" ) )
533             {
534                 // Handle wildcard at the end, beginning or middle of the nonProxyHost
535                 final int pos = nonProxyHost.indexOf( '*' );
536                 String nonProxyHostPrefix = nonProxyHost.substring( 0, pos );
537                 String nonProxyHostSuffix = nonProxyHost.substring( pos + 1 );
538                 // prefix*
539                 if ( StringUtils.isNotEmpty( nonProxyHostPrefix ) && host.startsWith( nonProxyHostPrefix )
540                     && StringUtils.isEmpty( nonProxyHostSuffix ) )
541                 {
542                     return null;
543                 }
544                 // *suffix
545                 if ( StringUtils.isEmpty( nonProxyHostPrefix ) && StringUtils.isNotEmpty( nonProxyHostSuffix )
546                     && host.endsWith( nonProxyHostSuffix ) )
547                 {
548                     return null;
549                 }
550                 // prefix*suffix
551                 if ( StringUtils.isNotEmpty( nonProxyHostPrefix ) && host.startsWith( nonProxyHostPrefix )
552                     && StringUtils.isNotEmpty( nonProxyHostSuffix ) && host.endsWith( nonProxyHostSuffix ) )
553                 {
554                     return null;
555                 }
556             }
557             else if ( host.equals( nonProxyHost ) )
558             {
559                 return null;
560             }
561         }
562         return proxyInfo;
563     }
564 
565     /**
566      * Get proxy information for Maven 3.
567      *
568      * @param repository
569      * @param settingsDecrypter
570      * @return
571      */
572     private ProxyInfo getProxy( Repository repository, SettingsDecrypter settingsDecrypter )
573     {
574         String protocol = repository.getProtocol();
575         String url = repository.getUrl();
576 
577         getLog().debug( "repository protocol " + protocol );
578 
579         String originalProtocol = protocol;
580         // olamy: hackish here protocol (wagon hint in fact !) is dav
581         // but the real protocol (transport layer) is http(s)
582         // and it's the one use in wagon to find the proxy arghhh
583         // so we will check both
584         if ( StringUtils.equalsIgnoreCase( "dav", protocol ) && url.startsWith( "dav:" ) )
585         {
586             url = url.substring( 4 );
587             if ( url.startsWith( "http" ) )
588             {
589                 try
590                 {
591                     URL urlSite = new URL( url );
592                     protocol = urlSite.getProtocol();
593                     getLog().debug( "found dav protocol so transform to real transport protocol " + protocol );
594                 }
595                 catch ( MalformedURLException e )
596                 {
597                     getLog().warn( "fail to build URL with " + url );
598                 }
599 
600             }
601         }
602         else
603         {
604             getLog().debug( "getProxy 'protocol': " + protocol );
605         }
606         if ( mavenSession != null && protocol != null )
607         {
608             MavenExecutionRequest request = mavenSession.getRequest();
609 
610             if ( request != null )
611             {
612                 List<Proxy> proxies = request.getProxies();
613 
614                 if ( proxies != null )
615                 {
616                     for ( Proxy proxy : proxies )
617                     {
618                         if ( proxy.isActive() && ( protocol.equalsIgnoreCase( proxy.getProtocol() )
619                             || originalProtocol.equalsIgnoreCase( proxy.getProtocol() ) ) )
620                         {
621                             SettingsDecryptionResult result =
622                                 settingsDecrypter.decrypt( new DefaultSettingsDecryptionRequest( proxy ) );
623                             proxy = result.getProxy();
624 
625                             ProxyInfo proxyInfo = new ProxyInfo();
626                             proxyInfo.setHost( proxy.getHost() );
627                             // so hackish for wagon the protocol is https for site dav:
628                             // dav:https://dav.codehaus.org/mojo/
629                             proxyInfo.setType( protocol ); //proxy.getProtocol() );
630                             proxyInfo.setPort( proxy.getPort() );
631                             proxyInfo.setNonProxyHosts( proxy.getNonProxyHosts() );
632                             proxyInfo.setUserName( proxy.getUsername() );
633                             proxyInfo.setPassword( proxy.getPassword() );
634 
635                             getLog().debug( "found proxyInfo "
636                                                 + ( "host:port " + proxyInfo.getHost() + ":" + proxyInfo.getPort()
637                                                     + ", " + proxyInfo.getUserName() ) );
638 
639                             return proxyInfo;
640                         }
641                     }
642                 }
643             }
644         }
645         getLog().debug( "getProxy 'protocol': " + protocol + " no ProxyInfo found" );
646         return null;
647     }
648 
649     /**
650      * Configure the Wagon with the information from serverConfigurationMap ( which comes from settings.xml )
651      *
652      * @param wagon
653      * @param repositoryId
654      * @param settings
655      * @param container
656      * @param log
657      * @throws TransferFailedException
658      * @todo Remove when {@link WagonManager#getWagon(Repository) is available}. It's available in Maven 2.0.5.
659      */
660     private static void configureWagon( Wagon wagon, String repositoryId, Settings settings, PlexusContainer container,
661                                         Log log )
662         throws TransferFailedException
663     {
664         log.debug( " configureWagon " );
665 
666         // MSITE-25: Make sure that the server settings are inserted
667         for ( Server server : settings.getServers() )
668         {
669             String id = server.getId();
670 
671             log.debug( "configureWagon server " + id );
672 
673             if ( id != null && id.equals( repositoryId ) && ( server.getConfiguration() != null ) )
674             {
675                 final PlexusConfiguration plexusConf =
676                     new XmlPlexusConfiguration( (Xpp3Dom) server.getConfiguration() );
677 
678                 ComponentConfigurator componentConfigurator = null;
679                 try
680                 {
681                     componentConfigurator =
682                         (ComponentConfigurator) container.lookup( ComponentConfigurator.ROLE, "basic" );
683                     if ( isMaven3OrMore() )
684                     {
685                         componentConfigurator.configureComponent( wagon, plexusConf,
686                                                                   container.getContainerRealm() );
687                     }
688                     else
689                     {
690                         configureWagonWithMaven2( componentConfigurator, wagon, plexusConf, container );
691                     }
692                 }
693                 catch ( final ComponentLookupException e )
694                 {
695                     throw new TransferFailedException(
696                         "While configuring wagon for \'" + repositoryId + "\': Unable to lookup wagon configurator."
697                             + " Wagon configuration cannot be applied.", e );
698                 }
699                 catch ( ComponentConfigurationException e )
700                 {
701                     throw new TransferFailedException( "While configuring wagon for \'" + repositoryId
702                                                            + "\': Unable to apply wagon configuration.", e );
703                 }
704                 finally
705                 {
706                     if ( componentConfigurator != null )
707                     {
708                         try
709                         {
710                             container.release( componentConfigurator );
711                         }
712                         catch ( ComponentLifecycleException e )
713                         {
714                             log.error( "Problem releasing configurator - ignoring: " + e.getMessage() );
715                         }
716                     }
717                 }
718             }
719         }
720     }
721 
722     private static void configureWagonWithMaven2( ComponentConfigurator componentConfigurator, Wagon wagon,
723                                                   PlexusConfiguration plexusConf, PlexusContainer container )
724         throws ComponentConfigurationException
725     {
726         // in Maven 2.x   :
727         // * container.getContainerRealm() -> org.codehaus.classworlds.ClassRealm
728         // * componentConfiguration 3rd param is org.codehaus.classworlds.ClassRealm
729         // so use some reflection see MSITE-609
730         try
731         {
732             Method methodContainerRealm = container.getClass().getMethod( "getContainerRealm" );
733             ClassRealm realm = (ClassRealm) methodContainerRealm.invoke( container );
734 
735             Method methodConfigure = componentConfigurator.getClass().getMethod( "configureComponent",
736                                                                                  new Class[]{ Object.class,
737                                                                                      PlexusConfiguration.class,
738                                                                                      ClassRealm.class } );
739 
740             methodConfigure.invoke( componentConfigurator, wagon, plexusConf, realm );
741         }
742         catch ( Exception e )
743         {
744             throw new ComponentConfigurationException(
745                 "Failed to configure wagon component for a Maven2 use " + e.getMessage(), e );
746         }
747     }
748 
749     /**
750      * {@inheritDoc}
751      */
752     public void contextualize( Context context )
753         throws ContextException
754     {
755         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
756     }
757 
758     /**
759      * Extract the distributionManagement site from the given MavenProject.
760      *
761      * @param project the MavenProject. Not null.
762      * @return the project site. Not null.
763      *         Also site.getUrl() and site.getId() are guaranteed to be not null.
764      * @throws MojoExecutionException if any of the site info is missing.
765      */
766     protected static Site getSite( final MavenProject project )
767         throws MojoExecutionException
768     {
769         final DistributionManagement distributionManagement = project.getDistributionManagement();
770 
771         if ( distributionManagement == null )
772         {
773             throw new MojoExecutionException( "Missing distribution management in project " + getFullName( project ) );
774         }
775 
776         final Site site = distributionManagement.getSite();
777 
778         if ( site == null )
779         {
780             throw new MojoExecutionException( "Missing site information in the distribution management of the project "
781                 + getFullName( project ) );
782         }
783 
784         if ( site.getUrl() == null || site.getId() == null )
785         {
786             throw new MojoExecutionException( "Missing site data: specify url and id for project "
787                 + getFullName( project ) );
788         }
789 
790         return site;
791     }
792 
793     private static String getFullName( MavenProject project )
794     {
795         return project.getName() + " (" + project.getGroupId() + ':' + project.getArtifactId() + ':'
796             + project.getVersion() + ')';
797     }
798 
799     /**
800      * Extract the distributionManagement site of the top level parent of the given MavenProject.
801      * This climbs up the project hierarchy and returns the site of the last project
802      * for which {@link #getSite(org.apache.maven.project.MavenProject)} returns a site that resides in the
803      * same site. Notice that it doesn't take into account if the parent is in the reactor or not.
804      *
805      * @param project the MavenProject. Not <code>null</code>.
806      * @return the top level site. Not <code>null</code>.
807      *         Also site.getUrl() and site.getId() are guaranteed to be not <code>null</code>.
808      * @throws MojoExecutionException if no site info is found in the tree.
809      * @see URIPathDescriptor#sameSite(java.net.URI)
810      */
811     protected MavenProject getTopLevelProject( MavenProject project )
812         throws MojoExecutionException
813     {
814         Site site = getSite( project );
815 
816         MavenProject parent = project;
817 
818         while ( parent.getParent() != null )
819         {
820             MavenProject oldProject = parent;
821             // MSITE-585, MNG-1943
822             parent = siteTool.getParentProject( parent, reactorProjects, localRepository );
823 
824             Site oldSite = site;
825 
826             try
827             {
828                 site = getSite( parent );
829             }
830             catch ( MojoExecutionException e )
831             {
832                 return oldProject;
833             }
834 
835             // MSITE-600
836             URIPathDescriptor siteURI = new URIPathDescriptor( URIEncoder.encodeURI( site.getUrl() ), "" );
837             URIPathDescriptor oldSiteURI = new URIPathDescriptor( URIEncoder.encodeURI( oldSite.getUrl() ), "" );
838 
839             if ( !siteURI.sameSite( oldSiteURI.getBaseURI() ) )
840             {
841                 return oldProject;
842             }
843         }
844 
845         return parent;
846     }
847 
848     private static class URIEncoder
849     {
850         private static final String MARK = "-_.!~*'()";
851         private static final String RESERVED = ";/?:@&=+$,";
852 
853         public static String encodeURI( final String uriString )
854         {
855             final char[] chars = uriString.toCharArray();
856             final StringBuilder uri = new StringBuilder( chars.length );
857 
858             // MSITE-750: wagon dav: pseudo-protocol
859             if ( uriString.startsWith( "dav:http" ) )
860             {
861                 // transform dav:http to dav-http
862                 chars[3] = '-';
863             }
864 
865             for ( char c : chars )
866             {
867                 if ( ( c >= '0' && c <= '9' ) || ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' )
868                         || MARK.indexOf( c ) != -1  || RESERVED.indexOf( c ) != -1 )
869                 {
870                     uri.append( c );
871                 }
872                 else
873                 {
874                     uri.append( '%' );
875                     uri.append( Integer.toHexString( (int) c ) );
876                 }
877             }
878             return uri.toString();
879         }
880     }
881 }