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