001    package org.apache.maven.scm.provider.tfs.command;
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    
022    import java.io.File;
023    import java.util.Iterator;
024    
025    import org.apache.maven.scm.ScmException;
026    import org.apache.maven.scm.ScmFile;
027    import org.apache.maven.scm.ScmFileSet;
028    import org.apache.maven.scm.log.ScmLogger;
029    import org.apache.maven.scm.provider.ScmProviderRepository;
030    import org.apache.maven.scm.provider.tfs.command.consumer.ErrorStreamConsumer;
031    import org.apache.maven.scm.provider.tfs.command.consumer.FileListConsumer;
032    import org.codehaus.plexus.util.cli.CommandLineException;
033    import org.codehaus.plexus.util.cli.CommandLineUtils;
034    import org.codehaus.plexus.util.cli.Commandline;
035    import org.codehaus.plexus.util.cli.StreamConsumer;
036    import org.codehaus.plexus.util.cli.CommandLineUtils.StringStreamConsumer;
037    
038    public class TfsCommand
039    {
040    
041        private ScmLogger logger;
042    
043        private Commandline command;
044    
045        public TfsCommand( String cmd, ScmProviderRepository r, ScmFileSet f, ScmLogger logger )
046        {
047            command = new Commandline();
048            command.setExecutable( "tf" );
049            if ( f != null )
050            {
051                command.setWorkingDirectory( f.getBasedir().getAbsolutePath() );
052            }
053            
054            command.createArg().setValue( cmd );
055            
056            if ( r.getUser() != null )
057            {
058                command.createArg().setValue( "-login:" + r.getUser() + "," + r.getPassword() );
059            }
060            this.logger = logger;
061        }
062    
063        public void addArgument( ScmFileSet f )
064        {
065            info( "files: " + f.getBasedir().getAbsolutePath() );
066            Iterator<File> iter = f.getFileList().iterator();
067            while ( iter.hasNext() )
068            {
069                command.createArg().setValue( ( (File) iter.next() ).getPath() );
070            }
071        }
072    
073        public void addArgument( String s )
074        {
075            command.createArg().setValue( s );
076        }
077    
078        public int execute( StreamConsumer out, ErrorStreamConsumer err )
079            throws ScmException
080        {
081            info( "Command line - " + getCommandString() );
082            int status;
083            try
084            {
085                status = CommandLineUtils.executeCommandLine( command, out, err );
086            }
087            catch ( CommandLineException e )
088            {
089                throw new ScmException( "Error while executing TFS command line - " + getCommandString(), e );
090            }
091            info( "err - " + err.getOutput() );
092            if ( out instanceof StringStreamConsumer )
093            {
094                StringStreamConsumer sc = (StringStreamConsumer) out;
095                debug( sc.getOutput() );
096            }
097            if ( out instanceof FileListConsumer )
098            {
099                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    }