View Javadoc
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   *
44   */
45  public final class VssCommandLineUtils
46      // FIXME extend CommandLineUtils
47  {
48  
49      private VssCommandLineUtils() {
50      }
51  
52      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          Iterator<File> it = fileSet.getFileList().iterator();
59  
60          while ( it.hasNext() )
61          {
62              File file = it.next();
63  
64              cl.createArg().setValue( file.getPath().replace( '\\', '/' ) );
65          }
66  
67      }
68  
69      public static Commandline getBaseVssCommandLine( File workingDirectory, String cmd,
70                                                       VssScmProviderRepository repository )
71      {
72          Commandline cl = new Commandline();
73  
74          cl.setExecutable( VssConstants.SS_EXE );
75  
76          cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
77  
78          if ( !StringUtils.isEmpty( repository.getUser() ) )
79          {
80              cl.createArg().setValue( "-Y" );
81  
82              StringBuilder sb = new StringBuilder( repository.getUser() );
83              if ( !StringUtils.isEmpty( repository.getPassword() ) )
84              {
85                  sb.append( "," ).append( repository.getPassword() );
86              }
87              cl.createArg().setValue( sb.toString() );
88          }
89  
90          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              if ( logger.isInfoEnabled() )
100             {
101                 logger.info( "Executing: " + cl );
102                 logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
103             }
104 
105             int exitcode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
106 
107             if ( logger.isDebugEnabled() )
108             {
109                 logger.debug( "VSS Command Exit_Code: " + exitcode );
110             }
111 
112             return exitcode;
113         }
114         catch ( CommandLineException ex )
115         {
116             throw new ScmException( "Error while executing command.", ex );
117         }
118     }
119 
120 
121     public static final Settings getSettings()
122     {
123     	if ( settings == null )
124     	{
125     		settings = readSettings();
126     	}
127     	return settings;
128     }
129     
130     public static Settings readSettings()
131     {
132         Settings settings = null;
133         File settingsFile = getScmConfFile();
134         if ( settingsFile.exists() )
135         {
136             VssXpp3Reader reader = new VssXpp3Reader();
137             try
138             {
139                 settings = reader.read( ReaderFactory.newXmlReader( settingsFile ) );
140             }
141             catch ( FileNotFoundException e )
142             {
143                 // nop
144             }
145             catch ( IOException e )
146             {
147                 // nop
148             }
149             catch ( XmlPullParserException e )
150             {
151                 String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage();
152 
153                 System.err.println( message );
154             }
155         }
156 
157         // override settings with command line options
158         String vssDirectory = System.getProperty( "vssDirectory" );
159         if ( StringUtils.isNotEmpty( vssDirectory ) )
160         {
161             if ( settings == null )
162             {
163                 settings = new Settings();
164             }
165             settings.setVssDirectory( vssDirectory );
166         }
167         return settings;
168     }
169 
170     protected static final File getScmConfDir()
171     {
172         return scmConfDir;
173     }
174 
175     protected static final void setScmConfDir( File directory )
176     {
177         scmConfDir = directory;
178         settings = readSettings();
179     }
180 
181     public static final String getSsDir()
182     {
183         String ssDir = "";
184         if ( VssCommandLineUtils.getSettings() != null )
185         {
186             String ssDir2 = VssCommandLineUtils.getSettings().getVssDirectory();
187 
188             if ( ssDir2 != null )
189             {
190                 ssDir = StringUtils.replace( ssDir2, "\\", "/" );
191 
192                 if ( !ssDir.endsWith( "/" ) )
193                 {
194                     ssDir += "/";
195                 }
196             }
197         }
198         return ssDir;
199     }
200     
201     public static File getScmConfFile() 
202     {
203     	return new File( scmConfDir, "vss-settings.xml" );
204     }
205 }