PageRenderTime 172ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/maven-scm-providers/maven-scm-provider-vss/src/main/java/org/apache/maven/scm/provider/vss/commands/checkin/VssCheckInCommand.java

https://github.com/intelchen/maven-scm
Java | 251 lines | 157 code | 53 blank | 41 comment | 16 complexity | 2ac1b2f15dfb86e20e537887621e51e4 MD5 | raw file
  1. package org.apache.maven.scm.provider.vss.commands.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 java.io.File;
  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import org.apache.maven.scm.ScmException;
  25. import org.apache.maven.scm.ScmFile;
  26. import org.apache.maven.scm.ScmFileSet;
  27. import org.apache.maven.scm.ScmVersion;
  28. import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
  29. import org.apache.maven.scm.command.checkin.CheckInScmResult;
  30. import org.apache.maven.scm.provider.ScmProviderRepository;
  31. import org.apache.maven.scm.provider.vss.commands.VssCommandLineUtils;
  32. import org.apache.maven.scm.provider.vss.commands.VssConstants;
  33. import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
  34. import org.codehaus.plexus.util.cli.CommandLineUtils;
  35. import org.codehaus.plexus.util.cli.Commandline;
  36. /**
  37. * @author <a href="mailto:matpimenta@gmail.com">Mateus Pimenta</a>
  38. * @author Olivier Lamy
  39. * @since 1.3
  40. * @version $Id
  41. *
  42. */
  43. public class VssCheckInCommand
  44. extends AbstractCheckInCommand
  45. {
  46. /**
  47. * (non-Javadoc)
  48. *
  49. * @see org.apache.maven.scm.command.checkin.AbstractCheckInCommand# executeCheckInCommand
  50. * (org.apache.maven.scm.provider.ScmProviderRepository, org.apache.maven.scm.ScmFileSet,
  51. * java.lang.String, org.apache.maven.scm.ScmVersion)
  52. */
  53. protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repository, ScmFileSet fileSet,
  54. String message, ScmVersion scmVersion )
  55. throws ScmException
  56. {
  57. if ( getLogger().isDebugEnabled() )
  58. {
  59. getLogger().debug( "executing checkin command..." );
  60. }
  61. VssScmProviderRepository repo = (VssScmProviderRepository) repository;
  62. List<Commandline> commandLines = buildCmdLine( repo, fileSet, scmVersion );
  63. VssCheckInConsumer consumer = new VssCheckInConsumer( repo, getLogger() );
  64. // TODO handle deleted files from VSS
  65. CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
  66. int exitCode;
  67. StringBuilder sb = new StringBuilder();
  68. for ( Commandline cl : commandLines )
  69. {
  70. if ( getLogger().isDebugEnabled() )
  71. {
  72. getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
  73. }
  74. exitCode = VssCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
  75. if ( exitCode != 0 )
  76. {
  77. String error = stderr.getOutput();
  78. if ( getLogger().isDebugEnabled() )
  79. {
  80. getLogger().debug( "VSS returns error: [" + error + "] return code: [" + exitCode + "]" );
  81. }
  82. if ( error.indexOf( "A writable copy of" ) < 0 )
  83. {
  84. return new CheckInScmResult( cl.toString(), "The vss command failed.", error, false );
  85. }
  86. // print out the writable copy for manual handling
  87. if ( getLogger().isWarnEnabled() )
  88. {
  89. getLogger().warn( error );
  90. }
  91. }
  92. }
  93. return new CheckInScmResult( sb.toString(), new ArrayList<ScmFile>() );
  94. }
  95. public List<Commandline> buildCmdLine( VssScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version )
  96. throws ScmException
  97. {
  98. List<File> files = fileSet.getFileList();
  99. List<Commandline> commands = new ArrayList<Commandline>();
  100. if ( files.size() > 0 )
  101. {
  102. String base;
  103. try
  104. {
  105. base = fileSet.getBasedir().getCanonicalPath();
  106. }
  107. catch ( IOException e )
  108. {
  109. throw new ScmException( "Invalid canonical path", e );
  110. }
  111. for ( File file : files )
  112. {
  113. Commandline command = new Commandline();
  114. try
  115. {
  116. command.addSystemEnvironment();
  117. }
  118. catch ( Exception e )
  119. {
  120. throw new ScmException( "Can't add system environment.", e );
  121. }
  122. command.addEnvironment( "SSDIR", repo.getVssdir() );
  123. String ssDir = VssCommandLineUtils.getSsDir();
  124. command.setExecutable( ssDir + VssConstants.SS_EXE );
  125. command.createArg().setValue( VssConstants.COMMAND_CHECKIN );
  126. String absolute;
  127. try
  128. {
  129. absolute = file.getCanonicalPath();
  130. String relative;
  131. int index = absolute.indexOf( base );
  132. if ( index >= 0 )
  133. {
  134. relative = absolute.substring( index + base.length() );
  135. }
  136. else
  137. {
  138. relative = file.getPath();
  139. }
  140. relative = relative.replace( '\\', '/' );
  141. if ( !relative.startsWith( "/" ) )
  142. {
  143. relative = '/' + relative;
  144. }
  145. String relativeFolder = relative.substring( 0, relative.lastIndexOf( '/' ) );
  146. command.setWorkingDirectory( new File( fileSet.getBasedir().getAbsolutePath() + File.separatorChar
  147. + relativeFolder ).getCanonicalPath() );
  148. command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() + relative );
  149. }
  150. catch ( IOException e )
  151. {
  152. throw new ScmException( "Invalid canonical path", e );
  153. }
  154. //User identification to get access to vss repository
  155. if ( repo.getUserPassword() != null )
  156. {
  157. command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
  158. }
  159. // Ignore: Do not ask for input under any circumstances.
  160. command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
  161. // Ignore: Do not touch local writable files.
  162. command.createArg().setValue( VssConstants.FLAG_REPLACE_WRITABLE );
  163. commands.add( command );
  164. }
  165. }
  166. else
  167. {
  168. Commandline command = new Commandline();
  169. command.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
  170. try
  171. {
  172. command.addSystemEnvironment();
  173. }
  174. catch ( Exception e )
  175. {
  176. throw new ScmException( "Can't add system environment.", e );
  177. }
  178. command.addEnvironment( "SSDIR", repo.getVssdir() );
  179. String ssDir = VssCommandLineUtils.getSsDir();
  180. command.setExecutable( ssDir + VssConstants.SS_EXE );
  181. command.createArg().setValue( VssConstants.COMMAND_CHECKIN );
  182. command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
  183. //Display the history of an entire project list
  184. command.createArg().setValue( VssConstants.FLAG_RECURSION );
  185. //User identification to get access to vss repository
  186. if ( repo.getUserPassword() != null )
  187. {
  188. command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
  189. }
  190. // Ignore: Do not ask for input under any circumstances.
  191. command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
  192. // Ignore: Do not touch local writable files.
  193. command.createArg().setValue( VssConstants.FLAG_REPLACE_WRITABLE );
  194. commands.add( command );
  195. }
  196. return commands;
  197. }
  198. }