package org.apache.maven.deploy.deployers;

/* ====================================================================
 *   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.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.maven.deploy.RepositoryInfo;
import org.apache.maven.deploy.DeployRequest;
import org.apache.maven.deploy.exceptions.TransferFailedException;

/**
 * Deployer which deploys to directory visible in local
 * file system.
 * 
 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a> 
 * @version $Id: FileDeployer.java,v 1.12 2004/03/02 14:59:31 evenisse Exp $
 */
public class FileDeployer extends AbstractDeployer
{

    private RepositoryInfo repoInfo;

    /** Protocol understandable by this deployer*/
    public final static String PROTOCOL = "file://";

    /**
     * Do nothing
     */
    public void init(RepositoryInfo repoInfo)
    {
        this.repoInfo = repoInfo;
    }

    /**
     *  Do nothing
     */
    public void release()
    {
    }

    /**
     * @see org.apache.maven.fetch.fetchers.Fetcher#fetchUrl(java.lang.String, java.io.OutputStream)
     */
    public void deploy(DeployRequest request) throws TransferFailedException
    {
        try
        {
            File srcFile = new File(request.getSrcFile());

            File destFile =
                new File(
                    repoInfo.getHost(),
                    repoInfo.getBasedir());
            destFile = new File(destFile, request.dirname());
            if (!destFile.exists())
            {
                destFile.mkdirs();
            }
            destFile = new File(destFile, request.filename());
            FileUtils.copyFile(srcFile, destFile);
        }
        catch (IOException e)
        {
            throw new TransferFailedException(
                "Cannot copy file: " + e.getMessage());
        }
    }
}
