PageRenderTime 65ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/maven-scm-providers/maven-scm-provider-starteam/src/main/java/org/apache/maven/scm/provider/starteam/command/checkin/StarteamCheckInCommand.java

https://github.com/intelchen/maven-scm
Java | 165 lines | 105 code | 31 blank | 29 comment | 28 complexity | 9c0ac0f7a06d02a2ad7f20fa717b9e4e MD5 | raw file
  1. package org.apache.maven.scm.provider.starteam.command.checkin;
  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 org.apache.maven.scm.ScmException;
  21. import org.apache.maven.scm.ScmFileSet;
  22. import org.apache.maven.scm.ScmVersion;
  23. import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
  24. import org.apache.maven.scm.command.checkin.CheckInScmResult;
  25. import org.apache.maven.scm.provider.ScmProviderRepository;
  26. import org.apache.maven.scm.provider.starteam.command.StarteamCommand;
  27. import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
  28. import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
  29. import org.codehaus.plexus.util.StringUtils;
  30. import org.codehaus.plexus.util.cli.CommandLineUtils;
  31. import org.codehaus.plexus.util.cli.Commandline;
  32. import java.io.File;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. /**
  36. * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
  37. * @author Olivier Lamy
  38. *
  39. */
  40. public class StarteamCheckInCommand
  41. extends AbstractCheckInCommand
  42. implements StarteamCommand
  43. {
  44. // ----------------------------------------------------------------------
  45. // AbstractCheckInCommand Implementation
  46. // ----------------------------------------------------------------------
  47. /** {@inheritDoc} */
  48. protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
  49. ScmVersion version )
  50. throws ScmException
  51. {
  52. //work around until maven-scm-api allow this
  53. String issueType = System.getProperty( "maven.scm.issue.type" );
  54. String issueValue = System.getProperty( "maven.scm.issue.value" );
  55. String deprecatedIssue = System.getProperty( "maven.scm.issue" );
  56. if ( deprecatedIssue != null && deprecatedIssue.trim().length() > 0 )
  57. {
  58. issueType = "cr";
  59. issueValue = deprecatedIssue;
  60. }
  61. if ( getLogger().isInfoEnabled() )
  62. {
  63. getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() );
  64. }
  65. StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
  66. StarteamCheckInConsumer consumer = new StarteamCheckInConsumer( getLogger(), fileSet.getBasedir() );
  67. CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
  68. List<File> checkInFiles = fileSet.getFileList();
  69. if ( checkInFiles.isEmpty() )
  70. {
  71. Commandline cl = createCommandLine( repository, fileSet, message, version, issueType, issueValue );
  72. int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
  73. if ( exitCode != 0 )
  74. {
  75. return new CheckInScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false );
  76. }
  77. }
  78. else
  79. {
  80. //update only interested files already on the local disk
  81. for ( int i = 0; i < checkInFiles.size(); ++i )
  82. {
  83. ScmFileSet checkInFile = new ScmFileSet( fileSet.getBasedir(), (File) checkInFiles.get( i ) );
  84. Commandline cl = createCommandLine( repository, checkInFile, message, version, issueType, issueValue );
  85. int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
  86. if ( exitCode != 0 )
  87. {
  88. return new CheckInScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(),
  89. false );
  90. }
  91. }
  92. }
  93. return new CheckInScmResult( null, consumer.getCheckedInFiles() );
  94. }
  95. public static Commandline createCommandLine( StarteamScmProviderRepository repo, ScmFileSet fileSet, String message,
  96. ScmVersion version, String issueType, String issueValue )
  97. {
  98. List<String> args = new ArrayList<String>();
  99. if ( message != null && message.length() != 0 )
  100. {
  101. args.add( "-r" );
  102. args.add( message );
  103. }
  104. if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
  105. {
  106. args.add( "-vl" );
  107. args.add( version.getName() );
  108. }
  109. if ( issueType != null && issueType.trim().length() > 0 )
  110. {
  111. args.add( "-" + issueType.trim() );
  112. if ( issueValue != null && issueValue.trim().length() > 0 )
  113. {
  114. args.add( issueValue.trim() );
  115. }
  116. }
  117. boolean checkinDirectory = fileSet.getFileList().size() == 0;
  118. if ( !checkinDirectory )
  119. {
  120. if ( fileSet.getFileList().size() != 0 )
  121. {
  122. File subFile = (File) fileSet.getFileList().get( 0 );
  123. checkinDirectory = subFile.isDirectory();
  124. }
  125. }
  126. if ( checkinDirectory )
  127. {
  128. args.add( "-f" );
  129. args.add( "NCI" );
  130. }
  131. StarteamCommandLineUtils.addEOLOption( args );
  132. return StarteamCommandLineUtils.createStarteamCommandLine( "ci", args, fileSet, repo );
  133. }
  134. }