001    package org.apache.maven.scm.provider.bazaar.command.checkout;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.File;
023    import java.io.IOException;
024    import java.util.ArrayList;
025    import java.util.List;
026    
027    import org.apache.maven.scm.ScmException;
028    import org.apache.maven.scm.ScmFileSet;
029    import org.apache.maven.scm.ScmResult;
030    import org.apache.maven.scm.ScmVersion;
031    import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
032    import org.apache.maven.scm.command.checkout.CheckOutScmResult;
033    import org.apache.maven.scm.provider.ScmProviderRepository;
034    import org.apache.maven.scm.provider.bazaar.BazaarUtils;
035    import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
036    import org.apache.maven.scm.provider.bazaar.command.BazaarConsumer;
037    import org.apache.maven.scm.provider.bazaar.repository.BazaarScmProviderRepository;
038    import org.codehaus.plexus.util.FileUtils;
039    import org.codehaus.plexus.util.StringUtils;
040    
041    /**
042     * @author <a href="mailto:torbjorn@smorgrav.org">Torbjorn Eikli Smorgrav</a>
043     * @author Olivier Lamy
044     * @version $Id: BazaarCheckOutCommand.java 1054738 2011-01-03 20:40:39Z olamy $
045     */
046    public class BazaarCheckOutCommand
047        extends AbstractCheckOutCommand
048    {
049        /** {@inheritDoc} */
050        protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
051                                                            ScmVersion version, boolean recursive )
052            throws ScmException
053        {
054            BazaarScmProviderRepository repository = (BazaarScmProviderRepository) repo;
055            String url = repository.getURI();
056    
057            File checkoutDir = fileSet.getBasedir();
058            try
059            {
060                if ( getLogger().isInfoEnabled() )
061                {
062                    getLogger().info( "Removing " + checkoutDir );
063                }
064                FileUtils.deleteDirectory( checkoutDir );
065            }
066            catch ( IOException e )
067            {
068                throw new ScmException( "Cannot remove " + checkoutDir );
069            }
070    
071            // Do the actual checkout
072            List<String> checkoutCmd = new ArrayList<String>();
073            checkoutCmd.add( BazaarConstants.BRANCH_CMD );
074            checkoutCmd.add( url );
075            checkoutCmd.add( checkoutDir.getAbsolutePath() );
076            if ( version != null && StringUtils.isNotEmpty( version.getName() ) ) {
077                 checkoutCmd.add( BazaarConstants.REVISION_OPTION );
078                 checkoutCmd.add( "tag:" + version.getName() );
079            }
080            BazaarConsumer checkoutConsumer = new BazaarConsumer( getLogger() );
081            BazaarUtils.execute( checkoutConsumer, getLogger(), checkoutDir.getParentFile(),
082                                 (String[]) checkoutCmd.toArray( new String[0] ) );
083    
084            // Do inventory to find list of checkedout files
085            String[] inventoryCmd = new String[]{BazaarConstants.INVENTORY_CMD};
086            BazaarCheckOutConsumer consumer = new BazaarCheckOutConsumer( getLogger(), checkoutDir );
087            ScmResult result = BazaarUtils.execute( consumer, getLogger(), checkoutDir, inventoryCmd );
088            if ( !result.isSuccess() )
089            {
090                throw new ScmException( result.getProviderMessage() );
091            }
092            return new CheckOutScmResult( consumer.getCheckedOutFiles(), result );
093        }
094    }