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