View Javadoc

1   package org.apache.maven.scm.provider.tfs.command;
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.File;
23  import java.util.Iterator;
24  
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFile;
27  import org.apache.maven.scm.ScmFileSet;
28  import org.apache.maven.scm.log.ScmLogger;
29  import org.apache.maven.scm.provider.ScmProviderRepository;
30  import org.apache.maven.scm.provider.tfs.command.consumer.ErrorStreamConsumer;
31  import org.apache.maven.scm.provider.tfs.command.consumer.FileListConsumer;
32  import org.codehaus.plexus.util.cli.CommandLineException;
33  import org.codehaus.plexus.util.cli.CommandLineUtils;
34  import org.codehaus.plexus.util.cli.Commandline;
35  import org.codehaus.plexus.util.cli.StreamConsumer;
36  import org.codehaus.plexus.util.cli.CommandLineUtils.StringStreamConsumer;
37  
38  public class TfsCommand
39  {
40  
41      private ScmLogger logger;
42  
43      private Commandline command;
44  
45      public TfsCommand( String cmd, ScmProviderRepository r, ScmFileSet f, ScmLogger logger )
46      {
47          command = new Commandline();
48          command.setExecutable( "tf" );
49          if ( f != null )
50          {
51              command.setWorkingDirectory( f.getBasedir().getAbsolutePath() );
52          }
53          
54          command.createArg().setValue( cmd );
55          
56          if ( r.getUser() != null )
57          {
58              command.createArg().setValue( "-login:" + r.getUser() + "," + r.getPassword() );
59          }
60          this.logger = logger;
61      }
62  
63      public void addArgument( ScmFileSet f )
64      {
65          info( "files: " + f.getBasedir().getAbsolutePath() );
66          Iterator<File> iter = f.getFileList().iterator();
67          while ( iter.hasNext() )
68          {
69              command.createArg().setValue( ( (File) iter.next() ).getPath() );
70          }
71      }
72  
73      public void addArgument( String s )
74      {
75          command.createArg().setValue( s );
76      }
77  
78      public int execute( StreamConsumer out, ErrorStreamConsumer err )
79          throws ScmException
80      {
81          info( "Command line - " + getCommandString() );
82          int status;
83          try
84          {
85              status = CommandLineUtils.executeCommandLine( command, out, err );
86          }
87          catch ( CommandLineException e )
88          {
89              throw new ScmException( "Error while executing TFS command line - " + getCommandString(), e );
90          }
91          info( "err - " + err.getOutput() );
92          if ( out instanceof StringStreamConsumer )
93          {
94              StringStreamConsumer sc = (StringStreamConsumer) out;
95              debug( sc.getOutput() );
96          }
97          if ( out instanceof FileListConsumer )
98          {
99              FileListConsumer f = (FileListConsumer) out;
100             for ( Iterator<ScmFile> i = f.getFiles().iterator(); i.hasNext(); )
101             {
102                 ScmFile file = i.next();
103                 debug( file.getPath() );
104             }
105         }
106 
107         return status;
108     }
109 
110     public String getCommandString()
111     {
112         return command.toString();
113     }
114     
115     public Commandline getCommandline() {
116         return command;
117     }
118 
119     private void info( String message )
120     {
121         if ( logger != null )
122             logger.info( message );
123     }
124 
125     private void debug( String message )
126     {
127         if ( logger != null )
128             logger.debug( message );
129     }
130 
131 }