001package org.apache.maven.wagon.tck.http;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.wagon.Wagon;
023import org.codehaus.plexus.PlexusConstants;
024import org.codehaus.plexus.PlexusContainer;
025import org.codehaus.plexus.classworlds.realm.ClassRealm;
026import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
027import org.codehaus.plexus.component.configurator.ComponentConfigurator;
028import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
029import org.codehaus.plexus.configuration.PlexusConfiguration;
030import org.codehaus.plexus.context.Context;
031import org.codehaus.plexus.context.ContextException;
032import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
033
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037/**
038 * 
039 */
040public class WagonTestCaseConfigurator
041    implements Contextualizable
042{
043    private static final String UNSUPPORTED_ELEMENT = "unsupported";
044
045    private PlexusConfiguration useCaseConfigs;
046
047    private ComponentConfigurator configurator;
048
049    private ClassRealm realm;
050
051    private String wagonHint;
052
053    private static Logger logger = LoggerFactory.getLogger( WagonTestCaseConfigurator.class );
054
055    public boolean isSupported( final String useCaseId )
056    {
057        if ( useCaseConfigs != null )
058        {
059            PlexusConfiguration config = useCaseConfigs.getChild( useCaseId, false );
060
061            if ( config != null && config.getChild( UNSUPPORTED_ELEMENT, false ) != null )
062            {
063                logger.info( "Test case '" + useCaseId + "' is marked as unsupported by this wagon." );
064                return false;
065            }
066        }
067
068        return true;
069    }
070
071    public boolean configureWagonForTest( final Wagon wagon, final String useCaseId )
072        throws ComponentConfigurationException
073    {
074        if ( useCaseConfigs != null )
075        {
076            PlexusConfiguration config = useCaseConfigs.getChild( useCaseId, false );
077
078            if ( config != null )
079            {
080                if ( config.getChild( UNSUPPORTED_ELEMENT, false ) != null )
081                {
082                    logger.error( "Test case '" + useCaseId + "' is marked as unsupported by this wagon." );
083                    return false;
084                }
085                else
086                {
087                    logger.info( "Configuring wagon for test case: " + useCaseId + " with:\n\n" + config );
088                    configurator.configureComponent( wagon, useCaseConfigs.getChild( useCaseId, false ), realm );
089                }
090            }
091            else
092            {
093                logger.info( "No wagon configuration found for test case: " + useCaseId );
094            }
095        }
096        else
097        {
098            logger.info( "No test case configurations found." );
099        }
100
101        return true;
102    }
103
104    public void contextualize( final Context context )
105        throws ContextException
106    {
107        PlexusContainer container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
108        this.realm = container.getContainerRealm();
109        try
110        {
111            configurator = (ComponentConfigurator) container.lookup( ComponentConfigurator.ROLE );
112        }
113        catch ( ComponentLookupException e )
114        {
115            throw new ContextException( "Failed to lookup component configurator: " + e.getMessage(), e );
116        }
117    }
118
119    public PlexusConfiguration getUseCaseConfigs()
120    {
121        return useCaseConfigs;
122    }
123
124    public void setUseCaseConfigs( final PlexusConfiguration useCaseConfigs )
125    {
126        this.useCaseConfigs = useCaseConfigs;
127    }
128
129    public String getWagonHint()
130    {
131        return wagonHint;
132    }
133
134    public void setWagonHint( final String wagonHint )
135    {
136        this.wagonHint = wagonHint;
137    }
138
139}