View Javadoc
1   package org.apache.maven.scm.provider.cvslib.command.checkout;
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.CommandParameter;
23  import org.apache.maven.scm.CommandParameters;
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.ScmResult;
27  import org.apache.maven.scm.ScmVersion;
28  import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
29  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
30  import org.apache.maven.scm.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.cvslib.command.CvsCommand;
32  import org.apache.maven.scm.provider.cvslib.command.CvsCommandUtils;
33  import org.apache.maven.scm.provider.cvslib.repository.CvsScmProviderRepository;
34  import org.codehaus.plexus.util.FileUtils;
35  import org.codehaus.plexus.util.StringUtils;
36  import org.codehaus.plexus.util.cli.Commandline;
37  
38  import java.io.IOException;
39  
40  /**
41   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse </a>
42   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
43   *
44   */
45  public abstract class AbstractCvsCheckOutCommand
46      extends AbstractCheckOutCommand
47      implements CvsCommand
48  {
49  
50      /**
51       * The overriden {@link #executeCommand(ScmProviderRepository, ScmFileSet, CommandParameters)}
52       * in this class will not call this method!
53       * <p>
54       * {@inheritDoc}
55       */
56      protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
57                                                         ScmVersion version, boolean recursive, boolean shallow )
58          throws ScmException
59      {
60          throw new UnsupportedOperationException( "Should not get here" );
61      }
62  
63      @Override
64      public ScmResult executeCommand( ScmProviderRepository repo, ScmFileSet fileSet,
65                                       CommandParameters parameters )
66          throws ScmException
67      {
68          ScmVersion version = parameters.getScmVersion( CommandParameter.SCM_VERSION, null );
69          boolean binary = parameters.getBoolean( CommandParameter.BINARY, false );
70  
71          if ( fileSet.getBasedir().exists() )
72          {
73              try
74              {
75                  FileUtils.deleteDirectory( fileSet.getBasedir() );
76              }
77              catch ( IOException e )
78              {
79                  if ( getLogger().isWarnEnabled() )
80                  {
81                      getLogger().warn( "Can't delete " + fileSet.getBasedir().getAbsolutePath(), e );
82                  }
83              }
84          }
85  
86          CvsScmProviderRepository repository = (CvsScmProviderRepository) repo;
87  
88          Commandline cl = CvsCommandUtils.getBaseCommand( "checkout", repository, fileSet );
89  
90          cl.setWorkingDirectory( fileSet.getBasedir().getParentFile().getAbsolutePath() );
91  
92          if ( binary )
93          {
94              cl.createArg().setValue( "-kb" );
95          }
96  
97          if ( version != null && !StringUtils.isEmpty( version.getName() ) )
98          {
99              cl.createArg().setValue( "-r" );
100             cl.createArg().setValue( version.getName() );
101         }
102 
103         cl.createArg().setValue( "-d" );
104 
105         cl.createArg().setValue( fileSet.getBasedir().getName() );
106 
107         cl.createArg().setValue( repository.getModule() );
108 
109         if ( getLogger().isInfoEnabled() )
110         {
111             getLogger().info( "Executing: " + cl );
112             getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
113         }
114 
115         return executeCvsCommand( cl );
116     }
117 
118     protected abstract CheckOutScmResult executeCvsCommand( Commandline cl )
119         throws ScmException;
120 }