package org.apache.maven.deploy;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * ====================================================================
 */

import java.util.Iterator;
import java.util.List;

import org.apache.maven.deploy.deployers.Deployer;
import org.apache.maven.deploy.deployers.ExternalDeployer;
import org.apache.maven.deploy.deployers.FileDeployer;
import org.apache.maven.deploy.deployers.FtpDeployer;
import org.apache.maven.deploy.deployers.HttpDeployer;
import org.apache.maven.deploy.deployers.SFtpDeployer;
import org.apache.maven.deploy.deployers.ScpDeployer;
import org.apache.maven.deploy.exceptions.AuthenticationException;
import org.apache.maven.deploy.exceptions.TransferFailedException;
import org.apache.maven.deploy.exceptions.UnsupportedProtocolException;

/**
 * Utility class
 * 
 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a> 
 * @version $Id: DeployTool.java,v 1.10 2004/03/02 14:59:31 evenisse Exp $
 */
public class DeployTool
{
    /** Current Version **/
    public static final String VERSION = "1.0-dev";

    /**
     * Returns deployer which is able to deploy to repository
     * described by host info.   
     * @param repoInfo 
     * @throws DeployException When no deployer is supporting given protocol
     */
    public Deployer getDeployer(RepositoryInfo repoInfo)
        throws IllegalArgumentException, UnsupportedProtocolException
    {
        String url = repoInfo.getUrl();
        Deployer deployer = null;

        if (url == null || url.length() == 1)
        {
            throw new UnsupportedProtocolException(
                "No URL was provided repository: "
                    + repoInfo.getRepositoryAlias());
        }
        if (url.startsWith(HttpDeployer.PROTOCOL))
        {
            deployer = new HttpDeployer();
        }
        if (url.startsWith(FileDeployer.PROTOCOL))
        {
            deployer = new FileDeployer();
        }
        if (url.startsWith(FtpDeployer.PROTOCOL))
        {
            deployer = new FtpDeployer();
        }
        if (url.startsWith(SFtpDeployer.PROTOCOL))
        {
            deployer = new SFtpDeployer();
        }
        if (url.startsWith(ScpDeployer.PROTOCOL))
        {
            deployer = new ScpDeployer();
        }
        if (url.startsWith(ExternalDeployer.PROTOCOL))
        {
            deployer = new ExternalDeployer();
        }
        if (deployer == null)
        {
            throw new UnsupportedProtocolException(url);
        }

        return deployer;
    }

    
    /**
     * Deploy given list of files to remote repository
     * @param repoInfo the 
     * @param srcFiles the list containing local filenames which will be deployed to remote host 
     * @param destFiles the list contating the remote filenames. The size of this list 
     *         must match the size of srcFiles list  
     * @throws TransferFailedException when copying operation fails
     * @throws UnsupportedProtocolException when no deployer is capable to deploy to given host 
     * @throws AuthenticationException when authentication related problem occures
     */
    public void deploy(
        RepositoryInfo repoInfo,
        List srcFiles,
        List destFiles)
        throws
            TransferFailedException,
            UnsupportedProtocolException,            
            AuthenticationException
    {

        if (srcFiles.size() != destFiles.size())
        {
            String msg = "Lengths of the lists should be the same";
            throw new IllegalArgumentException(msg);
        }
        Deployer deployer = getDeployer(repoInfo);

        try
        {
            deployer.init(repoInfo);
            Iterator srcIterator = srcFiles.iterator();
            Iterator destIterator = destFiles.iterator();
            while (srcIterator.hasNext())
            {
                String srcFile = (String) srcIterator.next();
                String destFile = (String) destIterator.next();
                DeployRequest request = new DeployRequest(srcFile, destFile);
                System.out.println("Deploying: " + srcFile + "-->" + destFile);
                deployer.deploy(request);
            }
        }
        finally
        {
            try
            {
                deployer.release();
            }
            catch (Exception e)
            {
            }
        }
    }
}