Coverage Report - org.apache.maven.plugins.help.EffectiveSettingsMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
EffectiveSettingsMojo
0 %
0/76
0 %
0/22
3,714
 
 1  
 package org.apache.maven.plugins.help;
 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.IOException;
 23  
 import java.io.StringWriter;
 24  
 import java.net.InetAddress;
 25  
 import java.net.UnknownHostException;
 26  
 import java.util.Iterator;
 27  
 import java.util.Properties;
 28  
 
 29  
 import org.apache.maven.plugin.MojoExecutionException;
 30  
 import org.apache.maven.settings.Profile;
 31  
 import org.apache.maven.settings.Proxy;
 32  
 import org.apache.maven.settings.Server;
 33  
 import org.apache.maven.settings.Settings;
 34  
 import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
 35  
 import org.codehaus.plexus.util.StringUtils;
 36  
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 37  
 import org.codehaus.plexus.util.xml.XMLWriter;
 38  
 import org.codehaus.plexus.util.xml.XmlWriterUtil;
 39  
 
 40  
 /**
 41  
  * Displays the calculated settings as XML for this project, given any profile enhancement and the inheritance
 42  
  * of the global settings into the user-level settings.
 43  
  *
 44  
  * @version $Id: EffectiveSettingsMojo.java 689770 2008-08-28 09:47:18Z vsiveton $
 45  
  * @since 2.0
 46  
  * @goal effective-settings
 47  
  * @requiresProject false
 48  
  */
 49  0
 public class EffectiveSettingsMojo
 50  
     extends AbstractEffectiveMojo
 51  
 {
 52  
     // ----------------------------------------------------------------------
 53  
     // Mojo parameters
 54  
     // ----------------------------------------------------------------------
 55  
 
 56  
     /**
 57  
      * The system settings for Maven. This is the instance resulting from
 58  
      * merging global and user-level settings files.
 59  
      *
 60  
      * @parameter expression="${settings}"
 61  
      * @readonly
 62  
      * @required
 63  
      */
 64  
     private Settings settings;
 65  
 
 66  
     /**
 67  
      * For security reasons, all passwords are hidden by default. Set this to <code>true</code> to show all passwords.
 68  
      *
 69  
      * @since 2.1
 70  
      * @parameter expression="${showPasswords}" default-value="false"
 71  
      */
 72  
     private boolean showPasswords;
 73  
 
 74  
     // ----------------------------------------------------------------------
 75  
     // Public methods
 76  
     // ----------------------------------------------------------------------
 77  
 
 78  
     /** {@inheritDoc} */
 79  
     public void execute()
 80  
         throws MojoExecutionException
 81  
     {
 82  
         Settings copySettings;
 83  0
         if ( showPasswords )
 84  
         {
 85  0
             copySettings = settings;
 86  
         }
 87  
         else
 88  
         {
 89  0
             copySettings = copySettings( settings );
 90  0
             hidePasswords( copySettings );
 91  
         }
 92  
 
 93  0
         StringWriter w = new StringWriter();
 94  0
         XMLWriter writer =
 95  
             new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
 96  
                                       copySettings.getModelEncoding(), null );
 97  
 
 98  0
         writeHeader( writer );
 99  
 
 100  0
         writeEffectiveSettings( copySettings, writer );
 101  
 
 102  0
         String effectiveSettings = w.toString();
 103  
 
 104  0
         if ( output != null )
 105  
         {
 106  
             try
 107  
             {
 108  0
                 writeXmlFile( output, effectiveSettings, copySettings.getModelEncoding() );
 109  
             }
 110  0
             catch ( IOException e )
 111  
             {
 112  0
                 throw new MojoExecutionException( "Cannot write effective-settings to output: " + output, e );
 113  0
             }
 114  
 
 115  0
             if ( getLog().isInfoEnabled() )
 116  
             {
 117  0
                 getLog().info( "Effective-settings written to: " + output );
 118  
             }
 119  
         }
 120  
         else
 121  
         {
 122  0
             StringBuffer message = new StringBuffer();
 123  
 
 124  0
             message.append( "\nEffective user-specific configuration settings:\n\n" );
 125  0
             message.append( effectiveSettings );
 126  0
             message.append( "\n" );
 127  
 
 128  0
             if ( getLog().isInfoEnabled() )
 129  
             {
 130  0
                 getLog().info( message.toString() );
 131  
             }
 132  
         }
 133  0
     }
 134  
 
 135  
     // ----------------------------------------------------------------------
 136  
     // Private methods
 137  
     // ----------------------------------------------------------------------
 138  
 
 139  
     /**
 140  
      * Hide proxy and server passwords.
 141  
      *
 142  
      * @param aSettings not null
 143  
      */
 144  
     private static void hidePasswords( Settings aSettings )
 145  
     {
 146  0
         for ( Iterator it = aSettings.getProxies().iterator(); it.hasNext(); )
 147  
         {
 148  0
             Proxy proxy = (Proxy) it.next();
 149  
 
 150  0
             if ( StringUtils.isNotEmpty( proxy.getPassword() ) )
 151  
             {
 152  0
                 proxy.setPassword( StringUtils.repeat( "*", proxy.getPassword().length() ) );
 153  
             }
 154  0
         }
 155  
 
 156  0
         for ( Iterator it = aSettings.getServers().iterator(); it.hasNext(); )
 157  
         {
 158  0
             Server server = (Server) it.next();
 159  
 
 160  0
             if ( StringUtils.isNotEmpty( server.getPassword() ) )
 161  
             {
 162  0
                 server.setPassword( StringUtils.repeat( "*", server.getPassword().length() ) );
 163  
             }
 164  0
         }
 165  0
     }
 166  
 
 167  
     /**
 168  
      * TODO: should be replaced by SettingsUtils#copySettings() in 2.0.10+.
 169  
      *
 170  
      * @param settings could be null
 171  
      * @return a new instance of settings or null if settings was null.
 172  
      */
 173  
     private static Settings copySettings( Settings settings )
 174  
     {
 175  0
         if ( settings == null )
 176  
         {
 177  0
             return null;
 178  
         }
 179  
 
 180  0
         Settings clone = new Settings();
 181  0
         clone.setActiveProfiles( settings.getActiveProfiles() );
 182  0
         clone.setInteractiveMode( settings.isInteractiveMode() );
 183  0
         clone.setLocalRepository( settings.getLocalRepository() );
 184  0
         clone.setMirrors( settings.getMirrors() );
 185  0
         clone.setOffline( settings.isOffline() );
 186  0
         clone.setPluginGroups( settings.getPluginGroups() );
 187  0
         clone.setProfiles( settings.getProfiles() );
 188  0
         clone.setProxies( settings.getProxies() );
 189  0
         clone.setRuntimeInfo( settings.getRuntimeInfo() );
 190  0
         clone.setServers( settings.getServers() );
 191  0
         clone.setSourceLevel( settings.getSourceLevel() );
 192  0
         clone.setUsePluginRegistry( settings.isUsePluginRegistry() );
 193  
 
 194  0
         return clone;
 195  
     }
 196  
 
 197  
     /**
 198  
      * Method for writing the effective settings informations.
 199  
      *
 200  
      * @param settings the settings, not null.
 201  
      * @param writer the XML writer used, not null.
 202  
      * @throws MojoExecutionException if any
 203  
      */
 204  
     private static void writeEffectiveSettings( Settings settings, XMLWriter writer )
 205  
         throws MojoExecutionException
 206  
     {
 207  0
         cleanSettings( settings );
 208  
 
 209  
         String effectiveSettings;
 210  
 
 211  0
         StringWriter sWriter = new StringWriter();
 212  0
         SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer();
 213  
         try
 214  
         {
 215  0
             settingsWriter.write( sWriter, settings );
 216  
         }
 217  0
         catch ( IOException e )
 218  
         {
 219  0
             throw new MojoExecutionException( "Cannot serialize Settings to XML.", e );
 220  0
         }
 221  
 
 222  0
         effectiveSettings = addMavenNamespace( sWriter.toString(), false );
 223  
 
 224  0
         writeComment( writer, "Effective Settings for '" + getUserName() + "' on '" + getHostName() + "'" );
 225  
 
 226  0
         writer.writeMarkup( effectiveSettings );
 227  0
     }
 228  
 
 229  
     /**
 230  
      * Apply some logic to clean the model before writing it.
 231  
      *
 232  
      * @param settings not null
 233  
      */
 234  
     private static void cleanSettings( Settings settings )
 235  
     {
 236  0
         for ( Iterator it = settings.getProfiles().iterator(); it.hasNext(); )
 237  
         {
 238  0
             Profile profile = (Profile) it.next();
 239  
 
 240  0
             Properties properties = new SortedProperties();
 241  0
             properties.putAll( profile.getProperties() );
 242  0
             profile.setProperties( properties );
 243  0
         }
 244  0
     }
 245  
 
 246  
     /**
 247  
      * @return the current host name or <code>unknown</code> if error
 248  
      * @see InetAddress#getLocalHost()
 249  
      */
 250  
     private static String getHostName()
 251  
     {
 252  
         try
 253  
         {
 254  0
             return InetAddress.getLocalHost().getHostName();
 255  
         }
 256  0
         catch ( UnknownHostException e )
 257  
         {
 258  0
             return "unknown";
 259  
         }
 260  
     }
 261  
 
 262  
     /**
 263  
      * @return the user name or <code>unknown</code> if <code>user.name</code> is not a system property.
 264  
      */
 265  
     private static String getUserName()
 266  
     {
 267  0
         String userName = System.getProperty( "user.name" );
 268  0
         if ( StringUtils.isEmpty( userName ) )
 269  
         {
 270  0
             return "unknown";
 271  
         }
 272  
 
 273  0
         return userName;
 274  
     }
 275  
 }