View Javadoc
1   package org.apache.maven.wagon.tck.http;
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 org.apache.maven.wagon.Wagon;
23  import org.codehaus.plexus.PlexusConstants;
24  import org.codehaus.plexus.PlexusContainer;
25  import org.codehaus.plexus.classworlds.realm.ClassRealm;
26  import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
27  import org.codehaus.plexus.component.configurator.ComponentConfigurator;
28  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
29  import org.codehaus.plexus.configuration.PlexusConfiguration;
30  import org.codehaus.plexus.context.Context;
31  import org.codehaus.plexus.context.ContextException;
32  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
33  
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  /**
38   * 
39   */
40  public class WagonTestCaseConfigurator
41      implements Contextualizable
42  {
43      private static final String UNSUPPORTED_ELEMENT = "unsupported";
44  
45      private PlexusConfiguration useCaseConfigs;
46  
47      private ComponentConfigurator configurator;
48  
49      private ClassRealm realm;
50  
51      private String wagonHint;
52  
53      private static Logger logger = LoggerFactory.getLogger( WagonTestCaseConfigurator.class );
54  
55      public boolean isSupported( final String useCaseId )
56      {
57          if ( useCaseConfigs != null )
58          {
59              PlexusConfiguration config = useCaseConfigs.getChild( useCaseId, false );
60  
61              if ( config != null && config.getChild( UNSUPPORTED_ELEMENT, false ) != null )
62              {
63                  logger.info( "Test case '" + useCaseId + "' is marked as unsupported by this wagon." );
64                  return false;
65              }
66          }
67  
68          return true;
69      }
70  
71      public boolean configureWagonForTest( final Wagon wagon, final String useCaseId )
72          throws ComponentConfigurationException
73      {
74          if ( useCaseConfigs != null )
75          {
76              PlexusConfiguration config = useCaseConfigs.getChild( useCaseId, false );
77  
78              if ( config != null )
79              {
80                  if ( config.getChild( UNSUPPORTED_ELEMENT, false ) != null )
81                  {
82                      logger.error( "Test case '" + useCaseId + "' is marked as unsupported by this wagon." );
83                      return false;
84                  }
85                  else
86                  {
87                      logger.info( "Configuring wagon for test case: " + useCaseId + " with:\n\n" + config );
88                      configurator.configureComponent( wagon, useCaseConfigs.getChild( useCaseId, false ), realm );
89                  }
90              }
91              else
92              {
93                  logger.info( "No wagon configuration found for test case: " + useCaseId );
94              }
95          }
96          else
97          {
98              logger.info( "No test case configurations found." );
99          }
100 
101         return true;
102     }
103 
104     public void contextualize( final Context context )
105         throws ContextException
106     {
107         PlexusContainer container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
108         this.realm = container.getContainerRealm();
109         try
110         {
111             configurator = (ComponentConfigurator) container.lookup( ComponentConfigurator.ROLE );
112         }
113         catch ( ComponentLookupException e )
114         {
115             throw new ContextException( "Failed to lookup component configurator: " + e.getMessage(), e );
116         }
117     }
118 
119     public PlexusConfiguration getUseCaseConfigs()
120     {
121         return useCaseConfigs;
122     }
123 
124     public void setUseCaseConfigs( final PlexusConfiguration useCaseConfigs )
125     {
126         this.useCaseConfigs = useCaseConfigs;
127     }
128 
129     public String getWagonHint()
130     {
131         return wagonHint;
132     }
133 
134     public void setWagonHint( final String wagonHint )
135     {
136         this.wagonHint = wagonHint;
137     }
138 
139 }