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