View Javadoc

1   package org.apache.maven.scm.provider.clearcase.command.remove;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
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  import java.io.File;
36  
37  /**
38   * @author <a href="mailto:wim.deblauwe@gmail.com">Wim Deblauwe</a>
39   */
40  public class ClearCaseRemoveCommand
41      extends AbstractRemoveCommand
42      implements ClearCaseCommand
43  {
44      protected ScmResult executeRemoveCommand( ScmProviderRepository scmProviderRepository, ScmFileSet scmFileSet,
45                                                String string )
46          throws ScmException
47      {
48          getLogger().debug( "executing remove command..." );
49          Commandline cl = createCommandLine( getLogger(), scmFileSet );
50  
51          ClearCaseRemoveConsumer consumer = new ClearCaseRemoveConsumer( getLogger() );
52  
53          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
54  
55          int exitCode;
56          try
57          {
58              // First we need to 'check out' the current directory
59              Commandline checkoutCurrentDirCommandLine =
60                  ClearCaseEditCommand.createCheckoutCurrentDirCommandLine( scmFileSet );
61              getLogger().debug( "Executing: " + checkoutCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath() +
62                  ">>" + checkoutCurrentDirCommandLine.toString() );
63              exitCode = CommandLineUtils.executeCommandLine( checkoutCurrentDirCommandLine,
64                                                              new CommandLineUtils.StringStreamConsumer(), stderr );
65  
66              if ( exitCode == 0 )
67              {
68                  // Then we add the file
69                  getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
70                  exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
71  
72                  if ( exitCode == 0 )
73                  {
74                      // Then we check in the current directory again.
75                      Commandline checkinCurrentDirCommandLine =
76                          ClearCaseEditCommand.createCheckinCurrentDirCommandLine( scmFileSet );
77                      getLogger().debug( "Executing: " +
78                          checkinCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath() + ">>" +
79                          checkinCurrentDirCommandLine.toString() );
80                      exitCode = CommandLineUtils.executeCommandLine( checkinCurrentDirCommandLine,
81                                                                      new CommandLineUtils.StringStreamConsumer(),
82                                                                      stderr );
83                  }
84              }
85          }
86          catch ( CommandLineException ex )
87          {
88              throw new ScmException( "Error while executing clearcase command.", ex );
89          }
90  
91          if ( exitCode != 0 )
92          {
93              return new StatusScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
94          }
95  
96          return new StatusScmResult( cl.toString(), consumer.getRemovedFiles() );
97      }
98  
99      // ----------------------------------------------------------------------
100     //
101     // ----------------------------------------------------------------------
102 
103     public static Commandline createCommandLine( ScmLogger logger, ScmFileSet scmFileSet )
104     {
105         Commandline command = new Commandline();
106 
107         File workingDirectory = scmFileSet.getBasedir();
108 
109         command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
110 
111         command.setExecutable( "cleartool" );
112 
113         command.createArgument().setValue( "rmname" );
114 
115         command.createArgument().setValue( "-nc" );
116 
117         File[] files = scmFileSet.getFiles();
118         for ( int i = 0; i < files.length; i++ )
119         {
120             File file = files[i];
121             logger.info( "Deleting file: " + file.getAbsolutePath() );
122             command.createArgument().setValue( file.getName() );
123         }
124 
125         return command;
126     }
127 }