View Javadoc
1   package org.apache.maven.scm.provider.starteam.command.update;
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.changelog.ChangeLogCommand;
26  import org.apache.maven.scm.command.update.AbstractUpdateCommand;
27  import org.apache.maven.scm.command.update.UpdateScmResult;
28  import org.apache.maven.scm.provider.ScmProviderRepository;
29  import org.apache.maven.scm.provider.starteam.command.StarteamCommand;
30  import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
31  import org.apache.maven.scm.provider.starteam.command.changelog.StarteamChangeLogCommand;
32  import org.apache.maven.scm.provider.starteam.command.checkout.StarteamCheckOutConsumer;
33  import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
34  import org.codehaus.plexus.util.StringUtils;
35  import org.codehaus.plexus.util.cli.CommandLineUtils;
36  import org.codehaus.plexus.util.cli.Commandline;
37  import org.codehaus.plexus.util.cli.DefaultConsumer;
38  import org.codehaus.plexus.util.cli.StreamConsumer;
39  
40  import java.io.File;
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  /**
45   * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
46   *
47   */
48  public class StarteamUpdateCommand
49      extends AbstractUpdateCommand
50      implements StarteamCommand
51  {
52      // ----------------------------------------------------------------------
53      // AbstractUpdateCommand Implementation
54      // ----------------------------------------------------------------------
55  
56      /** {@inheritDoc} */
57      protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version )
58          throws ScmException
59      {
60          if ( getLogger().isInfoEnabled() )
61          {
62              getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() );
63          }
64  
65          StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
66  
67          StarteamCheckOutConsumer consumer = new StarteamCheckOutConsumer( getLogger(), fileSet.getBasedir() );
68  
69          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
70  
71          List<File> updateFiles = fileSet.getFileList();
72  
73          if ( updateFiles.size() == 0 )
74          {
75              //update everything
76              Commandline cl = createCommandLine( repository, fileSet, version );
77  
78              int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
79  
80              if ( exitCode != 0 )
81              {
82                  return new UpdateScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false );
83              }
84              else
85              {
86                  //hiden feature to allow Continuous Integration machine to
87                  // delete local files. It affectively remove all build ouput as well
88                  String doDeleteLocal = System.getProperty( "maven.scm.starteam.deleteLocal" );
89  
90                  if ( "true".equalsIgnoreCase( doDeleteLocal ) )
91                  {
92                      this.deleteLocal( repository, fileSet, version );
93                  }
94              }
95          }
96          else
97          {
98              //update only interested files already on the local disk
99              for ( int i = 0; i < updateFiles.size(); ++i )
100             {
101                 File updateFile = (File) updateFiles.get( i );
102                 ScmFileSet scmFileSet = new ScmFileSet( fileSet.getBasedir(), updateFile );
103                 Commandline cl = createCommandLine( repository, scmFileSet, version );
104 
105                 int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
106 
107                 if ( exitCode != 0 )
108                 {
109                     return new UpdateScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(),
110                                                 false );
111                 }
112             }
113         }
114 
115         return new UpdateScmResult( null, consumer.getCheckedOutFiles() );
116 
117     }
118 
119     // ----------------------------------------------------------------------
120     //
121     // ----------------------------------------------------------------------
122 
123     public static Commandline createCommandLine( StarteamScmProviderRepository repo, ScmFileSet fileSet,
124                                                  ScmVersion version )
125     {
126         List<String> args = new ArrayList<String>();
127 
128         args.add( "-merge" );
129         args.add( "-neverprompt" );
130 
131         if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
132         {
133             args.add( "-vl" );
134             args.add( version.getName() );
135         }
136 
137         StarteamCommandLineUtils.addEOLOption( args );
138 
139         return StarteamCommandLineUtils.createStarteamCommandLine( "co", args, fileSet, repo );
140     }
141 
142     /** {@inheritDoc} */
143     protected ChangeLogCommand getChangeLogCommand()
144     {
145         StarteamChangeLogCommand command = new StarteamChangeLogCommand();
146 
147         command.setLogger( getLogger() );
148 
149         return command;
150     }
151 
152     private void deleteLocal( StarteamScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version )
153         throws ScmException
154     {
155         if ( fileSet.getFileList().size() != 0 )
156         {
157             return;
158         }
159 
160         Commandline cl = createDeleteLocalCommand( repo, fileSet, version );
161 
162         StreamConsumer consumer = new DefaultConsumer();
163 
164         CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
165 
166         int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
167 
168         if ( exitCode != 0 )
169         {
170             throw new ScmException( "Error executing delete-local: " + stderr.toString() );
171         }
172     }
173 
174     public static Commandline createDeleteLocalCommand( StarteamScmProviderRepository repo, ScmFileSet dir,
175                                                         ScmVersion version )
176     {
177         List<String> args = new ArrayList<String>();
178 
179         if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
180         {
181             args.add( "-cfgl" );
182             args.add( version.getName() );
183         }
184 
185         args.add( "-filter" );
186         args.add( "N" );
187 
188         return StarteamCommandLineUtils.createStarteamCommandLine( "delete-local", args, dir, repo );
189     }
190 
191 }