/*-
 * Copyright (c) 2013 Red Hat, Inc.
 *
 * 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.
 */
package org.fedoraproject.maven.config.impl;

import java.util.Collections;
import java.util.List;
import java.util.Properties;

import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.fedoraproject.maven.config.Configurator;
import org.fedoraproject.maven.config.RepositoryConfigurator;
import org.fedoraproject.maven.repository.Repository;

/**
 * @author Mikolaj Izdebski
 */
@Component( role = RepositoryConfigurator.class )
public class DefaultRepositoryConfigurator
    implements RepositoryConfigurator
{
    @Requirement
    private Configurator configurator;

    @Requirement
    private PlexusContainer container;

    @Requirement
    private Logger logger;

    @Override
    public Repository configureRepository( String repoId )
    {
        Properties properties = null;
        Xpp3Dom configurationXml = null;
        String type = null;
        List<String> artifactTypes = null;

        for ( org.fedoraproject.maven.config.Repository repository : configurator.getConfiguration().getRepositories() )
        {
            if ( repository.getId() != null && repository.getId().equals( repoId ) )
            {
                properties = repository.getProperties();
                configurationXml = (Xpp3Dom) repository.getConfiguration();
                type = repository.getType();
                artifactTypes = repository.getArtifactTypes();
                break;
            }
        }

        if ( properties == null )
            properties = new Properties();
        if ( configurationXml == null )
            configurationXml = new Xpp3Dom( "configuration" );
        if ( type == null )
            type = repoId;
        if ( artifactTypes == null )
            artifactTypes = Collections.emptyList();

        try
        {
            Repository repository = container.lookup( Repository.class, type );
            repository.configure( artifactTypes, properties, configurationXml );
            return repository;
        }
        catch ( ComponentLookupException e )
        {
            throw new RuntimeException( "Unable to lookup implementation for repository type '" + type + "'", e );
        }
    }
}

@@ -645,38 +645,8 @@
 l;%0A%0A
-import java.util.Collections;%0A
 impo
@@ -1525,24 +1525,386 @@
 er logger;%0A%0A
+    private org.fedoraproject.maven.config.Repository findDescriptor( String repoId )%0A    %7B%0A        for ( org.fedoraproject.maven.config.Repository repository : configurator.getConfiguration().getRepositories() )%0A            if ( repository.getId() != null && repository.getId().equals( repoId ) )%0A                return repository;%0A%0A        return null;%0A    %7D%0A%0A
     @Overrid
@@ -2120,38 +2120,32 @@
 = null;%0A%0A       
- for (
  org.fedoraproje
@@ -2175,72 +2175,40 @@
 ory 
-repository : configurator.getConfiguration().getRepositories()
+desc = findDescriptor( repoId
  )
+;
 %0A   
@@ -2216,117 +2216,121 @@
     
-%7B%0A            if ( repository.getId() != null && repository.getId().equals( repoId ) )%0A            %7B%0A        
+if ( desc == null )%0A            throw new RuntimeException( %22Repository '%22 + repoId + %22' is not configured.%22 );%0A%0A
     
@@ -2342,34 +2342,28 @@
 roperties = 
-repository
+desc
 .getProperti
@@ -2372,32 +2372,24 @@
 ();%0A        
-       
- 
 configuratio
@@ -2405,26 +2405,20 @@
 pp3Dom) 
-repository
+desc
 .getConf
@@ -2442,33 +2442,19 @@
     
-   
-     
 type = 
-repository
+desc
 .get
@@ -2465,32 +2465,24 @@
 ();%0A        
-      
-  
 artifactType
@@ -2485,26 +2485,20 @@
 Types = 
-repository
+desc
 .getArti
@@ -2513,55 +2513,8 @@
 s();
-%0A                break;%0A            %7D%0A        %7D
 %0A%0A  
@@ -2524,26 +2524,20 @@
    if ( 
-properties
+type
  == null
@@ -2555,36 +2555,84 @@
     
-properties = new Properties(
+throw new RuntimeException( %22Repository '%22 + repoId + %22' has missing type.%22 
 );%0A 
@@ -2736,153 +2736,8 @@
 %22 );
-%0A        if ( type == null )%0A            type = repoId;%0A        if ( artifactTypes == null )%0A            artifactTypes = Collections.emptyList();
 %0A%0A  
@@ -3066,20 +3066,18 @@
 le to lo
-okup
+ad
  impleme

Improve repository configuration