View Javadoc
1   package org.apache.maven.scm.provider.clearcase.command.checkin;
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.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.clearcase.command.ClearCaseCommand;
32  import org.codehaus.plexus.util.cli.CommandLineException;
33  import org.codehaus.plexus.util.cli.CommandLineUtils;
34  import org.codehaus.plexus.util.cli.Commandline;
35  
36  /**
37   * @author <a href="mailto:wim.deblauwe@gmail.com">Wim Deblauwe</a>
38   * @author Olivier Lamy
39   *
40   */
41  public class ClearCaseCheckInCommand
42      extends AbstractCheckInCommand
43      implements ClearCaseCommand
44  {
45      // ----------------------------------------------------------------------
46      // AbstractCheckOutCommand Implementation
47      // ----------------------------------------------------------------------
48  
49      /** {@inheritDoc} */
50      protected CheckInScmResult executeCheckInCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet,
51                                                        String message, ScmVersion version )
52          throws ScmException
53      {
54          if ( getLogger().isDebugEnabled() )
55          {
56              getLogger().debug( "executing checkin command..." );
57          }
58          Commandline cl = createCommandLine( fileSet, message );
59  
60          ClearCaseCheckInConsumer consumer = new ClearCaseCheckInConsumer( getLogger() );
61  
62          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
63  
64          int exitCode;
65  
66          try
67          {
68              if ( getLogger().isDebugEnabled() )
69              {
70                  getLogger().debug(
71                                     "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>"
72                                         + cl.toString() );
73              }
74              exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
75          }
76          catch ( CommandLineException ex )
77          {
78              throw new ScmException( "Error while executing clearcase command.", ex );
79          }
80  
81          if ( exitCode != 0 )
82          {
83              return new CheckInScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
84          }
85  
86          return new CheckInScmResult( cl.toString(), consumer.getCheckedInFiles() );
87      }
88  
89      // ----------------------------------------------------------------------
90      //
91      // ----------------------------------------------------------------------
92  
93      public static Commandline createCommandLine( ScmFileSet scmFileSet, String message )
94          throws ScmException
95      {
96          Commandline command = new Commandline();
97  
98          File workingDirectory = scmFileSet.getBasedir();
99  
100         command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
101 
102         command.setExecutable( "cleartool" );
103 
104         command.createArg().setValue( "ci" );
105 
106         if ( message != null )
107         {
108             command.createArg().setValue( "-c" );
109             command.createArg().setLine( "\"" + message + "\"" );
110         }
111         else
112         {
113             command.createArg().setValue( "-nc" );
114         }
115 
116         List<File> files = scmFileSet.getFileList();
117         if ( files.isEmpty() )
118         {
119             throw new ScmException( "There are no files in the fileset to check in!" );
120         }
121         for ( File file : files )
122         {
123             command.createArg().setValue( file.getAbsolutePath() );
124         }
125 
126         return command;
127     }
128 }