View Javadoc
1   package org.apache.maven.scm.provider.starteam.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 org.apache.maven.scm.ScmException;
23  import org.apache.maven.scm.ScmFileSet;
24  import org.apache.maven.scm.ScmVersion;
25  import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
26  import org.apache.maven.scm.command.checkin.CheckInScmResult;
27  import org.apache.maven.scm.provider.ScmProviderRepository;
28  import org.apache.maven.scm.provider.starteam.command.StarteamCommand;
29  import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
30  import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
31  import org.codehaus.plexus.util.StringUtils;
32  import org.codehaus.plexus.util.cli.CommandLineUtils;
33  import org.codehaus.plexus.util.cli.Commandline;
34  
35  import java.io.File;
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  /**
40   * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
41   * @author Olivier Lamy
42   *
43   */
44  public class StarteamCheckInCommand
45      extends AbstractCheckInCommand
46      implements StarteamCommand
47  {
48      // ----------------------------------------------------------------------
49      // AbstractCheckInCommand Implementation
50      // ----------------------------------------------------------------------
51  
52      /** {@inheritDoc} */
53      protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
54                                                        ScmVersion version )
55          throws ScmException
56      {
57  
58          //work around until maven-scm-api allow this
59          String issueType = System.getProperty( "maven.scm.issue.type" );
60          String issueValue = System.getProperty( "maven.scm.issue.value" );
61          String deprecatedIssue = System.getProperty( "maven.scm.issue" );
62  
63          if ( deprecatedIssue != null && deprecatedIssue.trim().length() > 0 )
64          {
65              issueType = "cr";
66              issueValue = deprecatedIssue;
67          }
68  
69          if ( getLogger().isInfoEnabled() )
70          {
71              getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() );
72          }
73  
74          StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
75  
76          StarteamCheckInConsumer consumer = new StarteamCheckInConsumer( getLogger(), fileSet.getBasedir() );
77  
78          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
79  
80          List<File> checkInFiles = fileSet.getFileList();
81  
82          if ( checkInFiles.isEmpty() )
83          {
84              Commandline cl = createCommandLine( repository, fileSet, message, version, issueType, issueValue );
85  
86              int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
87  
88              if ( exitCode != 0 )
89              {
90                  return new CheckInScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false );
91              }
92          }
93          else
94          {
95              //update only interested files already on the local disk
96              for ( int i = 0; i < checkInFiles.size(); ++i )
97              {
98                  ScmFileSet checkInFile = new ScmFileSet( fileSet.getBasedir(), (File) checkInFiles.get( i ) );
99  
100                 Commandline cl = createCommandLine( repository, checkInFile, message, version, issueType, issueValue );
101 
102                 int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
103 
104                 if ( exitCode != 0 )
105                 {
106                     return new CheckInScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(),
107                                                  false );
108                 }
109             }
110         }
111 
112         return new CheckInScmResult( null, consumer.getCheckedInFiles() );
113 
114     }
115 
116 
117     public static Commandline createCommandLine( StarteamScmProviderRepository repo, ScmFileSet fileSet, String message,
118                                                  ScmVersion version, String issueType, String issueValue )
119     {
120         List<String> args = new ArrayList<String>();
121 
122         if ( message != null && message.length() != 0 )
123         {
124             args.add( "-r" );
125             args.add( message );
126         }
127 
128         if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
129         {
130             args.add( "-vl" );
131             args.add( version.getName() );
132         }
133 
134         if ( issueType != null && issueType.trim().length() > 0 )
135         {
136             args.add( "-" + issueType.trim() );
137             if ( issueValue != null && issueValue.trim().length() > 0 )
138             {
139                 args.add( issueValue.trim() );
140             }
141         }
142 
143         boolean checkinDirectory = fileSet.getFileList().size() == 0;
144         if ( !checkinDirectory )
145         {
146             if ( fileSet.getFileList().size() != 0 )
147             {
148                 File subFile = (File) fileSet.getFileList().get( 0 );
149                 checkinDirectory = subFile.isDirectory();
150             }
151         }
152 
153         if ( checkinDirectory )
154         {
155             args.add( "-f" );
156             args.add( "NCI" );
157         }
158 
159         StarteamCommandLineUtils.addEOLOption( args );
160 
161         return StarteamCommandLineUtils.createStarteamCommandLine( "ci", args, fileSet, repo );
162 
163     }
164 
165 }