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.log4j.Logger;
023import org.apache.maven.wagon.Wagon;
024import org.codehaus.plexus.PlexusConstants;
025import org.codehaus.plexus.PlexusContainer;
026import org.codehaus.plexus.classworlds.realm.ClassRealm;
027import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
028import org.codehaus.plexus.component.configurator.ComponentConfigurator;
029import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
030import org.codehaus.plexus.configuration.PlexusConfiguration;
031import org.codehaus.plexus.context.Context;
032import org.codehaus.plexus.context.ContextException;
033import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
034
035/**
036 * 
037 */
038public class WagonTestCaseConfigurator
039    implements Contextualizable
040{
041    private static final String UNSUPPORTED_ELEMENT = "unsupported";
042
043    private PlexusConfiguration useCaseConfigs;
044
045    private ComponentConfigurator configurator;
046
047    private ClassRealm realm;
048
049    private String wagonHint;
050
051    private static Logger logger = Logger.getLogger( WagonTestCaseConfigurator.class );
052
053    public boolean isSupported( final String useCaseId )
054    {
055        if ( useCaseConfigs != null )
056        {
057            PlexusConfiguration config = useCaseConfigs.getChild( useCaseId, false );
058
059            if ( config != null && config.getChild( UNSUPPORTED_ELEMENT, false ) != null )
060            {
061                logger.info( "Test case '" + useCaseId + "' is marked as unsupported by this wagon." );
062                return false;
063            }
064        }
065
066        return true;
067    }
068
069    public boolean configureWagonForTest( final Wagon wagon, final String useCaseId )
070        throws ComponentConfigurationException
071    {
072        if ( useCaseConfigs != null )
073        {
074            PlexusConfiguration config = useCaseConfigs.getChild( useCaseId, false );
075
076            if ( config != null )
077            {
078                if ( config.getChild( UNSUPPORTED_ELEMENT, false ) != null )
079                {
080                    logger.error( "Test case '" + useCaseId + "' is marked as unsupported by this wagon." );
081                    return false;
082                }
083                else
084                {
085                    logger.info( "Configuring wagon for test case: " + useCaseId + " with:\n\n" + config );
086                    configurator.configureComponent( wagon, useCaseConfigs.getChild( useCaseId, false ), realm );
087                }
088            }
089            else
090            {
091                logger.info( "No wagon configuration found for test case: " + useCaseId );
092            }
093        }
094        else
095        {
096            logger.info( "No test case configurations found." );
097        }
098
099        return true;
100    }
101
102    public void contextualize( final Context context )
103        throws ContextException
104    {
105        PlexusContainer container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
106        this.realm = container.getContainerRealm();
107        try
108        {
109            configurator = (ComponentConfigurator) container.lookup( ComponentConfigurator.ROLE );
110        }
111        catch ( ComponentLookupException e )
112        {
113            throw new ContextException( "Failed to lookup component configurator: " + e.getMessage(), e );
114        }
115    }
116
117    public PlexusConfiguration getUseCaseConfigs()
118    {
119        return useCaseConfigs;
120    }
121
122    public void setUseCaseConfigs( final PlexusConfiguration useCaseConfigs )
123    {
124        this.useCaseConfigs = useCaseConfigs;
125    }
126
127    public String getWagonHint()
128    {
129        return wagonHint;
130    }
131
132    public void setWagonHint( final String wagonHint )
133    {
134        this.wagonHint = wagonHint;
135    }
136
137}