package org.apache.maven.artifact.deployer;

/* ====================================================================
 *   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.net.MalformedURLException;

import org.apache.maven.deploy.RepositoryInfo;
import org.apache.maven.project.Project;

/**
 * 
 * Perform mapping between project's properties and attributes of DeployRequest class.
 * 
 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a> 
 * @version $Id: RepositoryInfoBuilder.java,v 1.2 2004/03/02 14:59:31 evenisse Exp $
 */
public class RepositoryInfoBuilder
{

    /**
     * 
     * @param project. Will lookup the properties in  the context of this project 
     * @param repository The name (alias) of the repository 
     *       like </i>repo1</i> taken from property: <i>maven.deploy.repos= repo1, repo2</i>
     * @return Deploy request 
     * @throws WrongParameterException
     */
    public static RepositoryInfo getRepositoryInfo(
        Project project,
        String repository)
        throws MalformedURLException
    {

        RepositoryInfo repoInfo = new RepositoryInfo();
        repoInfo.setRepositoryAlias(repository);
        String url =
            (String) project.getContext().getVariable(
                "maven.repo." + repository);
        String username =
            (String) project.getContext().getVariable(
                "maven.repo." + repository + ".username");

        String password =
            (String) project.getContext().getVariable(
                "maven.repo." + repository + ".password");

        String passphrase =
            (String) project.getContext().getVariable(
                "maven.repo." + repository + ".passphrase");

        String privateKey =
            (String) project.getContext().getVariable(
                "maven.repo." + repository + ".privatekey");

        String dir =
            (String) project.getContext().getVariable(
                "maven.repo." + repository + ".directory");

        String port =
            (String) project.getContext().getVariable(
                "maven.repo." + repository + ".port");

        String remoteGroup =
            (String) project.getContext().getVariable(
                "maven.repo." + repository + ".group");

        String proxyHost = (String) project.getContext().getProxyHost();
        String proxyUser = (String) project.getContext().getProxyUserName();

        String proxyPassword = (String) project.getContext().getProxyPassword();

        String proxyPort = (String) project.getContext().getProxyPort();

        repoInfo.setUserName(username);
        repoInfo.setPassword(password);
        repoInfo.setPassphrase(passphrase);
        repoInfo.setPrivateKey(privateKey);
        repoInfo.setGroup(remoteGroup);
        repoInfo.setUrl(url);
        repoInfo.setProxyHost(proxyHost);
        repoInfo.setProxyUserName(proxyUser);
        repoInfo.setProxyPassword(proxyPassword);
        if (port != null)
        {
            try
            {

                repoInfo.setPort(Integer.parseInt(port));
            }
            catch (Exception e)
            {
                throw new MalformedURLException(
                    "maven.repo." + repository + ".port should be an integer");
            }
        }
        if (proxyPort != null)
        {
            try
            {
                repoInfo.setProxyPort(
                    Integer.parseInt(proxyPort.trim()));
            }
            catch (Exception e)
            {
                throw new MalformedURLException(
                    "maven.repo."
                        + repository
                        + ".proxy.port should be an integer");
            }
        }

        repoInfo.setBasedir(dir);
        return repoInfo;

    }

}
