View Javadoc

1   package org.apache.maven.scm.provider.clearcase.command.add;
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.add.AbstractAddCommand;
26  import org.apache.maven.scm.command.status.StatusScmResult;
27  import org.apache.maven.scm.provider.ScmProviderRepository;
28  import org.apache.maven.scm.provider.clearcase.command.ClearCaseCommand;
29  import org.apache.maven.scm.provider.clearcase.command.edit.ClearCaseEditCommand;
30  import org.codehaus.plexus.util.cli.CommandLineException;
31  import org.codehaus.plexus.util.cli.CommandLineUtils;
32  import org.codehaus.plexus.util.cli.Commandline;
33  
34  import java.io.File;
35  
36  /**
37   * @author <a href="mailto:wim.deblauwe@gmail.com">Wim Deblauwe</a>
38   */
39  public class ClearCaseAddCommand
40      extends AbstractAddCommand
41      implements ClearCaseCommand
42  {
43      protected ScmResult executeAddCommand( ScmProviderRepository scmProviderRepository, ScmFileSet scmFileSet,
44                                             String string, boolean b )
45          throws ScmException
46      {
47          getLogger().debug( "executing add command..." );
48          Commandline cl = createCommandLine( scmFileSet );
49  
50          ClearCaseAddConsumer consumer = new ClearCaseAddConsumer( getLogger() );
51  
52          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
53  
54          int exitCode;
55  
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.getAddedFiles() );
97      }
98  
99      // ----------------------------------------------------------------------
100     //
101     // ----------------------------------------------------------------------
102 
103     public static Commandline createCommandLine( 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( "mkelem" );
114 
115         command.createArgument().setValue( "-c" );
116 
117         command.createArgument().setValue( "new file" );
118 
119         command.createArgument().setValue( "-nco" );
120 
121         File[] files = scmFileSet.getFiles();
122         for ( int i = 0; i < files.length; i++ )
123         {
124             File file = files[i];
125             command.createArgument().setValue( file.getName() );
126         }
127 
128         return command;
129     }
130 
131 }