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