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.configurator.AbstractComponentConfigurator;
028import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
029import org.codehaus.plexus.component.configurator.ConfigurationListener;
030import org.codehaus.plexus.component.configurator.converters.composite.ObjectWithFieldsConverter;
031import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
032import org.codehaus.plexus.configuration.PlexusConfiguration;
033import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
034import org.codehaus.plexus.util.xml.Xpp3Dom;
035import org.eclipse.aether.transport.wagon.WagonConfigurator;
036
037import javax.inject.Inject;
038import javax.inject.Named;
039import javax.inject.Singleton;
040
041/**
042 * A wagon configurator based on the Plexus component configuration framework.
043 */
044@Named ( "plexus" )
045@Singleton
046public class PlexusWagonConfigurator
047    implements WagonConfigurator
048{
049    private PlexusContainer container;
050
051    /**
052     * Creates a wagon configurator using the specified Plexus container.
053     *
054     * @param container The Plexus container instance to use, must not be {@code null}.
055     */
056    @Inject
057    public PlexusWagonConfigurator( final PlexusContainer container )
058    {
059        this.container = requireNonNull( container, "plexus container cannot be null" );
060    }
061
062    public void configure( Wagon wagon, Object configuration )
063        throws Exception
064    {
065        PlexusConfiguration config;
066        if ( configuration instanceof PlexusConfiguration )
067        {
068            config = (PlexusConfiguration) configuration;
069        }
070        else if ( configuration instanceof Xpp3Dom )
071        {
072            config = new XmlPlexusConfiguration( (Xpp3Dom) configuration );
073        }
074        else if ( configuration == null )
075        {
076            return;
077        }
078        else
079        {
080            throw new IllegalArgumentException( "unexpected configuration type: " + configuration.getClass().getName() );
081        }
082
083        WagonComponentConfigurator configurator = new WagonComponentConfigurator();
084
085        configurator.configureComponent( wagon, config, container.getContainerRealm() );
086    }
087
088    static class WagonComponentConfigurator
089        extends AbstractComponentConfigurator
090    {
091
092        @Override
093        public void configureComponent( Object component, PlexusConfiguration configuration,
094                                        ExpressionEvaluator expressionEvaluator, ClassRealm containerRealm,
095                                        ConfigurationListener listener )
096            throws ComponentConfigurationException
097        {
098            ObjectWithFieldsConverter converter = new ObjectWithFieldsConverter();
099
100            converter.processConfiguration( converterLookup, component, containerRealm, configuration,
101                                            expressionEvaluator, listener );
102        }
103
104    }
105
106}