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  
53      private static File scmConfDir = new File( System.getProperty( "user.home" ), ".scm" );
54  
55      private static Settings settings;
56  
57      public static void addFiles( Commandline cl, ScmFileSet fileSet )
58      {
59          Iterator<File> it = fileSet.getFileList().iterator();
60  
61          while ( it.hasNext() )
62          {
63              File file = it.next();
64  
65              cl.createArg().setValue( file.getPath().replace( '\\', '/' ) );
66          }
67  
68      }
69  
70      public static Commandline getBaseVssCommandLine( File workingDirectory, String cmd,
71                                                       VssScmProviderRepository repository )
72      {
73          Commandline cl = new Commandline();
74  
75          cl.setExecutable( VssConstants.SS_EXE );
76  
77          cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
78  
79          if ( !StringUtils.isEmpty( repository.getUser() ) )
80          {
81              cl.createArg().setValue( "-Y" );
82  
83              StringBuilder sb = new StringBuilder( repository.getUser() );
84              if ( !StringUtils.isEmpty( repository.getPassword() ) )
85              {
86                  sb.append( "," ).append( repository.getPassword() );
87              }
88              cl.createArg().setValue( sb.toString() );
89          }
90  
91          return cl;
92      }
93  
94      public static int executeCommandline( Commandline cl, StreamConsumer consumer,
95                                            CommandLineUtils.StringStreamConsumer stderr, ScmLogger logger )
96          throws ScmException
97      {
98          try
99          {
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 }