/maven-scm-providers/maven-scm-provider-perforce/src/main/java/org/apache/maven/scm/provider/perforce/command/login/PerforceLoginCommand.java

https://github.com/intelchen/maven-scm · Java · 105 lines · 71 code · 11 blank · 23 comment · 4 complexity · 0b6f8f5e6790cc9fce10093f22a79ae4 MD5 · raw file

  1. package org.apache.maven.scm.provider.perforce.command.login;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.io.ByteArrayInputStream;
  21. import java.io.File;
  22. import org.apache.maven.scm.CommandParameters;
  23. import org.apache.maven.scm.ScmException;
  24. import org.apache.maven.scm.ScmFileSet;
  25. import org.apache.maven.scm.command.login.AbstractLoginCommand;
  26. import org.apache.maven.scm.command.login.LoginScmResult;
  27. import org.apache.maven.scm.provider.ScmProviderRepository;
  28. import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
  29. import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
  30. import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
  31. import org.codehaus.plexus.util.StringUtils;
  32. import org.codehaus.plexus.util.cli.CommandLineException;
  33. import org.codehaus.plexus.util.cli.CommandLineUtils;
  34. import org.codehaus.plexus.util.cli.Commandline;
  35. /**
  36. * @author Mike Perham
  37. *
  38. */
  39. public class PerforceLoginCommand
  40. extends AbstractLoginCommand
  41. implements PerforceCommand
  42. {
  43. /** {@inheritDoc} */
  44. public LoginScmResult executeLoginCommand( ScmProviderRepository repo, ScmFileSet files, CommandParameters params )
  45. throws ScmException
  46. {
  47. Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir() );
  48. PerforceLoginConsumer consumer = new PerforceLoginConsumer();
  49. boolean isSuccess = false;
  50. try
  51. {
  52. String password = repo.getPassword();
  53. if ( StringUtils.isEmpty( password ) )
  54. {
  55. if ( getLogger().isInfoEnabled() )
  56. {
  57. getLogger().info( "No password found, proceeding without it." );
  58. }
  59. isSuccess = true;
  60. }
  61. else
  62. {
  63. CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
  64. int exitCode = CommandLineUtils.executeCommandLine( cl, new ByteArrayInputStream( password.getBytes() ),
  65. consumer, err );
  66. isSuccess = consumer.isSuccess();
  67. if ( !isSuccess )
  68. {
  69. String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
  70. StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
  71. msg.append( '\n' );
  72. msg.append( "Command line was:" + cmdLine );
  73. throw new CommandLineException( msg.toString() );
  74. }
  75. }
  76. }
  77. catch ( CommandLineException e )
  78. {
  79. throw new ScmException( e.getMessage(), e );
  80. }
  81. return new LoginScmResult( cl.toString(), isSuccess ? "Login successful" : "Login failed",
  82. consumer.getOutput(), isSuccess );
  83. }
  84. public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDir )
  85. {
  86. Commandline command = PerforceScmProvider.createP4Command( repo, workingDir );
  87. command.createArg().setValue( "login" );
  88. if ( !StringUtils.isEmpty( repo.getUser() ) )
  89. {
  90. command.createArg().setValue( repo.getUser() );
  91. }
  92. return command;
  93. }
  94. }