Coverage Report - org.apache.maven.scm.provider.vss.commands.VssCommandLineUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
VssCommandLineUtils
50 %
31/61
54 %
13/24
2,8
 
 1  
 package org.apache.maven.scm.provider.vss.commands;
 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.scm.ScmException;
 23  
 import org.apache.maven.scm.ScmFileSet;
 24  
 import org.apache.maven.scm.log.ScmLogger;
 25  
 import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
 26  
 import org.apache.maven.scm.providers.vss.settings.Settings;
 27  
 import org.apache.maven.scm.providers.vss.settings.io.xpp3.VssXpp3Reader;
 28  
 import org.codehaus.plexus.util.ReaderFactory;
 29  
 import org.codehaus.plexus.util.StringUtils;
 30  
 import org.codehaus.plexus.util.cli.CommandLineException;
 31  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 32  
 import org.codehaus.plexus.util.cli.Commandline;
 33  
 import org.codehaus.plexus.util.cli.StreamConsumer;
 34  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 35  
 
 36  
 import java.io.File;
 37  
 import java.io.FileNotFoundException;
 38  
 import java.io.IOException;
 39  
 import java.util.Iterator;
 40  
 
 41  
 /**
 42  
  * @author <a href="mailto:triek@thrx.de">Thorsten Riek</a>
 43  
  * @version $Id: VssCommandLineUtils.java 1306867 2012-03-29 13:45:10Z olamy $
 44  
  */
 45  
 public final class VssCommandLineUtils
 46  
     // FIXME extend CommandLineUtils
 47  
 {
 48  
 
 49  0
     private VssCommandLineUtils() {
 50  0
     }
 51  
 
 52  1
     private static File scmConfDir = new File( System.getProperty( "user.home" ), ".scm" );
 53  
 
 54  
     private static Settings settings;
 55  
 
 56  
     public static void addFiles( Commandline cl, ScmFileSet fileSet )
 57  
     {
 58  1
         Iterator<File> it = fileSet.getFileList().iterator();
 59  
 
 60  1
         while ( it.hasNext() )
 61  
         {
 62  0
             File file = it.next();
 63  
 
 64  0
             cl.createArg().setValue( file.getPath().replace( '\\', '/' ) );
 65  0
         }
 66  
 
 67  1
     }
 68  
 
 69  
     public static Commandline getBaseVssCommandLine( File workingDirectory, String cmd,
 70  
                                                      VssScmProviderRepository repository )
 71  
     {
 72  0
         Commandline cl = new Commandline();
 73  
 
 74  0
         cl.setExecutable( VssConstants.SS_EXE );
 75  
 
 76  0
         cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
 77  
 
 78  0
         if ( !StringUtils.isEmpty( repository.getUser() ) )
 79  
         {
 80  0
             cl.createArg().setValue( "-Y" );
 81  
 
 82  0
             StringBuilder sb = new StringBuilder( repository.getUser() );
 83  0
             if ( !StringUtils.isEmpty( repository.getPassword() ) )
 84  
             {
 85  0
                 sb.append( "," ).append( repository.getPassword() );
 86  
             }
 87  0
             cl.createArg().setValue( sb.toString() );
 88  
         }
 89  
 
 90  0
         return cl;
 91  
     }
 92  
 
 93  
     public static int executeCommandline( Commandline cl, StreamConsumer consumer,
 94  
                                           CommandLineUtils.StringStreamConsumer stderr, ScmLogger logger )
 95  
         throws ScmException
 96  
     {
 97  
         try
 98  
         {
 99  0
             if ( logger.isInfoEnabled() )
 100  
             {
 101  0
                 logger.info( "Executing: " + cl );
 102  0
                 logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
 103  
             }
 104  
 
 105  0
             int exitcode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
 106  
 
 107  0
             if ( logger.isDebugEnabled() )
 108  
             {
 109  0
                 logger.debug( "VSS Command Exit_Code: " + exitcode );
 110  
             }
 111  
 
 112  0
             return exitcode;
 113  
         }
 114  0
         catch ( CommandLineException ex )
 115  
         {
 116  0
             throw new ScmException( "Error while executing command.", ex );
 117  
         }
 118  
     }
 119  
 
 120  
 
 121  
     public static final Settings getSettings()
 122  
     {
 123  35
             if ( settings == null )
 124  
             {
 125  22
                     settings = readSettings();
 126  
             }
 127  35
             return settings;
 128  
     }
 129  
     
 130  
     public static Settings readSettings()
 131  
     {
 132  29
         Settings settings = null;
 133  29
         File settingsFile = getScmConfFile();
 134  29
         if ( settingsFile.exists() )
 135  
         {
 136  1
             VssXpp3Reader reader = new VssXpp3Reader();
 137  
             try
 138  
             {
 139  1
                 settings = reader.read( ReaderFactory.newXmlReader( settingsFile ) );
 140  
             }
 141  0
             catch ( FileNotFoundException e )
 142  
             {
 143  
                 // nop
 144  
             }
 145  0
             catch ( IOException e )
 146  
             {
 147  
                 // nop
 148  
             }
 149  0
             catch ( XmlPullParserException e )
 150  
             {
 151  0
                 String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage();
 152  
 
 153  0
                 System.err.println( message );
 154  1
             }
 155  
         }
 156  
 
 157  
         // override settings with command line options
 158  29
         String vssDirectory = System.getProperty( "vssDirectory" );
 159  29
         if ( StringUtils.isNotEmpty( vssDirectory ) )
 160  
         {
 161  6
             if ( settings == null )
 162  
             {
 163  6
                 settings = new Settings();
 164  
             }
 165  6
             settings.setVssDirectory( vssDirectory );
 166  
         }
 167  29
         return settings;
 168  
     }
 169  
 
 170  
     protected static final File getScmConfDir()
 171  
     {
 172  0
         return scmConfDir;
 173  
     }
 174  
 
 175  
     protected static final void setScmConfDir( File directory )
 176  
     {
 177  7
         scmConfDir = directory;
 178  7
         settings = readSettings();
 179  7
     }
 180  
 
 181  
     public static final String getSsDir()
 182  
     {
 183  27
         String ssDir = "";
 184  27
         if ( VssCommandLineUtils.getSettings() != null )
 185  
         {
 186  5
             String ssDir2 = VssCommandLineUtils.getSettings().getVssDirectory();
 187  
 
 188  5
             if ( ssDir2 != null )
 189  
             {
 190  5
                 ssDir = StringUtils.replace( ssDir2, "\\", "/" );
 191  
 
 192  5
                 if ( !ssDir.endsWith( "/" ) )
 193  
                 {
 194  2
                     ssDir += "/";
 195  
                 }
 196  
             }
 197  
         }
 198  27
         return ssDir;
 199  
     }
 200  
     
 201  
     public static File getScmConfFile() 
 202  
     {
 203  29
             return new File( scmConfDir, "vss-settings.xml" );
 204  
     }
 205  
 }