Coverage Report - org.codehaus.plexus.component.configurator.converters.composite.ObjectWithFieldsConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectWithFieldsConverter
66 %
22/33
61 %
11/18
3,4
 
 1  
 package org.codehaus.plexus.component.configurator.converters.composite;
 2  
 
 3  
 /*
 4  
  * The MIT License
 5  
  *
 6  
  * Copyright (c) 2004, The Codehaus
 7  
  *
 8  
  * Permission is hereby granted, free of charge, to any person obtaining a copy of
 9  
  * this software and associated documentation files (the "Software"), to deal in
 10  
  * the Software without restriction, including without limitation the rights to
 11  
  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 12  
  * of the Software, and to permit persons to whom the Software is furnished to do
 13  
  * so, subject to the following conditions:
 14  
  *
 15  
  * The above copyright notice and this permission notice shall be included in all
 16  
  * copies or substantial portions of the Software.
 17  
  *
 18  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 24  
  * SOFTWARE.
 25  
  */
 26  
 
 27  
 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
 28  
 import org.codehaus.plexus.component.configurator.ConfigurationListener;
 29  
 import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter;
 30  
 import org.codehaus.plexus.component.configurator.converters.ComponentValueSetter;
 31  
 import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
 32  
 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
 33  
 import org.codehaus.plexus.configuration.PlexusConfiguration;
 34  
 
 35  
 import java.util.Collection;
 36  
 import java.util.Dictionary;
 37  
 import java.util.Map;
 38  
 
 39  
 /**
 40  
  * @author <a href="mailto:michal@codehaus.org">Michal Maczka</a>
 41  
  * @version $Id$
 42  
  */
 43  85
 public class ObjectWithFieldsConverter
 44  
     extends AbstractConfigurationConverter
 45  
 {
 46  
     public boolean canConvert( Class type )
 47  
     {
 48  18
         boolean retValue = true;
 49  
 
 50  18
         if ( Dictionary.class.isAssignableFrom( type ) )
 51  
         {
 52  0
             retValue = false;
 53  
         }
 54  
 
 55  18
         else if ( Map.class.isAssignableFrom( type ) )
 56  
         {
 57  0
             retValue = false;
 58  
         }
 59  18
         else if ( Collection.class.isAssignableFrom( type ) )
 60  
         {
 61  0
             retValue = false;
 62  
         }
 63  
 
 64  18
         return retValue;
 65  
     }
 66  
 
 67  
     public Object fromConfiguration( ConverterLookup converterLookup,
 68  
                                      PlexusConfiguration configuration,
 69  
                                      Class type,
 70  
                                      Class baseType,
 71  
                                      ClassLoader classLoader,
 72  
                                      ExpressionEvaluator expressionEvaluator,
 73  
                                      ConfigurationListener listener )
 74  
         throws ComponentConfigurationException
 75  
     {
 76  25
         Object retValue = fromExpression( configuration, expressionEvaluator, type );
 77  
 
 78  25
         if ( retValue == null )
 79  
         {
 80  
             try
 81  
             {
 82  
                 // it is a "composite" - we compose it from its children. It does not have a value of its own
 83  25
                 Class implementation = getClassForImplementationHint( type, configuration, classLoader );
 84  
 
 85  25
                 if ( type == implementation && type.isInterface() && configuration.getChildCount() <= 0 )
 86  
                 {
 87  1
                     return null;
 88  
                 }
 89  
 
 90  24
                 retValue = instantiateObject( implementation );
 91  
 
 92  24
                 processConfiguration( converterLookup, retValue, classLoader, configuration, expressionEvaluator, listener );
 93  
             }
 94  0
             catch ( ComponentConfigurationException e )
 95  
             {
 96  0
                 if ( e.getFailedConfiguration() == null )
 97  
                 {
 98  0
                     e.setFailedConfiguration( configuration );
 99  
                 }
 100  
 
 101  0
                 throw e;
 102  24
             }
 103  
         }
 104  24
         return retValue;
 105  
     }
 106  
 
 107  
 
 108  
     public void processConfiguration( ConverterLookup converterLookup,
 109  
                                       Object object,
 110  
                                       ClassLoader classLoader,
 111  
                                       PlexusConfiguration configuration )
 112  
         throws ComponentConfigurationException
 113  
     {
 114  0
         processConfiguration( converterLookup, object, classLoader, configuration, null );
 115  0
     }
 116  
 
 117  
     public void processConfiguration( ConverterLookup converterLookup,
 118  
                                       Object object,
 119  
                                       ClassLoader classLoader,
 120  
                                       PlexusConfiguration configuration,
 121  
                                       ExpressionEvaluator expressionEvaluator )
 122  
         throws ComponentConfigurationException
 123  
     {
 124  0
         processConfiguration( converterLookup, object, classLoader, configuration, expressionEvaluator, null );
 125  0
     }
 126  
 
 127  
     public void processConfiguration( ConverterLookup converterLookup,
 128  
                                       Object object,
 129  
                                       ClassLoader classLoader,
 130  
                                       PlexusConfiguration configuration,
 131  
                                       ExpressionEvaluator expressionEvaluator,
 132  
                                       ConfigurationListener listener )
 133  
         throws ComponentConfigurationException
 134  
     {
 135  42
         int items = configuration.getChildCount();
 136  
 
 137  112
         for ( int i = 0; i < items; i++ )
 138  
         {
 139  71
             PlexusConfiguration childConfiguration = configuration.getChild( i );
 140  
 
 141  71
             String elementName = childConfiguration.getName();
 142  
 
 143  71
             ComponentValueSetter valueSetter = new ComponentValueSetter( fromXML( elementName ), object, converterLookup, listener );
 144  
 
 145  71
             valueSetter.configure( childConfiguration, classLoader, expressionEvaluator );
 146  
         }
 147  41
     }
 148  
 }