View Javadoc
1   package org.apache.maven.scm.provider.perforce.command.login;
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.ByteArrayInputStream;
23  import java.io.File;
24  
25  import org.apache.maven.scm.CommandParameters;
26  import org.apache.maven.scm.ScmException;
27  import org.apache.maven.scm.ScmFileSet;
28  import org.apache.maven.scm.command.login.AbstractLoginCommand;
29  import org.apache.maven.scm.command.login.LoginScmResult;
30  import org.apache.maven.scm.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
32  import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
33  import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
34  import org.codehaus.plexus.util.StringUtils;
35  import org.codehaus.plexus.util.cli.CommandLineException;
36  import org.codehaus.plexus.util.cli.CommandLineUtils;
37  import org.codehaus.plexus.util.cli.Commandline;
38  
39  /**
40   * @author Mike Perham
41   *
42   */
43  public class PerforceLoginCommand
44      extends AbstractLoginCommand
45      implements PerforceCommand
46  {
47      /** {@inheritDoc} */
48      public LoginScmResult executeLoginCommand( ScmProviderRepository repo, ScmFileSet files, CommandParameters params )
49          throws ScmException
50      {
51          Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir() );
52          PerforceLoginConsumer consumer = new PerforceLoginConsumer();
53          boolean isSuccess = false;
54  
55          try
56          {
57              String password = repo.getPassword();
58              if ( StringUtils.isEmpty( password ) )
59              {
60                  if ( getLogger().isInfoEnabled() )
61                  {
62                      getLogger().info( "No password found, proceeding without it." );
63                  }
64                  isSuccess = true;
65              }
66              else
67              {
68                  CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
69                  int exitCode = CommandLineUtils.executeCommandLine( cl, new ByteArrayInputStream( password.getBytes() ),
70                                                                      consumer, err );
71                  isSuccess = consumer.isSuccess();
72  
73                  if ( !isSuccess )
74                  {
75                      String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
76  
77                      StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
78                      msg.append( '\n' );
79                      msg.append( "Command line was:" + cmdLine );
80  
81                      throw new CommandLineException( msg.toString() );
82                  }
83              }
84          }
85          catch ( CommandLineException e )
86          {
87              throw new ScmException( e.getMessage(), e );
88          }
89  
90          return new LoginScmResult( cl.toString(), isSuccess ? "Login successful" : "Login failed",
91                                     consumer.getOutput(), isSuccess );
92      }
93  
94      public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDir )
95      {
96          Commandline command = PerforceScmProvider.createP4Command( repo, workingDir );
97  
98          command.createArg().setValue( "login" );
99          if ( !StringUtils.isEmpty( repo.getUser() ) )
100         {
101             command.createArg().setValue( repo.getUser() );
102         }
103         return command;
104     }
105 }