PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/maven-scm-providers/maven-scm-provider-clearcase/src/main/java/org/apache/maven/scm/provider/clearcase/command/remove/ClearCaseRemoveCommand.java

https://github.com/intelchen/maven-scm
Java | 152 lines | 101 code | 21 blank | 30 comment | 12 complexity | 67481865b5a9acbfae0524c6d2b6670c MD5 | raw file
  1. package org.apache.maven.scm.provider.clearcase.command.remove;
  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.File;
  21. import java.util.List;
  22. import org.apache.maven.scm.ScmException;
  23. import org.apache.maven.scm.ScmFileSet;
  24. import org.apache.maven.scm.ScmResult;
  25. import org.apache.maven.scm.command.remove.AbstractRemoveCommand;
  26. import org.apache.maven.scm.command.status.StatusScmResult;
  27. import org.apache.maven.scm.log.ScmLogger;
  28. import org.apache.maven.scm.provider.ScmProviderRepository;
  29. import org.apache.maven.scm.provider.clearcase.command.ClearCaseCommand;
  30. import org.apache.maven.scm.provider.clearcase.command.edit.ClearCaseEditCommand;
  31. import org.codehaus.plexus.util.cli.CommandLineException;
  32. import org.codehaus.plexus.util.cli.CommandLineUtils;
  33. import org.codehaus.plexus.util.cli.Commandline;
  34. /**
  35. * @author <a href="mailto:wim.deblauwe@gmail.com">Wim Deblauwe</a>
  36. * @author Olivier Lamy
  37. *
  38. */
  39. public class ClearCaseRemoveCommand
  40. extends AbstractRemoveCommand
  41. implements ClearCaseCommand
  42. {
  43. /** {@inheritDoc} */
  44. protected ScmResult executeRemoveCommand( ScmProviderRepository scmProviderRepository, ScmFileSet scmFileSet,
  45. String string )
  46. throws ScmException
  47. {
  48. if ( getLogger().isDebugEnabled() )
  49. {
  50. getLogger().debug( "executing remove command..." );
  51. }
  52. Commandline cl = createCommandLine( getLogger(), scmFileSet );
  53. ClearCaseRemoveConsumer consumer = new ClearCaseRemoveConsumer( getLogger() );
  54. CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
  55. int exitCode;
  56. try
  57. {
  58. // First we need to 'check out' the current directory
  59. Commandline checkoutCurrentDirCommandLine =
  60. ClearCaseEditCommand.createCheckoutCurrentDirCommandLine( scmFileSet );
  61. if ( getLogger().isDebugEnabled() )
  62. {
  63. getLogger().debug(
  64. "Executing: "
  65. + checkoutCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath()
  66. + ">>" + checkoutCurrentDirCommandLine.toString() );
  67. }
  68. exitCode =
  69. CommandLineUtils.executeCommandLine( checkoutCurrentDirCommandLine,
  70. new CommandLineUtils.StringStreamConsumer(), stderr );
  71. if ( exitCode == 0 )
  72. {
  73. // Then we add the file
  74. if ( getLogger().isDebugEnabled() )
  75. {
  76. getLogger().debug(
  77. "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>"
  78. + cl.toString() );
  79. }
  80. exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
  81. if ( exitCode == 0 )
  82. {
  83. // Then we check in the current directory again.
  84. Commandline checkinCurrentDirCommandLine =
  85. ClearCaseEditCommand.createCheckinCurrentDirCommandLine( scmFileSet );
  86. if ( getLogger().isDebugEnabled() )
  87. {
  88. getLogger().debug(
  89. "Executing: "
  90. + checkinCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath()
  91. + ">>" + checkinCurrentDirCommandLine.toString() );
  92. }
  93. exitCode = CommandLineUtils.executeCommandLine( checkinCurrentDirCommandLine,
  94. new CommandLineUtils.StringStreamConsumer(),
  95. stderr );
  96. }
  97. }
  98. }
  99. catch ( CommandLineException ex )
  100. {
  101. throw new ScmException( "Error while executing clearcase command.", ex );
  102. }
  103. if ( exitCode != 0 )
  104. {
  105. return new StatusScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
  106. }
  107. return new StatusScmResult( cl.toString(), consumer.getRemovedFiles() );
  108. }
  109. // ----------------------------------------------------------------------
  110. //
  111. // ----------------------------------------------------------------------
  112. public static Commandline createCommandLine( ScmLogger logger, ScmFileSet scmFileSet )
  113. {
  114. Commandline command = new Commandline();
  115. File workingDirectory = scmFileSet.getBasedir();
  116. command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
  117. command.setExecutable( "cleartool" );
  118. command.createArg().setValue( "rmname" );
  119. command.createArg().setValue( "-nc" );
  120. List<File> files = scmFileSet.getFileList();
  121. for ( File file : files )
  122. {
  123. if ( logger.isInfoEnabled() )
  124. {
  125. logger.info( "Deleting file: " + file.getAbsolutePath() );
  126. }
  127. command.createArg().setValue( file.getName() );
  128. }
  129. return command;
  130. }
  131. }