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