001package org.apache.maven.scm.provider.vss.commands; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.scm.ScmException; 023import org.apache.maven.scm.ScmFileSet; 024import org.apache.maven.scm.log.ScmLogger; 025import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository; 026import org.apache.maven.scm.providers.vss.settings.Settings; 027import org.apache.maven.scm.providers.vss.settings.io.xpp3.VssXpp3Reader; 028import org.codehaus.plexus.util.ReaderFactory; 029import org.codehaus.plexus.util.StringUtils; 030import org.codehaus.plexus.util.cli.CommandLineException; 031import org.codehaus.plexus.util.cli.CommandLineUtils; 032import org.codehaus.plexus.util.cli.Commandline; 033import org.codehaus.plexus.util.cli.StreamConsumer; 034import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 035 036import java.io.File; 037import java.io.FileNotFoundException; 038import java.io.IOException; 039import java.util.Iterator; 040 041/** 042 * @author <a href="mailto:triek@thrx.de">Thorsten Riek</a> 043 * 044 */ 045public final class VssCommandLineUtils 046 // FIXME extend CommandLineUtils 047{ 048 049 private VssCommandLineUtils() 050 { 051 } 052 053 private static File scmConfDir = new File( System.getProperty( "user.home" ), ".scm" ); 054 055 private static Settings settings; 056 057 public static void addFiles( Commandline cl, ScmFileSet fileSet ) 058 { 059 Iterator<File> it = fileSet.getFileList().iterator(); 060 061 while ( it.hasNext() ) 062 { 063 File file = it.next(); 064 065 cl.createArg().setValue( file.getPath().replace( '\\', '/' ) ); 066 } 067 068 } 069 070 public static Commandline getBaseVssCommandLine( File workingDirectory, String cmd, 071 VssScmProviderRepository repository ) 072 { 073 Commandline cl = new Commandline(); 074 075 cl.setExecutable( VssConstants.SS_EXE ); 076 077 cl.setWorkingDirectory( workingDirectory.getAbsolutePath() ); 078 079 if ( !StringUtils.isEmpty( repository.getUser() ) ) 080 { 081 cl.createArg().setValue( "-Y" ); 082 083 StringBuilder sb = new StringBuilder( repository.getUser() ); 084 if ( !StringUtils.isEmpty( repository.getPassword() ) ) 085 { 086 sb.append( "," ).append( repository.getPassword() ); 087 } 088 cl.createArg().setValue( sb.toString() ); 089 } 090 091 return cl; 092 } 093 094 public static int executeCommandline( Commandline cl, StreamConsumer consumer, 095 CommandLineUtils.StringStreamConsumer stderr, ScmLogger logger ) 096 throws ScmException 097 { 098 try 099 { 100 if ( logger.isInfoEnabled() ) 101 { 102 logger.info( "Executing: " + cl ); 103 logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() ); 104 } 105 106 int exitcode = CommandLineUtils.executeCommandLine( cl, consumer, stderr ); 107 108 if ( logger.isDebugEnabled() ) 109 { 110 logger.debug( "VSS Command Exit_Code: " + exitcode ); 111 } 112 113 return exitcode; 114 } 115 catch ( CommandLineException ex ) 116 { 117 throw new ScmException( "Error while executing command.", ex ); 118 } 119 } 120 121 122 public static Settings getSettings() 123 { 124 if ( settings == null ) 125 { 126 settings = readSettings(); 127 } 128 return settings; 129 } 130 131 public static Settings readSettings() 132 { 133 Settings settings = null; 134 File settingsFile = getScmConfFile(); 135 if ( settingsFile.exists() ) 136 { 137 VssXpp3Reader reader = new VssXpp3Reader(); 138 try 139 { 140 settings = reader.read( ReaderFactory.newXmlReader( settingsFile ) ); 141 } 142 catch ( FileNotFoundException e ) 143 { 144 // nop 145 } 146 catch ( IOException e ) 147 { 148 // nop 149 } 150 catch ( XmlPullParserException e ) 151 { 152 String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage(); 153 154 System.err.println( message ); 155 } 156 } 157 158 // override settings with command line options 159 String vssDirectory = System.getProperty( "vssDirectory" ); 160 if ( StringUtils.isNotEmpty( vssDirectory ) ) 161 { 162 if ( settings == null ) 163 { 164 settings = new Settings(); 165 } 166 settings.setVssDirectory( vssDirectory ); 167 } 168 return settings; 169 } 170 171 protected static File getScmConfDir() 172 { 173 return scmConfDir; 174 } 175 176 protected static void setScmConfDir( File directory ) 177 { 178 scmConfDir = directory; 179 settings = readSettings(); 180 } 181 182 public static String getSsDir() 183 { 184 String ssDir = ""; 185 if ( VssCommandLineUtils.getSettings() != null ) 186 { 187 String ssDir2 = VssCommandLineUtils.getSettings().getVssDirectory(); 188 189 if ( ssDir2 != null ) 190 { 191 ssDir = StringUtils.replace( ssDir2, "\\", "/" ); 192 193 if ( !ssDir.endsWith( "/" ) ) 194 { 195 ssDir += "/"; 196 } 197 } 198 } 199 return ssDir; 200 } 201 202 public static File getScmConfFile() 203 { 204 return new File( scmConfDir, "vss-settings.xml" ); 205 } 206}