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 org.apache.maven.deploy.RepositoryInfo;
import org.apache.maven.deploy.DeployRequest;
import org.apache.maven.deploy.exceptions.AuthenticationException;
import org.apache.maven.deploy.exceptions.TransferFailedException;


/**
 * Deployers which for every deployment request 
 * starts a new external  process (e.g batch file, shell script) 
 * 
 * @version $Id: ExternalDeployer.java,v 1.4 2004/03/02 14:59:31 evenisse Exp $
 * @todo still have to account for differing setups for people deploying to
 *      their own sites and to the central repository.
 * @todo finich
 */
public class ExternalDeployer extends AbstractDeployer
{

    public final static String PROTOCOL = "external://";

    private String cmd = null;

    /* (non-Javadoc)
     * @see org.apache.maven.deploy.deployers.Deployer#init(org.apache.maven.deploy.HostInfo)
     */
    public void init(RepositoryInfo repoInfo) throws AuthenticationException
    {

        cmd = repoInfo.getHost();

    }

    /**
     * @see org.apache.maven.deploy.deployers.Deployer#release()
     */
    public void release()
    {
       

    }

    /**
     * @see org.apache.maven.deploy.deployers.Deployer#deploy(org.apache.maven.deploy.DeployRequest)
     */
    public void deploy(DeployRequest request) throws TransferFailedException
    {

        String[] params = { request.getSrcFile(), request.getDestFile(), };

        try
        {
            System.out.println(
                "Staring external process: '"
                    + cmd
                    + "' to deploy:'"
                    + request.getDestFile());
            Process process = Runtime.getRuntime().exec(cmd, params);
            process.waitFor();
            System.out.println("External process finished");
        }
        catch (Exception e)
        {
            throw new TransferFailedException(
                "Failed to deploy with external program",
                e);
        }

    }

}
