001package org.eclipse.aether.internal.transport.wagon;
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 static java.util.Objects.requireNonNull;
023
024import org.apache.maven.wagon.Wagon;
025import org.codehaus.plexus.PlexusContainer;
026import org.codehaus.plexus.classworlds.realm.ClassRealm;
027import org.codehaus.plexus.component.annotations.Component;
028import org.codehaus.plexus.component.annotations.Requirement;
029import org.codehaus.plexus.component.configurator.AbstractComponentConfigurator;
030import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
031import org.codehaus.plexus.component.configurator.ConfigurationListener;
032import org.codehaus.plexus.component.configurator.converters.composite.ObjectWithFieldsConverter;
033import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
034import org.codehaus.plexus.configuration.PlexusConfiguration;
035import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
036import org.codehaus.plexus.util.xml.Xpp3Dom;
037import org.eclipse.aether.transport.wagon.WagonConfigurator;
038
039/**
040 * A wagon configurator based on the Plexus component configuration framework.
041 */
042@Component( role = WagonConfigurator.class, hint = "plexus" )
043public class PlexusWagonConfigurator
044    implements WagonConfigurator
045{
046
047    @Requirement
048    private PlexusContainer container;
049
050    /**
051     * Creates an uninitialized wagon configurator.
052     * 
053     * @noreference This constructor only supports the Plexus IoC container and should not be called directly by
054     *              clients.
055     */
056    public PlexusWagonConfigurator()
057    {
058        // enables no-arg constructor
059    }
060
061    /**
062     * Creates a wagon configurator using the specified Plexus container.
063     *
064     * @param container The Plexus container instance to use, must not be {@code null}.
065     */
066    public PlexusWagonConfigurator( PlexusContainer container )
067    {
068        this.container = requireNonNull( container, "plexus container cannot be null" );
069    }
070
071    public void configure( Wagon wagon, Object configuration )
072        throws Exception
073    {
074        PlexusConfiguration config = null;
075        if ( configuration instanceof PlexusConfiguration )
076        {
077            config = (PlexusConfiguration) configuration;
078        }
079        else if ( configuration instanceof Xpp3Dom )
080        {
081            config = new XmlPlexusConfiguration( (Xpp3Dom) configuration );
082        }
083        else if ( configuration == null )
084        {
085            return;
086        }
087        else
088        {
089            throw new IllegalArgumentException( "unexpected configuration type: " + configuration.getClass().getName() );
090        }
091
092        WagonComponentConfigurator configurator = new WagonComponentConfigurator();
093
094        configurator.configureComponent( wagon, config, container.getContainerRealm() );
095    }
096
097    static class WagonComponentConfigurator
098        extends AbstractComponentConfigurator
099    {
100
101        @Override
102        public void configureComponent( Object component, PlexusConfiguration configuration,
103                                        ExpressionEvaluator expressionEvaluator, ClassRealm containerRealm,
104                                        ConfigurationListener listener )
105            throws ComponentConfigurationException
106        {
107            ObjectWithFieldsConverter converter = new ObjectWithFieldsConverter();
108
109            converter.processConfiguration( converterLookup, component, containerRealm, configuration,
110                                            expressionEvaluator, listener );
111        }
112
113    }
114
115}