View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.transport.wagon;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import org.apache.maven.wagon.Wagon;
26  import org.codehaus.plexus.PlexusContainer;
27  import org.codehaus.plexus.classworlds.realm.ClassRealm;
28  import org.codehaus.plexus.component.configurator.AbstractComponentConfigurator;
29  import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
30  import org.codehaus.plexus.component.configurator.ConfigurationListener;
31  import org.codehaus.plexus.component.configurator.converters.composite.ObjectWithFieldsConverter;
32  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
33  import org.codehaus.plexus.configuration.PlexusConfiguration;
34  import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
35  import org.codehaus.plexus.util.xml.Xpp3Dom;
36  import org.eclipse.aether.transport.wagon.WagonConfigurator;
37  
38  import static java.util.Objects.requireNonNull;
39  
40  /**
41   * A wagon configurator based on the Plexus component configuration framework.
42   */
43  @Named("plexus")
44  @Singleton
45  public class PlexusWagonConfigurator implements WagonConfigurator {
46      private final PlexusContainer container;
47  
48      /**
49       * Creates a wagon configurator using the specified Plexus container.
50       *
51       * @param container The Plexus container instance to use, must not be {@code null}.
52       */
53      @Inject
54      public PlexusWagonConfigurator(final PlexusContainer container) {
55          this.container = requireNonNull(container, "plexus container cannot be null");
56      }
57  
58      public void configure(Wagon wagon, Object configuration) throws Exception {
59          requireNonNull(wagon, "wagon cannot be null");
60          requireNonNull(configuration, "configuration cannot be null");
61  
62          PlexusConfiguration config;
63          if (configuration instanceof PlexusConfiguration) {
64              config = (PlexusConfiguration) configuration;
65          } else if (configuration instanceof Xpp3Dom) {
66              config = new XmlPlexusConfiguration((Xpp3Dom) configuration);
67          } else {
68              throw new IllegalArgumentException(
69                      "unexpected configuration type: " + configuration.getClass().getName());
70          }
71  
72          WagonComponentConfigurator configurator = new WagonComponentConfigurator();
73  
74          configurator.configureComponent(wagon, config, container.getContainerRealm());
75      }
76  
77      static class WagonComponentConfigurator extends AbstractComponentConfigurator {
78  
79          @Override
80          public void configureComponent(
81                  Object component,
82                  PlexusConfiguration configuration,
83                  ExpressionEvaluator expressionEvaluator,
84                  ClassRealm containerRealm,
85                  ConfigurationListener listener)
86                  throws ComponentConfigurationException {
87              ObjectWithFieldsConverter converter = new ObjectWithFieldsConverter();
88  
89              converter.processConfiguration(
90                      converterLookup, component, containerRealm, configuration, expressionEvaluator, listener);
91          }
92      }
93  }