1 package org.apache.maven.scm.provider.perforce.command.login;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
41
42
43 public class PerforceLoginCommand
44 extends AbstractLoginCommand
45 implements PerforceCommand
46 {
47
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 }