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 | 0 | 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 | 0 | Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir() ); |
52 | 0 | PerforceLoginConsumer consumer = new PerforceLoginConsumer(); |
53 | 0 | boolean isSuccess = false; |
54 | |
|
55 | |
try |
56 | |
{ |
57 | 0 | String password = repo.getPassword(); |
58 | 0 | if ( StringUtils.isEmpty( password ) ) |
59 | |
{ |
60 | 0 | if ( getLogger().isInfoEnabled() ) |
61 | |
{ |
62 | 0 | getLogger().info( "No password found, proceeding without it." ); |
63 | |
} |
64 | 0 | isSuccess = true; |
65 | |
} |
66 | |
else |
67 | |
{ |
68 | 0 | CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer(); |
69 | 0 | int exitCode = CommandLineUtils.executeCommandLine( cl, new ByteArrayInputStream( password.getBytes() ), |
70 | |
consumer, err ); |
71 | 0 | isSuccess = consumer.isSuccess(); |
72 | |
|
73 | 0 | if ( !isSuccess ) |
74 | |
{ |
75 | 0 | String cmdLine = CommandLineUtils.toString( cl.getCommandline() ); |
76 | |
|
77 | 0 | StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() ); |
78 | 0 | msg.append( '\n' ); |
79 | 0 | msg.append( "Command line was:" + cmdLine ); |
80 | |
|
81 | 0 | throw new CommandLineException( msg.toString() ); |
82 | |
} |
83 | |
} |
84 | |
} |
85 | 0 | catch ( CommandLineException e ) |
86 | |
{ |
87 | 0 | throw new ScmException( e.getMessage(), e ); |
88 | 0 | } |
89 | |
|
90 | 0 | 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 | 1 | Commandline command = PerforceScmProvider.createP4Command( repo, workingDir ); |
97 | |
|
98 | 1 | command.createArg().setValue( "login" ); |
99 | 1 | if ( !StringUtils.isEmpty( repo.getUser() ) ) |
100 | |
{ |
101 | 0 | command.createArg().setValue( repo.getUser() ); |
102 | |
} |
103 | 1 | return command; |
104 | |
} |
105 | |
} |