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   */
40  public class ClearCaseAddCommand
41      extends AbstractAddCommand
42      implements ClearCaseCommand
43  {
44      /** {@inheritDoc} */
45      protected ScmResult executeAddCommand( ScmProviderRepository scmProviderRepository, ScmFileSet scmFileSet,
46                                             String string, boolean b )
47          throws ScmException
48      {
49          if ( getLogger().isDebugEnabled() )
50          {
51              getLogger().debug( "executing add command..." );
52          }
53          Commandline cl = createCommandLine( scmFileSet );
54  
55          ClearCaseAddConsumer consumer = new ClearCaseAddConsumer( getLogger() );
56  
57          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
58  
59          int exitCode;
60  
61          try
62          {
63              // First we need to 'check out' the current directory
64              Commandline checkoutCurrentDirCommandLine =
65                  ClearCaseEditCommand.createCheckoutCurrentDirCommandLine( scmFileSet );
66              if ( getLogger().isDebugEnabled() )
67              {
68                  getLogger().debug(
69                                     "Executing: "
70                                         + checkoutCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath()
71                                         + ">>" + checkoutCurrentDirCommandLine.toString() );
72              }
73              exitCode = CommandLineUtils.executeCommandLine( checkoutCurrentDirCommandLine,
74                                                              new CommandLineUtils.StringStreamConsumer(), stderr );
75  
76              if ( exitCode == 0 )
77              {
78                  // Then we add the file
79                  if ( getLogger().isDebugEnabled() )
80                  {
81                      getLogger().debug(
82                                         "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>"
83                                             + cl.toString() );
84                  }
85                  exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
86  
87                  if ( exitCode == 0 )
88                  {
89                      // Then we check in the current directory again.
90                      Commandline checkinCurrentDirCommandLine =
91                          ClearCaseEditCommand.createCheckinCurrentDirCommandLine( scmFileSet );
92                      if ( getLogger().isDebugEnabled() )
93                      {
94                          getLogger().debug(
95                                             "Executing: "
96                                                 + checkinCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath()
97                                                 + ">>" + checkinCurrentDirCommandLine.toString() );
98                      }
99                      exitCode = CommandLineUtils.executeCommandLine( checkinCurrentDirCommandLine,
100                                                                     new CommandLineUtils.StringStreamConsumer(),
101                                                                     stderr );
102                 }
103             }
104         }
105         catch ( CommandLineException ex )
106         {
107             throw new ScmException( "Error while executing clearcase command.", ex );
108         }
109 
110         if ( exitCode != 0 )
111         {
112             return new StatusScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
113         }
114 
115         return new StatusScmResult( cl.toString(), consumer.getAddedFiles() );
116     }
117 
118     // ----------------------------------------------------------------------
119     //
120     // ----------------------------------------------------------------------
121 
122     public static Commandline createCommandLine( ScmFileSet scmFileSet )
123     {
124         Commandline command = new Commandline();
125 
126         File workingDirectory = scmFileSet.getBasedir();
127 
128         command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
129 
130         command.setExecutable( "cleartool" );
131 
132         command.createArg().setValue( "mkelem" );
133 
134         command.createArg().setValue( "-c" );
135 
136         command.createArg().setValue( "new file" );
137 
138         command.createArg().setValue( "-nco" );
139 
140         for ( File file : scmFileSet.getFileList() )
141         {
142             command.createArg().setValue( file.getName() );
143         }
144 
145         return command;
146     }
147 
148 }