001    package org.codehaus.mojo.wagon.shared;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
005     * agreements. See the NOTICE file distributed with this work for additional information regarding
006     * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance with the License. You may obtain a
008     * copy of the License at
009     * 
010     * http://www.apache.org/licenses/LICENSE-2.0
011     * 
012     * Unless required by applicable law or agreed to in writing, software distributed under the License
013     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
014     * or implied. See the License for the specific language governing permissions and limitations under
015     * the License.
016     */
017    
018    import java.io.File;
019    import java.io.IOException;
020    
021    import org.apache.maven.plugin.logging.Log;
022    import org.apache.maven.shared.model.fileset.FileSet;
023    import org.apache.maven.wagon.Wagon;
024    import org.apache.maven.wagon.WagonException;
025    import org.codehaus.plexus.util.FileUtils;
026    
027    /**
028     * Copy a set of file from a wagon repo to another wagon repo
029     * 
030     * @plexus.component role="org.codehaus.mojo.wagon.shared.WagonCopy" role-hint="default"
031     */
032    
033    public class DefaultWagonCopy
034        implements WagonCopy
035    {
036        /**
037         * @plexus.requirement role="org.codehaus.mojo.wagon.shared.WagonDownload"
038         */
039        private WagonDownload downloader;
040    
041        /**
042         * @plexus.requirement role="org.codehaus.mojo.wagon.shared.WagonUpload"
043         */
044        private WagonUpload uploader;
045    
046        public void copy( Wagon src, WagonFileSet wagonFileSet, Wagon target, boolean optimize, Log logger )
047            throws WagonException, IOException
048        {
049            if ( wagonFileSet == null )
050            {
051                wagonFileSet = new WagonFileSet();
052            }
053            
054            boolean removeDownloadDir = false;
055    
056            if ( wagonFileSet.getDownloadDirectory() == null )
057            {
058                File downloadSrcDir = File.createTempFile( "wagon", "wagon" );
059                downloadSrcDir.delete();
060                wagonFileSet.setDownloadDirectory( downloadSrcDir );
061                removeDownloadDir = true;
062            }
063    
064            try
065            {
066                this.downloader.download( src, wagonFileSet, logger );
067    
068                FileSet localFileSet = new FileSet();
069                localFileSet.setDirectory( wagonFileSet.getDownloadDirectory().getAbsolutePath() );
070                localFileSet.setOutputDirectory( wagonFileSet.getOutputDirectory() );
071                
072                this.uploader.upload( target, localFileSet, optimize, logger );
073            }
074            finally
075            {
076                if ( removeDownloadDir )
077                {
078                    FileUtils.deleteDirectory( wagonFileSet.getDownloadDirectory() );
079                }
080            }
081    
082        }
083    }