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