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