001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.transport.wagon;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import org.apache.maven.wagon.Wagon;
026import org.codehaus.plexus.PlexusContainer;
027import org.codehaus.plexus.classworlds.realm.ClassRealm;
028import org.codehaus.plexus.component.configurator.AbstractComponentConfigurator;
029import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
030import org.codehaus.plexus.component.configurator.ConfigurationListener;
031import org.codehaus.plexus.component.configurator.converters.composite.ObjectWithFieldsConverter;
032import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
033import org.codehaus.plexus.configuration.PlexusConfiguration;
034import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
035import org.codehaus.plexus.util.xml.Xpp3Dom;
036import org.eclipse.aether.transport.wagon.WagonConfigurator;
037
038import static java.util.Objects.requireNonNull;
039
040/**
041 * A wagon configurator based on the Plexus component configuration framework.
042 */
043@Named("plexus")
044@Singleton
045public class PlexusWagonConfigurator implements WagonConfigurator {
046    private final PlexusContainer container;
047
048    /**
049     * Creates a wagon configurator using the specified Plexus container.
050     *
051     * @param container The Plexus container instance to use, must not be {@code null}.
052     */
053    @Inject
054    public PlexusWagonConfigurator(final PlexusContainer container) {
055        this.container = requireNonNull(container, "plexus container cannot be null");
056    }
057
058    public void configure(Wagon wagon, Object configuration) throws Exception {
059        requireNonNull(wagon, "wagon cannot be null");
060        requireNonNull(configuration, "configuration cannot be null");
061
062        PlexusConfiguration config;
063        if (configuration instanceof PlexusConfiguration) {
064            config = (PlexusConfiguration) configuration;
065        } else if (configuration instanceof Xpp3Dom) {
066            config = new XmlPlexusConfiguration((Xpp3Dom) configuration);
067        } else {
068            throw new IllegalArgumentException(
069                    "unexpected configuration type: " + configuration.getClass().getName());
070        }
071
072        WagonComponentConfigurator configurator = new WagonComponentConfigurator();
073
074        configurator.configureComponent(wagon, config, container.getContainerRealm());
075    }
076
077    static class WagonComponentConfigurator extends AbstractComponentConfigurator {
078
079        @Override
080        public void configureComponent(
081                Object component,
082                PlexusConfiguration configuration,
083                ExpressionEvaluator expressionEvaluator,
084                ClassRealm containerRealm,
085                ConfigurationListener listener)
086                throws ComponentConfigurationException {
087            ObjectWithFieldsConverter converter = new ObjectWithFieldsConverter();
088
089            converter.processConfiguration(
090                    converterLookup, component, containerRealm, configuration, expressionEvaluator, listener);
091        }
092    }
093}