View Javadoc
1   package org.apache.maven.scm.provider.clearcase.command.tag;
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.ScmTagParameters;
29  import org.apache.maven.scm.command.tag.AbstractTagCommand;
30  import org.apache.maven.scm.command.tag.TagScmResult;
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.checkin.ClearCaseCheckInConsumer;
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 ClearCaseTagCommand
44      extends AbstractTagCommand
45      implements ClearCaseCommand
46  {
47      
48      protected ScmResult executeTagCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String tag,
49                                             String message )
50          throws ScmException
51      {
52          return executeTagCommand( scmProviderRepository, fileSet, tag, new ScmTagParameters( message ) );
53      }
54      
55      /** {@inheritDoc} */
56      protected ScmResult executeTagCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String tag,
57                                             ScmTagParameters scmTagParameters )
58          throws ScmException
59      {
60          if ( getLogger().isDebugEnabled() )
61          {
62              getLogger().debug( "executing tag command..." );
63          }
64          Commandline cl = createCommandLine( fileSet, tag );
65  
66          ClearCaseCheckInConsumer consumer = new ClearCaseCheckInConsumer( getLogger() );
67  
68          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
69  
70          int exitCode;
71  
72          try
73          {
74              if ( getLogger().isDebugEnabled() )
75              {
76                  getLogger().debug( "Creating label: " + tag );
77              }
78              Commandline newLabelCommandLine = createNewLabelCommandLine( fileSet, tag );
79              if ( getLogger().isDebugEnabled() )
80              {
81                  getLogger().debug(
82                                     "Executing: " + newLabelCommandLine.getWorkingDirectory().getAbsolutePath()
83                                         + ">>" + newLabelCommandLine.toString() );
84              }
85              exitCode = CommandLineUtils.executeCommandLine( newLabelCommandLine,
86                                                              new CommandLineUtils.StringStreamConsumer(), stderr );
87  
88              if ( exitCode == 0 )
89              {
90                  getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
91                  exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
92              }
93          }
94          catch ( CommandLineException ex )
95          {
96              throw new ScmException( "Error while executing clearcase command.", ex );
97          }
98  
99          if ( exitCode != 0 )
100         {
101             return new TagScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
102         }
103 
104         return new TagScmResult( cl.toString(), consumer.getCheckedInFiles() );
105     }
106 
107     // ----------------------------------------------------------------------
108     //
109     // ----------------------------------------------------------------------
110 
111     public static Commandline createCommandLine( ScmFileSet scmFileSet, String tag )
112     {
113         Commandline command = new Commandline();
114 
115         File workingDirectory = scmFileSet.getBasedir();
116 
117         command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
118 
119         command.setExecutable( "cleartool" );
120 
121         command.createArg().setValue( "mklabel" );
122         List<File> files = scmFileSet.getFileList();
123         if ( files.isEmpty() )
124         {
125             command.createArg().setValue( "-recurse" );
126         }
127         command.createArg().setValue( tag );
128 
129         if ( files.size() > 0 )
130         {
131             for ( File file : files )
132             {
133                 command.createArg().setValue( file.getName() );
134             }
135         }
136         else
137         {
138             command.createArg().setValue( "." );
139         }
140 
141         return command;
142     }
143 
144     private static Commandline createNewLabelCommandLine( ScmFileSet scmFileSet, String tag )
145     {
146         Commandline command = new Commandline();
147 
148         File workingDirectory = scmFileSet.getBasedir();
149 
150         command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
151 
152         command.setExecutable( "cleartool" );
153 
154         command.createArg().setValue( "mklbtype" );
155         command.createArg().setValue( "-nc" );
156         command.createArg().setValue( tag );
157 
158         return command;
159     }
160 }