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.annotations.Component;
28  import org.codehaus.plexus.component.annotations.Requirement;
29  import org.codehaus.plexus.component.configurator.AbstractComponentConfigurator;
30  import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
31  import org.codehaus.plexus.component.configurator.ConfigurationListener;
32  import org.codehaus.plexus.component.configurator.converters.composite.ObjectWithFieldsConverter;
33  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
34  import org.codehaus.plexus.configuration.PlexusConfiguration;
35  import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
36  import org.codehaus.plexus.util.xml.Xpp3Dom;
37  import org.eclipse.aether.transport.wagon.WagonConfigurator;
38  
39  /**
40   * A wagon configurator based on the Plexus component configuration framework.
41   */
42  @Component( role = WagonConfigurator.class, hint = "plexus" )
43  public class PlexusWagonConfigurator
44      implements WagonConfigurator
45  {
46  
47      @Requirement
48      private PlexusContainer container;
49  
50      /**
51       * Creates an uninitialized wagon configurator.
52       * 
53       * @noreference This constructor only supports the Plexus IoC container and should not be called directly by
54       *              clients.
55       */
56      public PlexusWagonConfigurator()
57      {
58          // enables no-arg constructor
59      }
60  
61      /**
62       * Creates a wagon configurator using the specified Plexus container.
63       *
64       * @param container The Plexus container instance to use, must not be {@code null}.
65       */
66      public PlexusWagonConfigurator( PlexusContainer container )
67      {
68          this.container = requireNonNull( container, "plexus container cannot be null" );
69      }
70  
71      public void configure( Wagon wagon, Object configuration )
72          throws Exception
73      {
74          PlexusConfiguration config = null;
75          if ( configuration instanceof PlexusConfiguration )
76          {
77              config = (PlexusConfiguration) configuration;
78          }
79          else if ( configuration instanceof Xpp3Dom )
80          {
81              config = new XmlPlexusConfiguration( (Xpp3Dom) configuration );
82          }
83          else if ( configuration == null )
84          {
85              return;
86          }
87          else
88          {
89              throw new IllegalArgumentException( "unexpected configuration type: " + configuration.getClass().getName() );
90          }
91  
92          WagonComponentConfigurator configurator = new WagonComponentConfigurator();
93  
94          configurator.configureComponent( wagon, config, container.getContainerRealm() );
95      }
96  
97      static class WagonComponentConfigurator
98          extends AbstractComponentConfigurator
99      {
100 
101         @Override
102         public void configureComponent( Object component, PlexusConfiguration configuration,
103                                         ExpressionEvaluator expressionEvaluator, ClassRealm containerRealm,
104                                         ConfigurationListener listener )
105             throws ComponentConfigurationException
106         {
107             ObjectWithFieldsConverter converter = new ObjectWithFieldsConverter();
108 
109             converter.processConfiguration( converterLookup, component, containerRealm, configuration,
110                                             expressionEvaluator, listener );
111         }
112 
113     }
114 
115 }