001    package org.apache.maven.scm.provider.perforce.command.login;
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.ByteArrayInputStream;
023    import java.io.File;
024    
025    import org.apache.maven.scm.CommandParameters;
026    import org.apache.maven.scm.ScmException;
027    import org.apache.maven.scm.ScmFileSet;
028    import org.apache.maven.scm.command.login.AbstractLoginCommand;
029    import org.apache.maven.scm.command.login.LoginScmResult;
030    import org.apache.maven.scm.provider.ScmProviderRepository;
031    import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
032    import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
033    import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
034    import org.codehaus.plexus.util.StringUtils;
035    import org.codehaus.plexus.util.cli.CommandLineException;
036    import org.codehaus.plexus.util.cli.CommandLineUtils;
037    import org.codehaus.plexus.util.cli.Commandline;
038    
039    /**
040     * @author Mike Perham
041     * @version $Id: PerforceLoginCommand.java 1306867 2012-03-29 13:45:10Z olamy $
042     */
043    public class PerforceLoginCommand
044        extends AbstractLoginCommand
045        implements PerforceCommand
046    {
047        /** {@inheritDoc} */
048        public LoginScmResult executeLoginCommand( ScmProviderRepository repo, ScmFileSet files, CommandParameters params )
049            throws ScmException
050        {
051            Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir() );
052            PerforceLoginConsumer consumer = new PerforceLoginConsumer();
053            boolean isSuccess = false;
054    
055            try
056            {
057                String password = repo.getPassword();
058                if ( StringUtils.isEmpty( password ) )
059                {
060                    if ( getLogger().isInfoEnabled() )
061                    {
062                        getLogger().info( "No password found, proceeding without it." );
063                    }
064                    isSuccess = true;
065                }
066                else
067                {
068                    CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
069                    int exitCode = CommandLineUtils.executeCommandLine( cl, new ByteArrayInputStream( password.getBytes() ),
070                                                                        consumer, err );
071                    isSuccess = consumer.isSuccess();
072    
073                    if ( !isSuccess )
074                    {
075                        String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
076    
077                        StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
078                        msg.append( '\n' );
079                        msg.append( "Command line was:" + cmdLine );
080    
081                        throw new CommandLineException( msg.toString() );
082                    }
083                }
084            }
085            catch ( CommandLineException e )
086            {
087                throw new ScmException( e.getMessage(), e );
088            }
089    
090            return new LoginScmResult( cl.toString(), isSuccess ? "Login successful" : "Login failed",
091                                       consumer.getOutput(), isSuccess );
092        }
093    
094        public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDir )
095        {
096            Commandline command = PerforceScmProvider.createP4Command( repo, workingDir );
097    
098            command.createArg().setValue( "login" );
099            if ( !StringUtils.isEmpty( repo.getUser() ) )
100            {
101                command.createArg().setValue( repo.getUser() );
102            }
103            return command;
104        }
105    }